Submission #253303

#TimeUsernameProblemLanguageResultExecution timeMemory
253303DystoriaXPotatoes and fertilizers (LMIO19_bulves)C++14
100 / 100
241 ms7532 KiB
#include <bits/stdc++.h>

using namespace std;

int n;
long long lazy, vc, slope;
priority_queue<long long, vector<long long>, greater<long long> > pq;

int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	
	cin >> n;

	pq.push(0);

	for(int i = 1; i <= n; i++){
		int a, b;
		cin >> a >> b;

		int d = a - b;

		lazy += d;

		if(pq.top() + lazy < d){
			vc += d - (pq.top() + lazy);

			if(pq.top() != 0) pq.pop();

			if(d <= lazy){
				pq.push(d - lazy);
				pq.push(d - lazy);
			}
		} else {
			pq.push(d - lazy);
		}
	}

	long long cur = pq.top() + lazy;

	slope = 0;
	while(!pq.empty()){
		long long nxt = pq.top() + lazy; pq.pop();

		nxt = min(nxt, 0LL);

		vc += (nxt - cur) * slope;
		slope++; cur = nxt;
	}

	if(cur != 0) vc += -cur * slope;

	cout << vc << "\n";

	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...