#pragma GCC optimize("O3")
#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
inline char gc()
{
const static int SZ = 1 << 16;
static char buf[SZ], *p1, *p2;
if (p1 == p2 && (p2 = buf + fread(p1 = buf, 1, SZ, stdin), p1 == p2))
return -1;
return *p1++;
}
inline void rd() {}
template <typename T, typename... U>
inline void rd(T &x, U &...y)
{
x = 0;
bool f = 0;
char c = gc();
while (!isdigit(c))
f ^= !(c ^ 45), c = gc();
while (isdigit(c))
x = (x << 1) + (x << 3) + (c ^ 48), c = gc();
f && (x = -x), rd(y...);
}
template <typename T>
inline void prt(T x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
prt(x / 10);
putchar((x % 10) ^ 48);
}
inline void solve(){
int n, a, b;
rd(n);
long long d[n + 1];
d[0] = 0;
for(int i = 1; i <= n; i++){
rd(a), rd(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();
}
prt(ans);
}
signed main(){
solve();
}
/*
input:
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
472 KB |
Output is correct |
4 |
Correct |
4 ms |
1740 KB |
Output is correct |
5 |
Correct |
7 ms |
2424 KB |
Output is correct |
6 |
Correct |
23 ms |
8068 KB |
Output is correct |
7 |
Correct |
46 ms |
15388 KB |
Output is correct |
8 |
Correct |
42 ms |
8640 KB |
Output is correct |
9 |
Correct |
43 ms |
12656 KB |
Output is correct |
10 |
Correct |
40 ms |
10432 KB |
Output is correct |
11 |
Correct |
35 ms |
10432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
472 KB |
Output is correct |
4 |
Correct |
4 ms |
1740 KB |
Output is correct |
5 |
Correct |
7 ms |
2424 KB |
Output is correct |
6 |
Correct |
23 ms |
8068 KB |
Output is correct |
7 |
Correct |
46 ms |
15388 KB |
Output is correct |
8 |
Correct |
42 ms |
8640 KB |
Output is correct |
9 |
Correct |
43 ms |
12656 KB |
Output is correct |
10 |
Correct |
40 ms |
10432 KB |
Output is correct |
11 |
Correct |
35 ms |
10432 KB |
Output is correct |
12 |
Correct |
13 ms |
4296 KB |
Output is correct |
13 |
Correct |
34 ms |
10688 KB |
Output is correct |
14 |
Correct |
48 ms |
15288 KB |
Output is correct |
15 |
Correct |
46 ms |
8640 KB |
Output is correct |
16 |
Correct |
33 ms |
8640 KB |
Output is correct |
17 |
Correct |
41 ms |
10424 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
472 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 |
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 |
508 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 |
2 ms |
508 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
472 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 |
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 |
508 KB |
Output is correct |
11 |
Correct |
4 ms |
1740 KB |
Output is correct |
12 |
Correct |
7 ms |
2424 KB |
Output is correct |
13 |
Correct |
23 ms |
8068 KB |
Output is correct |
14 |
Correct |
46 ms |
15388 KB |
Output is correct |
15 |
Correct |
42 ms |
8640 KB |
Output is correct |
16 |
Correct |
43 ms |
12656 KB |
Output is correct |
17 |
Correct |
40 ms |
10432 KB |
Output is correct |
18 |
Correct |
35 ms |
10432 KB |
Output is correct |
19 |
Correct |
13 ms |
4296 KB |
Output is correct |
20 |
Correct |
34 ms |
10688 KB |
Output is correct |
21 |
Correct |
48 ms |
15288 KB |
Output is correct |
22 |
Correct |
46 ms |
8640 KB |
Output is correct |
23 |
Correct |
33 ms |
8640 KB |
Output is correct |
24 |
Correct |
41 ms |
10424 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
2 ms |
508 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
2 ms |
504 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
19 ms |
4296 KB |
Output is correct |
34 |
Correct |
51 ms |
10676 KB |
Output is correct |
35 |
Correct |
83 ms |
15248 KB |
Output is correct |
36 |
Correct |
89 ms |
12576 KB |
Output is correct |
37 |
Correct |
60 ms |
13336 KB |
Output is correct |
38 |
Correct |
75 ms |
15384 KB |
Output is correct |
39 |
Correct |
53 ms |
11448 KB |
Output is correct |
40 |
Correct |
70 ms |
10936 KB |
Output is correct |
41 |
Correct |
53 ms |
10396 KB |
Output is correct |
42 |
Correct |
46 ms |
10404 KB |
Output is correct |
43 |
Correct |
47 ms |
10432 KB |
Output is correct |
44 |
Correct |
60 ms |
10424 KB |
Output is correct |
45 |
Correct |
51 ms |
15288 KB |
Output is correct |
46 |
Correct |
63 ms |
8640 KB |
Output is correct |
47 |
Correct |
60 ms |
7616 KB |
Output is correct |