#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T> //order_of_key = menores, find_by_order = consultar indexado en 0, regresa iterador
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vii = vector<int>;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define mid ((l+r)>>1)
#define f first
#define s second
#define pb push_back
#define MP make_pair
#define all(x) (x).begin(),(x).end()
#define inicio(x) (*(x).begin())
#define rinicio(x) (*(x).rbegin())
#define SZ(x) (int)(x.size())
const ll MAXN = (ll)5e5 + 5;
const ll MOD = (ll)1e9 + 7;
const ll INF = (ll)1e12 + 5;
const ll OFFSET = (ll)1e6;
void tc() {
priority_queue<ll> pq;
ll N, a, b, sum = 0, brute = 0, ans = 0, M = 0, pre; cin >> N;
for (int i = 0; i < N - 1; i++) {
cin >> a >> b;
a -= b;
sum += a;
brute += abs(sum);
pq.push(max(sum, 0LL));
pq.push(max(sum, 0LL));
pq.pop();
}
cin >> a >> b;
sum += a - b;
while (pq.top() > sum) {
M--;
pq.pop();
}
pq.push(0);
pre = pq.top();
while (!pq.empty()) {
ans -= M*abs(pre - pq.top());
pre = pq.top();
pq.pop();
M--;
}
cout << brute - ans << "\n";
return;
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int t = 1;
//cin >> t;
for (int i = 1; i <= t; i++) {
tc();
}
return 0;
}
//checa tus constantes, n = 1? overflow?
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
12 ms |
1244 KB |
Output is correct |
5 |
Correct |
22 ms |
2032 KB |
Output is correct |
6 |
Correct |
83 ms |
5728 KB |
Output is correct |
7 |
Correct |
99 ms |
11200 KB |
Output is correct |
8 |
Execution timed out |
1042 ms |
9152 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
12 ms |
1244 KB |
Output is correct |
5 |
Correct |
22 ms |
2032 KB |
Output is correct |
6 |
Correct |
83 ms |
5728 KB |
Output is correct |
7 |
Correct |
99 ms |
11200 KB |
Output is correct |
8 |
Execution timed out |
1042 ms |
9152 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |