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