# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
389044 | faresbasbs | Boxes with souvenirs (IOI15_boxes) | C++14 | 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;
long long n,k,l,num,in[10000001],dp1[10000001],dp2[10000001];
int main(){
cin >> n >> k >> l;
num = l/2;
long long ans = 1000000000000000000 , v1 = 0 , v2 = 0;
for(long long i = 1 ; i <= n ; i += 1){
cin >> in[i];
if(in[i] <= num){
if(i >= k){
dp1[i] = dp1[i-k];
}
dp1[i] += 2*in[i];
v1 = dp1[i];
}
}
for(long long i = n ; i >= 1 ; i -= 1){
if(in[i] <= num){
break;
}
if(i+k <= n){
dp2[i] = dp2[i+k];
}
dp2[i] += 2*(l-in[i]);
v2 = dp2[i];
}
ans = v1+v2;
if(k >= n){
ans = min(ans,l);
}
for(long long i = 1 ; i+k-1 <= n ; i += 1){
if(in[i] <= num && in[i+k-1] > num){
ans = min(ans,dp1[i-1]+l+dp2[i+k]);
}
}
cout << ans << endl;
}