// File coincollecting.cpp created on 02.10.2025 at 11:58:40
#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG 
    #include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
    #define debug(...) void(23)
#endif
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N;
    std::cin >> N;
    std::vector<std::array<int, 2>> A(N * 2);
    for (int i = 0; i < N * 2; ++i) {
        std::cin >> A[i][0] >> A[i][1];
        --A[i][0], --A[i][1];
    }
    i64 ans = 0;
    for (int i = 0; i < N * 2; ++i) {
        int x, y;
        if (A[i][0] < 0) {
            x = 0; 
        } else if (A[i][0] < N) {
            x = A[i][0];
        } else {
            x = N - 1;
        }
        if (A[i][1] <= 0) {
            y = 0; 
        } else {
            y = 1;
        }
        ans += std::abs(x - A[i][0]) + std::abs(y - A[i][1]);
        A[i][0] = x;
        A[i][1] = y;
        debug(x, y);
    }
    {
        int cnt[2] {};
        for (int i = 0; i < N * 2; ++i) {
            cnt[A[i][1]] += 1;
        }
        ans += std::abs(cnt[0] - cnt[1]) / 2;
    }
    {
        std::vector<int> pre(N + 1);
        for (int i = 0; i < N * 2; ++i) {
            pre[A[i][0] + 1]++;
        }
        for (int i = 0; i < N; ++i) {
            pre[i + 1] += pre[i];
        }
        for (int i = 0; i < N; ++i) {
            ans += std::abs(2 * (i + 1) - pre[i + 1]);
        }
    }
    std::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... |