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;
#define pb push_back
#define int long long
#define mp make_pair
#define F first
#define S second
struct item{
int w;
int v;
int k;
};
bool compare(item a, item b){
if(a.w==b.w){
return a.v>b.v;
}
return a.w<b.w;
}
int dp[18000][2010];
signed main()
{
int w, n;
cin >> w >> n;
item a[n];
for(int i=0; i< n; i++){
cin >> a[i].v >> a[i].w >> a[i].k;
}
sort(a, a+n, compare);
vector<pair<int, int>> b;
int cnt[w+1]={0};
for(int i=0; i< n; i++){
while(cnt[a[i].w]<w/a[i].w && a[i].k > 0){
cnt[a[i].w]++;
a[i].k--;
b.pb(mp(a[i].w, a[i].v));
}
}
n=b.size();
for(int i=1; i<= n; i++){
for(int j=0; j<=w; j++){
if(b[i-1].F<=j){
dp[i][j]=max(dp[i-1][j], dp[i-1][j-b[i-1].F]+b[i-1].S);
}
else{
dp[i][j]=dp[i][j-1];
}
}
}
cout << dp[n][w] << '\n';
}
# | 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... |