QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#700305 | #7739. Knapsack | yumingsk# | WA | 1ms | 3928kb | C++20 | 2.1kb | 2024-11-02 12:41:02 | 2024-11-02 12:41:04 |
Judging History
answer
#pragma GCC optimize(3, "Ofast", "inline")
#include <iostream>
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define INF 0x3f3f3f3f
#define L_INF 0x7f3f3f3f3f3f3f3f
#define db cout << "debug\n";
using namespace std;
const int Mod = 998244353;
using ll = long long;
int v[5005];
int w[5005];
int dp[5005][10001];
void solve()
{
int n, m;
int k;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
cin >> v[i] >> w[i];
}
// for (int i = 0; i <= n; i++)
// {
// for (int j = 0; j <= m; j++)
// {
// dp[i][j] = INF;
// }
// }
dp[0][0] = 0;
ll ans = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (j >= v[i])
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - v[i]] + w[i]);
else
{
dp[i][j] = dp[i - 1][j];
}
}
}
for (int i = 1; i <= m; i++)
{
ans = max(ans, 1LL * dp[n][i]);
}
// cout << ans << '\n';
int last = m;
vector<int> vis(n + 1, 0);
for (int i = n; i >= 1; i--)
{
if (i == 1)
{
if (last >= v[i])
vis[i] = 1;
break;
}
if (dp[i - 1][last - v[i]] + w[i] == dp[i][last])
{
vis[i] = 1;
last -= v[i];
}
}
priority_queue<int> q;
for (int i = 1; i <= n; i++)
{
if (vis[i] == 0)
{
q.push(w[i]);
}
}
while (k-- && q.size() > 0)
{
ans += q.top();
q.pop();
}
cout << ans << '\n';
}
int main()
{
IOS;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#ifndef ONLINE_JUDGE
clock_t start_time = clock();
#endif
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
#ifndef ONLINE_JUDGE
cout << "Used " << (double)(clock() - start_time) << " ms" << endl;
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3896kb
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: 3676kb
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: 1ms
memory: 3688kb
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: 1ms
memory: 3928kb
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:
3349545497
result:
wrong answer 1st numbers differ - expected: '3765024872', found: '3349545497'