#include <bits/stdc++.h>
using namespace std;
inline void setin(string s) {
freopen(s.c_str(), "r", stdin);
}
template <typename T> void Min(T &a, T b) {
a = min(a, b);
}
template <typename T> void Max(T &a, T b) {
a = max(a, b);
}
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 1e6 + 15;
int n, a[N], b[N], diff[N], d[N];
priority_queue <int> q;
int ans;
int opt[N];
signed main() {
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
// setin("input.txt");
cin >> n;
for(int i = 1; i <= n; ++i)
cin >> a[i] >> b[i];
for(int i = 1; i <= n; ++i)
diff[i] = a[i] - b[i];
for(int i = 1; i <= n; ++i)
d[i] = d[i-1] + diff[i];
for(int i = 1; i <= n; ++i) {
if(d[i] < 0)
ans += abs(d[i]);
Max(d[i], 0LL);
}
for(int i = 1; i <= n; ++i) {
q.push(d[i]);
q.push(d[i]);
opt[i] = q.top();
q.pop();
}
opt[n] = d[n];
for(int i = n - 1; i; --i) {
Min(opt[i], opt[i + 1]);
ans += abs(opt[i] - d[i]);
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
640 KB |
Output is correct |
4 |
Correct |
16 ms |
3200 KB |
Output is correct |
5 |
Correct |
30 ms |
5964 KB |
Output is correct |
6 |
Correct |
100 ms |
15592 KB |
Output is correct |
7 |
Correct |
169 ms |
30888 KB |
Output is correct |
8 |
Correct |
159 ms |
28900 KB |
Output is correct |
9 |
Correct |
142 ms |
28264 KB |
Output is correct |
10 |
Correct |
113 ms |
25964 KB |
Output is correct |
11 |
Correct |
115 ms |
25960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
640 KB |
Output is correct |
4 |
Correct |
16 ms |
3200 KB |
Output is correct |
5 |
Correct |
30 ms |
5964 KB |
Output is correct |
6 |
Correct |
100 ms |
15592 KB |
Output is correct |
7 |
Correct |
169 ms |
30888 KB |
Output is correct |
8 |
Correct |
159 ms |
28900 KB |
Output is correct |
9 |
Correct |
142 ms |
28264 KB |
Output is correct |
10 |
Correct |
113 ms |
25964 KB |
Output is correct |
11 |
Correct |
115 ms |
25960 KB |
Output is correct |
12 |
Correct |
49 ms |
8048 KB |
Output is correct |
13 |
Correct |
127 ms |
20076 KB |
Output is correct |
14 |
Correct |
165 ms |
30820 KB |
Output is correct |
15 |
Correct |
172 ms |
28904 KB |
Output is correct |
16 |
Correct |
184 ms |
28256 KB |
Output is correct |
17 |
Correct |
112 ms |
25976 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 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 |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
640 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 |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
640 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 |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
16 ms |
3200 KB |
Output is correct |
12 |
Correct |
30 ms |
5964 KB |
Output is correct |
13 |
Correct |
100 ms |
15592 KB |
Output is correct |
14 |
Correct |
169 ms |
30888 KB |
Output is correct |
15 |
Correct |
159 ms |
28900 KB |
Output is correct |
16 |
Correct |
142 ms |
28264 KB |
Output is correct |
17 |
Correct |
113 ms |
25964 KB |
Output is correct |
18 |
Correct |
115 ms |
25960 KB |
Output is correct |
19 |
Correct |
49 ms |
8048 KB |
Output is correct |
20 |
Correct |
127 ms |
20076 KB |
Output is correct |
21 |
Correct |
165 ms |
30820 KB |
Output is correct |
22 |
Correct |
172 ms |
28904 KB |
Output is correct |
23 |
Correct |
184 ms |
28256 KB |
Output is correct |
24 |
Correct |
112 ms |
25976 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
640 KB |
Output is correct |
30 |
Correct |
2 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
640 KB |
Output is correct |
32 |
Correct |
67 ms |
8048 KB |
Output is correct |
33 |
Correct |
127 ms |
20320 KB |
Output is correct |
34 |
Correct |
200 ms |
30948 KB |
Output is correct |
35 |
Correct |
164 ms |
28384 KB |
Output is correct |
36 |
Correct |
162 ms |
28900 KB |
Output is correct |
37 |
Correct |
185 ms |
30820 KB |
Output is correct |
38 |
Correct |
120 ms |
26988 KB |
Output is correct |
39 |
Correct |
131 ms |
24756 KB |
Output is correct |
40 |
Correct |
119 ms |
26004 KB |
Output is correct |
41 |
Correct |
109 ms |
25960 KB |
Output is correct |
42 |
Correct |
116 ms |
25952 KB |
Output is correct |
43 |
Correct |
108 ms |
25952 KB |
Output is correct |
44 |
Correct |
200 ms |
30688 KB |
Output is correct |
45 |
Correct |
141 ms |
26320 KB |
Output is correct |
46 |
Correct |
116 ms |
21616 KB |
Output is correct |
47 |
Correct |
2 ms |
512 KB |
Output is correct |