#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "grader.h"
#define int long long
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N=getN(),K=getK(),M=4e8;
ordered_set<int>s;
auto calc=[&](int x)->int{
int c=0;
for(int i:s){
if(i<x){
c+=x-i;
}
if(i>x+K){
c+=i-(x+K);
}
}
return c;
};
for(int i=0;i<N;i++){
s.insert(Data());
int ans=1e18;
{
int l=0,h=i;
while(l<h){
int m=(l+h)/2;
if(m>=i-s.order_of_key(*s.find_by_order(m)+K)){
h=m;
}
else{
l=m+1;
}
}
ans=min(ans,calc(*s.find_by_order(h)));
}
{
int l=0,h=i;
while(l<h){
int m=(l+h)/2;
if(s.order_of_key(*s.find_by_order(m)-K)>=i-m){
h=m;
}
else{
l=m+1;
}
}
ans=min(ans,calc(*s.find_by_order(h)-K));
}
Solution(ans);
}
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1828 KiB | ||||
2 | Accepted | 3ms | 2024 KiB | ||||
subtask2 | 0/20 | ||||||
3 | Accepted | 4ms | 2240 KiB | ||||
4 | Accepted | 4ms | 2456 KiB | ||||
5 | Accepted | 7ms | 2432 KiB | ||||
6 | Wrong answer | 4ms | 2560 KiB | ||||
7 | Wrong answer | 3ms | 2768 KiB | ||||
8 | Wrong answer | 14ms | 3140 KiB | ||||
subtask3 | 0/20 | ||||||
9 | Wrong answer | 3ms | 3180 KiB | ||||
10 | Accepted | 23ms | 3580 KiB | ||||
11 | Wrong answer | 17ms | 3532 KiB | ||||
12 | Accepted | 23ms | 3796 KiB | ||||
13 | Accepted | 23ms | 3880 KiB | ||||
14 | Accepted | 23ms | 4004 KiB | ||||
subtask4 | 0/20 | ||||||
15 | Wrong answer | 4ms | 3828 KiB | ||||
16 | Accepted | 2.006s | 4952 KiB | ||||
17 | Wrong answer | 3ms | 3696 KiB | ||||
18 | Accepted | 1.986s | 5060 KiB | ||||
19 | Wrong answer | 9ms | 3844 KiB | ||||
20 | Accepted | 1.008s | 5088 KiB | ||||
subtask5 | 0/40 | ||||||
21 | Time limit exceeded | 3.526s | 4172 KiB | ||||
22 | Time limit exceeded | 3.53s | 3992 KiB | ||||
23 | Wrong answer | 6ms | 3720 KiB | ||||
24 | Wrong answer | 3ms | 3708 KiB | ||||
25 | Time limit exceeded | 3.509s | 4020 KiB | ||||
26 | Time limit exceeded | 3.509s | 3788 KiB |