# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
782454 | Lyrically | Boxes with souvenirs (IOI15_boxes) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
int read(){int x;scanf("%d",&x);return x;}
void print(int x){printf("%d\n",x);}
const int mod=1000000007;
long long delivery(int n,int k,int L,int p[])
{
multiset<pair<long long,long long>> ldis,rdis,all;
long long l=L;
rep(i,n)
{
if(p[i]<=l-p[i])
{
ldis.insert({p[i],0});all.insert({p[i],0});
}
else
{
rdis.insert({l-p[i],1});all.insert({l-p[i],1});
}
}
long long res=0;
while(!all.empty())
{
if((int)all.size()<=k)
{
long long ld=0,rd=0;
for(auto v:all)
{
if(v.second==0){ld=max(ld,v.first);}
else{rd=max(rd,v.first);}
}
res+=min(2*(ld+rd),l);all.clear();
}
else
{
pair<long long,long long> v=*(--all.end());
all.erase(--all.end());
long long ld=0,rd=0;
if(v.second==0){ld=max(ld,v.first);}
else{rd=max(rd,v.first);}
rep1(i,k-1)
{
v=*(--all.end());
all.erase(v);
if(v.second==0){ld=max(ld,v.first);}
else{rd=max(rd,v.first);}
}
res+=min(2*(ld+rd),l);
}
}
return res;
}
signed main()
{
int N=read(),K=read(),L=read();
int X[N];
rep(i,N){int val=read();X[i]=val;}
cout<<delivery(N,K,L,X)<<endl;
}