Sponsors

Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2), problem: (E) Aquarium decoration Solution In C/C++

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 200010
#define ll long long
#define INF 2000000000
ll Ans=1e18,Res;
int i,j,k,n,Sum,m,x,y,l[4],a[N],b[4][N],p[4],M,f[N];
inline int Min(int x,int y){return x<y?x:y;}
inline ll Min(ll x,ll y){return x<y?x:y;}
int main(){
scanf(“%d%d%d”,&n,&m,&k);
for(i=1;i<=n;i++)scanf(“%d”,&a[i]);
scanf(“%d”,&x);
while(x–)scanf(“%d”,&y),f[y]=1;
scanf(“%d”,&x);
while(x–){
scanf(“%d”,&y);
if(f[y])f[y]=3;else f[y]=2;
}
for(i=1;i<=n;i++)
if(f[i]==3)b[0][++l[0]]=a[i];else
if(f[i]==1)b[1][++l[1]]=a[i];else
if(f[i]==2)b[2][++l[2]]=a[i];else b[3][++l[3]]=a[i],f[i]=l[3];
sort(b[0]+1,b[0]+l[0]+1);sort(b[1]+1,b[1]+l[1]+1);
sort(b[2]+1,b[2]+l[2]+1);sort(b[3]+1,b[3]+l[3]+1);
for(i=0;i<4;i++)b[i][l[i]+1]=INF,p[i]=1;
for(i=0;i<=l[0]&&i<=m;i++){
if(k-i>l[1]||k-i>l[2])continue;
while(p[0]<=i){
Res+=b[0][p[0]++];
Sum++;
}
if(k-i+2==p[1]){
Res-=b[1][–p[1]];
Sum–;
}
while(k-i>=p[1]){
Res+=b[1][p[1]++];
Sum++;
}
if(k-i+2==p[2]){
Res-=b[2][–p[2]];
Sum–;
}
while(k-i>=p[2]){
Res+=b[2][p[2]++];
Sum++;
}
while(Sum<m){
M=INF;
for(j=0;j<4;j++)if(p[j]<=l[j])M=Min(M,b[j][p[j]]);
if(M==INF)break;
for(j=0;j<4;j++)
if(b[j][p[j]]==M){
Res+=b[j][p[j]++];
break;
}
Sum++;
}
if(Sum==m)Ans=Min(Ans,Res);
}
if(Ans==1e18)puts(“-1”);else printf(“%I64d\n”,Ans);
return 0;
}

Ronda Rousey vs. Gina...

The Dream Match: Ronda Rousey vs. Gina Carano at...

Why Everyone is Obsessed with the Portable Handheld Misting Fan

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Mini Magnetic Bag Sealer

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Ronda Rousey vs. Gina Carano MVP MMA live blog

The Dream Match: Ronda Rousey vs. Gina Carano at MVP MMA The world of mixed martial arts is currently witnessing a moment many thought would...

Why Everyone is Obsessed with the Electric Spin Scrubber Pro

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Mini Magnetic Bag Sealer

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Electric Spin Scrubber Pro

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Sunset Projection Lamp LED

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

Why Everyone is Obsessed with the Electric Spin Scrubber Cordless

Are you tired of dealing with everyday frustrations that slow down your routine? We've all been...

The Ultimate Guide to the Keyboard and Car Universal Cleaning Gel

I discovered the Keyboard and Car Universal Cleaning Gel and it is a total game-changer for my daily routine. Here is why you need...