Submission #253216

# Submission time Handle Problem Language Result Execution time Memory
253216 2020-07-27T13:03:13 Z Berted Potatoes and fertilizers (LMIO19_bulves) C++14
100 / 100
198 ms 8632 KB
#include <iostream>
#include <queue>
#define ll long long
using namespace std;

/*
	Transform the array into A - B
	We could change an operation into -1 +1 adjacent indices
	
	Transform the array into prefix sum
	Now, an operation changed A[i] into A[i] + 1 or A[i] - 1
	for 1 <= i < N. A[0] = 0, A[N] = SUM;
	
	Therefore the array must be transformed such that it is non-decreasing
	We can achieve this with slope trick.
*/

const ll INF = 1e18;

int n;
ll ar[500001], res = 0;
priority_queue<ll> pq;

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int a, b; cin >> a >> b;
		ar[i] = a - b;
		if (i) ar[i] += ar[i - 1];
	}

	pq.push(0);

	for (int i = 0; i < n - 1; i++)
	{
		if (ar[i] < pq.top())
		{
			res += pq.top() - ar[i]; pq.pop();
			pq.push(max(ar[i], 0LL));
		}
		// cout << "Push at " << ar[i] << "\n";
		pq.push(max(ar[i], 0LL));
	}

	int i;
	ll pv = pq.top();
	for (i = 0; pq.top() > ar[n - 1]; i++)
	{
		res += (pv - pq.top()) * i;
		pv = pq.top(); pq.pop();
	}

	res += (pv - ar[n - 1]) * i;

	cout << res << "\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 15 ms 1408 KB Output is correct
5 Correct 31 ms 2300 KB Output is correct
6 Correct 114 ms 4464 KB Output is correct
7 Correct 152 ms 8428 KB Output is correct
8 Correct 174 ms 8428 KB Output is correct
9 Correct 141 ms 8552 KB Output is correct
10 Correct 109 ms 8556 KB Output is correct
11 Correct 117 ms 8472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 15 ms 1408 KB Output is correct
5 Correct 31 ms 2300 KB Output is correct
6 Correct 114 ms 4464 KB Output is correct
7 Correct 152 ms 8428 KB Output is correct
8 Correct 174 ms 8428 KB Output is correct
9 Correct 141 ms 8552 KB Output is correct
10 Correct 109 ms 8556 KB Output is correct
11 Correct 117 ms 8472 KB Output is correct
12 Correct 53 ms 2420 KB Output is correct
13 Correct 132 ms 6888 KB Output is correct
14 Correct 150 ms 8556 KB Output is correct
15 Correct 174 ms 8556 KB Output is correct
16 Correct 195 ms 8424 KB Output is correct
17 Correct 79 ms 8556 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 15 ms 1408 KB Output is correct
12 Correct 31 ms 2300 KB Output is correct
13 Correct 114 ms 4464 KB Output is correct
14 Correct 152 ms 8428 KB Output is correct
15 Correct 174 ms 8428 KB Output is correct
16 Correct 141 ms 8552 KB Output is correct
17 Correct 109 ms 8556 KB Output is correct
18 Correct 117 ms 8472 KB Output is correct
19 Correct 53 ms 2420 KB Output is correct
20 Correct 132 ms 6888 KB Output is correct
21 Correct 150 ms 8556 KB Output is correct
22 Correct 174 ms 8556 KB Output is correct
23 Correct 195 ms 8424 KB Output is correct
24 Correct 79 ms 8556 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 41 ms 2428 KB Output is correct
33 Correct 103 ms 6928 KB Output is correct
34 Correct 175 ms 8552 KB Output is correct
35 Correct 159 ms 8432 KB Output is correct
36 Correct 152 ms 8556 KB Output is correct
37 Correct 173 ms 8428 KB Output is correct
38 Correct 94 ms 8428 KB Output is correct
39 Correct 103 ms 8172 KB Output is correct
40 Correct 111 ms 8428 KB Output is correct
41 Correct 97 ms 8428 KB Output is correct
42 Correct 97 ms 8524 KB Output is correct
43 Correct 96 ms 8516 KB Output is correct
44 Correct 198 ms 8632 KB Output is correct
45 Correct 79 ms 8428 KB Output is correct
46 Correct 98 ms 7660 KB Output is correct
47 Correct 1 ms 384 KB Output is correct