Submission #1131569

#TimeUsernameProblemLanguageResultExecution timeMemory
1131569LeonaRagingKnapsack (NOI18_knapsack)C++20
100 / 100
31 ms3084 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb emplace_back
#define db(val) "[" #val " = " << (val) << "] "

const int N = 2005;
const int INF = 1e18;

int n, s;
vector<pair<int,int>> a[N];

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> s >> n;
    for (int i = 1, v, w, k; i <= n; i++) {
    	cin >> v >> w >> k;
    	a[w].pb(v, k);
    }
    for (int i = 1; i <= s; i++) {
    	sort(a[i].begin(), a[i].end());
    	reverse(a[i].begin(), a[i].end());
    }
    vector<int> f(s + 4, -INF), g(s + 4);
    f[0] = 0;
    for (int i = 1; i <= s; i++) {
    	fill(g.begin(), g.end(), -INF);
    	int tot = 0, cur = 0, num = 0;
    	for (int j = 1; j <= s / i && cur < (int)a[i].size(); j++) {
    		tot += a[i][cur].first; num++;
    		if (num == a[i][cur].second) cur++, num = 0;
    		for (int w = 0; w + i * j <= s; w++) { 
    			g[w + i * j] = max(g[w + i * j], f[w] + tot);
    		}
    	}
    	for (int w = 0; w <= s; w++)
    		g[w] = max(g[w], f[w]);
    	swap(f, g);
    }
    cout << *max_element(f.begin(), f.end());
}

#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...