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;
long long n,h;
long long s[200100];
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
cin>>n>>h;
for (long long i = 1;i <= n;i ++){
cin>>s[i];
}
multiset <long long> l,r;
long long add_l = 0,add_r = 0;
long long a = 0, b = 0;
for (long long i = 1;i <= n;i ++){
add_l -= h;
add_r += h;
b += h * a;
a--;
b += s[i];
l.insert(s[i] - add_l);
l.insert(s[i] - add_l);
while (!r.empty() && *l.rbegin() + add_l > *r.begin() + add_r){
long long tmp = *l.rbegin() + add_l;
l.erase(l.find(*l.rbegin()));
r.insert(tmp - add_r);
}
while (l.size() > r.size()){
long long tmp = *l.rbegin() + add_l;
l.erase(l.find(*l.rbegin()));
r.insert(tmp - add_r);
}
while (r.size() > l.size()){
long long tmp = *r.begin() + add_r;
r.erase(r.find(*r.begin()));
l.insert(tmp - add_l);
}
}
for (auto x:l){
x += add_l;
a++;
b -= x;
if (a == 0){
cout<<b<<'\n';
return 0;
}
}
for (auto x:r){
x += add_r;
a++;
b -= x;
if (a == 0){
cout<<b<<'\n';
return 0;
}
}
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... |