Language: C++

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <deque>
#include <cassert>
#define For(i, a, b) for (int i = a; i < b; ++i)
#define Out(i, a, b) for (int i = a - 1; i >= b; --i)
#define pb push_back
#define x first
#define y second
#define files(FileName) read(FileName); write(FileName)
#define read(FileName) freopen((FileName + ".in").c_str(), "r", stdin)
#define write(FileName) freopen((FileName + ".out").c_str(), "w", stdout)
using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
 
  
const string FileName = "input";
 
typedef long long base;
 
const int MAXN = 2e4;
 
 int n;
int LR[401][2];
int dp[401][401];

int main() {
    ios::sync_with_stdio(0);
    //read(FileName);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> LR[i][0] >> LR[i][1];
        --LR[i][0], --LR[i][1];
    }
    for (int i = 0; i < 400; ++i) {
        for (int j = 0; j < 400; ++j) {
            dp[i][j] = 1e9;
        }
    }
    for (int i = 0; i < 400; ++i) {
        dp[0][i] = abs(i - LR[0][0]);
    }
    for (int i = 1; i < n; ++i) {
        int Ln = LR[i][0], Rn = LR[i][1];
        for (int j = 0; j < 400; ++j) {
            // if (j <= 5) {
            //     cout << i - 1 << ' ' << j << ' ' << dp[i - 1][j] << endl; 
            // }
            // int L = j, R = j - LR[i][0] + LR[i][1];
            // if (j <= 5) {
            //     cout << Ln << ' ' << Rn << ' ' << L << ' ' << R << endl;
            // }
            if (max(L, Ln) <= min(R, Rn)) {
                chkmin(dp[i][L], dp[i - 1][j]);
                for (int q = Ln - R + L; q <= Rn; ++q) {
                    if (q >= 0 && q <= 399)
                        chkmin(dp[i][q], dp[i - 1][q] + abs(q - L));
                }
            } else {
                if (R < Ln) {
                    for (int q = Ln - R + L, d = Ln - R; q <= Rn; ++q, ++d) {
                        if (q >= 0 && q <= 399)
                            chkmin(dp[i][q], dp[i - 1][q] + d);
                    }
                } else {
                    for (int q = Rn, d = L - Rn; q - L + R >= Ln; --q, ++d) {
                        if (q >= 0 && q <= 399) 
                            chkmin(dp[i][q], dp[i - 1][q] + d);
                    }
                }
            }
        }
    }
    int ans = 1e9;
    for (int i = 0; i < 399; ++i) {
        chkmin(ans, dp[n - 1][i]);
    }
    cout << ans << endl;
}  
 
/*
2 AB 11
2!2
3 ABB 122
3!3
4 ABB 2222
4!3
5 ABBBB 12222
5!5
6 ABBB 121222
6!4
7 ABBBB 1212122
7!5
8 ABBB 12121212
8!4
9 ABBBBBBBB 122222222
9!9
10 ABBBBB 1212222222
10!6
11 ABBBBBBBB 12121222222
11!9
12 ABBBBB 121212122222
12!6
13 ABBBBBBBB 1212121212222
13!9
14 ABBBBB 12121212121222
14!6
15 ABBBBBBBB 121212121212122
15!9
16 ABBBBB 1212121212121212
16!6
17 ABBBBBBBBBBBBBBBB 12222222222222222
17!17
18 ABBBBBBBBB 121222222222222222
18!10
19 ABBBBBBBBBBBBBBBB 1212122222222222222
19!17
20 ABBBBBBBBB 12121212222222222222
20!10
[Finished in 3.1s]
*/

Compiler output:
a.cc: In function 'int main()':
a.cc:72:21: error: 'L' was not declared in this scope
   72 |             if (max(L, Ln) <= min(R, Rn)) {
      |                     ^
a.cc:72:35: error: 'R' was not declared in this scope
   72 |             if (max(L, Ln) <= min(R, Rn)) {
      |                                   ^
a.cc:80:74: error: 'd' was not declared in this scope
   80 |                     for (int q = Ln - R + L, d = Ln - R; q <= Rn; ++q, ++d) {
      |                                                                          ^