#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<long long> diff(n);
for (int i=0; i<n; i++) {
int a, b;
cin >> a >> b;
diff[i] = a - b + (i > 0 ? diff[i-1] : 0);
}
assert(diff[n-1] >= 0);
long long ret = 0;
priority_queue<long long> pq;
for (int i=0; i<n-1; i++) {
if (diff[i] < 0) {
ret -= diff[i];
diff[i] = 0;
} else if (diff[i] > diff[n-1]) {
ret += diff[i] - diff[n-1];
diff[i] = diff[n-1];
}
pq.push(diff[i]);
pq.push(diff[i]);
ret += pq.top() - diff[i];
pq.pop();
}
cout << ret << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
13 ms |
1684 KB |
Output is correct |
5 |
Correct |
23 ms |
2876 KB |
Output is correct |
6 |
Correct |
68 ms |
7752 KB |
Output is correct |
7 |
Correct |
140 ms |
15108 KB |
Output is correct |
8 |
Correct |
127 ms |
13328 KB |
Output is correct |
9 |
Correct |
125 ms |
12604 KB |
Output is correct |
10 |
Correct |
106 ms |
10352 KB |
Output is correct |
11 |
Correct |
104 ms |
10352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
13 ms |
1684 KB |
Output is correct |
5 |
Correct |
23 ms |
2876 KB |
Output is correct |
6 |
Correct |
68 ms |
7752 KB |
Output is correct |
7 |
Correct |
140 ms |
15108 KB |
Output is correct |
8 |
Correct |
127 ms |
13328 KB |
Output is correct |
9 |
Correct |
125 ms |
12604 KB |
Output is correct |
10 |
Correct |
106 ms |
10352 KB |
Output is correct |
11 |
Correct |
104 ms |
10352 KB |
Output is correct |
12 |
Correct |
38 ms |
4092 KB |
Output is correct |
13 |
Correct |
85 ms |
10960 KB |
Output is correct |
14 |
Correct |
171 ms |
15172 KB |
Output is correct |
15 |
Correct |
124 ms |
13372 KB |
Output is correct |
16 |
Correct |
120 ms |
12652 KB |
Output is correct |
17 |
Correct |
119 ms |
10340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 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 |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
13 ms |
1684 KB |
Output is correct |
12 |
Correct |
23 ms |
2876 KB |
Output is correct |
13 |
Correct |
68 ms |
7752 KB |
Output is correct |
14 |
Correct |
140 ms |
15108 KB |
Output is correct |
15 |
Correct |
127 ms |
13328 KB |
Output is correct |
16 |
Correct |
125 ms |
12604 KB |
Output is correct |
17 |
Correct |
106 ms |
10352 KB |
Output is correct |
18 |
Correct |
104 ms |
10352 KB |
Output is correct |
19 |
Correct |
38 ms |
4092 KB |
Output is correct |
20 |
Correct |
85 ms |
10960 KB |
Output is correct |
21 |
Correct |
171 ms |
15172 KB |
Output is correct |
22 |
Correct |
124 ms |
13372 KB |
Output is correct |
23 |
Correct |
120 ms |
12652 KB |
Output is correct |
24 |
Correct |
119 ms |
10340 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
43 ms |
4072 KB |
Output is correct |
34 |
Correct |
103 ms |
10940 KB |
Output is correct |
35 |
Correct |
193 ms |
15344 KB |
Output is correct |
36 |
Correct |
196 ms |
12772 KB |
Output is correct |
37 |
Correct |
140 ms |
13244 KB |
Output is correct |
38 |
Correct |
167 ms |
15216 KB |
Output is correct |
39 |
Correct |
148 ms |
11384 KB |
Output is correct |
40 |
Correct |
127 ms |
10940 KB |
Output is correct |
41 |
Correct |
117 ms |
10352 KB |
Output is correct |
42 |
Correct |
111 ms |
10356 KB |
Output is correct |
43 |
Correct |
112 ms |
10428 KB |
Output is correct |
44 |
Correct |
115 ms |
10348 KB |
Output is correct |
45 |
Correct |
148 ms |
15084 KB |
Output is correct |
46 |
Correct |
137 ms |
10740 KB |
Output is correct |
47 |
Correct |
113 ms |
10268 KB |
Output is correct |