# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
863134 | Kodik | Knapsack (NOI18_knapsack) | C++17 | 72 ms | 35192 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 xx real()
// #define yy imag()
//
#define ff first
#define ss second
//
typedef long long ll;
typedef long double ld;
// typedef complex<ld> point;
void sieve(int n, vector<bool>& is_composite, vector<int>& prime) {
for (int i = 2; i < n; ++i) {
if (!is_composite[i]) prime.push_back(i);
for (int j = 0; j < int(prime.size()) && i * prime[j] < n; ++j) {
is_composite[i * prime[j]] = true;
if (i % prime[j] == 0) break;
}
}
}
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
// setIO("shuffle");
int limit, type_num;
cin >> limit >> type_num;
map<int, vector<pair<int,int>>> by_weight;
for(int i = 0; i < type_num; ++i){
int value, weight, amt;
cin >> value >> weight >> amt;
if(weight <= limit && amt > 0){
by_weight[weight].push_back({value, amt});
}
}
vector<vector<ll>> best(by_weight.size()+1, vector<ll>(limit+1, INT32_MIN));
best[0][0] = 0;
int at = 1;
for(auto &[w, items] : by_weight){
sort(items.begin(), items.end(), greater<pair<int,int>>());
for(int i = 0; i <= limit; i++){
best[at][i] = best[at-1][i];
int copies = 0, type_at = 0, curr_used = 0;
ll profit = 0;
while((copies+1)*w<=i&&type_at<items.size()){
++copies;
profit += items[type_at].ff;
if(best[at-1][i-copies*w]!=INT32_MIN){
best[at][i] = max(best[at][i], best[at-1][i-copies*w]+profit);
}
++curr_used;
if(curr_used==items[type_at].ss){
curr_used = 0;
++type_at;
}
}
}
++at;
}
cout << *max_element(best.back().begin(), best.back().end()) << '\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... |