# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1003131 | fryingduc | Knapsack (NOI18_knapsack) | C++17 | 292 ms | 5108 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.
// #pragma GCC optimize("Ofast,unroll-loops")
#include "bits/stdc++.h"
using namespace std;
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define yes cout << "YES\n"
#define no cout << "NO\n"
#ifdef LOCAL
#define debug(val) cerr << "["#val << " = " << (val) << "] "
#define slash() cerr << "\n-----------\n"
#define time() cerr << "Time is: " << 1000 * clock()/CLOCKS_PER_SEC << " ms\n"
#else
#define debug(val) 1407
#define slash() 1407
#define time() 1407
#endif
const string filename = "";
void IO(){
ios_base::sync_with_stdio(0);
cin.tie(0);
if(filename.size()){
string in = filename + ".inp";
string out = filename + ".out";
freopen(in.c_str(), "r", stdin);
freopen(out.c_str(), "w", stdout);
}
}
int n, W;
vector<pair<long long, long long>> item[2005];
long long f[2005];
void solve(){
cin >> W >> n;
for(int i=0;i<n;i++){
int v, w, k; cin >> v >> w >> k;
item[w].push_back({v, k});
}
for(int i = 1; i <= W; ++i) {
sort(item[i].begin(), item[i].end(), greater<pair<int, int>>());
for(int j = W; j >= 0; --j) {
int cur = 0;
long long sum = 0;
for(int k = 0; k < (int)item[i].size(); ++k) {
for(int l = 0; l < item[i][k].second; ++l) {
++cur;
sum = sum + item[i][k].first;
if(j < cur * i) break;
f[j] = max(f[j], f[j - cur * i] + sum);
}
}
}
}
cout << *max_element(f + 1, f + W + 1);
}
signed main(){
IO();
int test = 1;
/* cin >> test; */
for(int i=1;i<=test;i++){
// cout << "Case " << "#" << i << ": ";
solve();
}
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... |