#include <bits/stdc++.h>
using namespace std;
#define int long long
int N;
int fst = 0; // value of DP function at 0
priority_queue<int> points; // points where DP function changes slope
signed main() {
cin >> N;
vector<int> dif(N+1);
for (int i = 1; i <= N; ++i) {
int a,b; cin >> a >> b;
dif[i] = a-b+dif[i-1];
}
assert(dif[N] >= 0); // assume solution exists
for (int i = 1; i < N; ++i) {
if (dif[i] < 0) fst -= dif[i], dif[i] = 0;
points.push(dif[i]); points.push(dif[i]);
fst += points.top() - dif[i];
points.pop();
}
if(N == 1){
cout << 0 << endl;
return 0;
}
int mult = 0;
int last = points.top();
while(!points.empty()){
int at = points.top();
//cout << "/ " << at << " " << last << " " << mult << endl;
if(at <= dif[N]){
fst += (last - dif[N]) * mult;
break;
}
fst += (last - at) * mult;
last = at;
mult ++;
points.pop();
}
cout << fst << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
30 ms |
1268 KB |
Output is correct |
5 |
Correct |
64 ms |
2108 KB |
Output is correct |
6 |
Correct |
227 ms |
4412 KB |
Output is correct |
7 |
Correct |
384 ms |
8416 KB |
Output is correct |
8 |
Incorrect |
403 ms |
8348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
30 ms |
1268 KB |
Output is correct |
5 |
Correct |
64 ms |
2108 KB |
Output is correct |
6 |
Correct |
227 ms |
4412 KB |
Output is correct |
7 |
Correct |
384 ms |
8416 KB |
Output is correct |
8 |
Incorrect |
403 ms |
8348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |