Submission #1099996

#TimeUsernameProblemLanguageResultExecution timeMemory
1099996iwtbabKnapsack (NOI18_knapsack)C++14
73 / 100
1050 ms3528 KiB
#include<bits/stdc++.h>
#define ll long long

using namespace std;
const int N=2505, mod=998244353;

ll dp[N],c[N],w[N],v[N];

//dp[j] = max(dp[j], dp[j - z*w[i]] + z*v[i])
map<ll,vector<pair<ll,ll>>> mp;
int main(){
   int tar, n; cin >> tar >> n;
   for(int i=0;i<n;i++){
      ll w,v,k; cin>>v >> w >> k;
      mp[w].push_back({v,k});
   }

   ll ans = 0;
   memset(dp,0,sizeof(dp));
   // w: {v1...vn}
   for(auto&it:mp){
      sort(begin(it.second),end(it.second),greater<>());
      ll w = it.first;

      for(auto& p:it.second){
         ll v = p.first, copies=p.second;
         for(int j=tar;j>=0;j--){
            if(j < w) continue;
            for(int z=0;z<=copies;z++){
               if(j >= z*w)
                  dp[j] = max(dp[j], dp[j - z*w]+v*z);
               else break;
            }
            ans = max(dp[j],ans);
         }
      }
   }
   cout << dp[tar] << '\n';

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...