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

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

#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;
}

📦 Looking for Playrix Codescapes Cup Codeforces Round 413? Check the best deals on Amazon.

🛒 Shop Playrix Codescapes Cup Codeforces Round 413 on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

Affiliate Disclosure: This post contains Amazon affiliate links. If you purchase through these links, eBlogarithm may earn a commission at no extra cost to you. Prices and availability are subject to change.

#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;
}

📦 Looking for Playrix Codescapes Cup Codeforces Round 413? Check the best deals on Amazon.

🛒 Shop Playrix Codescapes Cup Codeforces Round 413 on Amazon

As an Amazon Associate, eBlogarithm earns from qualifying purchases. Prices and availability are subject to change.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Yongxing YogasuitFactory Seamless Thermal Underwear For Winter

When winter arrives, dressing for the freezing weather often feels like an uncomfortable compromise between staying warm and retaining natural freedom of movement. Bulky...

How Viral Kitchen Gadgets Is Quietly Changing the Game

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

Lil Durk Found Not Guilty in Federal Murder-for-Hire Trial

In a dramatic conclusion to one of the hip-hop community's most closely watched legal battles, Chicago rapper Lil Durk has been found not guilty...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!