QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#692951 | #7739. Knapsack | Rosmontis_L# | WA | 0ms | 5832kb | C++20 | 1.5kb | 2024-10-31 15:16:53 | 2024-10-31 15:16:55 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll N = 5e3 + 10;
ll n, W, k;
ll dp[N][10010];
void solve(){
cin >> n >> W >> k;
vector<pii> p(n + 1);
for(ll i = 1; i <= n; i++)
{
auto &[x, y] = p[i];
cin >> x >> y;
}
sort(p.begin() + 1, p.end());
for(ll i = 1; i <= n; i++)
{
auto [x, y] = p[i];
//cout << x << ' ' << y << endl;
for(ll j = x; j <= W; j++)
{
dp[i][j] = dp[i - 1][j];
dp[i][j] = max(dp[i][j], dp[i - 1][j - x] + y);
}
}
if(k == 0)
{
cout << dp[n][W] << '\n';
return ;
}
vector<ll> mx(n + 1);
priority_queue<ll,vector<ll>, greater<>> q;
ll sum = 0;
for(ll i = n; i >= 1; i--)
{
auto [x, y] = p[i];
if(q.empty() || q.size() < k) q.push(y), sum += y;
else
{
auto t = q.top();
if(y > t) q.pop(), q.push(y), sum += y - t;
}
mx[i] = sum;
}
ll res = mx[1];
for(ll i = 1; i < n; i++)
{
ll tsum = dp[i][W] + mx[i + 1];
//cout << i << ' ' << tsum << endl;
res = max(tsum, res);
}
res = max(res, dp[n][W]);
cout << res << '\n';
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll 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: 5832kb
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: 3632kb
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: 3632kb
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'