This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define matrix vector<vector<int>>
using namespace std;
const int N = 1e5 + 5, S = 2e3 + 5, MOD = 1e9 + 7, INF = 2e9;
int v[N], w[N], k[N], n, s, a[N], dp[S];
vector<pii> items[S];
vector<pii> fitems;
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> s >> n;
for(int i = 1; i <= n; i++){
cin >> v[i] >> w[i] >> k[i];
items[w[i]].push_back({v[i], k[i]});
}
for(int i = 1; i <= s; i++){
sort(items[i].begin(), items[i].end(), [](pii x, pii y){return x.second < y.second;});
int needed = s / i;
for(pii item : items[i]){
item.second = min(item.second, needed);
int cnt = 1;
for(; cnt <= item.second; cnt *= 2){
fitems.push_back({item.first * cnt, i * cnt});
item.second -= cnt;
needed -= cnt;
}
if(item.second && needed){
fitems.push_back({item.first * item.second, item.second * i});
needed -= item.second;
}
}
}
for(int i = 0; i <= s; i++) dp[i] = -INF;
dp[0] = 0;
for(pii x : fitems){
for(int i = s; i >= 1; i--){
if(i >= x.second) dp[i] = max(dp[i], dp[i - x.second] + x.first);
}
}
cout << *max_element(dp, dp + s + 1) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |