Problem:
G: 移動の最小化
問題
N
点が数直線上に存在する。点
i
は座標
x_i
に存在する。
M
点以上を含む長さ
L
の区間が存在するように点を移動させる。移動距離の和を最小化してください。
入力形式
N
M
L
x_1
x_2
...
x_N
制約
2\leq M\leq N \leq 10^5
1\leq L\leq 10^9
0\leq x_1 \lt ... \lt x_N\leq 10^9
入力はすべて整数
出力形式
移動距離の和の最小値を1行で出力してください。
入力例1
3 3 10
1 5 13
出力例1
2
点
1
を
3
に移動させることで条件を満たします。
入力例2
3 3 10
1 5 11
出力例2
0
移動させる必要はありません。
入力例3
7 4 10
2 10 19 22 34 35 41
出力例3
8

Submitted solutions:

code_snippet: #include "bits/stdc++.h"
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FOR(i, a) for (auto &i: a)
#define ALL(obj) begin(obj), end(obj)
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define SUM(x) accumulate(ALL(x), 0LL)
#define LOWER_BOUND(A, key) distance(A.begin(), lower_bound(ALL(A), key))
#define UPPER_BOUND(A, key) distance(A.begin(), upper_bound(ALL(A), key))

using namespace std;
const int MOD    = (int)(1e9 + 7);
const int INF    = (int)(1e13 + 7);
const double EPS = 1e-14;
const double PI  = acos(-1);

int CEIL(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); } //ceil() for int
int mod(int a, int b) { return a >= 0 ? a % b : a - (b * CEIL(a, b)); }             //always return positive num
int bpm(int a, int b) {                                                             //return x^y in order(log(y))
  int res = 1;
  for (a %= MOD; b; a = a * a % MOD, b >>= 1)
    if (b & 1) res = res * a % MOD;
  return res;
}

signed main() {
  int N, M, L;
  cin >> N >> M >> L;
  vector<int> x(N);
  rep(i, N) cin >> x[i];
  int ans = INF;
  rep(i, N - M + 1) {
    int ret   = 0;
    int l     = i;
    int r     = i + M - 1;
    int count = 1;
    if (x[r] - x[l] <= L) {
      ans = 0;
      break;
    }
    while (true) {
      if (x[r - 1] - x[l + 1] <= L) {
        ret += (x[r] - x[l] - L) * count;
        ans = min(ans, ret);
        break;
      } else {
        ret += ((x[r] - x[l]) - (x[r - 1] - x[l + 1])) * count;
        r--;
        l++;
        count++;
      }
    }
  }
  cout << max(ans, 0LL) << endl;
}
accuracy: 1
time_ms: 390
memory_kb: 3496
final_rank: 14

code_snippet: // AOJ #3199 Minimize movement
// 2025.3.21

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(ll n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

int main(){
    int N = Cin(), M = Cin();
    ll L = Cin();
    vector<ll> x(N);
    for (int i=0; i<N; i++) x[i] = Cin();

    vector<ll> P(N+1, 0);
    for (int i=0; i<N; i++) P[i+1] = P[i] + x[i];

    ll ans = LLONG_MAX;

    for (int i = 0; i <= N - M; i++){
        int j = i + M - 1;
        if(x[j] - x[i] <= L){
            ans = 0;
            break;
        }
        auto F = [&](ll a) -> ll {
            int pos = int(lower_bound(x.begin()+i, x.begin()+j+1, a) - x.begin());
            ll cntL = pos - i;
            ll sumL = P[pos] - P[i];
            ll costL = a * cntL - sumL;
            int pos2 = int(upper_bound(x.begin()+i, x.begin()+j+1, a+L) - x.begin());
            ll cntR = (j + 1) - pos2;
            ll sumR = P[j+1] - P[pos2];
            ll costR = sumR - (a + L) * cntR;
            return costL + costR;
        };

        ll lo = x[i], hi = x[j];
        while(hi - lo > 2){
            ll m1 = lo + (hi - lo) / 3;
            ll m2 = hi - (hi - lo) / 3;
            ll f1 = F(m1);
            ll f2 = F(m2);
            if(f1 > f2) lo = m1;
            else hi = m2;
        }
        for(ll a = lo; a <= hi; a++) ans = min(ans, F(a));
    }
    Cout(ans);
    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 4232
final_rank: 10

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int N,M,L;
long x[1<<17];
long Lsum[1<<17],Rsum[1<<17];
long calc()
{
	long ans=9e18;
	Rsum[0]=0;
	for(int i=0;i<N;i++)
	{
		Rsum[i+1]=Rsum[i]+x[i];
	}
	Lsum[N]=0;
	for(int i=N;i--;)
	{
		Lsum[i]=Lsum[i+1]+(long)1e9-x[i];
	}
	for(int i=0;i<N;i++)
	{
		long l=x[i];
		long r=l+L;
		int dL=-1,dR=1e9;
		while(dR-dL>1)
		{
			int mid=dL+dR>>1;
			if(upper_bound(x,x+N,r+mid)-lower_bound(x,x+N,l-mid)>=M)dR=mid;
			else dL=mid;
		}
		int li=lower_bound(x,x+N,l-dR)-x;
		int ri=upper_bound(x,x+N,r+dR)-x;
		int j=upper_bound(x,x+N,r)-x;
		long now=Rsum[ri]-Rsum[j]-r*(ri-j);
		now+=Lsum[li]-Lsum[i]-((long)1e9-x[i])*(i-li);
		ans=min(ans,now);
	}
	return ans;
}
int main()
{
	cin>>N>>M>>L;
	for(int i=0;i<N;i++)cin>>x[i];
	long a=calc();
	for(int i=0;i<N;i++)x[i]=1e9-x[i];
	reverse(x,x+N);
	cout<<min(a,calc())<<endl;
}
accuracy: 1
time_ms: 250
memory_kb: 5192
final_rank: 11

code_snippet: #include <bits/stdc++.h>

using namespace std;

long long solve(vector<long long> x, long long m, long long l)
{
    long long res = 1e18;
    vector<long long> p = x;
    for (int i = 1; i < p.size(); i++)
        p[i] += p[i - 1];
    int j = 0;
    int n = x.size();
    for (int i = 0; i < n; i++)
    {
        long long st = x[i];
        long long fn = x[i] + l;
        while (j + 1 < n && x[j + 1] <= fn)
            j++;
        int cnt = j - i + 1;
        if (cnt >= m)
        {
            res = 0;
        }
        else
        {
            int want = m - cnt;
            int left = i, right = n - j - 1;
            {
                int tookl = min(left, want);
                int tookr = want - tookl;
                if (tookl >= 0 && tookl <= left && tookr >= 0 && tookr <= right)
                {
                    long long costL = st * tookl - ((i ? p[i - 1] : 0) - (i - tookl ? p[i - tookl - 1] : 0));
                    long long costR = (p[j + tookr] - p[j]) - fn * tookr;
                    res = min(res, costL + costR);
                }
            }
            {
                int tookr = min(right, want);
                int tookl = want - tookr;
                if (tookl >= 0 && tookl <= left && tookr >= 0 && tookr <= right)
                {
                    long long costL = st * tookl - ((i ? p[i - 1] : 0) - (i - tookl > 0 ? p[i - tookl - 1] : 0));
                    long long costR = (p[j + tookr] - p[j]) - fn * tookr;
                    res = min(res, costL + costR);
                }
            }
            {
                int tookl = want / 2;
                int tookr = (want + 1) / 2;
                if (tookl >= 0 && tookl <= left && tookr >= 0 && tookr <= right)
                {
                    long long costL = st * tookl - ((i ? p[i - 1] : 0) - (i - tookl > 0 ? p[i - tookl - 1] : 0));
                    long long costR = (p[j + tookr] - p[j]) - fn * tookr;
                    res = min(res, costL + costR);
                }
            }
            {
                int tookr = want / 2;
                int tookl = (want + 1) / 2;
                if (tookl >= 0 && tookl <= left && tookr >= 0 && tookr <= right)
                {
                    long long costL = st * tookl - ((i ? p[i - 1] : 0) - (i - tookl > 0 ? p[i - tookl - 1] : 0));
                    long long costR = (p[j + tookr] - p[j]) - fn * tookr;
                    res = min(res, costL + costR);
                }
            }
        }
    }
    return res;
}

int main()
{
    int n, m, l;
    cin >> n >> m >> l;
    vector<long long> x(n);
    for (int i = 0; i < n; i++)
        cin >> x[i];
    long long a = solve(x, m, l);
    for (int i = 0; i < n; i++)
        x[i] = 1e9 - x[i];
    reverse(x.begin(), x.end());
    long long b = solve(x, m, l);
    //cout << a << " " << b << endl;
    cout << min(a, b) << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 4824
final_rank: 3

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 9223372036854775807
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 100005


struct Data{

	ll L1,R1,L2,R2,num;
};

int N,M;
ll LEN;
ll X[SIZE],ruiseki1_L[SIZE],ruiseki2_L[SIZE];
ll ruiseki1_R[SIZE],ruiseki2_R[SIZE];


ll getNUM(ll L,ll R){

	//座標L～Rにある点の個数を求める
	int left = 0,right = N-1,mid = (left+right)/2;
	int head = N;
	while(left <= right){

		if(X[mid] >= L){

			head = mid;
			right = mid-1;
		}else{

			left = mid+1;
		}
		mid = (left+right)/2;
	}

	left = 0,right = N-1,mid = (left+right)/2;
	int tail = -1;
	while(left <= right){

		if(X[mid] <= R){

			tail = mid;
			left = mid+1;
		}else{

			right = mid-1;
		}
		mid = (left+right)/2;
	}

	return max(0,tail-head+1);
}

Data count_outer(ll L,ll R,ll dist){

	Data ret;
	int num_L,num_R;

	ll left = 0,right = N-1,mid = (left+right)/2;
	int index_L = N;

	while(left <= right){

		if(X[mid] >= L-dist){

			index_L = mid;
			right = mid-1;
		}else{

			left = mid+1;
		}
		mid = (left+right)/2;
	}

	left = 0,right = N-1,mid = (left+right)/2;
	int index_R = -1;

	while(left <= right){

		if(X[mid] <= L-1){

			index_R = mid;
			left = mid+1;
		}else{

			right = mid-1;
		}
		mid = (left+right)/2;
	}

	num_L = max(0,index_R-index_L+1);
	ret.L1 = index_L;
	ret.R1 = index_R;


	left = 0,right = N-1,mid = (left+right)/2;
	index_L = N;

	while(left <= right){

		if(X[mid] >= R+1){

			index_L = mid;
			right = mid-1;
		}else{

			left = mid+1;
		}
		mid = (left+right)/2;
	}

	left = 0,right = N-1,mid = (left+right)/2;
	index_R = -1;

	while(left <= right){

		if(X[mid] <= R+dist){

			index_R = mid;
			left = mid+1;
		}else{

			right = mid-1;
		}
		mid = (left+right)/2;
	}

	num_R = max(0,index_R-index_L+1);

	ret.L2 = index_L;
	ret.R2 = index_R;

	ret.num = num_L+num_R;

	return ret;
}
int main(){

	//canf("%d %d %lld",&N,&M,&LEN);

	cin >> N >> M >> LEN;


	ruiseki1_L[0] = 0;
	ruiseki2_L[0] = 0;

	for(int i = 0; i < N; i++){

		cin >> X[i];
		if(i == 0){

			ruiseki1_L[1] = 0;
		}else{

			ruiseki1_L[i+1] = X[i]-X[i-1];
			ruiseki1_L[i+1] += ruiseki1_L[i];
		}
	}

	for(int i = 1; i <= N; i++){

		ruiseki2_L[i] = ruiseki1_L[i];
		ruiseki2_L[i] += ruiseki2_L[i-1];
	}
	for(int i = N-1; i >= 0; i--){

		if(i == N-1){

			ruiseki1_R[N] = 0;
		}else{

			ruiseki1_R[i+1] = X[i+1]-X[i];
			ruiseki1_R[i+1] += ruiseki1_R[i+2];
		}
	}
	for(int i = N-1; i >= 0; i--){

		ruiseki2_R[i] = ruiseki1_R[i];
		ruiseki2_R[i] += ruiseki2_R[i+1];
	}

	for(int i = 0; i+M-1 < N; i++){

		if(X[i+M-1]-X[i] <= LEN){

			printf("0\n");
			return 0;
		}
	}

	ll ans = HUGE_NUM;

	//左端決め打ち
	for(int k = 0; k < N; k++){

		ll head = X[k],tail=X[k]+LEN;
		ll count = getNUM(head,tail);
		ll diff_R = tail-X[k+count-1];

		int left = 0,right = BIG_NUM,mid = (left+right)/2;

		Data min_data;

		while(left <= right){

			Data data = count_outer(head,tail,mid);

			if(count+data.num >= M){

				min_data = data;
				right = mid-1;

			}else{

				left = mid+1;
			}
			mid = (left+right)/2;
		}

		ll tmp_ans = 0;

		ll num_L = 0,num_R = 0;

		//printf("L1:%lld R1:%lld L2:%lld R2:%lld\n",min_data.L1,min_data.R1,min_data.L2,min_data.R2);

		if(min_data.L1 <= min_data.R1){

			tmp_ans += ruiseki2_R[min_data.L1+1]-ruiseki2_R[min_data.R1+1]-(min_data.R1-min_data.L1)*ruiseki1_R[min_data.R1+1];
			tmp_ans += (head-X[min_data.R1])*(min_data.R1-min_data.L1+1);
			num_L = min_data.R1-min_data.L1+1;
		}
		if(min_data.L2 <= min_data.R2){

			tmp_ans += ruiseki2_L[min_data.R2+1]-ruiseki2_L[min_data.L2+1]-(min_data.R2-min_data.L2)*ruiseki1_L[min_data.L2+1];
			tmp_ans += (X[min_data.L2]-tail)*(min_data.R2-min_data.L2+1);
			num_R = min_data.R2-min_data.L2+1;
		}

		if(num_L > num_R){
			ll diff_L = X[k]-(X[min_data.R1]+1);

			/*printf("k:%d diff_L:%lld diff_R:%lld\n",k,diff_L,diff_R);
			printf("num_L:%lld num_R:%lld tmp_ans:%lld\n",num_L,num_R,tmp_ans);*/

			tmp_ans -= min(diff_L,diff_R)*(num_L-num_R);
		}

		ans = min(ans,tmp_ans);
	}


	//右端決め打ち
	for(int k = 0; k < N; k++){

		ll tail = X[k],head=X[k]-LEN;
		ll count = getNUM(head,tail);
		ll diff_L = X[k-(count-1)]-head;

		int left = 0,right = BIG_NUM,mid = (left+right)/2;

		Data min_data;

		while(left <= right){

			Data data = count_outer(head,tail,mid);

			if(count+data.num >= M){

				min_data = data;
				right = mid-1;

			}else{

				left = mid+1;
			}
			mid = (left+right)/2;
		}

		ll tmp_ans = 0;

		ll num_L = 0,num_R = 0;

		//printf("L1:%lld R1:%lld L2:%lld R2:%lld\n",min_data.L1,min_data.R1,min_data.L2,min_data.R2);

		if(min_data.L1 <= min_data.R1){

			tmp_ans += ruiseki2_R[min_data.L1+1]-ruiseki2_R[min_data.R1+1]-(min_data.R1-min_data.L1)*ruiseki1_R[min_data.R1+1];
			tmp_ans += (head-X[min_data.R1])*(min_data.R1-min_data.L1+1);
			num_L = min_data.R1-min_data.L1+1;
		}
		if(min_data.L2 <= min_data.R2){

			tmp_ans += ruiseki2_L[min_data.R2+1]-ruiseki2_L[min_data.L2+1]-(min_data.R2-min_data.L2)*ruiseki1_L[min_data.L2+1];
			tmp_ans += (X[min_data.L2]-tail)*(min_data.R2-min_data.L2+1);
			num_R = min_data.R2-min_data.L2+1;
		}

		if(num_L < num_R){
			ll diff_R =	(X[min_data.L2]-1)-X[k];

			/*printf("k:%d diff_L:%lld diff_R:%lld\n",k,diff_L,diff_R);
			printf("num_L:%lld num_R:%lld tmp_ans:%lld\n",num_L,num_R,tmp_ans);*/

			tmp_ans -= min(diff_L,diff_R)*(num_L-num_R);
		}

		ans = min(ans,tmp_ans);
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 570
memory_kb: 6264
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
using tp = tuple<ll, ll, ll>;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
//constexpr long long MOD = (ll)1e9 + 7;
constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

//グラフ関連
struct Edge {
    ll to, cap, rev;
    Edge(ll _to, ll _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve() {
    ll N, M, L;
    cin >> N >> M >> L;
    vec<ll> x(N);
    REP(i, N) {
        cin >> x[i];
    }
    vec<ll> sum(N + 1, 0);
    REP(i, N) {
        sum[i + 1] = sum[i] + x[i];
    }

    ll ans = INF;
    REP(i, N - M + 1) {
        if(x[i + M - 1] - x[i] <= L) {
            cout << 0 << en;
            return;
        }

        ll l = x[i];
        ll r = x[i + M - 1];
        while(l + 1 < r) {
            ll m = l + r >> 1;
            ll A = lower_bound(all(x), m) - x.begin() - i;
            ll B = max(0LL, i + M - (upper_bound(all(x), m + L) - x.begin()));
            if(A < B)
                l = m;
            else
                r = m;
        }

        ll A = lower_bound(all(x), r) - x.begin() - i;
        ll B = max(0LL, i + M - (upper_bound(all(x), r + L) - x.begin()));
        ll tmp = A * r - (sum[i + A] - sum[i]);
        tmp += max(0LL, sum[i + M] - sum[i + M - B] - B * (r + L));
        chmin(ans, tmp);
    }
    cout << ans << en;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    /*
    ll t;
    cin >> t;
    while(t--)*/
    solve();

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 4340
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  Int n,m,len;
  cin>>n>>m>>len;
  auto xs=read(n);

  const Int INF = 1e18;
  Int ans=INF;
  for(Int uku=0;uku<2;uku++){
    vector<Int> sm(n+1,0);
    for(Int i=0;i<n;i++) sm[i+1]=sm[i]+xs[i];
    auto query=[&](Int l,Int r){return sm[r]-sm[l];};

    for(Int i=0;i<n;i++){
      Int j=upper_bound(xs.begin(),xs.end(),xs[i]+len)-xs.begin();
      // [0, i), [i, j), [j, n)

      Int x=i-0;
      Int y=j-i;
      Int z=n-j;

      if(y>=m) drop(0);

      Int need=m-y;
      auto calc=[&](Int lft)->Int{
        Int rgh=need-lft;
        assert(0<=lft and lft<=x);
        assert(0<=rgh and rgh<=z);

        Int res=(xs[i]*lft)-query(i-lft,i)+query(j,j+rgh)-(xs[i]+len)*rgh;
        chmin(ans,res);
        return res;
      };

      Int L=max(0LL,need-z)-1,R=min(x,need);
      calc(R);
      while(L+1<R){
        Int M=(L+R)>>1;
        if(calc(M)<calc(M+1)) R=M;
        else L=M;
      }
    }

    reverse(xs.begin(),xs.end());
    for(Int &x:xs) x*=-1;
  }
  cout<<ans<<newl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4284
final_rank: 1

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}

int n,m;ll L;
vector<ll> x,S;

ll g(int x,ll A,int z1,int z2){
    return (2*A+L)*x+S[z1-x]+S[z2-x];
}

ll calc(int s,int t,ll A){
    int v=m-(t-s+1);
    if(v<=0) return 0;
    int z1=s,z2=t+v+1;
    int l=max(0,z2-n),r=z1-1;
    ll F=INF;
    //cout << A << " " << s << " " << t << endl;
    if(r-l>1){
        while(r-l>1){
           int mid=(r+l)/2;
           ll D=(2*A+L)-x[z1-mid-1]-x[z2-mid-1];
           if(D<=0)l=mid;
           else r=mid; 
        }
       chmin(F,g(l+1,A,z1,z2));
       //cout << l << " " << g(l,A,z1,z2)-(A+L)*v-S[s]-S[t+1] << endl;
    }
    chmin(F,g(max(0,z2-n),A,z1,z2));
    //cout << max(0,z2-n) << " " << g(max(0,z2-n),A,z1,z2)-(A+L)*v-S[s]-S[t+1] << endl;
    chmin(F,g(z1,A,z1,z2));
    //cout << z1 << " " << g(z1,A,z1,z2)-(A+L)*v-S[s]-S[t+1] << endl;
    return F-(A+L)*v-S[s]-S[t+1];
}

void solve(){
    cin >> n >> m >> L;
    x.resize(n);S.resize(n+1);
    rep(i,n){
        cin >> x[i];
        S[i+1]=S[i]+x[i];
    }
    ll ans=INF;
    rep(s,n){
        int t=upper_bound(x.begin(),x.end(),x[s]+L)-x.begin()-1;
        chmin(ans,calc(s,t,x[s]));
        //cout << x[s] << " " << calc(s,t,x[s]) << endl;
    }
    rep(t,n){
        int s=lower_bound(x.begin(),x.end(),x[t]-L)-x.begin();
        chmin(ans,calc(s,t,x[t]-L));
        //cout << x[t]-L << " " << s << " " << t << " " << calc(s,t,x[t]-L) << endl;
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
accuracy: 1
time_ms: 20
memory_kb: 4292
final_rank: 2

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>

using lint = long long;
constexpr lint INF = 1LL << 60;

void abort() {
    std::cout << "0\n";
    std::exit(0);
}

void solve() {
    int n, m, k;
    std::cin >> n >> m >> k;

    std::vector<lint> xs(n);
    for (auto& x : xs) std::cin >> x;

    std::vector<lint> xsum(n + 1, 0);
    for (int i = 0; i < n; ++i) xsum[i + 1] = xsum[i] + xs[i];

    lint ans = INF;
    for (int i = 0; i + m <= n; ++i) {
        // x[i, i+m)を長さkに収める

        if (xs[i + m - 1] - xs[i] <= k) abort();

        // [ok, ok+k]にて、lout <= rout
        lint ok = xs[i], ng = xs[i + m - 1] - k;
        while (ng - ok > 1) {
            auto mid = (ok + ng) / 2;

            int lout = std::lower_bound(xs.begin(), xs.end(), mid) -
                       (xs.begin() + i);
            int rout = (xs.begin() + i + m) -
                       std::upper_bound(xs.begin(), xs.end(), mid + k);
            if (lout <= rout) {
                ok = mid;
            } else {
                ng = mid;
            }
        }

        // コストを求める
        int lout = std::lower_bound(xs.begin(), xs.end(), ok) -
                   (xs.begin() + i);
        int rout = (xs.begin() + i + m) -
                   std::upper_bound(xs.begin(), xs.end(), ok + k);
        auto lcost = lout * ok - (xsum[i + lout] - xsum[i]);
        auto rcost = (xsum[i + m] - xsum[i + m - rout]) - rout * (ok + k);

        ans = std::min(ans, lcost + rcost);
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 4196
final_rank: 7

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>

using lint = long long;
constexpr lint INF = 1LL << 60;

void abort() {
    std::cout << "0\n";
    std::exit(0);
}

void solve() {
    int n, m, k;
    std::cin >> n >> m >> k;

    std::vector<lint> xs(n);
    for (auto& x : xs) std::cin >> x;

    std::vector<lint> xsum(n + 1, 0);
    for (int i = 0; i < n; ++i) xsum[i + 1] = xsum[i] + xs[i];

    lint ans = INF;
    for (int i = 0; i + m <= n; ++i) {
        // x[i, i+m)を長さkに収める

        if (xs[i + m - 1] - xs[i] <= k) abort();

        // [ok, ok+k]にて、lout <= rout
        lint ok = xs[i], ng = xs[i + m - 1] - k;
        while (ng - ok > 1) {
            auto mid = (ok + ng) / 2;

            int lout = std::lower_bound(xs.begin(), xs.end(), mid) -
                       (xs.begin() + i);
            int rout = (xs.begin() + i + m) -
                       std::upper_bound(xs.begin(), xs.end(), mid + k);
            if (lout <= rout) {
                ok = mid;
            } else {
                ng = mid;
            }
        }

        int lout = std::lower_bound(xs.begin(), xs.end(), ok) -
                   (xs.begin() + i);
        int rout = (xs.begin() + i + m) -
                   std::upper_bound(xs.begin(), xs.end(), ok + k);
        auto lcost = lout * ok - (xsum[i + lout] - xsum[i]);
        auto rcost = (xsum[i + m] - xsum[i + m - rout]) - rout * (ok + k);
        ans = std::min(ans, lcost + rcost);
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 4120
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int main() {
	int N, M, L;
	cin >> N >> M >> L;
	
	vector<int> X(N);
	REP(i, N) scanf("%d", &X[i]);
	
	vector<ll> sum(N + 1); // [i] := sum(x [0, i) )
	REP(i, N) sum[i + 1] = sum[i] + X[i];
	
	ll ans = longINF;
	REP(i, N - M + 1) {
		// x [i, i + M - 1]
		int lidx = i, ridx = i + M - 1; // [lidx, ridx]
		if (X[ridx] - X[lidx] <= L) {
			chmin(ans, 0);
			continue;
		}
		int l = X[lidx], r = X[ridx] - L + 1;
		while (r - l > 1) {
			int m = (l + r) >> 1;
			// [m, m + L]
			// [X[lidx], m) の個数 cl
			// (m + L, X[ridx]] の個数 cr
			int lwidx = lower_bound(ALL(X), m) - X.begin();
			int cl = lwidx - lidx;
			
			int upidx = upper_bound(ALL(X), m + L) - X.begin();
			int cr = ridx - upidx + 1;
			
			if (cl <= cr) l = m;
			else r = m;
		}
	
		int lwidx = lower_bound(ALL(X), l) - X.begin();
		int cl = lwidx - lidx;
		
		int upidx = upper_bound(ALL(X), l + L) - X.begin();
		int cr = ridx - upidx + 1;
		
		ll cur = (ll)cl * l;
		cur -= sum[lwidx] - sum[lidx];
		
		cur += sum[ridx + 1] - sum[upidx];
		cur -= (ll)cr * (l + L);
		chmin(ans, cur);
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 4184
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int N, M, L;
vector<ll> x, cum, cum2;

ll calc(ll y) {
    ll lb = -1, ub = 1e9;
    while (ub - lb > 1) {
        ll m = (lb + ub) / 2;
        int l = lower_bound(x.begin(), x.end(), y - m) - x.begin();
        int r = upper_bound(x.begin(), x.end(), y + L + m) - x.begin() - 1;
        if (r - l + 1 >= M) ub = m;
        else lb = m;
    }
    ll m = ub;
    int l = lower_bound(x.begin(), x.end(), y - m) - x.begin();
    int i = lower_bound(x.begin(), x.end(), y) - x.begin();
    ll tmp = (i - l) * y - ((i ? cum2[i-1] : 0) - (l ? cum2[l-1] : 0));
    int r = upper_bound(x.begin(), x.end(), y + L + m) - x.begin() - 1;
    int j = upper_bound(x.begin(), x.end(), y + L) - x.begin() - 1;
    tmp += cum2[r] - cum2[j] - (r - j) * (y + L);
    if (r - l + 1 == M + 1) tmp -= m;
    return tmp;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> M >> L;
    x.resize(N);
    for (int i = 0; i < N; i++) cin >> x[i];
    cum.resize(N);
    cum[0] = x[0];
    for (int i = 1; i < N; i++) cum[i] = cum[i-1] + x[i];
    cum2.resize(N);
    cum2[0] = cum[0];
    for (int i = 1; i < N; i++) cum2[i] = cum2[i-1] + x[i];
    ll ans = 1e18;
    for (int i = 0; i < N; i++) {
        ans = min(ans, calc(x[i]));
        ans = min(ans, calc(x[i] - L));
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 270
memory_kb: 4648
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int N, M, L;
vector<ll> x, cum, cum2;

ll calc(int y) {
    ll lb = -1, ub = 1e9;
    while (ub - lb > 1) {
        ll m = (lb + ub) / 2;
        int l = lower_bound(x.begin(), x.end(), y - m) - x.begin();
        int r = upper_bound(x.begin(), x.end(), y + L + m) - x.begin();
        if (r - l >= M) ub = m;
        else lb = m;
    }
    ll m = ub;
    int l = lower_bound(x.begin(), x.end(), y - m) - x.begin();
    int i = lower_bound(x.begin(), x.end(), y) - x.begin();
    ll tmp = (i - l) * y - ((i ? cum2[i-1] : 0) - (l ? cum2[l-1] : 0));
    int r = upper_bound(x.begin(), x.end(), y + L + m) - x.begin() - 1;
    int j = upper_bound(x.begin(), x.end(), y + L) - x.begin() - 1;
    tmp += cum2[r] - cum2[j] - (r - j) * (y + L);
    if (r - l + 1 == M + 1) tmp -= m;
    return tmp;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> M >> L;
    x.resize(N);
    for (int i = 0; i < N; i++) cin >> x[i];
    cum.resize(N);
    cum[0] = x[0];
    for (int i = 1; i < N; i++) cum[i] = cum[i-1] + x[i];
    cum2.resize(N);
    cum2[0] = cum[0];
    for (int i = 1; i < N; i++) cum2[i] = cum2[i-1] + x[i];
    ll ans = 1e18;
    for (int i = 0; i < N; i++) {
        ans = min(ans, calc(x[i]));
        ans = min(ans, calc(x[i] - L));
    }
    cout << ans << endl;
}
accuracy: 0.8412698412698413
time_ms: 70
memory_kb: 3812
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int N, M, L;
vector<ll> x, cum, cum2;

ll calc(int y) {
    ll lb = -1, ub = 1e9;
    while (ub - lb > 1) {
        ll m = (lb + ub) / 2;
        int l = lower_bound(x.begin(), x.end(), y - m) - x.begin();
        int r = upper_bound(x.begin(), x.end(), y + L + m) - x.begin();
        if (r - l >= M) ub = m;
        else lb = m;
    }
    ll m = ub;
    int l = lower_bound(x.begin(), x.end(), y - m) - x.begin();
    int i = lower_bound(x.begin(), x.end(), y) - x.begin();
    ll tmp = (i - l) * y - ((i ? cum2[i-1] : 0) - (l ? cum2[l-1] : 0));
    int r = upper_bound(x.begin(), x.end(), y + L + m) - x.begin() - 1;
    int j = upper_bound(x.begin(), x.end(), y + L) - x.begin() - 1;
    tmp += cum2[r] - cum2[j] - (r - j) * (y + L);
    return tmp;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> M >> L;
    x.resize(N);
    for (int i = 0; i < N; i++) cin >> x[i];
    cum.resize(N);
    cum[0] = x[0];
    for (int i = 1; i < N; i++) cum[i] = cum[i-1] + x[i];
    cum2.resize(N);
    cum2[0] = cum[0];
    for (int i = 1; i < N; i++) cum2[i] = cum2[i-1] + x[i];
    ll ans = 1e18;
    for (int i = 0; i < N; i++) {
        ans = min(ans, calc(x[i]));
        ans = min(ans, calc(x[i] - L));
    }
    cout << ans << endl;
}
accuracy: 0.8412698412698413
time_ms: 70
memory_kb: 3760
final_rank: 17

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 2020000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << "debug: " << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << "debug: " << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int main(){
	ll n,m,L; cin >> n >> m >> L;
	vl x(n); rep(i,n) cin >> x[i];
	vl sum(n+1,0);
	rep(i,n) sum[i+1] = sum[i] + x[i];
	ll ans = linf;
	auto f = [&](int id, ll pos) -> ll{
		auto itr = upper_bound(all(x),pos);
		int d = itr - x.begin();
		ll res = pos * (d - id) - (sum[d] - sum[id]);
		itr = upper_bound(all(x), pos + L);
		d = itr - x.begin();
		res += sum[id+m] - sum[d] - (pos + L) * (id+m - d);
		//print3(id,pos,res);
		return res;
	};
	rep(i,n-m+1){
		if(x[i+m-1] - x[i] <= L){
			cout << 0 << "\n";
			return 0;
		}
		ll left = x[i]-1, right = x[i+m-1] - L + 1;
		while(right - left > 2){
			ll l = (left * 2 + right) / 3;
			ll r = (left + right * 2) / 3;
			if(f(i,l) < f(i,r)) right = r;
			else left = l;
		}
		chmin(ans, f(i,left+1));
	}
	cout << ans << "\n";
}
accuracy: 1
time_ms: 300
memory_kb: 4036
final_rank: 13

code_snippet: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

const ll INF=1e18;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N,M;
  ll L;
  cin>>N>>M>>L;
  vector<ll> X(N);
  rep(i,N) cin>>X[i];

  vector<ll> ls(N+1,0);
  rep(i,N) ls[i+1]=ls[i]+X[i];
  vector<ll> rs(N+1,0);
  for(int i=N-1;i>=0;i--) rs[i]=rs[i+1]+X[i];

  for(int i=0;i+M-1<N;i++){
      if(X[i+M-1]-X[i]<=L){
          cout<<0<<endl;
          return 0;
      }
  }

  ll ans=INF;
  for(int i=0;i+M-1<N;i++){
      int st=i;int ed=i+M-1;
      int lef=X[st],rig=X[i+M-1]-L;
      while(rig-lef>1){
          int mid=(lef+rig)/2;
          int l=lower_bound(all(X),mid)-X.begin();
          l-=st;
          int r=upper_bound(all(X),mid+L)-X.begin();
          r=max(0,ed+1-r);

          if(l<r) lef=mid;
          else rig=mid;
      }

      auto calc = [&](ll ps){
          ll pe=ps+L;
          int l=lower_bound(all(X),ps)-X.begin();
          l-=st;
          int r=upper_bound(all(X),pe)-X.begin();
          r=max(0,ed+1-r);

          ll res=0;
          res+=ps*l-(ls[st+l]-ls[st]);
          res+=(rs[ed+1-r]-rs[ed+1])-pe*r;

          return res;
      };

      chmin(ans,calc(lef));
      chmin(ans,calc(rig));
  }
  cout<<ans<<endl;

  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 4464
final_rank: 9

code_snippet: // 添え字ぶっこわれたぜ！

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
#define totori main
#define tottori signed
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
ll dx[4]={0,1,-1,0};
ll dy[4]={1,0,0,-1};

ll n,m,w,x[100010];

ll solve(){
    ll ans = 1e18;
    // 右端から、(左から見て)i番目まで取るときの距離の合計
    ll rsum[n+1]={};
    // 左端から、左端から見てi番目まで取るときの距離の合計
    ll lsum[n+1]={};
    for(ll i=n-1;i>=0;i--){
        rsum[i] = rsum[i+1] + (x[n-1]-x[i]);
    }
    for(ll i=1;i<n;i++){
        lsum[i] = lsum[i-1] + (x[i] - x[0]);
    }
    for(ll i=0;i<n;i++){
        ll l = x[i], r = min(x[n-1],x[i] + w);
        // if(r == x[n-1]){
        //     chmin(ans, rsum[n-m] - rsum[i] - (x[n-1]-x[i]) * (i-n+m));
        //     break;
        // }
        // 左側から j 番目まで移動させる距離 -> 
        // 右側から j 番目まで移動させる距離 -> 
        ll ub = upper_bound(x,x+n,r) - x;
        ll cnt = ub - i;
        if(cnt>=m)return 0;
        // 左側から何個取るか
        ll low = -1, up = i+1;
        while(up-low>1){
            ll mid = (up+low)/2;
            if(mid+cnt>m){
                up = mid;
                continue;
            }
            ll ri = min(n-1,i-mid+m-1);
            ll li = i-mid;
            if(ri - li + 1 < m){
                low = mid;
                continue;
            }
            if(x[ri]-r >= l-x[li])low=mid;
            else up = mid;
        }
        for(ll k=-5;k<=5;k++){
            ll lef = low - k;
            if(i-lef<=i && i-lef>=0 && i-lef+m-1>=0 && i-lef+m-1<n){
                ll ret = rsum[i-lef]-rsum[i]-lef*(x[n-1] - x[i]);
                if(ub < n){
                    ret += lsum[i-lef+m-1] - lsum[ub-1] - (r-x[0])*(m-lef-cnt);
                }
                chmin(ans,ret);
            }
        }
    }

    return ans;
}

tottori totori(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    cin>>n>>m>>w;
    for(ll i=0;i<n;i++){
        cin>>x[i];
    }
    ll ans = solve();
    for(ll i=0;i<n;i++){
        x[i] = x[n-1] - x[i];
    }
    reverse(x,x+n);
    chmin(ans,solve());
    cout << ans << endl;

}
accuracy: 1
time_ms: 20
memory_kb: 5184
final_rank: 4

code_snippet: #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
using lint = long long int;
long long int INF = 1LL * 1e12;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()

/* do your best */

lint culc(lint a, lint l, lint m, const vector<lint> &x) {
  lint b = a + l;
  // 区間 [a, b]
  int n = x.size();
  vector<lint> dist(n);
  for (int i = 0; i < n; i++) {
    if (a <= x[i] and x[i] <= b) dist[i] = 0;
    else if (x[i] < a) dist[i] = a - x[i];
    else dist[i] = x[i] - b;
  }
  sort(ALL(dist));
  lint ret = 0;
  for (int i = 0; i < m; i++) {
    ret += dist[i];
  }
  return ret;
}


int main() {
  
  lint n, m, l; cin >> n >> m >> l;
  vector<lint> x(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }

  // 区間の左端の候補を列挙
  vector<lint> left;
  left.push_back(-INF);
  left.push_back(INF);

  for (int i = 0; i < n; i++) {
    left.push_back(x[i] - l);
    left.push_back(x[i]);
  }


  // 三分探索
  int k = left.size();
  
  int lb = 0;
  int rb = k;

  while (rb - lb > 1500) {
    int m1 = (rb - lb) / 3 + lb;
    int m2 = (rb - lb) / 3 * 2 + lb;

    if (culc(left[m1], l, m, x) > culc(left[m2], l, m, x)) {
      lb = m1;
    } else {
      rb = m2;
    }
  }

  if (lb != 0) {
    lb--;
  }
  if (rb != k) {
    rb++;
  }

  lint ans = 1e18;
  for (int i = lb; i < rb; i++) {
    ans = min(ans, culc(left[i], l, m, x));
  }

  cout << ans << endl;
  return 0;
}
accuracy: 0.8412698412698413
time_ms: 800
memory_kb: 4348
final_rank: 20

code_snippet: #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
using lint = long long int;
long long int INF = 1LL * 1e12;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()

/* do your best */

lint culc(lint a, lint l, lint m, const vector<lint> &x) {
  lint b = a + l;
  // 区間 [a, b]
  int n = x.size();
  vector<lint> dist(n);
  for (int i = 0; i < n; i++) {
    if (a <= x[i] and x[i] <= b) dist[i] = 0;
    else if (x[i] < a) dist[i] = a - x[i];
    else dist[i] = x[i] - b;
  }
  sort(ALL(dist));
  lint ret = 0;
  for (int i = 0; i < m; i++) {
    ret += dist[i];
  }
  return ret;
}


int main() {
  
  lint n, m, l; cin >> n >> m >> l;
  vector<lint> x(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }

  // 区間の左端の候補を列挙
  vector<lint> left;
  left.push_back(-INF);
  left.push_back(INF);

  for (int i = 0; i < n; i++) {
    left.push_back(x[i] - l);
    left.push_back(x[i]);
  }


  // 三分探索
  int k = left.size();
  
  int lb = 0;
  int rb = k;

  while (rb - lb > 1000) {
    int m1 = (rb - lb) / 3 + lb;
    int m2 = (rb - lb) / 3 * 2 + lb;

    if (culc(left[m1], l, m, x) > culc(left[m2], l, m, x)) {
      lb = m1;
    } else {
      rb = m2;
    }
  }

  if (lb != 0) {
    lb--;
  }
  if (rb != k) {
    rb++;
  }

  lint ans = 1e18;
  for (int i = lb; i < rb; i++) {
    ans = min(ans, culc(left[i], l, m, x));
  }

  cout << ans << endl;
  return 0;
}
accuracy: 0.8412698412698413
time_ms: 560
memory_kb: 4196
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int MAX = 1 << 30;
const long long INF = 10000000000000000;
struct segment_tree{
  unordered_map<int, pair<int, long long>> ST;
  segment_tree(){
  }
  void range_add(int L, int R, int a, int b, int i, int l, int r){
    if (r <= L || R <= l){
      return;
    } else if (L <= l && r <= R){
      ST[i].first += a;
      ST[i].second += b;
    } else {
      int m = (l + r) / 2;
      range_add(L, R, a, b, i * 2 + 1, l, m);
      range_add(L, R, a, b, i * 2 + 2, m, r);
    }
  }
  void range_add(int L, int R, int a, int b){
    range_add(L, R, a, b, 0, 0, MAX);
  }
  pair<int, long long> operator [](int k){
    int i = 0;
    int L = 0;
    int R = MAX;
    int ans1 = 0;
    long long ans2 = 0;
    while (1){
      if (ST.count(i)){
        ans1 += ST[i].first;
        ans2 += ST[i].second;
      }
      if (R - L == 1){
        break;
      }
      int m = (L + R) / 2;
      int next;
      if (k < m){
        next = i * 2 + 1;
        R = m;
      } else {
        next = i * 2 + 2;
        L = m;
      }
      i = next;
    }
    return make_pair(ans1, ans2);
  }
};
int main(){
  int N, M, L;
  cin >> N >> M >> L;
  vector<int> x(N);
  for (int i = 0; i < N; i++){
    cin >> x[i];
  }
  vector<long long> pos;
  for (int i = 0; i < N; i++){
    if (x[i] >= L){
      pos.push_back(x[i] - L);
    }
    pos.push_back(x[i]);
  }
  sort(pos.begin(), pos.end());
  int cnt = pos.size();
  int p = 0;
  segment_tree ST;
  for (int i = 0; i < M; i++){
    if (x[i] > L){
      ST.range_add(0, x[i] - L, -1, x[i] - L);
    }
    ST.range_add(x[i], MAX, 1, - x[i]);
  }
  long long ans = INF;
  long long tmp = INF;
  while (p < cnt){
    pair<int, long long> f = ST[pos[p]];
    long long sum = f.first * pos[p] + f.second;
    if (sum > tmp){
      p--;
      break;
    } else {
      p++;
      ans = min(ans, sum);
      tmp = sum;
    }
  }
  for (int i = M; i < N; i++){
    if (x[i] > L){
      ST.range_add(0, x[i] - L, -1, x[i] - L);
    }
    ST.range_add(x[i], MAX, 1, - x[i]);
    if (x[i - M] > L){
      ST.range_add(0, x[i - M] - L, 1, L - x[i - M]);
    }
    ST.range_add(x[i - M], MAX, -1, x[i - M]);
    tmp = INF;
    while (p < cnt){
      pair<int, long long> f = ST[pos[p]];
      long long sum = f.first * pos[p] + f.second;
      if (sum > tmp){
        p--;
        break;
      } else {
        p++;
        ans = min(ans, sum);
        tmp = sum;
      }
    }
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 1000
memory_kb: 78736
final_rank: 16