# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
633131 | Alma | Knapsack (NOI18_knapsack) | C++17 | 734 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 <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
using ll = long long;
using ii = pair<int,int>;
const int INF = 1e9;
const ll LLINF = 1e18;
using vi = vector<int>;
using vvi = vector<vi>;
void setIO (string fileName) {
ios::sync_with_stdio(false);
cin.tie(NULL);
if (fileName != "std") {
freopen((fileName + ".in").c_str(), "r", stdin);
freopen((fileName + ".out").c_str(), "w", stdout);
}
}
int S, N;
vi v, w, k;
vector<vector<ll>> DP;
ll knapsack (int i, int rem) {
if (i == N or rem == 0) return 0;
ll &ans = DP[i][rem];
if (ans != -1) return ans;
if (rem < w[i]) return ans = knapsack(i+1, rem);
for (int x = 0; x <= k[i] and rem >= w[i]*x; x++) {
ans = max(ans, v[i]*x + knapsack(i+1, rem - w[i]*x));
}
return ans;
}
int main() {
setIO("std");
cin >> S >> N;
v = w = k = vi(N);
for (int i = 0; i < N; i++) {
cin >> v[i] >> w[i] >> k[i];
k[i] = min(k[i], S / w[i]);
}
DP = vector<vector<ll>>(N, vector<ll>(S+1, -1));
cout << knapsack(0, S) << '\n';
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... |