#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MXN = 1e5+5;
int n, a[MXN][2];
ll ans;
int32_t main() {
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
    cin >> n;
    for(int i=1,x,y; i<=2*n; i++) {
        cin >> x >> y;
        if(y>=2) {
            ans += y-2;
            y = 2;
        }
        else {
            ans += 1-y;
            y = 1;
        }
        if(x<1) {
            ans += 1-x;
            x = 1;
        }
        if(x>n) {
            ans += x-n;
            x = n;
        }
        a[x][y-1]++;
    }
    for(int i=1; i<=n; i++) {
        a[i][0] += a[i-1][0] - 1;
        a[i][1] += a[i-1][1] - 1;
        for(int t : {0, 1})
            if(a[i][t]>0 && 0>a[i][t^1]) {
                int x = min(a[i][t], -a[i][t^1]);
                a[i][t] -= x;
                a[i][t^1] += x;
                ans += x;
            }
        ans += abs(a[i][0]);
        ans += abs(a[i][1]);
    }
    cout << ans << '\n';
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |