Problem:
Score : 700 points

Problem Statement

You are given sequences A and B consisting of non-negative integers.
The lengths of both A and B are N, and the sums of the elements in A and B are equal.
The i-th element in A is A_i, and the i-th element in B is B_i.

Tozan and Gezan repeats the following sequence of operations:

If A and B are equal sequences, terminate the process.

Otherwise, first Tozan chooses a positive element in A and decrease it by 1.

Then, Gezan chooses a positive element in B and decrease it by 1.

Then, give one candy to Takahashi, their pet.

Tozan wants the number of candies given to Takahashi until the process is terminated to be as large as possible, while Gezan wants it to be as small as possible.
Find the number of candies given to Takahashi when both of them perform the operations optimally.

Constraints

1 \leq N \leq 2 × 10^5

0 \leq A_i,B_i \leq 10^9(1\leq i\leq N)

The sums of the elements in A and B are equal.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the number of candies given to Takahashi when both Tozan and Gezan perform the operations optimally.

Sample Input 1

2
1 2
3 2

Sample Output 1

2

When both Tozan and Gezan perform the operations optimally, the process will proceed as follows:

Tozan decreases A_1 by 1.

Gezan decreases B_1 by 1.

One candy is given to Takahashi.

Tozan decreases A_2 by 1.

Gezan decreases B_1 by 1.

One candy is given to Takahashi.

As A and B are equal, the process is terminated.

Sample Input 2

3
8 3
0 1
4 8

Sample Output 2

9

Sample Input 3

1
1 1

Sample Output 3

0

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <bits/stdc++.h>

using std::cerr;
using std::cin;
using std::cout;

using std::abs;
using std::min;
using std::max;
using std::swap;

using std::map;
using std::pair;
using std::set;
using std::string;
using std::vector;

using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define ff first
#define ss second
#define pb emplace_back
template<typename T> 
void _dbg(const char* _s, T _h) {
    cerr << _s << " = " << _h <<"\n"; 
}
template<typename T, typename... Ts>
void _dbg(const char* _s, T _h, Ts... _t) {
    int _b = 0;
    while (((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',')
        cerr << *_s++; 
    cerr << " = " << _h << ","; 
    _dbg(_s+1, _t...);
}
#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...) 
#endif

struct init {
    init() {
        cin.tie(0);
        std::iostream::sync_with_stdio(0);
        cout << std::fixed << std::setprecision(10);
        cerr << std::fixed << std::setprecision(5);
        #ifdef LOCAL
        srand(300); 
        #else
        using namespace std::chrono;
        srand(duration_cast<nanoseconds>(high_resolution_clock::now().time_since_epoch()).count()); 
        #endif
    }
    ~init() {
        #ifdef LOCAL
        cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC << "s.\n";
        #endif
    }
} init;

const int MAXN = 2e5 + 2, INF = 1e9 + 7;
int a[MAXN], b[MAXN];

// struct node{
//     int mn, mni, mx, mxi;
// };

// struct join{
//     bool operator() (const node & p1, const node & p2) {
//         node result;
//         if ()
//     }
// };

// template <class T, class Join=std::plus<T>>
// struct segment_tree {
//     int n, sh = 1;
//     T E;
//     vector<T> tree;
//     segment_tree(int _n, T _E = T()) : n(_n), E(_E) {
//         init(); build();
//     }
//     template<class InputIterator>
//     segment_tree(InputIterator begin, InputIterator end, T _E = T()) : n(int(end - begin)), E(_E) {
//         init();
//         copy(begin, end, tree.begin() + sh);
//         build();
//     }
//     inline void update(int i, T val) { 
//         for (tree[i += sh] = val; i /= 2; ) 
//             tree[i] = Join()(tree[i + i], tree[i + i + 1]);
//     }
//     inline T get(int l, int r) { 
//         T res = E;
//         for (l += sh, r += sh + 1; l < r; l /= 2, r /= 2) {
//             if (l & 1) res = Join()(res, tree[l++]);
//             if (r & 1) res = Join()(res, tree[--r]);
//         }
//         return res;
//     }
//     inline T top() {
//         return tree[1];
//     }
//     inline void init() {
//         while (sh < n)
//             sh *= 2;
//         tree.assign(sh * 2, E);
//     }
//     inline void build() {
//         for (int i = sh; --i; ) 
//             tree[i] = Join()(tree[i + i], tree[i + i + 1]);
//     }
//     inline T assoc_get(int v, int tl, int tr, int l, int r) {
//         if (l > tr || r < tl)
//             return E;
//         if (l <= tl && tr <= r) 
//             return tree[v];
//         int tm = tl + tr >> 1;
//         return Join()(assoc_get(v + v, tl, tm, l, r), assoc_get(v + v + 1, tm + 1, tr, l, r));
//     }
//     inline T assoc_get(int l, int r) { 
//         return assoc_get(1, 0, sh - 1, l, r);
//     }

//     /////////////////////////// spusk /////////////
//     // int spusk(int val) {
//     //     if (tree[1] < val) 
//     //         return n;
//     //     int v = 1;
//     //     while (v < sh)
//     //         if (tree[v += v] < val) 
//     //             val -= tree[v++];
//     //     return v - sh;
//     // }
// };

int main() {
    int n;
    cin >> n;
    vector<pii> g(n), l(n);

    ll ans = 0;

    for (int i = 0; i < n; ++i) {
        cin >> a[i] >> b[i];
        if (a[i] < b[i])
            ans += b[i];
    }
    cout << ans << '\n';
    // sort(g.begin(), g.end(), [](const pii & p1, const pii & p2) {
    //     return p1.ff - p1.ss > p2.ff - p2.ss;
    // });

    // ll ans = 0;
    // for (pii p : g) {
    //     p.ss -= p.ff;
    //     ans += p.ff;
    //     p.ff = 0;
    // }



    // segment_tree<pii> st(d.begin(), d.end(), pii(INF, -INF));

    // while (1) {
    //     auto p = 
    // }

    return 0;
}