이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define f0r(i,n) for(ll i = 0; i<(ll)(n); i++)
int main(){
ll capacity,n;
cin>>capacity>>n;
ll value[n], weight[n], quantity[n];
f0r(i,n) cin>>value[i]>>weight[i]>>quantity[i];
ll dp[capacity+1]; // in theory: dp[n+1][capacity+1]; but made 1D for memory saving
ll nextDp[capacity+1]; // AUXILIARY FOR TEMPORARILY HOLDING EDITS TO DP
f0r(j,capacity+1) dp[j] = 0; // initial row with nothing considered
f0r(i,n){
// considering item i
for(ll j = capacity; j>=0; j--){
nextDp[j] = 0;
ll buyCount = 0;
for(ll buyCount = 0; j+buyCount*weight[i]<=capacity&&buyCount<=quantity[i]; buyCount++){
nextDp[j] = max(nextDp[j],dp[j+buyCount*weight[i]]+buyCount*value[i]);
}
}
f0r(j,capacity+1){
dp[j] = nextDp[j];
// cout<<setw(5)<<dp[j];
}
// cout<<"\n";
}
cout<<dp[0];
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:20:10: warning: unused variable 'buyCount' [-Wunused-variable]
20 | ll buyCount = 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... |