# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1096239 | InvMOD | Knapsack (NOI18_knapsack) | C++14 | 1 ms | 520 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;
using ll = long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 2e3+5;
int S, n, w[N], v[N], k[N];
ll dp[N], answer;
vector<int> bag[N];
void process(int cur_w, ll cur_v){
for(int i = S; i >= cur_w; i--){
ckmx(dp[i], dp[i-cur_w] + cur_v);
ckmx(answer, dp[i]);
}
return;
}
void solve()
{
cin >> S >> n;
for(int i = 1; i <= n; i++){
cin >> v[i] >> w[i] >> k[i];
if(w[i] <= S){
bag[w[i]].push_back(i);
}
}
for(int cur_w = 1; cur_w <= S; cur_w++){
sort(bag[cur_w].begin(), bag[cur_w].end(), [&](int x, int y){
return v[x] > v[y];
});
int need = S / cur_w+1, pointer = 0;
while(pointer < (int) bag[cur_w].size() && need){
ll cur_v = v[bag[cur_w][pointer]]; int cnt = k[bag[cur_w][pointer]];
int can = min(need, cnt);
need = need - can;
for(int j = 0; j < (32 - __builtin_clz(can)); j++){
if(can >= (1<<j)){
can -= (1<<j);
process(cur_w * (1<<j), cur_v * (1ll<<j));
}
else break;
}
if(can){
process(cur_w * can, cur_v * (1ll * can));
}
pointer++;
}
}
cout << answer <<"\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "task"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; //cin >> t;
while(t--){
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... |