QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#701115 | #7739. Knapsack | retired_midlights# | WA | 1ms | 5580kb | C++14 | 1.4kb | 2024-11-02 13:46:31 | 2024-11-02 13:46:32 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (int)a; i <= (int)b; i ++)
#define per(i, a, b) for(int i = (int)a; i >= (int)b; i --)
#define ll long long
#define no { puts("No"); return; }
#define yes { puts("Yes"); return; }
using namespace std;
const int maxn = 5010;
int n, m, k;
ll dp[maxn][maxn * 2];
struct Data {
int w, v;
bool operator < (const Data & a) const {
return v > a.v;
}
} a[maxn];
bool cmp(Data x, Data y) { return x.w > y.w; }
void solve() {
cin >> n >> m >> k;
ll tot = 0, res = 0;
rep(i, 1, n) cin >> a[i].w >> a[i].v;
sort(a + 1, a + 1 + n, cmp);
rep(i, 1, n + 1) rep(j, 0, m) dp[i][j] = 0;
per(i, n, 1) rep(j, 0, m)
if(j >= a[i].w) dp[i][j] = max(dp[i][j], dp[i + 1][j - a[i].w] + a[i].v);
priority_queue < Data > pq;
rep(i, 1, k) pq.push(a[i]), tot += a[i].v;
res = tot;
rep(i, k + 1, n) {
res = max(res, dp[i][m] + tot);
Data tmp = pq.top();
if(a[i].v > tmp.v) {
pq.pop();
tot += a[i].v - tmp.v;
pq.push(a[i]);
}
}
cout << res << '\n';
}
int main() {
#ifdef LOCAL
freopen("data.in", "r", stdin);
#endif
ios :: sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
// cin >> T;
while(T --) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3660kb
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: 1ms
memory: 5580kb
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
Wrong Answer
time: 0ms
memory: 3692kb
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:
1577075983
result:
wrong answer 1st numbers differ - expected: '2009456281', found: '1577075983'