#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, -1e15);
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 | 1888 KiB | ||||
2 | Wrong answer | 3ms | 2116 KiB | ||||
subtask2 | 0/20 | ||||||
3 | Wrong answer | 3ms | 2332 KiB | ||||
4 | Wrong answer | 3ms | 2672 KiB | ||||
5 | Wrong answer | 3ms | 2728 KiB | ||||
6 | Wrong answer | 3ms | 2940 KiB | ||||
7 | Wrong answer | 3ms | 3040 KiB | ||||
8 | Wrong answer | 3ms | 3148 KiB | ||||
subtask3 | 0/20 | ||||||
9 | Wrong answer | 3ms | 3152 KiB | ||||
10 | Wrong answer | 3ms | 3380 KiB | ||||
11 | Wrong answer | 3ms | 3592 KiB | ||||
12 | Wrong answer | 3ms | 3800 KiB | ||||
13 | Wrong answer | 3ms | 3828 KiB | ||||
14 | Wrong answer | 3ms | 3896 KiB | ||||
subtask4 | 0/20 | ||||||
15 | Wrong answer | 2ms | 3896 KiB | ||||
16 | Wrong answer | 3ms | 4004 KiB | ||||
17 | Wrong answer | 3ms | 4004 KiB | ||||
18 | Wrong answer | 3ms | 4132 KiB | ||||
19 | Wrong answer | 3ms | 3996 KiB | ||||
20 | Wrong answer | 3ms | 4080 KiB | ||||
subtask5 | 0/40 | ||||||
21 | Wrong answer | 3ms | 4336 KiB | ||||
22 | Wrong answer | 3ms | 4296 KiB | ||||
23 | Wrong answer | 3ms | 4296 KiB | ||||
24 | Wrong answer | 3ms | 4320 KiB | ||||
25 | Wrong answer | 3ms | 4320 KiB | ||||
26 | Wrong answer | 3ms | 4220 KiB |