This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define pll pair<ll,ll>
#define pii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
ll dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
ll dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const ll INF = 1e9 + 16, M = 1e9 + 7, MaxN = 2e5 +6;
struct item {
ll v, w, c;
};
void solve(int tc) {
int k, n;
cin >> k >> n;
int v[n], w[n], c[n];
for (int i = 0; i < n; ++i) {
cin >> v[i] >> w[i] >> c[i];
}
vi inx(n);
iota(inx.begin(), inx.end(), 0);
sort(inx.begin(), inx.end(), [&](int &i1, int &i2)->bool {
return v[i1] > v[i2];
});
vector<item> a;
vector<int> cnt(k + 1, 0);
for (int i = 0; i < n; ++i) {
int j = inx[i];
int l = 0, r = c[j], ans = 0;
while (l <= r) {
int md = (l + r) / 2;
if (w[j] * md + cnt[w[j]] <= k)
ans = l, l = md + 1;
else
r = md - 1;
}
cnt[w[j]] += ans * w[j];
if (ans > 0)
a.push_back({v[j], w[j], ans});
}
n = a.size();
vector<vll > dp(n + 1, vll(k + 1, 0));
for (int i = 0; i < n; ++i) {
for (ll ct = 0; ct <= a[i].c; ++ct) {
for (int j = k; j >= 0; --j) {
if (j - a[i].w * ct >= 0)
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - a[i].w * ct] + a[i].v * ct);
else
break;
}
}
}
cout << dp[n][k] << '\n';
}
int main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr), cout.tie(nullptr);
int tt = 1;
// cin >> tt;
for (int tc = 1; tc <= tt; tc++) {
solve(tc);
}
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... |