제출 #515814

#제출 시각아이디문제언어결과실행 시간메모리
515814LunaMemeKnapsack (NOI18_knapsack)C++14
12 / 100
1 ms204 KiB
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> ii; typedef vector<pair<int, int>> vii; typedef vector<int> vi; typedef long long ll; #define PB push_back #define MP make_pair #define FOR(i, x, y) for (int i = x; i < y ; i ++) int main(){ int s, n; cin >> s >> n; vector<pair<ll, ll>> weights[s + 1]; FOR(i, 0, n){ ll v, w, k; cin >> v >> w >> k; weights[w].PB(MP(v, k)); } FOR(i, 0, s){ sort(weights[i].rbegin(), weights[i].rend()); } ll dp[s + 1]; FOR(i, 1, s + 1){ dp[i] = -1e9; } dp[0] = 0; ll ans = 0; FOR(j, 1, s + 1){ int indx = 0; ll prev = 0; FOR(cnt, 1, s/j + 1){ for(int i = s; i >= 1; i --){ //cout << dp[s] << '\n'; if (j > i || indx >= (int)weights[j].size()){ break; } if (cnt - prev > weights[j][indx].second){ prev += weights[j][indx].second; indx ++; } if (indx >= (int)weights[j].size()){ break; } dp[i] = max(dp[i], dp[i - j] + weights[j][indx].first); } } } FOR(i, 1, s + 1){ //cout << i << " : " << dp[i] << '\n'; ans = max(ans, dp[i]); } cout << ans << '\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...