# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
701610 | ifateen | Knapsack (NOI18_knapsack) | C++14 | 320 ms | 262144 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 <iostream>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define int long long
#define double long double
#define vec vector
#define vi vec<int>
#define pii pair<int,int>
#define vpi vec<pii>
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define pq priority_queue
#define all(v) v.begin(), v.end()
signed main() {
vi weights, vals;
int W, n;
cin >> W >> n;
for (int i = 0; i < n; i++) {
int val, weight, num;
cin >> val >> weight >> num;
num = min(num, W / weight + 1);
for (int j = 0; j < num; j++) {
weights.PB(weight);
vals.PB(val);
}
}
/*
* now we solve normal knapsack
*/
vi dp;
dp.assign(W + 1, 0);
for (int i = 1; i <= weights.size(); i++) {
for (int w = W; w >= 0; w--) {
if (weights[i - 1] <= w) {
dp[w] = max(dp[w], dp[w - weights[i - 1]] + vals[i - 1]);
}
}
}
cout << dp[W] << endl;
return 0;
}
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... |