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 <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const ll MOD=998244353;
ll N,M,a[200005];
struct SlopeTrick{
ll minimum=0,negStart=0,posStart=0;
priority_queue <ll> negSlope;
priority_queue <ll,vector<ll>,greater<ll> > posSlope;
void PrefixMin(){
posStart=0;
while(!posSlope.empty()){
posSlope.pop();
}
}
void SuffixMin(){
negStart=0;
while(!negSlope.empty()){
negSlope.pop();
}
}
void addPos(ll x){
ll y;
if(negSlope.empty()){
y=-1e18;
}
else{
y=negSlope.top()+negStart;
}
if(x>=y||negSlope.empty()){
posSlope.push(x-posStart);
}
else{
minimum+=y-x;
negSlope.push(x-negStart);
negSlope.pop();
posSlope.push(y-posStart);
}
}
void addNeg(ll x){
ll y;
if(posSlope.empty()){
y=-1e18;
}
else{
y=posSlope.top()+posStart;
}
if(x<=y||posSlope.empty()){
negSlope.push(x-negStart);
}
else{
minimum+=x-y;
posSlope.push(x-posStart);
posSlope.pop();
negSlope.push(y-negStart);
}
}
void RangeMin(ll l, ll r){
if(!negSlope.empty()){
negStart-=r;
}
if(!posSlope.empty()){
posStart+=l;
}
}
};
int main(){
cin>>N>>M;
for(int i=1;i<=N;i++){
cin>>a[i];
}
SlopeTrick dp=SlopeTrick();
for(int i=1;i<=N;i++){
dp.RangeMin(M,M);
dp.addPos(a[i]);
dp.addNeg(a[i]);
}
cout<<dp.minimum<<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... |