#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int, int>> a(n << 1);
long long ans = 0;
vector<pair<int, int>> cnt(n, {-1, -1});
for (auto& [x, y] : a) {
cin >> x >> y;
if (x < 1) ans += 1 - x, x = 1;
if (x > n) ans += x - n, x = n;
if (y < 1) ans += 1 - y, y = 1;
if (y > 2) ans += y - 2, y = 2;
if (y == 1) cnt[x - 1].first++;
else cnt[x - 1].second++;
}
int cnta = 0, cntb = 0;
for (auto [aa, bb] : cnt) {
cnta += aa; cntb += bb;
int tmp = min(cnta, -cntb);
if (tmp > 0) cnta -= tmp, cntb += tmp, ans += tmp;
tmp = min(-cnta, cntb);
if (tmp > 0) cnta += tmp, cntb -= tmp, ans += tmp;
ans += abs(cnta) + abs(cntb);
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |