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>
typedef long long ll;
using namespace std;
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	ll N, D, ans = 0;
	cin >> N >> D;
	
	ll Loff = 0, Roff = 0;
	priority_queue<ll> L;
	priority_queue<ll, vector<ll>, greater<ll>> R;
	for (int i = 1; i <= N; ++i) {
		ll n;
		cin >> n;
		Loff -= D; Roff += D;
		if (L.empty() || n <= R.top() + Roff) {
			if (!L.empty() && n < L.top() + Loff) ans += (L.top() + Loff) - n;
			L.push(n - Loff);
			L.push(n - Loff);
		}
		else {
			ans += n - (R.top() + Roff);
			R.push(n - Roff);
			R.push(n - Roff);
		}
		while (L.size() > R.size()) {
			R.push(L.top() + Loff - Roff);
			L.pop();
		}
		while (R.size() > L.size()) {
			L.push(R.top() + Roff - Loff);
			R.pop();
		}
	}
	cout << ans;
	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... |