#include<bits/stdc++.h>
using namespace std;
#define task "a"
#define se second
#define fi first
#define ll long long
#define ii pair<ll, ll>
const long mxN = 2e5 + 7;
int n, cnt[2][mxN];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen(task".INP", "r", stdin);
    //freopen(task".OUT", "w", stdout);
    ll ans = 0;
    cin >> n;
    for (int i = 1; i <= 2 * n; i++)
    {
        int u, v;
        cin >> u >> v;
        int a, b;
        if (u < 1)
            a = 1;
        else if (u <= n)
            a = u;
        else
            a = n;
        b = (v > 1);
        ans += abs(v - (b + 1)) + abs(u - a);
        cnt[b][a]++;
    }
    int u = 0, v = 0;
    for (int i = 1; i <= n; i++)
    {
        u += cnt[0][i] - 1;
        v += cnt[1][i] - 1;
        if (u > 0 && v < 0)
        {
            int inc = min(u, -v);
            ans += inc;
            u -= inc;
            v += inc;
        }
        else if (u < 0 && v > 0)
        {
            int inc = min(-u, v);
            ans += inc;
            u += inc;
            v -= inc;
        }
        ans += abs(u) + abs(v);
    }
    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... |