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 ll long long
#define pll pair<long long, long long>
#define vll vector<ll>
#define fi first
#define sc second
#define ld long double
#define pb push_back
const ll mod = 1e9 + 7;
int chngi[] = {0, 0, 1, -1};
int chngj[] = {1, -1, 0, 0};
char chng[] = {'R', 'L', 'D', 'U'};
ll add(ll a, ll b){
a += b;
if(a >= mod) a-=mod;
return a;
}
ll sub(ll a, ll b){
return ((a % mod) - (b % mod) + mod) % mod;
}
ll mul(ll a, ll b){
return (a*b)%mod;
}
void usaco_submit(string s){
string in = s + ".in";
string out = s + ".out";
const char* sin = in.c_str();
const char* sout = out.c_str();
freopen(sin, "r", stdin);
freopen(sout, "w", stdout);
}
//! CODE FROM HERE
int main(){
// usaco_submit("stacking");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll tc= 1;
// ll tc;
// cin>>tc;
while(tc--){
ll n, s;
cin>>s>>n;
map<ll, vector<pll> > wts;
for(int i = 0; i<n; i++){
ll val, wt, amt;
cin>>val>>wt>>amt;
if(wt <= s && amt > 0){
wts[wt].push_back({val, amt});
}
}
vector<vector<ll> > dp(wts.size() +1, vector<ll> (s+1, INT_MIN));
dp[0][0] = 0;
ll i = 1;
for(auto &[wt, items]: wts){
sort(items.begin(), items.end(), greater<pll>());
ll total_items = items.size();
for(ll cw = 0; cw <= s; cw++){
dp[i][cw] = dp[i-1][cw];
ll curr = 0;
ll copies_taken = 0;
ll val = 0;
ll curr_taken = 0;
while((copies_taken+1)*wt <= cw && curr < total_items){
copies_taken++;
curr_taken++;
val += items[curr].first;
if(dp[i-1][cw - copies_taken*wt] != INT_MIN){
dp[i][cw] = max(dp[i][cw],
dp[i-1][cw - copies_taken*wt] + val);
}
if(items[curr].second == curr_taken){
curr_taken = 0;
curr++;
}
}
}
i++;
}
cout << *max_element(dp.back().begin(), dp.back().end()) << endl;
}
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void usaco_submit(std::string)':
knapsack.cpp:40:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen(sin, "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~
knapsack.cpp:41:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen(sout, "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... |