This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* RE */
#include<bits/stdc++.h>
#define int ll
#define forn(i,n) for(int i=0;i<(n);i++)
#define Forn(i,n) for(int i=1;i<=(n);i++)
#define ll long long
#define pb push_back
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define rz resize
#define vi vector<int>
#define vl vector<long long>
#define vpi vector<pair<int,int> >
#define pii pair<int,int>
#define pll pair<long long,long long>
#define mp make_pair
#define all(p) p.begin(),p.end()
#define alr(p,q) p+1,p+q+1
#define ull unsigned long long
#define st0(p) memset((p),0,sizeof(p))
#define T(x) ((xb)%2 ? s[(x)/2] : '.')
#define lowb(x) x&-x
#define ls(x) (x)*2
#define rs(x) (x)*2+1
#define sz(x) (x).size()
using namespace std;
inline void USACO(const string &s){
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
void debug() {cout << endl;}
template <class T, class ...U> void debug(T a, U ... b) { cout << a << " "; debug(b...);}
#define fail() {cout << "-1\n";return;}
const int mod = 1e9+7;
const int N = 2e3+9;
int n,s;
vpi stuff;
vpi a[N];
int dp[N];
void solve(){
cin >> s >> n;
forn(i,n){
int w,v,k;
cin >> v >> w >> k;
a[w].pb(mp(v,k));
}
Forn(i,s) sort(all(a[i]));
Forn(i,s){
int cnt = s/i ;
for(auto &j : a[i]){
int res = min(cnt,j.S);
cnt -= res;
while(res--){
// debug(i,j.F);
stuff.pb(mp(i,j.F));
}
if(cnt == 0) break;
}
}
for(pii p : stuff){
int w = p.F,v = p.S;
// debug(w,v);
for(int i = s;i >=0 ;i--){
if(i >= w){
dp[i] = max(dp[i],dp[i-w] + v);
}
}
}
int ans = 0;
cout << dp[s] << '\n';
}
signed main(){
// USACO("feast");
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
// int t;
// cin >> t;
// while(t--)
solve();
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve()':
knapsack.cpp:75:6: warning: unused variable 'ans' [-Wunused-variable]
75 | int ans = 0;
| ^~~
# | 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... |