U565348 安全箱 题解

· · 题解

显然01背包,二维dp会MLE三个点,要用一维dp。

十年OI一场空,不开longlong见祖宗。

本题不开longlong仅得30pts。

#include <bits/stdc++.h>
using namespace std;

const int N = 1e4 +5;
int n, m, v[N], w[N];
long long f[N];

int main() {
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> w[i] >> v[i];
    for(int i = 1; i <= n; i++)
        for(int j = m; j >= v[i]; j--) {
            f[j] = max(f[j], f[j - v[i]] + w[i]);
        }
    cout << f[m] << endl;
    return 0;
}