이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#ifdef EZ
#include "./ez/ez.h"
#else
#include <bits/stdc++.h>
#endif
#define mp make_pair
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
void EZinit() {
#ifndef EZ
cin.tie(0)->sync_with_stdio(0);
#else
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
}
int main()
{
EZinit();
int s, n; cin >> s >> n;
vector<int> dp(s+1);
vector<vector<pair<int,int>>> byWeight(2001);
for (int i = 1; i <= n; ++i)
{
int v, w, k; cin >> v >> w >> k;
byWeight[w].pb(mp(v, k));
}
for (int w = 1; w <= s; ++w)
{
sort(byWeight[w].begin(), byWeight[w].end(), greater<pair<int,int>>());
int used = 0; vector<int> spv{0};
for (int i = 0; i < byWeight[w].size() && used < s/w; ++i)
for (; used < s/w && byWeight[w][i].se; ++used, byWeight[w][i].se--)
spv.pb(spv.back() + byWeight[w][i].fi);
for (int i = s; i >= w; --i)
for (int j = 1; j < spv.size() && i - j*w >= 0; ++j)
dp[i] = max(dp[i], dp[i - j*w] + spv[j]);
}
cout << *max_element(dp.begin(), dp.end());
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:40:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 0; i < byWeight[w].size() && used < s/w; ++i)
| ~~^~~~~~~~~~~~~~~~~~~~
knapsack.cpp:45:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int j = 1; j < spv.size() && i - j*w >= 0; ++j)
| ~~^~~~~~~~~~~~
# | 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... |