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;
ll dp[N], answer;
vector<pair<ll,int>> bag;
void process(ll v, ll w){
assert(w <= S);
for(int i = S; i >= w; i--){
if(dp[i-w] != -1) ckmx(dp[i], dp[i-w] + v);
ckmx(answer, dp[i]);
}
return;
}
void solve()
{
cin >> S >> n;
for(int i = 1; i <= n; i++){
ll v,w,k; cin >> v >> w >> k;
for(int j = 0; j < (32 - __builtin_clz(k)); j++){
if(k >= (1<<j)){
k -= (1<<j);
if(w * (1ll<<j) > S) break;
bag.push_back(make_pair(-v * (1ll<<j), w * (1ll<<j)));
}
else break;
}
if(k){
if(w * k > S) continue;
bag.push_back(make_pair(-v * k, w * k));
}
}
sort(bag.begin(), bag.end());
fill(dp+1, dp+1+S, -1);
for(int i = 0; (i < min((int)bag.size(), S*2)); i++){
process(-bag[i].first, bag[i].second);
}
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:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
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".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... |