# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
999528 | ef10 | Knapsack (NOI18_knapsack) | C++17 | 105 ms | 36176 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
using namespace std;
#define LL long long
int main() {
LL S, N; cin >> S >> N;
tuple<LL, LL, LL> A[N+1]; memset(A,0,sizeof(A));
for (LL i = 1; i <= N; i++) {
LL v,w,k;
cin >> v >> w >> k;
A[i] = make_tuple(w,v,k);
}
sort(A+1,A+N+1,greater<tuple<LL,LL,LL> >());
LL D[S+1][S+1]; memset(D,0,sizeof(D));
LL ind = 1;
while (ind <= N) {
LL s = get<0>(A[ind]);
LL id = 1;
while (ind <= N && get<0>(A[ind]) == s) {
for (LL j = 0; j < get<2>(A[ind]) && id <= S; j++) {
D[s][id++] = get<1>(A[ind]);
}
ind++;
if (id > S) break;
}
while (ind <= N && get<0>(A[ind]) == s) ind++;
}
LL dp[2][S+1];
for (LL i = 0; i < 2; i++) {
dp[i][0] = 0;
for (LL j = 1; j <= S; j++) {
dp[i][j] = -1;
}
}
for (LL i = 1; i <= S; i++) {
LL ind = i%2;
LL oi = (i+1)%2;
for (LL j = S; j >= 0; j--) {
dp[ind][j] = dp[oi][j];
if (dp[ind][j] < 0) continue;
for (LL w = i,id=1,v=D[i][id],V=v; w+j <= S && v > 0 && id <= S; w+=i,id++,v=D[i][id],V+=v) {
dp[ind][w+j] = max(dp[ind][w+j], dp[ind][j]+V);
}
}
}
LL res = 0;
for (LL i = 1; i <= S; i++) {
res = max(res, dp[S%2][i]);
}
cout << res << endl;
}
Compilation message (stderr)
# | 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... |