#include <bits/stdc++.h>
using namespace std;
long long n, k, l, m, cnt, t, a, lft[10000010], ans, x, y, v, ans1, rt[10000010], curr, p[10000010];
int main() {
cin>>n>>k>>l;
for (int i = 0; i < n; i++) {
cin>>p[i];
if (p[i] <= l-p[i]) lft[a] = p[i], a++;
else rt[v] = l-p[i], v++;
}
t = a-k+1;
ans = 1e18;
for (int i = 0; i < k; i++) {
curr = ans;
ans = 0;
x = t+i;
y = t+i+k;
if (t < 0) t = 0;
for (int j = 0; j < x; j++) {
cnt++;
if (cnt%k == 0) ans += 2*p[j], cnt = 0;
}
if (cnt) ans += 2*p[x-1], cnt = 0;
for (int j = t+i+k; j < n; j++) {
cnt++;
if (cnt%k == 0) ans += (l-p[j-k+1])*2, cnt = 0;
}
if (cnt) ans += 2*(l - p[n-cnt%k]), cnt = 0;
ans = min(ans, curr);
}
ans += l;
for (int i = 0; i < a; i++) {
cnt++;
if (cnt%k == 0) ans1 += lft[i], cnt = 0;
}
if (cnt) ans1 += 2*lft[a-1], cnt = 0;
sort(rt,rt+v);
for (int i = 0; i < v; i++) {
cnt++;
if (cnt%k == 0) ans1 += rt[i], cnt = 0;
}
if (cnt) ans1 += 2*rt[v-1], cnt = 0;
cout<<min(2*ans1,ans);
}
Compilation message
boxes.cpp: In function 'int main()':
boxes.cpp:24:19: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
for (int j = t+i+k; j < n; j++) {
~~~^~
/tmp/ccYCEKj7.o: In function `main':
grader.c:(.text.startup+0x0): multiple definition of `main'
/tmp/ccTG9Qln.o:boxes.cpp:(.text.startup+0x0): first defined here
/tmp/ccYCEKj7.o: In function `main':
grader.c:(.text.startup+0x22f): undefined reference to `delivery(int, int, int, int*)'
collect2: error: ld returned 1 exit status