This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long ans;
priority_queue<long long> l;
priority_queue<long long,vector<long long>,greater<long long>> r;
int main(){
long long n,k;
cin>>n>>k;
long long x;
cin>>x;
l.push(x);
r.push(x);
for(long long i=1;i<n;i++){
long long x;
cin>>x;
long long offset = 1LL*i*(k);
long long lborder = l.top()-offset;
long long rborder = r.top()+offset;
if(x<lborder){
l.push(x+offset);
l.push(x+offset);
l.pop();
r.push(lborder-offset);
ans+=abs(lborder-x);
}else if(x>rborder){
r.push(x-offset);
r.push(x-offset);
r.pop();
l.push(rborder+offset);
ans+=abs(rborder-x);
}else{
l.push(x+offset);
r.push(x-offset);
}
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |