제출 #976582

#제출 시각아이디문제언어결과실행 시간메모리
976582vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
61 ms4684 KiB
#include<bits/stdc++.h> #define ll long long #define endl "\n" #define fi first #define se second #define pb push_back #define pll pair<long long, long long> using namespace std; void solve(){ ll s,n; cin >> s >> n; vector<pll> arr[s+5]; for(int i=1;i<=n;i++){ ll a,b,c; cin >> a >> b >> c; arr[b].pb({a,c}); } vector<pll> v; for(int i=1;i<=s;i++){ if(arr[i].size()==0) continue; ll now = s/i; sort(arr[i].begin(),arr[i].end(),greater<pll>()); ll sz = arr[i].size(); for(int j=0;j<sz;j++){ if(now==0) break; while(arr[i][j].se>0&&now>0){ ll val = arr[i][j].fi; v.pb({i,val}); arr[i][j].se--; now--; } } } ll dp[s+5] = {0}; ll INF = -1; for(int i=0;i<=s+1;i++){ dp[i] = INF; } dp[0] = 0; for(auto i:v){ for(int j=s;j>=i.fi;j--){ dp[j] = max(dp[j],dp[j-1]); if(dp[j-i.fi]==INF) continue; dp[j] = max(dp[j],dp[j-i.fi]+i.se); } } ll ans = 0; for(int i=1;i<=s;i++){ ans = max(ans,dp[i]); } cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tc = 1; // cin >> tc; while(tc--){ solve(); } }
#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...