QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#738660 | #7739. Knapsack | highkj | WA | 0ms | 3804kb | C++14 | 2.3kb | 2024-11-12 19:39:50 | 2024-11-12 19:39:50 |
Judging History
answer
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define rep1(i,x,y) for(register int i=x;i>=y;--i)
#define int long long
#define fire signed
#define il inline
template<class T> il void print(T x) {
if(x<0) printf("-"),x=-x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
const int bufsize = 230005;
char buf[bufsize], *f1, *f2;
#define getchar() (f1 == f2 && (f2 = buf + fread(f1 = buf, 1, bufsize, stdin)) == buf? EOF: *f1++)
template<class T> il void in(T &x) {
x = 0; char ch = getchar();
int f = 1;
while (ch < '0' || ch > '9') {if(ch=='-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); }
x *= f;
}
int T=1;
const int N=5e3+10,M=1e4+10;
int f[N][M];
struct node{
int v,w;
friend bool operator<(const node&a,const node&b) {
return a.v<b.v;
}
}s[N];
int n,m,k;
int dis[N];
priority_queue<int,vector<int>,greater<int>>q;
void solve() {
in(n),in(m),in(k);
rep(i,1,n) in(s[i].v),in(s[i].w);
sort(s+1,s+1+n);
rep(i,1,n) {
rep(j,0,m) {
if(j<s[i].v) f[i][j]=f[i-1][j];
else f[i][j]=max(f[i-1][j],f[i-1][j-s[i].v]+s[i].w);
}
}
int ss=0;
rep1(i,n,1) {
dis[i]=ss;
if(q.size()<k) {
ss+=s[i].w;
q.push(s[i].w);
}else if(q.top()<s[i].w&&q.size()>k){
ss-=q.top();
q.push(s[i].w);
ss+=s[i].w;
q.pop();
}
}
int res=false;
rep(i,1,n) {
// se.clear();
// rep(j,1,n) vis[j]=false;
// int x=i,mm=m;
// while(x) {
// if(f[x][mm]==f[x-1][mm-s[x].v]+s[x].w) {
// vis[x]=1;
// mm-=s[x].v;
// }
// x--;
// }
// rep(j,i+1,n) {
// if(!vis[j]) {
// se.insert(s[j].w);
// }
// }
// auto it=se.end();
// if(it==se.begin()) continue;
// it--;
// int kk=k;
// int dis=0;
// for(;;it--) {
// dis+=*it;
// if(it==se.begin()) break;
// kk--;
// if(!kk||it==se.begin()) break;
// }
res=max(res,dis[i]+f[i][m]);
}
printf("%lld\n",res);
}
fire main() {
// freopen("knapsack.in","r",stdin);
// freopen("knapsack.out","w",stdout);
while(T--) {
solve();
}
return false;
}
/*
5 5 1
4 13627
3 17167
1 14354
2 8880
2 12354
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3804kb
input:
4 10 1 9 10 10 1 3 5 5 20
output:
26
result:
wrong answer 1st numbers differ - expected: '35', found: '26'