# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1268481 | herominhsteve | Knapsack (NOI18_knapsack) | C++20 | 39 ms | 2884 KiB |
#include <bits/stdc++.h>
#define el '\n'
#define FNAME "NAME"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9+7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}
void setup(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if (fopen(FNAME".inp","r")){
freopen(FNAME".inp","r",stdin);
freopen(FNAME".out","w",stdout);
}
}
const int MAXN = 2001;
struct Goods{
int maxTake;
long long val;
Goods(): val(0), maxTake(0) {}
Goods(long long V,int t) : val(V), maxTake(t) {}
bool operator < (const Goods &other) const{
return (val > other.val or (val==other.val and maxTake > other.maxTake));
}
};
int W,n;
vector<Goods> goodsByW[MAXN];
void init(){
cin>>W>>n;
for (int i=0;i<n;i++){
long long v; int w,maxTake;
cin>>v>>w>>maxTake;
goodsByW[w].emplace_back(v,maxTake);
}
}
void sol(){
vector<long long> dp(W+1,0);
for (int w=1;w<=W;w++){
sort(allof(goodsByW[w]));
vector<Goods> &curG = goodsByW[w];
int totalTake=0,cnt=0,ptr=0;
while (totalTake * w <= W and ptr < (int)curG.size()){
for (int j=W;j>=w;j--){
maximize(dp[j],dp[j-w] + curG[ptr].val);
}
cnt++;
totalTake++;
if (cnt>=curG[ptr].maxTake){
cnt=0;
ptr++;
}
}
}
cout<<dp[W];
}
int main(){
setup();
init();
sol();
}
Compilation message (stderr)
# | 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... |