atcoder#ARC094C. [ARC094E] Tozan and Gezan

[ARC094E] Tozan and Gezan

Score : 700700 points

Problem Statement

You are given sequences AA and BB consisting of non-negative integers. The lengths of both AA and BB are NN, and the sums of the elements in AA and BB are equal. The ii-th element in AA is AiA_i, and the ii-th element in BB is BiB_i.

Tozan and Gezan repeats the following sequence of operations:

  • If AA and BB are equal sequences, terminate the process.
  • Otherwise, first Tozan chooses a positive element in AA and decrease it by 11.
  • Then, Gezan chooses a positive element in BB and decrease it by 11.
  • 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

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 0Ai,Bi109(1iN)0 \leq A_i,B_i \leq 10^9(1\leq i\leq N)
  • The sums of the elements in AA and BB are equal.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 B1B_1

::

ANA_N BNB_N

Output

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

2
1 2
3 2
2

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

  • Tozan decreases A1A_1 by 11.
  • Gezan decreases B1B_1 by 11.
  • One candy is given to Takahashi.
  • Tozan decreases A2A_2 by 11.
  • Gezan decreases B1B_1 by 11.
  • One candy is given to Takahashi.
  • As AA and BB are equal, the process is terminated.
3
8 3
0 1
4 8
9
1
1 1
0