#include <bits/stdc++.h>
using namespace std;
#define name "knapsack"
#define MAXN 100005
#define pb push_back
#define pf push_front
#define ll long long
#define ii pair<int, int>
#define fs first
#define sc second
#define ill pair<int, ll>
#define lli pair<ll, int>
#define llll pair<ll, ll>
#define all(v) v.begin(),v.end()
#define uni(v) v.erase(unique(all(v)),v.end())
#define bit(n,i) (((n)>>(i))&1)
#define FOR(i,a,b) for (int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for (int i=(a),_b=(b); i>=_b; i--)
#define MASK(i) (1LL<<(i))
const int INF=1e9;
const int MOD=1e9+7;
void add(int &u, int v){
u+=v;
if (u>=MOD) u-=MOD;
}
void sub(int &u, int v){
u-=v;
if (u<0) u+=MOD;
}
void minimize(int &u, int v){
u=min(u,v);
}
void maximize(ll &u, ll v){
u=max(u,v);
}
long long Rand(long long l, long long r){
ll tmp=0;
FOR(i,1,4) tmp=((tmp<<15)^(((1<<15)-1)&rand()));
return l+tmp%(r-l+1);
}
int S,n;
int V[MAXN],W[MAXN],K[MAXN];
vector<ii> store[2003];
ll f[2003][2003];
ll dp[2003][2003];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin>>S>>n;
FOR(i,1,n) cin>>V[i]>>W[i]>>K[i];
FOR(i,1,n){
store[W[i]].pb({V[i],K[i]});
}
FOR(i,1,S){
sort(all(store[i]));
int LIM=S/i;
int j=1;
f[i][0]=0;
while (store[i].size()){
ii tmp=store[i].back();
store[i].pop_back();
while (tmp.sc>0&&j<=LIM){
f[i][j]=f[i][j-1]+1LL*tmp.fs;
++j;
--tmp.sc;
}
if (j>LIM) break;
}
}
FOR(i,1,S){
FOR(j,1,S){
maximize(dp[i][j],dp[i][j-1]);
FOR(x,0,(int)j/i){
maximize(dp[i][j],dp[i-1][j-x*i]+f[i][x]);
}
}
}
cout<<dp[S][S];
return 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... |