# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
583788 | jack715 | Knapsack (NOI18_knapsack) | C++14 | 53 ms | 2968 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 ll long long
#define ff first
#define ss second
#define pb push_back
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int s,n;
ll val, wei, num;
vector<pair<ll,ll>> p[2005];
vector<ll> v[2005];
ll dp[2005];
ll ans;
int main() {
IOS
cin >> s >> n; // s <= 2000
for(int i = 1; i <= n; i ++) {
cin >> val >> wei >> num;
p[wei].pb({-val, num});
}
for(int i = 1; i <= s; i ++) {
sort(p[i].begin(), p[i].end());
int tmp = s / i;
int pos = 0;
while(tmp > 0 && pos < p[i].size()) {
if(p[i][pos].ss == 0) {
pos ++;
continue;
}
tmp --;
v[i].pb(-p[i][pos].ff);
p[i][pos].ss --;
}
}
for(int i = 1; i <= s; i ++) { // weight
for(int j = s; j >= 0; j --) {
ll tmp = 0;
for(int k = 0; k < v[i].size(); k ++) {
if(j - (k + 1) * i < 0) break;
tmp += v[i][k];
dp[j] = max(dp[j], dp[j - (k + 1) * i] + tmp);
}
}
}
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... |