53362023-04-25 23:21:42zsomborEgyengetőcpp17Wrong answer 0/1003ms4408 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);
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer3ms1888 KiB
2Wrong answer3ms2088 KiB
subtask20/20
3Wrong answer3ms2300 KiB
4Wrong answer3ms2512 KiB
5Wrong answer3ms2740 KiB
6Wrong answer3ms2824 KiB
7Wrong answer3ms2812 KiB
8Wrong answer3ms2808 KiB
subtask30/20
9Wrong answer3ms3028 KiB
10Wrong answer3ms3148 KiB
11Wrong answer3ms3244 KiB
12Wrong answer2ms3240 KiB
13Wrong answer3ms3368 KiB
14Wrong answer3ms3444 KiB
subtask40/20
15Wrong answer3ms3468 KiB
16Wrong answer3ms3540 KiB
17Wrong answer3ms3544 KiB
18Wrong answer3ms3676 KiB
19Wrong answer3ms3780 KiB
20Wrong answer3ms3760 KiB
subtask50/40
21Wrong answer3ms3884 KiB
22Wrong answer3ms4120 KiB
23Wrong answer3ms4356 KiB
24Wrong answer3ms4176 KiB
25Wrong answer3ms4328 KiB
26Wrong answer3ms4408 KiB