이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#define INF 1e9
using namespace std;
typedef long long ll;
vector<ll>sum[2005];
ll dp[2005][2005];
void solve()
{
ll s, n; cin >> s >> n;
vector<pair<ll, pair<ll, ll>>>a;
for (int i = 1; i <= n; i++) {
ll v, w, k; cin >> v >> w >> k;
a.push_back({ v, {w, k} });
}
sort(a.rbegin(), a.rend());
for (int i = 1; i <= s; i++) sum[i].push_back(0);
for (int i = 0; i < n; i++) {
ll v = a[i].first; ll w = a[i].second.first; ll k = a[i].second.second;
int sz = sum[w].size();
if (sz >= (s / w) + 1) continue;
for (int j = sz; j < min(sz + k, (s / w) + 1); j++) {
sum[w].push_back(sum[w][j - 1] + v);
}
}
for (int i = 1; i <= s; i++) {
for (int j = 0; j <= s; j++) {
for (int k = 0; k < sum[i].size(); k++) {
if (j - (k * i) >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - (k * i)] + sum[i][k]);
}
}
}
cout << dp[s][s] << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;// cin>>t;
while (t--) solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void solve()':
knapsack.cpp:34:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int k = 0; k < sum[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~
# | 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... |