QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#713991 | #7739. Knapsack | PoorGhost | WA | 0ms | 3832kb | C++20 | 1.5kb | 2024-11-05 21:11:08 | 2024-11-05 21:11:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int N = 5e3 + 5, M = 1e4 + 5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f3f;
int n, m, k;
int w[N], v[N], dp[M], pre[M];
bool vis[N];
void solve()
{
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
cin >> w[i] >> v[i];
}
for (int i = 1; i <= n; i++)
{
for (int j = m; j >= w[i]; j--)
{
if (dp[j] < dp[j - w[i]] + v[i])
{
pre[j] = i;
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
}
}
// cout << "dp[m] = " << dp[m] << endl;
int j = m;
while (j > 0 && pre[j] > 0)
{
vis[pre[j]] = true;
j = j - w[pre[j]];
// cout << "j = " << j << endl;
}
// cout << "-------------" << endl;
vector<int>sum;
for (int i = 1; i <= n; i++)
{
if (!vis[i])
sum.push_back(v[i]);
// cout << "i = " << i << endl;
}
sort(sum.begin(), sum.end(), [&](int x, int y) {return x > y;});
int ans = dp[m];
for (int i = 0; i < min((int)(sum.size()), k); i++)
{
ans += sum[i];
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int test = 1;
// cin >> test;
for (int i = 1; i <= test; i++)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3564kb
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: 3832kb
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: 3620kb
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: 3648kb
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:
4701474626
result:
wrong answer 1st numbers differ - expected: '3765024872', found: '4701474626'