QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#725704 | #7739. Knapsack | jr_zlw | WA | 0ms | 3984kb | C++17 | 1.2kb | 2024-11-08 19:28:06 | 2024-11-08 19:28:07 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(a,b,c) for(int c(a);c<=(b);++c)
#define drep(a,b,c) for(int c(a);c>=(b);--c)
using namespace std;
inline int read()
{
int res=0;char ch=getchar();while(ch<'0'||ch>'9')ch=getchar();
while(ch<='9'&&ch>='0')res=res*10+(ch^48),ch=getchar();return res;
}
priority_queue<int,vector<int> ,greater<int> > h;
const int N=5010,WW=10010;
int n,W,K,f[WW],g[WW],s[N];
struct Node
{
int v,w;
inline bool operator < (const Node&x)const
{
return w==x.w?v<x.v:w<x.w;
}
}a[N];
inline void Solve()
{
n=read();W=read();K=read();
rep(1,n,i)a[i].w=read(),a[i].v=read();
sort(a+1,a+n+1);while(!h.empty())h.pop();
s[n+1]=0;
drep(n,max(1,n-K+1),i)
{
h.push(a[i].v);
s[i]=s[i+1]+a[i].v;
}
drep(n-K,1,i)
{
s[i]=s[i+1];
if(a[i].v>h.top())
{
s[i]-=h.top();h.pop();
s[i]+=a[i].v;h.push(a[i].v);
}
}
memset(f,0,sizeof(f));
int ans=s[1];
rep(1,n,i)
{
memcpy(g,f,sizeof(f));
memset(f,0,sizeof(f));
rep(0,W,j)
{
f[j]=g[j];
if(j>=a[i].w)f[j]=max(f[j],g[j-a[i].w]+a[i].v);
}
ans=max(ans,f[W]+s[i+1]);
}
printf("%d\n",ans);
}
int main()
{
int T=1;
while(T--)Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3916kb
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: 3984kb
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: 3864kb
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: 3872kb
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:
2027641330
result:
wrong answer 1st numbers differ - expected: '3765024872', found: '2027641330'