Submission #399558

#TimeUsernameProblemLanguageResultExecution timeMemory
399558ronnithPotatoes and fertilizers (LMIO19_bulves)C++14
30 / 100
210 ms13052 KiB
#include <bits/stdc++.h>

using namespace std;

struct Line {
	long long m, c;
	Line() {}
	Line(long long mm, long long cc) : m(mm), c(cc) {}
	inline long long getY(long long x) { return m*x+c; }
};

const int N = (int)5e5;
int n, a[N], b[N];
long long d[N];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n;

	for (int i = 0;i < n;i ++) {
		cin >> a[i] >> b[i];
		d[i] = a[i] - b[i];
		if (i != 0) d[i] += d[i - 1];
	}

	Line line;
	priority_queue<int> pq;
	if (d[0] > 0) {
		pq.push(d[0]);
		line = Line(-1, d[0]);
	} else {
		line = Line(0, -d[0]);
	}

	for (int i = 1;i < n - 1;i ++) {
		if (d[i] >= 0) {
			pq.push(d[i]);
			pq.push(d[i]);
			pq.pop();
			line.m --;
			line.c += d[i];
		} else {
			if (line.m + 1 > 0) {
				line.m = 0;
				line.c += -d[i];
				while ((int)pq.size()) pq.pop();
			} else {
				line.m ++;
				line.c += -d[i];
				pq.pop();
			}
		}
	}

	vector<int> val;
	while ((int)pq.size()) {
		val.push_back(pq.top());
		pq.pop();
	}
	sort(val.begin(), val.end());

	for (auto& e : val) {
		if (e >= d[n - 1]) break;
		long long y = line.getY(e);
		line.m ++;
		line.c = y - line.m * e; 
	}

	cout << line.getY(d[n - 1]) << '\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...