Submission #958200

#TimeUsernameProblemLanguageResultExecution timeMemory
958200IBorySafety (NOI18_safety)C++17
100 / 100
50 ms5492 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...