#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+5;
typedef long long ll;
int a[maxn];
int b[maxn];
ll d[maxn];
int n;
ll ans=0; // dp[n][0]
// dp[n][i]: excessive of at most i
priority_queue<ll> pq;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1 ; i<=n ; i++) {
cin>>a[i]>>b[i];
d[i]=d[i-1]+1ll*(a[i]-b[i]);
//cout<<"d "<<i<<" = "<<d[i]<<endl;
}
for(int i=1 ; i<n ; i++) {
ans+=abs(d[i]);
if(d[i]<0) d[i]=0;
pq.push(d[i]);
if(pq.top()>d[i]) {
pq.pop();
pq.push(d[i]);
}
//cout<<"ans = "<<ans<<endl;
// slope change point
}
while(!pq.empty()) {
ans-=min(pq.top(),d[n]);
pq.pop();
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
13 ms |
2000 KB |
Output is correct |
5 |
Correct |
28 ms |
3604 KB |
Output is correct |
6 |
Correct |
95 ms |
9768 KB |
Output is correct |
7 |
Correct |
122 ms |
19080 KB |
Output is correct |
8 |
Correct |
137 ms |
17252 KB |
Output is correct |
9 |
Correct |
122 ms |
16792 KB |
Output is correct |
10 |
Correct |
97 ms |
14264 KB |
Output is correct |
11 |
Correct |
104 ms |
14252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
13 ms |
2000 KB |
Output is correct |
5 |
Correct |
28 ms |
3604 KB |
Output is correct |
6 |
Correct |
95 ms |
9768 KB |
Output is correct |
7 |
Correct |
122 ms |
19080 KB |
Output is correct |
8 |
Correct |
137 ms |
17252 KB |
Output is correct |
9 |
Correct |
122 ms |
16792 KB |
Output is correct |
10 |
Correct |
97 ms |
14264 KB |
Output is correct |
11 |
Correct |
104 ms |
14252 KB |
Output is correct |
12 |
Correct |
42 ms |
5060 KB |
Output is correct |
13 |
Correct |
106 ms |
13248 KB |
Output is correct |
14 |
Correct |
133 ms |
19052 KB |
Output is correct |
15 |
Correct |
137 ms |
17184 KB |
Output is correct |
16 |
Correct |
162 ms |
16484 KB |
Output is correct |
17 |
Correct |
85 ms |
14220 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 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 |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 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 |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 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 |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 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 |
2000 KB |
Output is correct |
12 |
Correct |
28 ms |
3604 KB |
Output is correct |
13 |
Correct |
95 ms |
9768 KB |
Output is correct |
14 |
Correct |
122 ms |
19080 KB |
Output is correct |
15 |
Correct |
137 ms |
17252 KB |
Output is correct |
16 |
Correct |
122 ms |
16792 KB |
Output is correct |
17 |
Correct |
97 ms |
14264 KB |
Output is correct |
18 |
Correct |
104 ms |
14252 KB |
Output is correct |
19 |
Correct |
42 ms |
5060 KB |
Output is correct |
20 |
Correct |
106 ms |
13248 KB |
Output is correct |
21 |
Correct |
133 ms |
19052 KB |
Output is correct |
22 |
Correct |
137 ms |
17184 KB |
Output is correct |
23 |
Correct |
162 ms |
16484 KB |
Output is correct |
24 |
Correct |
85 ms |
14220 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
48 ms |
5084 KB |
Output is correct |
34 |
Correct |
121 ms |
13304 KB |
Output is correct |
35 |
Correct |
197 ms |
19024 KB |
Output is correct |
36 |
Correct |
187 ms |
16596 KB |
Output is correct |
37 |
Correct |
145 ms |
17244 KB |
Output is correct |
38 |
Correct |
181 ms |
19144 KB |
Output is correct |
39 |
Correct |
111 ms |
15196 KB |
Output is correct |
40 |
Correct |
122 ms |
14480 KB |
Output is correct |
41 |
Correct |
93 ms |
14280 KB |
Output is correct |
42 |
Correct |
90 ms |
14320 KB |
Output is correct |
43 |
Correct |
109 ms |
14256 KB |
Output is correct |
44 |
Correct |
104 ms |
14300 KB |
Output is correct |
45 |
Correct |
175 ms |
19092 KB |
Output is correct |
46 |
Correct |
100 ms |
14740 KB |
Output is correct |
47 |
Correct |
119 ms |
13328 KB |
Output is correct |