# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1030507 | LmaoLmao | Knapsack (NOI18_knapsack) | C++17 | 2 ms | 860 KiB |
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>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using aa = array<int,3>;
const int N = 1e6+5;
const int INF = 1e9;
ll dp[2005];
vector<ii> m[2005];
int pre[2005][2005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n,s;
cin >> s >> n;
for(int i=1;i<=s;i++) {
dp[i]=0;
}
dp[0]=0;
for(int i=1;i<=n;i++) {
int v,w,k;
cin >> v >> w >> k;
m[w].push_back({v,k});
}
for(int i=1;i<=s;i++) {
sort(m[i].begin(),m[i].end(),greater<>());
}
for(int i=1;i<=s;i++) {
int cur=0;
for(ii x:m[i]) {
int t=cur;
cur+=x.second*i;
cur=min(cur,s);
for(int j=t+i;j<=cur;j+=i) {
pre[i][j]=pre[i][j-i]+x.first;
}
if(cur>=s) break;
}
for(int j=1;j<=s;j++) pre[i][j]=max(pre[i][j],pre[i][j-1]);
}
ll ans=0;
for(int i=1;i<=s;i++) {
for(ll j=s;j>0;j--) {
for(int k=1;i*k<=j;k++) {
dp[j]=max(dp[j],dp[j-i*k]+pre[i][i*k]);
ans=max(ans,dp[j]);
}
}
}
cout << ans;
return 0;
}
Compilation message (stderr)
# | 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... |