Submission #692466

#TimeUsernameProblemLanguageResultExecution timeMemory
692466aedmhsnKnapsack (NOI18_knapsack)C++17
100 / 100
473 ms36344 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
 
#define A first
#define B second
#define MP make_pair
#define ms(a, x) memset(a, x, sizeof(a)) 
 
 
#define boost() ios_base::sync_with_stdio(false); cin.tie(); cout.tie()
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pld;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);

const int mxN=2e3+5;

vector <pll> withw[mxN];
ll dp[mxN][mxN]={};

int main(){
    ll s, n;
    cin >> s >> n;
    for(int i=0; i<n; i++){
        ll v, w, k;
        cin >> v >> w >> k;
        withw[w].push_back(MP(v, k));
    }
    for(int i=1; i<=2000; i++){
        sort(withw[i].rbegin(), withw[i].rend());
        for(int j=1; j<=s; j++){
            dp[i][j] = dp[i-1][j];
            ll totalval=0, totalw=0;
            for(auto [val, amnt]:withw[i]){
                ll temp=1;
                while(temp <= amnt && j >= totalw+i){
                    totalval+=val;
                    totalw+=i;
                    dp[i][j] = max(dp[i][j], dp[i-1][j-totalw]+totalval);
                    temp++;
                }
            }
        }
    }
    cout << dp[2000][s];
}
#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...