QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#873148 | #7739. Knapsack | makeran | WA | 0ms | 5724kb | C++14 | 954b | 2025-01-26 09:59:31 | 2025-01-26 09:59:33 |
Judging History
answer
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
struct node {
int w,v;
} obj[5010];
int dp[5010][10010];
int f[5010];
bool cmp(node a,node b) {
if(a.w==b.w) return a.v>b.v;
return a.w<b.w;
}
bool cmp2(int a,int b){
return a>b;
}
signed main() {
int n,w,k;
// freopen("knapsack8.in","r",stdin);
cin>>n>>w>>k;
for(int i=1; i<=n; i++) {
cin>>obj[i].w>>obj[i].v;
}
sort(obj+1,obj+1+n,cmp);
for(int i=1; i<=n; i++) {
for(int j=0; j<=w; j++) {
if(j-obj[i].w>=0)
dp[i][j]=max(dp[i-1][j],dp[i-1][j-obj[i].w]+obj[i].v);
}
}
int res=0;
int temp=0;
int idx=0;
for(int i=n;i>=1;i--){
temp=dp[i][w];
//cout<<dp[i][w]<<"\n";
idx++;
f[idx]=obj[i+1].v;
for(int j=idx;j>1;j--){
if(f[j]>f[j-1]) swap(f[j],f[j-1]);
else break;
}
for(int j=1;j<=min(idx,k);j++){
temp+=f[j];
}
res=max(res,temp);
}
cout<<res;
return 0;
}
// f[1]=0;
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5724kb
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: 3584kb
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: 5716kb
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'