#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){
freopen("/Users/iantsai/cpp/input.txt","r",stdin);
freopen("/Users/iantsai/cpp/output.txt","w",stdout);
}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
#define TOI_is_so_de ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);setio();
inline void solve(){
int n;
cin >> n;
int a, b;
long long d[n + 1];
d[0] = 0;
for(int i = 1; i <= n; i++){
cin >> a >> b;
d[i] = d[i - 1] + a - b;
}
priority_queue<long long>pq;
pq.push(0);
long long ans = 0;
for(int i = 1; i <= n; i++){
if(d[i] < 0) ans += -d[i], d[i] = 0;
else if(d[i] > d[n]) ans += d[i] - d[n], d[i] = d[n];
}
for(int i = 1; i <= n; i++){
pq.push(d[i]);
pq.push(d[i]);
ans += pq.top() - d[i];
pq.pop();
}
cout << ans << '\n';
}
signed main(){
TOI_is_so_de;
solve();
}
/*
input:
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
10 ms |
1492 KB |
Output is correct |
5 |
Correct |
13 ms |
2388 KB |
Output is correct |
6 |
Correct |
51 ms |
4548 KB |
Output is correct |
7 |
Correct |
83 ms |
8384 KB |
Output is correct |
8 |
Correct |
71 ms |
8476 KB |
Output is correct |
9 |
Correct |
78 ms |
8572 KB |
Output is correct |
10 |
Correct |
64 ms |
8384 KB |
Output is correct |
11 |
Correct |
63 ms |
8384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
10 ms |
1492 KB |
Output is correct |
5 |
Correct |
13 ms |
2388 KB |
Output is correct |
6 |
Correct |
51 ms |
4548 KB |
Output is correct |
7 |
Correct |
83 ms |
8384 KB |
Output is correct |
8 |
Correct |
71 ms |
8476 KB |
Output is correct |
9 |
Correct |
78 ms |
8572 KB |
Output is correct |
10 |
Correct |
64 ms |
8384 KB |
Output is correct |
11 |
Correct |
63 ms |
8384 KB |
Output is correct |
12 |
Correct |
22 ms |
2556 KB |
Output is correct |
13 |
Correct |
52 ms |
7012 KB |
Output is correct |
14 |
Correct |
104 ms |
8384 KB |
Output is correct |
15 |
Correct |
85 ms |
8580 KB |
Output is correct |
16 |
Correct |
71 ms |
8572 KB |
Output is correct |
17 |
Correct |
69 ms |
8384 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 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 |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
508 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
2 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
10 ms |
1492 KB |
Output is correct |
12 |
Correct |
13 ms |
2388 KB |
Output is correct |
13 |
Correct |
51 ms |
4548 KB |
Output is correct |
14 |
Correct |
83 ms |
8384 KB |
Output is correct |
15 |
Correct |
71 ms |
8476 KB |
Output is correct |
16 |
Correct |
78 ms |
8572 KB |
Output is correct |
17 |
Correct |
64 ms |
8384 KB |
Output is correct |
18 |
Correct |
63 ms |
8384 KB |
Output is correct |
19 |
Correct |
22 ms |
2556 KB |
Output is correct |
20 |
Correct |
52 ms |
7012 KB |
Output is correct |
21 |
Correct |
104 ms |
8384 KB |
Output is correct |
22 |
Correct |
85 ms |
8580 KB |
Output is correct |
23 |
Correct |
71 ms |
8572 KB |
Output is correct |
24 |
Correct |
69 ms |
8384 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 |
2 ms |
336 KB |
Output is correct |
29 |
Correct |
2 ms |
508 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 |
28 ms |
2512 KB |
Output is correct |
34 |
Correct |
74 ms |
6940 KB |
Output is correct |
35 |
Correct |
129 ms |
8580 KB |
Output is correct |
36 |
Correct |
127 ms |
10628 KB |
Output is correct |
37 |
Correct |
115 ms |
8572 KB |
Output is correct |
38 |
Correct |
133 ms |
15216 KB |
Output is correct |
39 |
Correct |
96 ms |
11376 KB |
Output is correct |
40 |
Correct |
80 ms |
10948 KB |
Output is correct |
41 |
Correct |
72 ms |
10436 KB |
Output is correct |
42 |
Correct |
70 ms |
10436 KB |
Output is correct |
43 |
Correct |
85 ms |
10352 KB |
Output is correct |
44 |
Correct |
71 ms |
10484 KB |
Output is correct |
45 |
Correct |
96 ms |
15220 KB |
Output is correct |
46 |
Correct |
89 ms |
10736 KB |
Output is correct |
47 |
Correct |
69 ms |
10180 KB |
Output is correct |