이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
* author: 3omar_ahmed
* date: 09-06-2023
* ya rb awsl CM :)
*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std ;
#define int long long
#define endl '\n'
#define all(a) a.begin() , a.end()
#define alr(a) a.rbegin() , a.rend()
int x, n;
vector < vector < int >> dp;
vector < vector < pair < int , int >>> a;
int DP(int idx, int still) {
if(still == 0 || idx == x) return 0;
if(dp[idx][still] != -1) return dp[idx][still];
int op1 = DP(idx + 1, still);
int op2 = 0, sum = 0, val = 0;
for(int i = 0 ; i < a[idx].size() ; i++) {
for(int j = 0 ; j < a[idx][i].second ; j++) {
sum += idx + 1, val += a[idx][i].first;
if(sum > still) break;
op2 = max(op2, DP(idx + 1, still - sum) + val);
}
}
return dp[idx][still] = max(op1, op2);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
cin >> x >> n;
a = vector < vector < pair < int , int >>> (x + 1);
for(int i = 0 ; i < n ; i++) {
int b, c, d;
cin >> b >> c >> d;
a[c - 1].push_back({b, d});
}
dp = vector < vector < int >> (x + 2, vector < int > (x + 2, -1));
for(auto &i : a) sort(alr(i));
cout << DP(0, x) << endl;
return 0 ;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'long long int DP(long long int, long long int)':
knapsack.cpp:21:23: 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]
21 | for(int i = 0 ; i < a[idx].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... |