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>
using namespace std;
typedef long long ll;
typedef __int128 i128;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
ll h;
cin >> n >> h;
i128 m=0,b=0;
priority_queue<i128> l;
priority_queue<i128,vector<i128>,greater<i128>> r;
i128 dl=0,dr=0; //real value is x+dx
for(int i=0;i<n;i++)
{
ll s;
cin >> s;
//f->g
dl-=h;
dr+=h;
b+=(m*h);
//add c
if(i==0||s<=l.top()+dl)
{
l.push(s-dl);
l.push(s-dl);
i128 t=l.top()+dl;
l.pop();
r.push(t-dr);
}
else
{
r.push(s-dr);
r.push(s-dr);
i128 t=r.top()+dr;
r.pop();
l.push(t-dl);
}
m--;
b+=s;
}
vector<i128> v;
for(int i=0;i<n;i++)
{
i128 t=l.top();
l.pop();
v.push_back(t+dl);
}
reverse(v.begin(),v.end());
for(int i=0;i<n;i++)
{
i128 t=r.top();
r.pop();
v.push_back(t+dr);
}
i128 res=(1ll<<60);
for(i128 x:v)
{
i128 y=m*x+b;
m++;
b=y-m*x;
res=min(res,y);
}
cout << ll(res) << "\n";
return 0;
}
# | 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... |