이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
using namespace std;
#define MOD 1000000007
#define int long long
#define endl '\n'
bool scnd(pair<int, int> a, pair<int, int> b){
if(a.first!=b.first) return(a.first<b.first);
else return(a.second>b.second);
}
void solve(){
int s,n; cin>>s>>n;
vector<pair<int, int> > p[s+1];
for(int i=0; i<n; i++){
int v,w,k; cin>>v>>w>>k;
p[w].push_back(make_pair(v,k));
}
vector<pair<int, int> > f;
for(int i=1; i<=s; i++){
sort(p[i].begin(),p[i].end(),scnd);
for(int j=0,k=0; j<s/i; j++){
if(j>=p[i].size()) break;
if(p[i][k].second>0) f.push_back(make_pair(i,p[i][k].first)),p[i][k].second--;
else k++,j--;
}
}
vector<int> dp(s+1);
for(int i=0; i<f.size(); i++){
for(int j=s; j>=f[i].first; j--){
dp[j]=max(dp[j],dp[j-f[i].first]+f[i].second);
}
}
cout<<dp[s];
}
signed main(){
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
cout<<endl;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void solve()':
knapsack.cpp:40:8: 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]
40 | if(j>=p[i].size()) break;
| ~^~~~~~~~~~~~~
knapsack.cpp:46:16: 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]
46 | for(int i=0; i<f.size(); i++){
| ~^~~~~~~~~
# | 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... |