#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 time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
12 ms |
1360 KB |
Output is correct |
5 |
Correct |
27 ms |
2124 KB |
Output is correct |
6 |
Correct |
90 ms |
7672 KB |
Output is correct |
7 |
Correct |
85 ms |
11344 KB |
Output is correct |
8 |
Correct |
147 ms |
13112 KB |
Output is correct |
9 |
Correct |
94 ms |
8952 KB |
Output is correct |
10 |
Correct |
86 ms |
10196 KB |
Output is correct |
11 |
Correct |
95 ms |
10148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
12 ms |
1360 KB |
Output is correct |
5 |
Correct |
27 ms |
2124 KB |
Output is correct |
6 |
Correct |
90 ms |
7672 KB |
Output is correct |
7 |
Correct |
85 ms |
11344 KB |
Output is correct |
8 |
Correct |
147 ms |
13112 KB |
Output is correct |
9 |
Correct |
94 ms |
8952 KB |
Output is correct |
10 |
Correct |
86 ms |
10196 KB |
Output is correct |
11 |
Correct |
95 ms |
10148 KB |
Output is correct |
12 |
Correct |
42 ms |
3824 KB |
Output is correct |
13 |
Correct |
105 ms |
8512 KB |
Output is correct |
14 |
Correct |
92 ms |
11208 KB |
Output is correct |
15 |
Correct |
140 ms |
13056 KB |
Output is correct |
16 |
Correct |
154 ms |
11000 KB |
Output is correct |
17 |
Correct |
76 ms |
10296 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
12 ms |
1360 KB |
Output is correct |
12 |
Correct |
27 ms |
2124 KB |
Output is correct |
13 |
Correct |
90 ms |
7672 KB |
Output is correct |
14 |
Correct |
85 ms |
11344 KB |
Output is correct |
15 |
Correct |
147 ms |
13112 KB |
Output is correct |
16 |
Correct |
94 ms |
8952 KB |
Output is correct |
17 |
Correct |
86 ms |
10196 KB |
Output is correct |
18 |
Correct |
95 ms |
10148 KB |
Output is correct |
19 |
Correct |
42 ms |
3824 KB |
Output is correct |
20 |
Correct |
105 ms |
8512 KB |
Output is correct |
21 |
Correct |
92 ms |
11208 KB |
Output is correct |
22 |
Correct |
140 ms |
13056 KB |
Output is correct |
23 |
Correct |
154 ms |
11000 KB |
Output is correct |
24 |
Correct |
76 ms |
10296 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
33 ms |
4060 KB |
Output is correct |
34 |
Correct |
83 ms |
10252 KB |
Output is correct |
35 |
Correct |
141 ms |
14960 KB |
Output is correct |
36 |
Correct |
125 ms |
12656 KB |
Output is correct |
37 |
Correct |
150 ms |
13100 KB |
Output is correct |
38 |
Correct |
126 ms |
15028 KB |
Output is correct |
39 |
Correct |
77 ms |
11188 KB |
Output is correct |
40 |
Correct |
82 ms |
10368 KB |
Output is correct |
41 |
Correct |
78 ms |
10164 KB |
Output is correct |
42 |
Correct |
73 ms |
10164 KB |
Output is correct |
43 |
Correct |
78 ms |
10196 KB |
Output is correct |
44 |
Correct |
79 ms |
10152 KB |
Output is correct |
45 |
Correct |
153 ms |
14164 KB |
Output is correct |
46 |
Correct |
72 ms |
10432 KB |
Output is correct |
47 |
Correct |
89 ms |
9080 KB |
Output is correct |