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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    static ArrayList<Integer> A = new ArrayList<>();
    static ArrayList<Integer> B = new ArrayList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int Q = scanner.nextInt();

        int count = 0;

        for (int i = 0; i < Q; i++) {
            A.add(scanner.nextInt());
            B.add(scanner.nextInt());
        }
        scanner.close();

        while (!A.isEmpty()) {
            // 要素チェック
            if (A.equals(B)) {
                System.out.println(count);
                return;
            }
            // A操作
            procA();

            // B操作
            procB();

            // count up
            count++;
        }

        System.out.println(count);
    }

    // A操作
    private static void procA() {
        int maxKey = 0;
        int max = -1;
        for (int i = 0; i < A.size(); i++) {
            int d = B.get(i) - A.get(i);
            if (d > 0 && d > max && A.get(i) != 0) {
                maxKey = i;
                max = d;
            }
        }
        if (max > -1) {
            A.set(maxKey, A.get(maxKey) - 1);
            return;
        }

        for (int i = 0; i < A.size(); i++) {
            int d = A.get(i) - B.get(i);
            if (d > max) {
                maxKey = i;
                max = d;
            }
        }
        if (max > -1) {
            A.set(maxKey, A.get(maxKey) - 1);
            if (A.get(maxKey) == B.get(maxKey)) {
                A.remove(maxKey);
                B.remove(maxKey);
            }
            return;
        }
    }

    // B操作
    private static void procB() {
        int minKey = 0;
        int min = 1000000001;
        for (int i = 0; i < B.size(); i++) {
            int d = B.get(i) - A.get(i);
            if (d > 0 && d < min && B.get(i) != 0) {
                minKey = i;
                min = d;
            }
        }
        if (min < 1000000001) {
            B.set(minKey, B.get(minKey) - 1);
            if (A.get(minKey) == B.get(minKey)) {
                A.remove(minKey);
                B.remove(minKey);
            }
        }
        return;
    }
}