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>
using namespace std;
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
#define ll long long
#define mp make_pair
#define f first
#define s second
int main(){
ll S, N;
cin >> S >> N;
map<ll, multiset<pair<ll, ll>>> T;
FOR(i, 0, N - 1){
ll v, w, ct;
cin >> v >> w >> ct;
if(ct == 0) continue;
if(T.find(w) == T.end()){
multiset<pair<ll, ll>> vv;
T.insert(mp(w, vv));
}
T[w].insert(mp(-v, ct));
}
ll dp[S + 1][S + 1], R = 0;
FOR(i, 0, S) FOR(j, 0, S) dp[i][j] = -1;
dp[0][0] = 0;
ll ofs = 1;
for(auto t : T){
FOR(i, 1, S){
dp[ofs][i] = dp[ofs - 1][i];
auto it = t.s.begin();
ll v = -(t.s.begin()->f), ct = t.s.begin()->s;
ll vs = 0;
for(ll w = t.f; w <= i; w += t.f){
vs += v;
if(dp[ofs - 1][i - w] != -1) {
dp[ofs][i] = max(dp[ofs][i], dp[ofs - 1][i - w] + vs);
R = max(R, dp[ofs][i]);
}
ct--;
if(ct == 0){
if(++it == t.s.end()) break;
v = -(it->f), ct = it->s;
}
}
}
ofs++;
}
cout << R << 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... |