이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define deb(x) cout<<#x<<'='<<x<<'\n';
#define deb2(x,y) cout<<#x<<'='<<x<<", "<<#y<<'='<<y<<'\n';
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
#define sz(x) (int)(x).size()
const long double PI = acos(-1);
const int mod = 1e9 + 7;
// usaco guide
signed main() {
fastio
int S, N; cin >> S >> N;
map<int, vector<pii>> bywt;
for (int i = 0; i < N; i++) {
int v, w, k; cin >> v >> w >> k;
bywt[w].pb({v, k});
}
int dp[sz(bywt) + 1][S + 1];
memset (dp, -1, sizeof(dp));
dp[0][0] = 0;
int at = 1;
for (auto& pp : bywt) {
int w = pp.f;
vector<pii>& itms = pp.s;
sort(all(itms));
reverse(all(itms));
for (int j = 0; j <= S; j++) {
dp[at][j] = dp[at - 1][j];
int copies = 0;
int type_at = 0;
int prof = 0;
int cur_used = 0;
while ((copies + 1) * w <= j and type_at < sz(itms)) {
copies++;
prof += itms[type_at].f;
if (dp[at - 1][j - w * copies] != -1)
dp[at][j] = max(dp[at][j], dp[at - 1][j - w * copies] + prof);
cur_used++;
if (cur_used == itms[type_at].s) {
cur_used = 0;
type_at++;
}
}
}
at++;
}
int ans = 0;
for (int j = 1; j <= S; j++)
ans = max(ans, dp[at - 1][j]);
cout << ans << '\n';
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... |