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 ll long long
#define vi vector <ll>
#define vii vector <pair<ll,ll>>
#define ii pair<ll,ll>
#define loop(n) for(ll i=0;i<n;i++)
#define loopj(n) for(ll j=0;j<n;j++)
#define pb push_back
#define ll_SIZE 60
const ll MAXDIV = 2000000000000LL;
ll mod=1e9+7;
const ll N=5e5+5;
ll n,m,k;
ll a,b;
ll dp[2005][2005];
vector<pair<ll,ll>> mp[2005];
ll solve(ll idx,ll we){
if(idx>m) return 0;
ll &ret=dp[idx][we];
if(~ret)return ret;
ret=solve(idx+1,we);
ll tt=0;
for (ll i = 0; i < mp[idx].size() && we+idx<=m; ++i) {
ll tmp= mp[idx][i].second;
while(we+idx<=m && tmp>0){
tt+=mp[idx][i].first;
we+=idx;
ret=max(ret,tt+solve(idx+1,we));
tmp--;
}
}
return ret;
}
int main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
ll tt=1;
// cin >>tt;
while(tt--){
cin >>m >> n;
memset(dp,-1,sizeof(dp));
loop(n){
ll v,w,k;
cin >> v >>w >>k;
mp[w].emplace_back(v,k);
}
for (ll i = 1; i <= m; ++i) {
sort(mp[i].begin(),mp[i].end());
reverse(mp[i].begin(),mp[i].end());
}
cout <<solve(0,0) <<endl;
}
}
Compilation message (stderr)
knapsack.cpp: In function 'long long int solve(long long int, long long int)':
knapsack.cpp:25:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (ll i = 0; i < mp[idx].size() && we+idx<=m; ++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... |