이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
const int inf = 1e9;
int s, n;
int dp[2001][2001];
vector<vector<pair<int, int>>> arr(2001);
long long sol(int idx, int weight) {
// base case
if (weight < 0) return -inf;
if (idx == s+1) return 0;
if (dp[idx][weight] != -1) return dp[idx][weight];
long long ans = 0;
// not include
ans = max(ans, sol(idx+1, weight));
// include
int currVal = 0, currwt = 0;
for (int i = 0; i < arr[idx].size(); i++) {
int count = arr[idx][i].s;
bool f = true;
while (count > 0) {
currVal += arr[idx][i].f;
currwt += idx;
if (currwt > weight) {
break;
f = false;
}
count--;
ans = max(ans, currVal + sol(idx+1, weight - currwt));
}
if (!f) break;
}
return dp[idx][weight] = ans;
}
int main () {
cin >> s >> n;
for (int i = 0; i < n; i++) {
int v, w, k; cin >> v >> w >> k;
if (w > s || k <= 0) continue;
arr[w].push_back({v, k});
}
for (int i = 0; i <= 2000; i++) sort (arr[i].begin(), arr[i].end(), greater<>());
memset(dp, -1, sizeof dp);
int ans = sol(1, s);
cout << ans << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'long long int sol(int, int)':
knapsack.cpp:23:23: 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 | for (int i = 0; i < arr[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... |