#include <iostream>
#include <algorithm>
#include <vector>
#include "grader.h"
using namespace std;
using ll = long long;
ll n, k, ans = 1e15;
vector <ll> v(1, 0);
vector <ll> p(1, 0);
ll sum(int l, int r) {
return p[r] - p[l - 1];
}
ll cost(ll MN) {
ll MX = MN + k;
int L = lower_bound(v.begin(), v.end(), MN) - 1 - v.begin();
int R = upper_bound(v.begin(), v.end(), MX) - v.begin();
return MN * L - sum(1, L) + sum(R, n) - MX * (n + 1 - R);
}
int main()
{
n = getN();
k = getK();
for (int i = 1; i <= n; i++) v.push_back(Data());
sort(v.begin(), v.end());
for (int i = 1; i <= n; i++) p.push_back(p[i - 1] + v[i]);
for (int i = 1; i <= n; i++) {
ans = min(ans, cost(v[i]));
ans = min(ans, cost(v[i] - k));
}
Solution(ans);
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Wrong answer | 3ms | 1896 KiB | ||||
2 | Wrong answer | 3ms | 2128 KiB | ||||
subtask2 | 0/20 | ||||||
3 | Wrong answer | 3ms | 2292 KiB | ||||
4 | Wrong answer | 3ms | 2512 KiB | ||||
5 | Wrong answer | 3ms | 2720 KiB | ||||
6 | Wrong answer | 3ms | 2936 KiB | ||||
7 | Wrong answer | 2ms | 3012 KiB | ||||
8 | Wrong answer | 3ms | 3244 KiB | ||||
subtask3 | 0/20 | ||||||
9 | Wrong answer | 3ms | 3352 KiB | ||||
10 | Wrong answer | 3ms | 3480 KiB | ||||
11 | Wrong answer | 2ms | 3472 KiB | ||||
12 | Wrong answer | 3ms | 3464 KiB | ||||
13 | Wrong answer | 3ms | 3444 KiB | ||||
14 | Wrong answer | 3ms | 3436 KiB | ||||
subtask4 | 0/20 | ||||||
15 | Wrong answer | 3ms | 3544 KiB | ||||
16 | Wrong answer | 3ms | 3568 KiB | ||||
17 | Wrong answer | 3ms | 3796 KiB | ||||
18 | Wrong answer | 3ms | 4004 KiB | ||||
19 | Wrong answer | 3ms | 4112 KiB | ||||
20 | Wrong answer | 3ms | 4240 KiB | ||||
subtask5 | 0/40 | ||||||
21 | Wrong answer | 3ms | 4156 KiB | ||||
22 | Wrong answer | 3ms | 4268 KiB | ||||
23 | Wrong answer | 3ms | 4280 KiB | ||||
24 | Wrong answer | 2ms | 4312 KiB | ||||
25 | Wrong answer | 3ms | 4280 KiB | ||||
26 | Wrong answer | 3ms | 4500 KiB |