Submission #488507

#TimeUsernameProblemLanguageResultExecution timeMemory
488507cheissmartPotatoes and fertilizers (LMIO19_bulves)C++14
100 / 100
154 ms15028 KiB
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
	cerr << to_string(h);
	if(sizeof ...(t)) cerr << ", ";
	DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif

const int INF = 1e9 + 7, N = 5e5 + 7;

ll diff[N];

signed main()
{
	IO_OP;

	int n;
	cin >> n;
	priority_queue<ll> pq;
	pq.push(0);
	ll lst = 0;
	for(int i = 1; i <= n; i++) {
		int a, b;
		cin >> a >> b;
		diff[i] = diff[i - 1] + b - a;
		ll x = -diff[i];
		if(x < 0) {
			lst +=  pq.top() - x;
			pq.pop();
			if(pq.empty())
				pq.push(0);
		} else {
			pq.push(x);
			if(pq.top() > x) {
				lst += pq.top() - x;
				pq.pop();
				pq.push(x);
			}
		}
	}
	assert(diff[n] <= 0);
	while(pq.size() && pq.top() > -diff[n]) {
		lst += pq.top() - (-diff[n]);
		pq.pop();
	}
	cout << lst << '\n';
}

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