# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1088630 | shidou26 | Knapsack (NOI18_knapsack) | C++14 | 274 ms | 4036 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 endl '\n'
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
const int N = 2008;
int s, n;
map<int, vector<ii>> items;
void prepare() {
}
void input() {
cin >> s >> n;
for(int i = 1; i <= n; i++) {
int v, w, k; cin >> v >> w >> k;
items[w].push_back(ii(v, k));
}
}
void process() {
vector<ll> dp, prev_dp;
prev_dp.resize(s + 1, -1e18); dp.resize(s + 1, -1e18);
prev_dp[0] = 0;
ll best = 0;
for(pair<int, vector<ii>> p : items) {
sort(all(p.second), greater<ii>());
for(int j = 0; j <= s; j++) {
dp[j] = prev_dp[j];
int total = 0; ll profit = 0;
for(int k = 0; k < (int)p.second.size(); k++) {
int v, a; tie(v, a) = p.second[k];
while(a > 0 && (total + 1) * p.first <= j) {
total++; profit += v; a--;
dp[j] = max(dp[j], prev_dp[j - total * p.first] + profit);
}
}
best = max(best, dp[j]);
}
prev_dp = dp;
}
cout << best << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define task "grouping"
if(fopen(task".INP", "r")) {
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
prepare();
int testcase = 1; // cin >> testcase;
for(int i = 1; i <= testcase; i++) {
input();
process();
}
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... |