# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
403166 | nwatx | Knapsack (NOI18_knapsack) | C++17 | 1084 ms | 2708 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> // see C++ Tips & Tricks
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair
void setIO(string name = "") { // name is nonempty for USACO file I/O
cin.tie(0)->sync_with_stdio(0); // see Fast Input & Output
if (sz(name)) {
freopen((name+".in").c_str(), "r", stdin); // see Input & Output
freopen((name+".out").c_str(), "w", stdout);
}
}
int S, N;
int dp[2001]; // dp[i] is the max SGD with weight i
int v[100001], w[100001], k[100001];
// Observations:
// there will be at most 2000 of any item.
int main() {
setIO();
cin >> S >> N;
for(int i = 0; i < N; i++) {
cin >> v[i] >> w[i] >> k[i];
k[i] = min(k[i], 2000);
}
// transitions
// we can include [0, K] elements.
for(int i = 0; i < N; i++) {
for(int l = 0; l < k[i]; l++)
for(int s = S; s >= 0; s--) {
if(w[i] + s <= S) dp[w[i] + s] = max(dp[w[i] + s], dp[s] + v[i]);
}
}
cout << dp[S];
}
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... |