#include <bits/stdc++.h>
#include "grader.h"
using namespace std;
using ll = long long;
int main()
{
int n = getN(), k = getK();
map<int, ll> m;
ll ans = 0;
ll l = 0, r = k, ldb = 0, lb = 0, rdb = 0, rb = 0;
for(int i = 1; i <= n; i++)
{
int x = Data();
m[x]++;
if(x < l)
{
ans += l - x;
ldb++;
}
if(x == l) lb++;
if(x > r)
{
ans += x - r;
rdb++;
}
if(x == r) rb++;
if(rdb > ldb + lb)
{
pair<int, int> it1 = *m.upper_bound(l), it2 = *m.upper_bound(r);
int mov = min(it1.first - l, it2.first - r);
l += mov;
r += mov;
ldb += lb;
lb = 0;
rb = 0;
ans -= mov * (rdb - ldb);
if(it1.first == l)
{
lb = it1.second;
}
if(it2.first == r)
{
rb = it2.second;
rdb -= it2.second;
}
}
if(ldb > rdb + rb)
{
auto x = m.lower_bound(l), y = m.lower_bound(r);
x--;
y--;
pair<int, int> it1 = *x, it2 = *y;
int mov = min(l - it1.first, r - it2.first);
l -= mov;
r -= mov;
rdb += rb;
lb = 0;
rb = 0;
ans -= mov * (ldb - rdb);
if(l == it1.first)
{
lb = it1.second;
ldb -= it1.second;
}
if(r == it2.first)
{
rb = it2.second;
}
}
Solution(ans);
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1832 KiB | ||||
2 | Accepted | 3ms | 2020 KiB | ||||
subtask2 | 20/20 | ||||||
3 | Accepted | 4ms | 2260 KiB | ||||
4 | Accepted | 4ms | 2328 KiB | ||||
5 | Accepted | 6ms | 2332 KiB | ||||
6 | Accepted | 7ms | 2460 KiB | ||||
7 | Accepted | 9ms | 2536 KiB | ||||
8 | Accepted | 1.187s | 3796 KiB | ||||
subtask3 | 20/20 | ||||||
9 | Accepted | 14ms | 2528 KiB | ||||
10 | Accepted | 13ms | 2948 KiB | ||||
11 | Accepted | 12ms | 2800 KiB | ||||
12 | Accepted | 14ms | 3104 KiB | ||||
13 | Accepted | 14ms | 3496 KiB | ||||
14 | Accepted | 14ms | 3404 KiB | ||||
subtask4 | 20/20 | ||||||
15 | Accepted | 65ms | 3484 KiB | ||||
16 | Accepted | 119ms | 4824 KiB | ||||
17 | Accepted | 120ms | 3568 KiB | ||||
18 | Accepted | 119ms | 4944 KiB | ||||
19 | Accepted | 122ms | 4548 KiB | ||||
20 | Accepted | 98ms | 5032 KiB | ||||
subtask5 | 40/40 | ||||||
21 | Accepted | 1.36s | 20088 KiB | ||||
22 | Accepted | 1.519s | 4188 KiB | ||||
23 | Accepted | 1.557s | 4120 KiB | ||||
24 | Accepted | 2.104s | 4180 KiB | ||||
25 | Accepted | 2.088s | 27280 KiB | ||||
26 | Accepted | 2.165s | 27148 KiB |