This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#define bupol __builtin_popcount
#define int long long
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
using namespace std;
const int MAXN = 1e5+10;
const int MAXK = 2010;
const int LOG = 20;
const int MOD = 1e9+7;
const int SQRT = 520;
const int INF = 1e18;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
int n, s;
int v[MAXN], w[MAXN], k[MAXN];
int dp[MAXK];
vector <pii> val[MAXK];
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> s >> n;
for(int i=1; i<=n; i++){
cin >> v[i] >> w[i] >> k[i];
val[w[i]].pb({v[i], k[i]});
}
for(int i=1; i<=s; i++) dp[i] = -INF;
for(int i=1; i<=s; i++){
sort(val[i].rbegin(), val[i].rend());
int tot = 0;
for(auto [v, k] : val[i]){
for(int p=1; p<=k && tot<=s; p++){
for(int cur=s; cur>=i; cur--){ // build cur
dp[cur] = max(dp[cur], dp[cur-i]+v);
}
tot += i;
}
}
}
// for(int i=1; i<=s; i++){
// cout << i << ' '<< dp[i] << " dp\n";
// }
int mx = -INF;
for(int i=1; i<=s; i++){
mx = max(mx, dp[i]);
}
cout << mx << '\n';
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:40:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
40 | for(auto [v, k] : val[i]){
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |