이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
//const int MOD=1e9+7;
//typedef pair<ll,ll>Point;
//typedef pair<ll,ll>Line;
//#define x first
//#define y second
ll knapsack(vector<pair<int,int>>arr,int s){ // (weight, reward)
vector<vector<ll>>dp(arr.size()+1,vector<ll>(s+1)); // dp[i][j] = if we take first i elements, how much can we take with j space left
for(int i=1;i<=arr.size();i++){
for(int j=0;j<=s;j++){
dp[i][j]=dp[i-1][j];
if(j-arr[i-1].first>=0){
dp[i][j]=max(dp[i][j],dp[i-1][j-arr[i-1].first]+arr[i-1].second);
}
}
}
return dp[(int)arr.size()][s];
}
void solve(){
int s,n;
cin>>s>>n;
vector<vector<pair<int,int>>>arr(2001);
for(int i=0;i<n;i++){
int w,r,c;
cin>>r>>w>>c;
arr[w].emplace_back(r,c);
}
vector<pair<int,int>>arr2;
for(int i=1;i<2001;i++){
sort(arr[i].begin(),arr[i].end());
reverse(arr[i].begin(),arr[i].end());
int rem=s/i;
for(auto j:arr[i]){
while(j.second&&rem){
arr2.emplace_back(i,j.first);
j.second--;
rem--;
}
if(rem==0)
break;
}
}
cout<<knapsack(arr2,s)<<"\n";
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("bank.in","r",stdin);freopen("bank.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
15 5
4 12 1
2 1 1
10 4 1
1 1 1
2 2 1
15
20 3
5000 15 1
100 1 3
50 1 4
5400
*/
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'll knapsack(std::vector<std::pair<int, int> >, int)':
knapsack.cpp:27:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=1;i<=arr.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... |