이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,S,i,j,l,res,dp[2005],s,w,k;
vector<ll> v[2001];
vector<pair<ll,ll>> t[2001];
bool cmp(pair<ll,ll> a,pair<ll,ll> b){
return a.first>b.first;
}
int main(){
cin >> S >> n;
for(i=1;i<=n;i++){
cin >> s >> w >> k;
t[w].push_back({s,k});
}
for(i=1;i<=S;i++){
sort(t[i].begin(),t[i].end(),cmp);
ll mx=S/i;
for(j=0;j<t[i].size();j++){
s=t[i][j].first;
k=t[i][j].second;
if(mx<=k){
for(l=1;l<=mx;l++) v[i].push_back(s);
break;
}
else{
for(l=1;l<=k;l++) v[i].push_back(s);
mx-=k;
}
}
for(j=1;j<v[i].size();j++) v[i][j]+=v[i][j-1];
}
for(i=1;i<=S;i++){
for(l=S;l>=i;l--){
for(j=0;j<v[i].size();j++){
if(l-i*(j+1)<0) break;
dp[l]=max(dp[l],dp[l-i*(j+1)]+v[i][j]);
}
}
}
for(i=0;i<=S;i++) res=max(res,dp[i]);
cout << res;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:19:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(j=0;j<t[i].size();j++){
| ~^~~~~~~~~~~~
knapsack.cpp:31:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(j=1;j<v[i].size();j++) v[i][j]+=v[i][j-1];
| ~^~~~~~~~~~~~
knapsack.cpp:35:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(j=0;j<v[i].size();j++){
| ~^~~~~~~~~~~~
# | 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... |