제출 #1043879

#제출 시각아이디문제언어결과실행 시간메모리
1043879Khanhcsp2Knapsack (NOI18_knapsack)C++14
100 / 100
67 ms5344 KiB
#include<bits/stdc++.h> #define el '\n' #define fi first #define sc second #define int ll #define pii pair<int, int> #define all(v) v.begin(), v.end() using namespace std; using ll=long long; using ull=unsigned long long; using ld=long double; const int mod=1e9+7; const int N=1e5+11; int s, n, sz[N], dp[2011][2]; struct gg { int v, w, sl; }a[N]; bool cmp(gg x, gg y) { return x.v>y.v; } vector<pii> v; void sol() { cin >> s >> n; for(int i=1;i<=n;i++) cin >> a[i].v >> a[i].w >> a[i].sl; sort(a+1, a+n+1, cmp); v.push_back({0, 0}); for(int i=1;i<=s;i++) sz[i]=s/i; for(int i=1;i<=n;i++) { for(int j=1;j<=a[i].sl;j++) { if(sz[a[i].w]) { v.push_back({a[i].v, a[i].w}); sz[a[i].w]--; } else break; } } n=v.size(); for(int i=1;i<=n;i++) for(int j=0;j<=s;j++) dp[j][i]=-1e9; dp[0][0]=0; // for(auto x:v) cout << x.fi << ' ' << x.sc << el; for(int i=1;i<=n;i++) { for(int j=v[i].sc;j<=s;j++) { dp[j][i%2]=max(dp[j][i%2], dp[j-v[i].sc][(i-1)%2]+v[i].fi); } for(int j=0;j<=s;j++) dp[j][i%2]=max(dp[j][i%2], dp[j][(i-1)%2]); } int ans=0; for(int i=0;i<=1;i++) for(int j=0;j<=s;j++) ans=max(ans, dp[j][i]); cout << ans; } signed main() { // freopen("divisor.INP", "r", stdin); // freopen("divisor.OUT", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); int t=1; //cin >> t; while(t--) { sol(); } }
#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...