53342023-04-25 23:07:54zsomborEgyengetőcpp17Wrong answer 0/1003ms4336 KiB
#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);
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer3ms1888 KiB
2Wrong answer3ms2116 KiB
subtask20/20
3Wrong answer3ms2332 KiB
4Wrong answer3ms2672 KiB
5Wrong answer3ms2728 KiB
6Wrong answer3ms2940 KiB
7Wrong answer3ms3040 KiB
8Wrong answer3ms3148 KiB
subtask30/20
9Wrong answer3ms3152 KiB
10Wrong answer3ms3380 KiB
11Wrong answer3ms3592 KiB
12Wrong answer3ms3800 KiB
13Wrong answer3ms3828 KiB
14Wrong answer3ms3896 KiB
subtask40/20
15Wrong answer2ms3896 KiB
16Wrong answer3ms4004 KiB
17Wrong answer3ms4004 KiB
18Wrong answer3ms4132 KiB
19Wrong answer3ms3996 KiB
20Wrong answer3ms4080 KiB
subtask50/40
21Wrong answer3ms4336 KiB
22Wrong answer3ms4296 KiB
23Wrong answer3ms4296 KiB
24Wrong answer3ms4320 KiB
25Wrong answer3ms4320 KiB
26Wrong answer3ms4220 KiB