이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ll MOD = 1e9 + 7;
struct item {
int value;
int weight;
int copies;
};
vector<item> items;
vector<vector<int>> dp;
int knapsack(vector<item>& items, int i, int j) {
if(i == items.size()) return 0;
if(dp[i][j] != -1) return dp[i][j];
if(j - items[i].weight >= 0) {
if(items[i].copies > 0) {
items[i].copies--;
int a = knapsack(items, i, j - items[i].weight);
items[i].copies++;
int b = knapsack(items, i + 1, j);
dp[i][j] = max({dp[i][j], a + items[i].value, b});
} else {
dp[i][j] = max(dp[i][j], knapsack(items, i + 1, j));
}
} else {
dp[i][j] = max(dp[i][j], knapsack(items, i + 1, j));
}
return dp[i][j];
}
void solve() {
int s, n;
cin >> s >> n;
items.resize(n);
dp.resize(n + 1, vector<int>(s + 1, -1));
for (int i = 0; i < n; i++) {
cin >> items[i].value >> items[i].weight >> items[i].copies;
}
cout << knapsack(items, 0, s) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int knapsack(std::vector<item>&, int, int)':
knapsack.cpp:17:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | if(i == items.size()) return 0;
| ~~^~~~~~~~~~~~~~~
# | 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... |