Submission #895258

#TimeUsernameProblemLanguageResultExecution timeMemory
895258codexistentKnapsack (NOI18_knapsack)C++14
12 / 100
20 ms31580 KiB
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
#define ll long long
#define mp make_pair
#define f first
#define s second
 
int main(){
    ll S, N;
    cin >> S >> N;
    map<ll, multiset<pair<ll, ll>>> T;
    FOR(i, 0, N - 1){
        ll v, w, ct;
        cin >> v >> w >> ct;
        if(T.find(w) == T.end()){
            multiset<pair<ll, ll>> vv;
            T.insert(mp(w, vv));
        }
        T[w].insert(mp(-v, ct));
    }
 
    ll dp[S + 1][S + 1], R = 0;
    FOR(i, 0, S) FOR(j, 0, S) dp[i][j] = -1;
    dp[0][0] = 0;
    ll ofs = 1;
    for(auto t : T){
        FOR(i, 1, S){
            auto it = t.s.begin();
            ll v = -(t.s.begin()->f), ct = t.s.begin()->s;
            ll vs = 0;
            for(ll w = t.f; w <= i; w += t.f){
                vs += v;
                if(dp[ofs - 1][i - w] != -1) {
                    dp[ofs][i] = max(dp[ofs][i], dp[ofs - 1][i - w] + vs);
                    R = max(R, dp[ofs][i]);
                }
                ct--;
                if(ct == 0){
                    if(++it == t.s.end()) break;
                    v = -(it->f), ct = it->s;
                }
            }
        }
        ofs++;
    }
 
    cout << R << endl;
}
#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...