洛谷P1510

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;
}
posted @ 2025-05-28 16:42  Chuan81  阅读(12)  评论(0)    收藏  举报