QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#762519 | #7739. Knapsack | icpc_zhzx034# | WA | 1ms | 7740kb | C++14 | 1.2kb | 2024-11-19 15:21:07 | 2024-11-19 15:21:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5005;
const int M = 1e4 + 5;
struct node {
ll w, v;
node() {}
node(ll w, ll v): w(w), v(v) {}
} a[N];
ll n, W, k, ans, f[N][M];
priority_queue <ll, vector <ll>, greater <ll> > Q;
int main() {
#ifdef LOCAL
assert(freopen("test.in", "r", stdin));
assert(freopen("test.out", "w", stdout));
#endif
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> W >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i].w >> a[i].v;
}
sort(a + 1, a + n + 1, [](node x, node y) {
return x.v < y.v;
});
for (int i = 1; i <= n; ++i) {
for (int j = a[i].w; j <= W; ++j) {
f[i][j] = max(f[i - 1][j], f[i - 1][j - a[i].w] + a[i].v);
}
for (int j = 1; j <= W; ++j) {
f[i][j] = max(f[i][j], f[i][j - 1]);
}
}
ll sum = 0;
for (int i = n; i >= n - k + 1; --i) {
Q.push(a[i].w);
sum += a[i].v;
}
ans = sum + f[n - k][W];
for (int i = n - k; i; --i) {
sum += a[i].v;
Q.push(a[i].w);
W -= Q.top();
Q.pop();
if (W < 0) {
break;
}
ans = max(ans, sum + f[i - 1][W]);
}
cout << ans << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7740kb
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: 3700kb
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: 0
Accepted
time: 0ms
memory: 3660kb
input:
10 50 1 44 182173741 38 163268500 36 114173760 30 521894533 25 89514235 12 516184197 42 971377551 35 28242326 31 480227821 31 388523197
output:
2009456281
result:
ok 1 number(s): "2009456281"
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 3736kb
input:
10 100 3 23 51015869 9 981426050 76 243762017 64 128189636 4 718411601 48 250140255 17 340478117 68 262055220 40 370503079 4 547232664
output:
3714009003
result:
wrong answer 1st numbers differ - expected: '3765024872', found: '3714009003'