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 mp make_pair
#define mt make_tuple
#define ff first
#define ss second
using namespace std;
template <typename T>
using matrix = vector<vector<T>>;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INFL = (1LL<<62)-1;
const int INF = (1<<30)-1;
const double EPS = 1e-7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6+1;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, h;
cin >> n >> h;
vector<ll> v(n);
for(int i = 0; i < n; i++)
cin >> v[i];
ll offsetl = 0, offsetr = 0;
ll resp = 0;
multiset<ll,greater<ll>> left;
multiset<ll> right;
left.insert(v[0]);
right.insert(v[0]);
for(int i = 1; i < n; i++){
offsetl-=h;
offsetr+=h;
pll last = {*left.begin()+offsetl,*right.begin()+offsetr};
if(v[i]<= last.ff){
left.insert(v[i]-offsetl), left.insert(v[i]-offsetl), right.insert(*left.begin()+offsetl-offsetr), left.erase(left.begin());
resp += last.ff-v[i];
}else right.insert(v[i]-offsetr), right.insert(v[i]-offsetr), left.insert(*right.begin()+offsetr-offsetl), right.erase(right.begin()), resp += max(0ll,v[i]-last.ss);
}
cout << resp << '\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... |