# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1039186 | DeathIsAwe | Knapsack (NOI18_knapsack) | C++14 | 0 ms | 0 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.
#include <bits/stdc++.h>
using namespace std;
int dp[2001][2001];
#define int long long
int main() {
int s, n, d1, d2, d3; cin >> s >> n;
vector<vector<int>> items(2001);
vector<vector<int>> unsorted(2001);
for (int i=0;i<n;i++) {
cin >> d1 >> d2 >> d3;
for (int i=0;i<d3;i++) {
unsorted[d2].push_back(d1);
}
}
for (int i=1;i<2001;i++) {
sort(unsorted[i].begin(), unsorted[i].end(), greater<int>());
d1 = 0;
for (int j: unsorted[i]) {
d1 += j;
items[i].push_back(d1);
}
}
//for (int i=0;i<2001;i++) {
// if (items[i].size() > 0) {
// cout << i << ':';
// for (int j: items[i]) {
// cout << j << ' ';
// }
// cout << '\n';
// }
//}
for (int i=0;i<2001;i++) {
dp[0][i] = 0;
dp[i][0] = 0;
}
for (int i=1;i<s+1;i++) {
for (int j=1;j<2001;j++) {
d2 = dp[i][j-1];
d1 = 0;
while (d1 < items[j].size() && (d1 + 1) * j <= i) {
d2 = max(d2, dp[i - ((d1 + 1) * j)][j-1] + items[j][d1]);
d1++;
}
dp[i][j] = d2;
}
}
cout << dp[s][2000];
}