Submission #2667169


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 501;

ll dp[N][N][2];

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    ll s, x, ans = 0;
    cin >> n >> s;
    vector<ll> w(n), v(n);
    for (int i = 0; i < n; ++i) {
        cin >> w[i] >> v[i];
        if (!i) {
            x = w[0];
        }
        w[i] -= x;
    }
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            dp[i][j][0] = -1;
        }
    }
    dp[0][0][0] = 0;
    for (int i = 0; i < n; ++i) {
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                dp[i][j][1] = dp[i][j][0];
            }
        }
        for (int j = 0; j < n; ++j) {
            for (int k = 0; k + w[i] < N; ++k) {
                if (dp[j][k][0] == -1) {
                    continue;
                }
                if (k + w[i] <= s - x * (j + 1)) {
                    dp[j + 1][k + w[i]][1] = max(dp[j + 1][k + w[i]][1], dp[j][k][0] + v[i]);
                }
            }
        }
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                dp[i][j][0] = dp[i][j][1];
                ans = max(ans, dp[i][j][0]);
            }
        }
    }
    cout << ans;
    return 0;
}

Submission Info

Submission Time
Task D - Simple Knapsack
User meatrow
Language C++14 (GCC 5.4.1)
Score 400
Code Size 1457 Byte
Status AC
Exec Time 61 ms
Memory 4224 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 16
Set Name Test Cases
Sample example0, example1, example2, example3
All antigreedy0, antigreedy1, antigreedy2, example0, example1, example2, example3, quarter0, quarter1, quarter2, rand0, rand1, rand2, smallw0, smallw1, smallw2
Case Name Status Exec Time Memory
antigreedy0 AC 61 ms 4224 KB
antigreedy1 AC 61 ms 4224 KB
antigreedy2 AC 61 ms 4224 KB
example0 AC 5 ms 4224 KB
example1 AC 5 ms 4224 KB
example2 AC 5 ms 4224 KB
example3 AC 5 ms 4224 KB
quarter0 AC 61 ms 4224 KB
quarter1 AC 61 ms 4224 KB
quarter2 AC 61 ms 4224 KB
rand0 AC 6 ms 4224 KB
rand1 AC 31 ms 4224 KB
rand2 AC 17 ms 4224 KB
smallw0 AC 61 ms 4224 KB
smallw1 AC 61 ms 4224 KB
smallw2 AC 61 ms 4224 KB