Submission #411911

#TimeUsernameProblemLanguageResultExecution timeMemory
411911kwongwengKnapsack (NOI18_knapsack)C++17
100 / 100
96 ms19392 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<ii> vii; typedef long double ld; #define FOR(i, a, b) for(int i = a; i < b; i++) #define ROF(i, a, b) for(int i = a; i >= b; i--) #define ms memset #define pb push_back #define F first #define S second ll MOD = 1000000007; ll power(ll base, ll n){ if (n == 0) return 1; if (n == 1) return base; ll halfn = power(base, n/2); if (n % 2 == 0) return (halfn * halfn) % MOD; return (((halfn * halfn) % MOD) * base) % MOD; } ll inverse(ll n){ return power(n, MOD-2); } ll add(ll a, ll b){ return (a+b) % MOD; } ll mul(ll a, ll b){ return (a*b) % MOD; } ll gcd(ll a, ll b){ if (a == 0) return b; if (a == 1) return 1; return gcd(b % a, a); } int main(){ ios::sync_with_stdio(false); int s, n; cin >> s >> n; vi cnt(s+1); vector<pair<int, ii> > knap; FOR(i, 0, n){ int v, w, k; cin >> v >> w >> k; if (w > s) continue; knap.pb({v, {w, k}}); } sort(knap.rbegin(), knap.rend()); n = knap.size(); vi V[s+1]; FOR(i,1,s+1) V[i].pb(0); FOR(i, 0, n){ int v = knap[i].F, w = knap[i].S.F, k = knap[i].S.S; int len = V[w].size(); if (len >= s/w+1) continue; FOR(j, len, min(len+k, s/w+1)){ V[w].pb(V[w][j-1] + v); } } int dp[s+1][s+1]; ms(dp, 0, sizeof(dp)); FOR(i, 1, s+1){ FOR(j, 0, s+1){ FOR(k, 0, min(j/i+1, (int)V[i].size())){ dp[i][j] = max(dp[i][j], dp[i-1][j-k*i] + V[i][k]); } } } cout << dp[s][s] << '\n'; 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...