Submission #1059493

#TimeUsernameProblemLanguageResultExecution timeMemory
1059493chautanphatKnapsack (NOI18_knapsack)C++11
37 / 100
17 ms31836 KiB
#include<bits/stdc++.h>

using namespace std;

const int N = 2e3 + 1;
long long dp[N][N];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int s, n;
    cin >> s >> n;

    vector<long long> f[s+1];
    while (n--)
    {
        int v, w, k, cur = 1;
        cin >> v >> w >> k;
        while (k >= cur)
        {
            k -= cur;
            if (w*cur <= s) f[w*cur].push_back(1ll*v*cur);
            cur *= 2;
        }
        if (k > 0 && k*w <= s) f[w*k].push_back(1ll*v*k);
    }

    vector<long long> p[s+1];
    for (int i = 1; i <= s; i++)
    {
        sort(f[i].begin(), f[i].end(), greater<long long>());
        p[i].push_back(0);
        for (int j = 0; j < (int)f[i].size(); j++) p[i].push_back(p[i].back()+f[i][j]);
    }

    for (int i = 1; i <= s; i++)
        for (int j = 1; j <= s; j++)
        {
            dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            for (int k = 0; k*j <= i && k <= (int)f[j].size(); k++)
                dp[i][j] = max(dp[i][j], dp[i-k*j][j-1]+p[j][k]);
        }
    cout << dp[s][s];

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...