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 fi first
#define se second
#define gcd __gcd
#define siz(v) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned 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+1;
int S, n, k[N], id[N];
ll dp[N], v[N], w[N], answer;
void process(ll cur_w, ll cur_val){
if(cur_w > S) return;
for(int i = S; i >= cur_w; i--){
ckmx(dp[i], dp[i-cur_w] + cur_val);
ckmx(answer, dp[i]);
}
return;
}
void solve()
{
cin >> S >> n;
for(int i = 1; i <= n; i++){
cin >> v[i] >> w[i] >> k[i];
id[i] = i;
}
sort(id+1, id+1+n, [&](int x, int y){
return v[x] > v[y];
});
for(int i = 1; i <= min(n,S); i++){
ll cur_k = min(S, k[id[i]]);
ll cur_v = v[id[i]], cur_w = w[id[i]];
for(int j = 0; j < (32 - __builtin_clz(cur_k)); j++){
if(cur_k >= (1<<j)){
cur_k -= (1<<j);
process(cur_w * (1<<j), cur_v * (1<<j));
}
else break;
}
if(cur_k){
process(cur_w * cur_k, cur_v * cur_k);
}
}
cout << answer <<"\n";
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
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)
knapsack.cpp: In function 'int main()':
knapsack.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |