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<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=1e5, MAXS=2000;
long long dp[MAXS+1];
int p[MAXN], w[MAXN], h[MAXN], cnt[MAXS];
vector<pair<int, int> > v[MAXS];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
long long s, n;
cin >> s >> n;
for(int i=0; i<=s; i++)
{
cnt[i]=0;
}
for(int i=0; i<n; i++)
{
int a, b, c;
cin >> a >> b >> c;
v[b].push_back({a, c});
cnt[b]+=c;
}
n=0;
for(int i=0; i<=s; i++)
{
sort(v[i].begin(), v[i].end());
while(cnt[i]!=0 && cnt[i]>s/i)
{
cnt[i]-=v[i].back().second;
v[i].pop_back();
}
for(pair<int, int> pr:v[i])
{
p[n]=pr.first, w[n]=i, h[n]=pr.second;
n++;
}
}
for(int i=0; i<=s; i++)
{
dp[i]=-1;
}
dp[0]=0;
long long ans=0;
for(int i=0; i<n; i++)
{
for(int j=s; j>=0; j--)
{
for(int u=1; u<=h[i] && u*w[i]<=j; u++)
{
if(dp[j-u*w[i]]==-1)
{
continue;
}
dp[j]=max(dp[j], dp[j-u*w[i]]+u*p[i]);
}
ans=max(ans, dp[j]);
}
}
cout << ans << "\n";
return 0;
}
# | 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... |