이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define trav(a, x) for(auto& a : x)
#define FOR(i, a, b) for (int i=(a); i<=(signed)(b); i++)
#define ROF(i, a, b) for (int i=(a); i>=(signed)(b); i--)
#define F0R(i, a) for (int i=0; i<(signed)(a); i++)
#define vi vector<int>
#define all(v) (v).begin(), (v).end()
#define f first
#define s second
typedef long long ll;
const int mod = 1e9 + 7, MOD = 998244353;
vector<pair<int, int>> wt[2005];
vector<int> sortwt[2005];
int dp[2005][2005];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
memset(dp, -1e9-15, sizeof(dp));
int s, n; cin >> s >> n;
F0R(i, n) {
int k, w, v; cin >> v >> w >> k;
wt[w].push_back(make_pair(v, k));
}
FOR(i, 1, s) sort(wt[i].rbegin(), wt[i].rend());
FOR(i, 1, s) {
int wwt = 0, vvs = 0;
int j = 0;
while(wwt <= s && j < wt[i].size()) {
wwt+=i;
vvs += wt[i][j].first;
sortwt[i].push_back(vvs);
wt[i][j].second--;
if(wt[i][j].second == 0) j++;
}
}
//trav(u, sortwt[1]) cout << u << " ";
dp[0][0] = 0;
FOR(i, 1, s) {
F0R(j, sortwt[i].size()) {
FOR(k, 0, s) {
if(k - i * (j+1) >= 0) dp[i][k] = max(dp[i][k], dp[i-1][k - i * (j+1)] + sortwt[i][j]);
}
}
FOR(k, 0, s) {
dp[i][k] = max(dp[i][k], dp[i-1][k]);
}
}
int res = 0;
FOR(i, 0, s) {FOR(j, 0, s) res = max(res, dp[i][j]);}
cout << res;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:34:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | while(wwt <= s && j < wt[i].size()) {
| ~~^~~~~~~~~~~~~~
# | 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... |