洛谷P1510
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int main() {
int v, n, c; cin >> v >> n >> c;
int V[n], cost[n];
vector<int>dp(c + 1, 0);
for(int i = 0; i < n; i++) {
cin >> V[i] >> cost[i];
for(int j = c; j >= cost[i]; j--) {
dp[j] = max(dp[j - cost[i]] + V[i], dp[j]);
}
}
int remaining = -1;
for (int j = 0; j <= c; j++) {
if (dp[j] >= v) {
remaining = c - j;
break;
}
}
if (remaining != -1) cout << remaining << '\n';
else cout << "Impossible" << '\n';
return 0;
}

浙公网安备 33010602011771号