Submission #418611

# Submission time Handle Problem Language Result Execution time Memory
418611 2021-06-05T15:46:21 Z Blagojce Potatoes and fertilizers (LMIO19_bulves) C++11
100 / 100
172 ms 13248 KB
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
  
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const ll inf = 1e18;
const int i_inf = 1e9;
const ll mod = 1e9+7;
 
mt19937 _rand(time(NULL));
const int mxn = 5e5+5;

int n;
ll d[mxn];

void solve(){
	cin >> n;
	fr(i, 1, n+1){
		int a, b;
		cin >> a >> b;
		d[i] = d[i-1] + a - b;
	}
	
	pq<int> Q;
	
	ll ans = 0;
	fr(i, 1, n+1){
		int vinrange = min(max(d[i], 0LL), d[n]);
		Q.push(vinrange);
		if(Q.top() > d[i]){
			Q.push(vinrange);
			ans += Q.top()-d[i];
			Q.pop();
		}
		else if(d[i] > d[n]){
			ans += d[i]-d[n];
		}
	}
	cout<<ans<<endl;
	
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();
}	

# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 10 ms 1380 KB Output is correct
5 Correct 19 ms 2376 KB Output is correct
6 Correct 60 ms 6804 KB Output is correct
7 Correct 129 ms 13064 KB Output is correct
8 Correct 95 ms 11328 KB Output is correct
9 Correct 111 ms 10616 KB Output is correct
10 Correct 74 ms 8260 KB Output is correct
11 Correct 74 ms 8264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 10 ms 1380 KB Output is correct
5 Correct 19 ms 2376 KB Output is correct
6 Correct 60 ms 6804 KB Output is correct
7 Correct 129 ms 13064 KB Output is correct
8 Correct 95 ms 11328 KB Output is correct
9 Correct 111 ms 10616 KB Output is correct
10 Correct 74 ms 8260 KB Output is correct
11 Correct 74 ms 8264 KB Output is correct
12 Correct 30 ms 3548 KB Output is correct
13 Correct 70 ms 8884 KB Output is correct
14 Correct 138 ms 13248 KB Output is correct
15 Correct 94 ms 11296 KB Output is correct
16 Correct 110 ms 10564 KB Output is correct
17 Correct 91 ms 8376 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 10 ms 1380 KB Output is correct
12 Correct 19 ms 2376 KB Output is correct
13 Correct 60 ms 6804 KB Output is correct
14 Correct 129 ms 13064 KB Output is correct
15 Correct 95 ms 11328 KB Output is correct
16 Correct 111 ms 10616 KB Output is correct
17 Correct 74 ms 8260 KB Output is correct
18 Correct 74 ms 8264 KB Output is correct
19 Correct 30 ms 3548 KB Output is correct
20 Correct 70 ms 8884 KB Output is correct
21 Correct 138 ms 13248 KB Output is correct
22 Correct 94 ms 11296 KB Output is correct
23 Correct 110 ms 10564 KB Output is correct
24 Correct 91 ms 8376 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 376 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 41 ms 3524 KB Output is correct
34 Correct 102 ms 8912 KB Output is correct
35 Correct 172 ms 13120 KB Output is correct
36 Correct 158 ms 10772 KB Output is correct
37 Correct 104 ms 11280 KB Output is correct
38 Correct 156 ms 13168 KB Output is correct
39 Correct 101 ms 9348 KB Output is correct
40 Correct 109 ms 8804 KB Output is correct
41 Correct 98 ms 8308 KB Output is correct
42 Correct 103 ms 8484 KB Output is correct
43 Correct 107 ms 8256 KB Output is correct
44 Correct 103 ms 8256 KB Output is correct
45 Correct 123 ms 13108 KB Output is correct
46 Correct 91 ms 8716 KB Output is correct
47 Correct 107 ms 8176 KB Output is correct