# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1043835 | vanea | Knapsack (NOI18_knapsack) | C++14 | 40 ms | 3240 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;
using ll = long long;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int s, n;
cin >> s >> n;
vector<array<ll, 3>> v(n);
for(int i = 0; i < n; i++) {
cin >> v[i][0] >> v[i][1] >> v[i][2];
}
sort(v.rbegin(), v.rend());
vector<array<ll, 2>> a;
map<int, int> st;
for(auto [v, w, k] : v) {
while(st[w]+w <= s && k) {
--k;
st[w]+=w;
a.push_back({v, w});
}
}
vector<ll> dp(s+1);
for(auto [v, w] : a) {
for(int x = s-w; x >= 0; x--) {
dp[x+w] = max(dp[x+w], dp[x]+v);
}
}
ll ans = 0;
for(int i = 0; i <= s; i++) {
ans = max(ans, dp[i]);
}
cout << ans;
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... |