제출 #531246

#제출 시각아이디문제언어결과실행 시간메모리
531246NetrKnapsack (NOI18_knapsack)C++14
100 / 100
485 ms17504 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> pi; #ifdef DEBUG #define D(x...) printf(x); #else #define D(x...) #endif const int MAXS = 2005; const int INF = 1<<30; int S, N, dp[MAXS][MAXS]; vector<pi> items[MAXS]; int prefSum(int w, int k){ int cnt = 0, val = 0; for(int i = items[w].size()-1; i >= 0; i--){ if(cnt + items[w][i].second > k){ val += (k - cnt) * items[w][i].first; cnt = k; break; }else{ val += items[w][i].first * items[w][i].second; cnt += items[w][i].second; } } D("pS: %d, %d, val = %d\n", w, k, val); return val; } int solve(int w, int i){ D("Solve: %d, %d\n", w, i); if(dp[w][i] > 0) return dp[w][i]; if(w < 0) return -INF; if(i == 1) return prefSum(i, w); int res = solve(w, i-1); if(!items[i].empty()){ for(int k = 1; k <= w/i; k++){ res = max(res, prefSum(i,k) + solve(w-k*i, i-1)); } } dp[w][i] = res; return res; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> S >> N; for(int i = 0; i < N; i++){ int vi, wi, ki; cin >> vi >> wi >> ki; items[wi].push_back({vi, ki}); } for(int i = 0; i < MAXS; i++){ sort(begin(items[i]), end(items[i])); } dp[S][S] = 0; cout << solve(S, S) << "\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...