이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll INF=1e9+7;
#define f first
#define s second
int main(){
ll limit, type;
cin>>limit>>type;
map<ll,vector<pair<ll,ll>>> byweight;
for(int i=0;i<type;i++){
ll v,w,copy;
cin>>v>>w>>copy;
if(w<=limit and copy>0)
byweight[w].push_back({v,copy});
}
vector<vector<ll>>dp(byweight.size()+1,vector<ll>(limit+1,-INF));
dp[0][0]=0;
ll at=1;
for(auto &[w,item]:byweight){
sort(item.begin(), item.end(),greater<pair<int, int>>());
for(int i=0;i<=limit;i++){
dp[at][i]=dp[at-1][i];
ll copy=0;
ll type_at=0;
ll curr=0;
ll profit =0;
while((copy+1)*w<=i and type_at<item.size()){
copy++;
profit+=item[type_at].f;
if(dp[at-1][i-copy*w]!=-INF)
dp[at][i]=max(dp[at][i],dp[at-1][i-w*copy]+profit);
curr++;
if(curr==item[type_at].s){
curr=0;
type_at++;
}
}
}
at++;
}
cout<<*max_element(dp.back().begin(),dp.back().end())<<endl;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:32:35: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | while((copy+1)*w<=i and type_at<item.size()){
| ~~~~~~~^~~~~~~~~~~~
# | 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... |