Language: C++

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e20;
const int MOD = 1e9 + 7;
#define dump(x) cout << #x << " = " << (x) << endl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define V vector<int>
#define P pair<int, int>
#define mp make_pair<int, int>
int N, l[100010], r[100010];
Vector<P> p;
int getscore(int i, int p)
{
    if (l[i] > p) {
        return l[i] - p;
    } else if (r[i] < p) {
        return p - r[i];
    } else {
        return 0;
    }
}
void solve()
{
    FOR(i, 0, N)
    {
        p.push_back(mp(l[i], -1));
        p.push_back(mp(r[i], 1));
    }
    sort(all(p));
    int ans = 0;
    FOR(i, 0, N)
    {
        ans += getscore(i, 0);
    }
    int now = 0;
    int d = N;
    int u = 0;
    FOR(i, 0, N * 2)
    {
        int t = ans - (d - u) * (p[i].first - now);
        ans = min(ans, t);
        now = p[i].first;
        if (p[i].second == -1) {
            d--;
        } else {
            u++;
        }
    }
    cout << ans << endl;
}

signed main()
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> N;
    FOR(i, 0, N)
    cin >> l[i] >> r[i];
    solve();
    return 0;
}


Compiler output:
a.cc:4:17: warning: overflow in conversion from 'double' to 'long long int' changes value from '1.0e+20' to '9223372036854775807' [-Woverflow]
    4 | const int INF = 1e20;
      |                 ^~~~
a.cc:14:1: error: 'Vector' does not name a type
   14 | Vector<P> p;
      | ^~~~~~
a.cc: In function 'void solve()':
a.cc:29:9: error: 'p' was not declared in this scope
   29 |         p.push_back(mp(l[i], -1));
      |         ^
a.cc:29:27: error: cannot bind rvalue reference of type 'long long int&&' to lvalue of type 'long long int'
   29 |         p.push_back(mp(l[i], -1));
      |                        ~~~^
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_pair.h:1132:21: note:   initializing argument 1 of 'constexpr std::pair<typename std::__strip_reference_wrapper<typename std::decay<_Tp>::type>::__type, typename std::__strip_reference_wrapper<typename std::decay<_Tp2>::type>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = long long int; _T2 = long long int; typename __strip_reference_wrapper<typename decay<_Tp>::type>::__type = long long int; typename decay<_Tp>::type = long long int; typename __strip_reference_wrapper<typename decay<_Tp2>::type>::__type = long long int; typename decay<_Tp2>::type = long long int]'
 1132 |     make_pair(_T1&& __x, _T2&& __y)
      |               ~~~~~~^~~
a.cc:30:27: error: cannot bind rvalue reference of type 'long long int&&' to lvalue of type 'long long int'
   30 |         p.push_back(mp(r[i], 1));
      |                        ~~~^
/usr/include/c++/14/bits/stl_pair.h:1132:21: note:   initializing argument 1 of 'constexpr std::pair<typename std::__strip_reference_wrapper<typename std::decay<_Tp>::type>::__type, typename std::__strip_reference_wrapper<typename std::decay<_Tp2>::type>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = long long int; _T2 = long long int; typename __strip_reference_wrapper<typename decay<_Tp>::type>::__type = long long int; typename decay<_Tp>::type = long long int; typename __strip_reference_wrapper<typename decay<_Tp2>::type>::__type = long long int; typename decay<_Tp2>::type = long long int]'
 1132 |     make_pair(_T1&& __x, _T2&& __y)
      |               ~~~~~~^~~
a.cc:32:14: error: 'p' was not declared in this scope
   32 |     sort(all(p));
      |              ^
a.cc:9:17: note: in definition of macro 'all'
    9 | #define all(v) (v).begin(), (v).end()
      |                 ^