| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1357774 | kai | Knapsack (NOI18_knapsack) | C++20 | 26 ms | 3188 KiB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define el '\n'
#define all(x) begin(x), end(x)
#define sz(s) (int)(s.size())
inline void setIO(string s) {
if(fopen((s + ".in").c_str(), "r")){
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
}
const int MOD = 1e9+7; //998244353;
const int N = 1e6 + 36;
const int MAX_S = 2000 + 18;
int f[MAX_S];
vector<pair<int, int>> vt[MAX_S];
int n, v, w, k, s;
void Solve(){
cin>> s>> n;
for(int i=0; i<n; i++){
cin>> v>> w>> k;
vt[w].push_back({v, k});
}
vector<pair<int, int>> process;
for(int w = 1; w<=s; w++){
sort(all(vt[w]), greater<pair<int, int>>());
int need = s/w;
for(auto &[v, k]: vt[w]){
int lim = min(k, need);
for(int t=0; t<lim; t++){
process.push_back({v, w});
}
need -= lim;
if(need == 0) break;
}
}
for(auto &[v, w]: process){
for(int j = s; j>=w; j--){
f[j] = max(f[j], f[j-w] + v);
}
}
cout<< *max_element(f+1, f+s+1);
}
signed main (){
setIO("Rem");
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T(1);
// cin >> T;
while(T--) Solve();
return 0;
}Compilation message (stderr)
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Result | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
