이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define fo(i, a, b) for (int i = (a), b_ = (b); i <= b_; ++i)
#define fod(i, a, b) for (int i = (a), b_ = (b); i >= b_; --i)
#define rep(i, n) fo(i, 1, n)
#define per(i, n) fod(i, n, 1)
const int N = 2005;
#define pii pair<int, int>
#define fi first
#define se second
int s, n, dp[N][N];
vector<pii> groups[N];
void sol() {
cin >> s >> n;
rep(i, n) {
int v, w, c; cin >> v >> w >> c;
if (w <= s) groups[w].pb({v, c});
}
rep(i, s) {
vector<pii> &v = groups[i];
sort(all(v), greater<pii>());
for (int w = 0; w <= s; ++w) {
dp[i][w] = max(dp[i][w], dp[i-1][w]);
int add_w = 0, add_value = 0;
for (auto &ob : v) {
for (int j = 1; j <= ob.se; ++j) {
add_w += i;
add_value += ob.fi;
if (add_w > w) goto ex;
dp[i][w] = max(dp[i][w], dp[i-1][w-add_w] + add_value);
}
}
ex:;
}
}
int ans = 0;
for (int w = 0; w <= s; ++w)
ans = max(ans, dp[s][w]);
cout << ans;
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
int tc = 1, test = 0; // cin >> tc;
while (++test <= tc) sol();
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:50:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | if (fopen("A.inp", "r")) freopen("A.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |