# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1268464 | herominhsteve | Knapsack (NOI18_knapsack) | C++20 | 1 ms | 328 KiB |
#include <bits/stdc++.h>
#define el '\n'
#define FNAME "Bounded Knapsack"
#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);
}
}
struct Goods{
int w;
long long val;
Goods(int W,long long V): w(W), val(V) {}
};
int W,n;
vector<Goods> goods;
void init(){
cin>>W>>n;
for (int i=0;i<n;i++){
int w,maxTake;
long long val;
cin>>val>>w>>maxTake;
int pow2=1;
while (maxTake>0){
int take = min(pow2,maxTake);
int newW = w * take;
long long newVal = val * 1ll * take;
goods.emplace_back(newW,newVal);
maxTake -= take;
pow2 <<=1;
}
}
}
void sol(){
vector<long long> dp(W+1,0);
for (Goods x : goods){
int w = x.w;
long long val = x.val;
for (int j=W;j>=w;j--){
maximize(dp[j],dp[j-w] + val);
}
}
cout<<dp[W];
}
int main(){
setup();
init();
sol();
}
컴파일 시 표준 에러 (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... |