# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
884189 | fanwen | Knapsack (NOI18_knapsack) | C++17 | 44 ms | 3084 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;
#define fi first
#define se second
#define ll long long
#define file(name) \
if(fopen(name".inp", "r")) \
freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
const int MAX = 2e3 + 5;
int n, S;
vector <pair <int, int>> weights[MAX];
int dp[MAX];
void you_make_it(void) {
cin >> S >> n;
memset(dp, -0x3f, sizeof dp);
dp[0] = 0;
for (int i = 1; i <= n; ++i) {
int v, w, k; cin >> v >> w >> k;
weights[w].emplace_back(v, k);
}
for (int i = 1; i <= S; ++i) {
if(weights[i].empty()) continue;
sort(weights[i].begin(), weights[i].end(), greater <pair <int, int>>());
int ind = 0;
for (int j = 0; j < S / i; ++j) {
if(ind >= weights[i].size()) break;
for (int s = S; s >= i; --s) {
dp[s] = max(dp[s], dp[s - i] + weights[i][ind].fi);
}
weights[i][ind].se--;
if(weights[i][ind].se == 0) ind++;
}
}
cout << *max_element(dp, dp + S + 1);
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
file("knapsack");
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
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... |