// UUID: 9570b5ce-0d91-4eb8-bfdf-53378df75a7f
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;
ll n, i, j, ans;
vector <ll> a;
vector <ll> b;
void f1(ll x) {
if (a[i] > 2e9 || a[i] > x && b[j] <= x) ans += abs(b[j++] - x) + 1;
else ans += abs(a[i++] - x);
}
void f2(ll x) {
if (b[j] > 2e9 || b[j] > x && a[i] <= x) ans += abs(a[i++] - x) + 1;
else ans += abs(b[j++] - x);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
ll x, y;
for (int k = 0; k < 2 * n; k++) {
cin >> x >> y;
if (y <= 1) { ans += 1 - y; a.push_back(x); }
else { ans += y - 2; b.push_back(x); }
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
a.push_back(3e9);
b.push_back(3e9);
for (int k = 1; k <= n; k++) {
if (a[i] < b[j]) { f1(k); f2(k); }
else { f2(k); f1(k); }
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |