QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#665571 | #7739. Knapsack | 369Pai# | RE | 0ms | 3820kb | C++14 | 989b | 2024-10-22 14:08:35 | 2024-10-22 14:08:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int dp[5005][10005];
struct node {int w, v;} a[5005];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> q;
long long sum, ans;
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].w, &a[i].v);
sort(a + 1, a + n + 1, [](const node &x, const node &y) {return x.v > y.v;});
for (int i = n; i >= 1; i--) {
for (int j = 0; j < a[i].v; j++) dp[i][j] = dp[i + 1][j];
for (int j = a[i].w; j <= m; j++)
dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - a[i].w] + a[i].v);
}
for (int i = 1; i <= k; i++) sum += a[i].v, q.emplace(a[i].w, i);
ans = sum + dp[k + 1][m];
for (int W = m, p = k; !q.empty() && W > 0; ) {
int id = q.top().second;
q.pop();
if (a[id].w > W) break;
W -= a[id].w;
if (p < n) {
p++;
sum += a[p].v;
q.emplace(a[p].w, p);
}
ans = max(ans, sum + dp[p + 1][W]);
}
printf("%lld\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3780kb
input:
4 10 1 9 10 10 1 3 5 5 20
output:
35
result:
ok 1 number(s): "35"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
5 13 2 5 16 5 28 7 44 8 15 8 41
output:
129
result:
ok 1 number(s): "129"
Test #3:
score: -100
Runtime Error
input:
10 50 1 44 182173741 38 163268500 36 114173760 30 521894533 25 89514235 12 516184197 42 971377551 35 28242326 31 480227821 31 388523197