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 ll long long
#define F first
#define S second
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int s, n;
cin >> s >> n;
vector<vector<pair<ll, int>>> a(s+1);
for(int i = 0; i < n; i++){
ll v, w, k;
cin >> v >> w >> k;
a[w].push_back({v, k});
}
for(int w = 1; w <= s; w++){
sort(a[w].begin(), a[w].end(), greater<pair<ll, int>>());
}
vector<vector<ll>> dp(2, vector<ll> (s+1));
for(int w = 1; w <= s; w++){
ll tot_weight = 0, tot_val = 0;
for(auto pr : a[w]){
while(pr.S > 0 && tot_weight <= s){
tot_weight += w;
tot_val += pr.F;
pr.S--;
for(int i = s; i >= tot_weight; i--){
dp[1][i] = max(dp[1][i], tot_val + dp[0][i - tot_weight]);
}
}
}
dp[0] = dp[1];
}
cout << dp[0][s] << "\n";
}
# | 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... |