01背包加了一个排序
题目链接
ACcode
#include<bits/stdc++.h>
using namespace std;
const int M = 1e5 + 9;
int dp[M], v[M], w[M];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, p, s;cin >> n >> p >> s;
for (int i = 1;i <= n;i++)cin >> v[i] >> w[i];
for (int i = 1;i < n;i++) {
for (int j = i + 1;j <= n;j++) {
if (v[i] > v[j]) {
swap(v[i], v[j]);
swap(w[i], w[j]);
}
}
}
for (int i = 1;i <= n;i++) {
for (int j = s;j >= v[i];j--) {
dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
if (dp[j] >= p) {
cout << v[i];
return 0;
}
}
}
cout << "No Solution!";
return 0;
}