#include<iostream>
#include<cmath>
#include<cstring>
#include<cassert>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<vector>
#include<set>
#include<unordered_set>
#include<bitset>
#include<climits>
#include<numeric>
#include<functional>
#include<iomanip>
#ifdef YJL
#include<debug.h>
#else
#define debug(args...)0
#define debug_n(a,n)0
#endif
#define ALL(a) a.begin(),a.end()
using namespace std;
using ll=long long;
constexpr ll inf = numeric_limits<ll>::max()/2;
struct slope_trick_concave {// 凹折线函数
priority_queue<ll> ql;
priority_queue<ll,vector<ll>,greater<>> qr;
ll tagl,tagr;// 左右堆中斜率改变点的懒标记
ll height;// 最低点高度
slope_trick_concave(): tagl(0), tagr(0), height(0) {
ql.push(-inf);
qr.push(inf);
}
ll get_left() {
return ql.top()+tagl;
}
ll get_right() {
return qr.top()+tagr;
}
// f(x) = g(x) + |x-c|
void add(ll c) {
ll xl=get_left();
ll xr=get_right();
if(xl <= c && c <= xr) {
ql.push(c-tagl);
qr.push(c-tagr);
}else if(c < xl) {
height+=xl-c;
qr.push(xl-tagr);
ql.pop();
ql.push(c-tagl);
ql.push(c-tagl);
}else {
height+=c-xr;
ql.push(xr-tagl);
qr.pop();
qr.push(c-tagr);
qr.push(c-tagr);
}
}
// g(x) = min_{y=x+low, x+high} f(y)
void range_min(ll low, ll high) {
tagl-=high;
tagr-=low;
}
// g(x) = min_{y=-inf, x} f(y)
void pref_min() {
tagr=0;
while(qr.size()>1)
qr.pop();
}
};
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin>>n;
vector<ll> a(n),b(n);
for(int i=0; i<n; ++i) {
cin>>a[i];
cin>>b[i];
a[i]-=b[i];
if(i) a[i]+=a[i-1];
}
// for(int i=0; i<n; ++i) {
// }
slope_trick_concave sp;
ll ans=0;
for(int i=0; i<n; ++i) {
if(a[i]<0) {
ans-=a[i];
a[i]=0;
}
if(a[i]>a[n-1]) {
ans+=a[i]-a[n-1];
a[i]=a[n-1];
}
sp.add(a[i]);
sp.pref_min();
}
cout<<ans+sp.height;
return 0;
}
/*
a[i]++, a[i+1]--
前缀和
a[i]++
最后
0<=a[0]<=..<=a[n-1]
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
2016 KB |
Output is correct |
5 |
Correct |
12 ms |
3684 KB |
Output is correct |
6 |
Correct |
45 ms |
9844 KB |
Output is correct |
7 |
Correct |
70 ms |
19772 KB |
Output is correct |
8 |
Correct |
59 ms |
18108 KB |
Output is correct |
9 |
Correct |
58 ms |
18384 KB |
Output is correct |
10 |
Correct |
45 ms |
16072 KB |
Output is correct |
11 |
Correct |
45 ms |
15740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
2016 KB |
Output is correct |
5 |
Correct |
12 ms |
3684 KB |
Output is correct |
6 |
Correct |
45 ms |
9844 KB |
Output is correct |
7 |
Correct |
70 ms |
19772 KB |
Output is correct |
8 |
Correct |
59 ms |
18108 KB |
Output is correct |
9 |
Correct |
58 ms |
18384 KB |
Output is correct |
10 |
Correct |
45 ms |
16072 KB |
Output is correct |
11 |
Correct |
45 ms |
15740 KB |
Output is correct |
12 |
Correct |
19 ms |
5100 KB |
Output is correct |
13 |
Correct |
46 ms |
14200 KB |
Output is correct |
14 |
Correct |
74 ms |
19516 KB |
Output is correct |
15 |
Correct |
58 ms |
19016 KB |
Output is correct |
16 |
Correct |
67 ms |
18596 KB |
Output is correct |
17 |
Correct |
57 ms |
14932 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
6 ms |
2016 KB |
Output is correct |
12 |
Correct |
12 ms |
3684 KB |
Output is correct |
13 |
Correct |
45 ms |
9844 KB |
Output is correct |
14 |
Correct |
70 ms |
19772 KB |
Output is correct |
15 |
Correct |
59 ms |
18108 KB |
Output is correct |
16 |
Correct |
58 ms |
18384 KB |
Output is correct |
17 |
Correct |
45 ms |
16072 KB |
Output is correct |
18 |
Correct |
45 ms |
15740 KB |
Output is correct |
19 |
Correct |
19 ms |
5100 KB |
Output is correct |
20 |
Correct |
46 ms |
14200 KB |
Output is correct |
21 |
Correct |
74 ms |
19516 KB |
Output is correct |
22 |
Correct |
58 ms |
19016 KB |
Output is correct |
23 |
Correct |
67 ms |
18596 KB |
Output is correct |
24 |
Correct |
57 ms |
14932 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
28 ms |
5076 KB |
Output is correct |
34 |
Correct |
70 ms |
14024 KB |
Output is correct |
35 |
Correct |
119 ms |
19900 KB |
Output is correct |
36 |
Correct |
125 ms |
16772 KB |
Output is correct |
37 |
Correct |
64 ms |
17596 KB |
Output is correct |
38 |
Correct |
109 ms |
20168 KB |
Output is correct |
39 |
Correct |
73 ms |
16588 KB |
Output is correct |
40 |
Correct |
69 ms |
16176 KB |
Output is correct |
41 |
Correct |
65 ms |
15428 KB |
Output is correct |
42 |
Correct |
63 ms |
14792 KB |
Output is correct |
43 |
Correct |
66 ms |
14792 KB |
Output is correct |
44 |
Correct |
67 ms |
15060 KB |
Output is correct |
45 |
Correct |
83 ms |
20284 KB |
Output is correct |
46 |
Correct |
56 ms |
15052 KB |
Output is correct |
47 |
Correct |
73 ms |
14872 KB |
Output is correct |