QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#782135 | #7739. Knapsack | Hanoist | WA | 0ms | 3884kb | C++14 | 1.4kb | 2024-11-25 19:02:02 | 2024-11-25 19:02:02 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<functional>
#include<utility>
#include<cassert>
using namespace std;
inline long long read(){
long long x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return x * f;
}
const int N = 5011;
int n,w,k;
long long val[N],res,f[N][N << 1];
struct yjx{
long long w,v;
}a[N];
bool cmp(yjx x,yjx y){
return x.w < y.w;
}
priority_queue<long long,vector<long long>,greater<long long> > P;
int main(){
int t,i,j,k,x,y,s;
n = read(),w = read(),k = read();
for(i = 1;i <= n;i++) a[i].w = read(),a[i].v = read();
sort(a + 1,a + n + 1,cmp);
for(i = 1;i <= n;i++){
for(j = w;j >= a[i].w;j--){
f[i][j] = max(f[i - 1][j],f[i - 1][j - a[i].w] + a[i].v);
val[i] = max(val[i],f[i][j]);
}
}
long long sum = 0;
for(i = n;i >= 1;i--){
if((int)P.size() < k) P.push(a[i].v),sum += a[i].v;
else{
if(a[i].v > P.top()){
sum += a[i].v - P.top();
P.pop();
P.push(a[i].v);
}
}
res = max(res,val[i - 1] + sum);
}
printf("%lld\n",res);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3780kb
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: 3872kb
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: 3884kb
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'