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-----
2
1 2
3 2

-----Sample Output-----
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.

Difficulty: competition

Starter code:

Solutions:
#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7
#R = 998244353

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = []
b = []
xb = 10**9+1
for i in range(n):
    aa,bb = inm()
    a.append(aa)
    b.append(bb)
    if aa>bb and xb>bb:
        xb = bb
        xi = i
if n==-2 and a[0]==1:
    3/0
print(0 if xb>10**9 else sum(a)-b[xi])


import sys
input = sys.stdin.readline
n = int(input())
m = 10**10
ans = 0
flag = 0
for i in range(n):
  a,b = list(map(int,input().split()))
  if a > b:
    flag = 1
    m = min(m,b)
  ans += a
print(((ans-m)*flag))


import sys
input = sys.stdin.readline

N = int(input())
s = 0
m = float('inf')
for _ in range(N):
    A, B = list(map(int, input().split()))
    s += A
    if A > B:
        m = min(m, B)
print((max(0, s - m)))


N=int(input())
d=0
s=10**9+1
for i in range(N):
  a,b=map(int, input().split())
  d+=a
  if a>b:
    s=min(s,b)
if s==10**9+1:
  print(0)
  return
print(d-s)

n = int(input())
res = 100000000000000000000000
s = 0
f = 0
for i in range(n):
    a, b = list(map(int, input().split()))
    s += a
    if a > b:
        res = min(res, b)
    if a != b:
        f = 1
if not f:
    print((0))
else:
    print((s-res))


import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return list(map(int, input().split()))
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    ans=0
    A=[0]*N
    B=[0]*N
    for i in range(N):
        A[i],B[i]=MI()
        
    if A==B:
        print((0))
        
    else:
        ans=sum(A)
        m=10**10
        for i in range(N):
            if A[i]>B[i]:
                m=min(m,B[i])
                
        print((ans-m))
            
        
    

main()


# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n = int(readline())
ans = 0
bmax = 1<<31
same = 1
for i in range(n):
    a,b = map(int,readline().split())
    ans += a
    if a > b:
        bmax = min(b,bmax)
        same = 0
ans -= bmax
if same: ans = 0
print(ans)

# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = [0] * N
B = [0] * N
for i in range(N):
    A[i], B[i] = MAP()

sm = 0
mn = INF
for i in range(N):
    a, b = A[i], B[i]
    if a > b:
        mn = min(mn, b)
    sm += b
if mn == INF:
    print((0))
    return
ans = sm - mn
print(ans)


n=int(input())
ans=0
c=[]
for i in range(n):
  a,b=map(int,input().split())
  ans+=a
  if a>b:c.append(b)
if len(c):print(ans-min(c))
else:print(0)

N = int(input())
a = [0] * N
b = [0] * N
for i in range(N):
    a1, b1 = map(int, input().split())
    a[i] = a1
    b[i] = b1

total = sum(a)
minans = total
for i in range(N):
    if a[i] > b[i]:
        minans = min(minans, b[i])

if minans == total:
    print(0)
else:
    print(total - minans)

import sys
input = sys.stdin.readline
n = int(input())
iptls = [list(map(int,input().split())) for i in range(n)]
a,b = zip(*iptls)
if a == b:
  print(0)
  return
ans = 10**18
for i in range(n):
  if a[i]>b[i]:
    ans = min(ans,b[i])
print(sum(b)-ans)

n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]
m = pow(10, 18)
s = 0
for i in range(n):
    s += ab[i][0]
    if ab[i][0] > ab[i][1]:
        m = min(m, ab[i][1])
ans = max(0, s - m)
print(ans)

n = int(input())
INF = 10**9
csum = 0
min_b = INF
for _ in range(n):
    a,b = list(map(int, input().split()))
    csum += a
    if a > b: min_b = min(min_b,b)

if min_b == INF: print((0))
else: print((csum-min_b))



N = int(input())
AB = [[int(i) for i in input().split()] for _ in range(N)]

s = 0
m = float('inf')
for i, (A, B) in enumerate(AB) :
    s += A
    if A > B :
        m = min(m, B)

if m == float('inf') :
    print(0)
else :
    print(s - m)

n=int(input())
a,b=[],[]
for i in range(n):x,y=map(int,input().split());a.append(x);b.append(y)
c=float("inf")
for i in range(n):
 if a[i]>b[i]:c=min(c,b[i])
print(max(0,sum(a)-c))

#!python3

import numpy as np


# input
N = int(input())
A, B = np.zeros(N, int), np.zeros(N, int)
for i in range(N):
    A[i], B[i] = list(map(int, input().split()))


def main():
    if (A == B).all():
        print((0))
        return
    
    ans = A.sum() - B[A > B].min()
    print(ans)


def __starting_point():
    main()

__starting_point()

'''
研究室PCでの解答
'''
import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(1,0),(0,-1),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"


def main():
    n = int(ipt())
    ans = 0
    bmi = 10**18
    sm = 0
    for i in range(n):
        a,b = list(map(int,ipt().split()))
        sm += a
        if a > b and bmi > b:
            bmi = b
    if bmi == 10**18:
        print((0))
    else:
        print((sm-bmi))

    return None

def __starting_point():
    main()

__starting_point()

N = int(input())
A,B = [],[]
for _ in range(N):
    a,b = list(map(int,input().split()))
    A.append(a)
    B.append(b)
if A == B:
    print((0))
    return
ans = 10**9+10
for i in range(N):
    if B[i] < A[i]:
        ans = min(ans,B[i])
print((sum(B) - ans))
        


N = int(input())
ABs = [tuple(map(int, input().split())) for _ in range(N)]

ans = 0
minB = 10**9
isSame = True
for A, B in ABs:
    ans += A
    if A > B:
        minB = min(minB, B)
    if A != B:
        isSame = False

if isSame:
    print((0))
else:
    print((ans-minB))


n,*l = list(map(int,open(0).read().split()))
a = l[::2]
b = l[1::2]
print((0 if a==b else sum(a) - min(b[i] for i in range(n) if b[i] < a[i])))


ans = 0
N = int(input())
A, B = zip(*list(list(map(int, input().split())) for _ in range(N)))
if A == B:
    print(0)
    return

Asum = sum(A)
ans = max(Asum - b for a, b in zip(A, B) if a > b)
print(ans)

n=int(input())
arr=[list(map(int,input().split())) for _ in range(n)]
arr2=[]
ans=0
mb=10**18
for a,b in arr:
  ans+=a
  if a>b:
    mb=min(mb,b)
if mb==10**18:
  print(0)
else:
  print(ans-mb)

N = int(input())
A, B = [], []
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

if A == B:
    print(0)
else:
    print(sum(A) - min([b for a, b in zip(A, B) if a > b]))

N=int(input())
AB=[list(map(int,input().split())) for i in range(N)]
print(0 if all([a==b for a,b in AB]) else sum([b for a,b in AB])-min([b for a,b in AB if a>b]))

N = int(input())
diff_a_b = []
for i in range(N):
  a, b = map(int, input().split())
  diff_a_b.append([a - b, a, b])

diff_a_b.sort()
if diff_a_b[0][0] == 0: # 最初から数列が等しい
  print(0)
  return

ans = 0
d = 0
temp = 10**10
for diff, a, b in diff_a_b:
  if diff <= 0: # a <= b
    ans += b
    d -= diff
  else:
    ans += b
    temp = min(temp, b)

print(ans - temp)

N=int(input())
s=0
minus=10**18
flag=True
for i in range(N):
    a,b=map(int,input().split())
    if a>b:
        minus=min(minus,b)
        flag=False
    elif b>a:
        flag=False
    s+=a
if flag:
    print(0)
else:
    print(s-minus)

N = int(input())
A = [0]*N
B = [0]*N
for i in range(N):
    a,b = list(map(int, input().split()))
    A[i] = a
    B[i] = b

num = 10**9+1
for i in range(N):
    if A[i] > B[i]:
        num = min(num, B[i])

if num == 10**9+1:
    print((0))
else:
    print((sum(A)-num))


N = int(input())
sum_B = 0
min_B = 10**9 + 1
diff = False
for i in range(N):
  A, B = list(map(int, input().split()))
  if A != B:
    diff = True
  sum_B += B
  if A > B:
    min_B = min(B, min_B)
print((sum_B - min_B) if diff else 0)

N = int(input())
AB = [tuple(map(int, input().split())) for _ in range(N)]

A, B = list(zip(*AB))

if A == B:
    print((0))
    return

ans = sum(A)

ans -= min([b for a, b in AB if b < a])
print(ans)


import sys
input = sys.stdin.readline
N = int(input())
res = 0
f = 1
t = 10 ** 10
for _ in range(N):
  x, y = map(int, input().split())
  if x != y: f = 0
  if x > y:
    t = min(t, y)
  res += y
if f:
  print(0)
  return

print(res - t)

import sys
input = sys.stdin.readline

N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]

k = 10**15
s = 0
for i in range(N):
    a, b = AB[i]
    s += b
    if a > b:
        k = min(k, b)

if k == 10**15:
    print(0)
else:
    print(s-k)

#!/usr/bin/env python3
import sys
def input(): return sys.stdin.readline().rstrip()

def trans(l_2d):#リストの転置
    return [list(x) for x in zip(*l_2d)]

def main():
    n=int(input())
    ab=[list(map(int, input().split())) for i in range(n)]
    ans=0
    same=[]
    abb=trans(ab)
    if abb[0]==abb[1]:
        print(0)
        return
    mins=10**10
    for aabb in ab:
        if aabb[0] > aabb[1]:
            mins=min(aabb[1],mins)
    print(sum(abb[0])-mins)

    
    
def __starting_point():
    main()
__starting_point()

import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return list(map(int,sys.stdin.readline().rstrip().split()))

N = I()
ans = 0
A,B = [],[]
for i in range(N):
    a,b = MI()
    A.append(a)
    B.append(b)

if A == B:
    print((0))
    return

s = sum(B)
m = s
for i in range(N):
    if A[i] > B[i]:
        m = min(m,B[i])

print((s-m))


n = int(input())
total = 0
rem = 10 ** 20
for _ in range(n):
    a, b = map(int, input().split())
    if a > b:
        rem = min(rem, b)
    total += b
if rem == 10 ** 20:
    print(0)
else:
    print(total - rem)

N=int(input())
num=0
result=0
min=10000000000
for i in range(N):
  a,b=list(map(int,input().split()))
  if a>b:
    if b<min:
      min=b
      num=1
  result+=a

if num ==0:
  print((0))
else:
  result-=min
  print(result)


import sys
input = sys.stdin.readline
from collections import defaultdict
mod = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

def main():
	N = int(input())
	jud = 0
	ans = 0
	S = 0
	m = INF
	for i in range(N):
		A, B = getlist()
		S += A
		if A != B:
			jud = 1
		if A > B:
			m = min(m, B)

	if jud == 0:
		print(0)
	else:
		print(S - m)


def __starting_point():
	main()
__starting_point()

n = int(input())
ab = [list(map(int, input().split())) for i in range(n)]
if all([a == b for a,b in ab]):
  print(0)
  return
ans = 0
diff = 10 ** 9
for a,b in ab:
  ans += b
  if a > b:
    diff = min(diff, b)
print(ans-diff)

def slv(n,ab):
    if [a for a,b in ab]==[b for a,b in ab]:return 0
    c=[]
    W=0
    ret=0
    for i,(a,b) in enumerate(ab):
        # どれだけの要素を0にできるか。
        # 0にできない要素の和を最小化する。
        # a<=bとなっているところは0にできる
        if a-b>0:
            c.append([b,a-b])
        else:
            ret+=b
            W+=b-a
    W-=1
    # ナップザック問題？
    # c=[v,w],W:
    # 価値v重さwの商品配列c、最大容量Wのナップザックに入る最大価値は
    # ナップザックに入るcの要素を0にできる
    # |c|<=10^5 v,w<=10^9 W<=10^14,sum(w)-1==W
    # sum(w)-1==Wより、はいらない商品は1つだけ。vの小さい商品をのこして他は入れればよい
    # dp不要
    c.sort(key=lambda x:x[0])
    ret+=sum([v for v,w in c[1:]])
    return ret



n=int(input())
ab=[list(map(int,input().split())) for _ in range(n)]
print((slv(n,ab)))


# ARC094E

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")



n = int(input())
a = [None] * n
b = [None] * n
ans = 0
same = 0
m = 0
diff = []
for i in range(n):
    a[i],b[i] = map(int, input().split())
    if a[i]<b[i]:
        ans += (b[i] - a[i])
    elif a[i]==b[i]:
        same += a[i]
    else:
        diff.append(b[i])
        m += b[i]
diff.sort()
c = 0
plus = 0
if ans==0:
    print(0)
else:
    plus = sum(diff[1:])
    print(sum(a) - (m - plus))

N = int(input())
S = 0
minB = 1000000000000000000
for _ in range(N):
    x, y = list(map(int, input().split()))
    S += x
    if x > y:
        minB = min(minB, y)

print((max(0, S - minB)))


import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A, B = np.array([lr() for _ in range(N)]).T
if np.all(A == B):
    print((0)); return

index = np.where(B<A)[0]
answer = np.sum(A) - np.min(B[index])
print(answer)

# 12


n=int(input())
total=0
allsame=1
minb=10**9+1
for _ in range(n):
  a,b=list(map(int,input().split()))
  total+=a
  if a>b:
    allsame=0
    minb=min(b,minb)
if allsame==1:
  print((0))
else:
  print((total-minb))


N = int(input())
ans = 0
a_chance = 0
a_remain = []
b_remain = []
for i in range(N):
    a,b = map(int,input().split())
    if b >= a:
        ans += b
        a_chance += b-a
    else:
        a_remain.append(a)
        b_remain.append(b)
if a_chance ==0:
    print(0)
else:
    print(ans+sum(b_remain)-min(b_remain))  

n = int(input())
ans = []
sum_a = 0
for i in range(n):
    a, b = list(map(int, input().split()))
    sum_a += a
    if a > b:
        ans.append(b)
if ans == []:
    min_ans = sum_a
else:
    min_ans = min(ans)
print((sum_a-min_ans))


n = int(input())
INF = 10**9
csum = 0
min_b = INF
for _ in range(n):
    a,b = list(map(int, input().split()))
    csum += a
    if a > b:
        min_b = min(min_b,b)

if min_b == INF:
    print((0))
else:
    ans = csum-min_b
    print(ans)


# a_down_sum = 0
# top_diff = 0
# for _ in range(n):
#     a,b = map(int, input().split())
#     if a < b:
#         a_down_sum += a
#     else:
#         top_diff = max(top_diff,a-b)




N = int(input())
res = float("inf")
S = 0
cnt = 0
for _ in range(N):
    A, B = map(int, input().split())
    S+=A
    if A>B:
        res = min(B,res)
    if A==B:
        cnt += 1
print(S-res if cnt!=N else 0)

def main():
    n = int(input())
    s = 0
    x = 10**10
    for _ in range(n):
        a, b = map(int, input().split())
        if a > b:
            if x > b:
                x = b
        s += a
    if x == 10**10:
        print(0)
    else:
        print(s-x)

def __starting_point():
    main()
__starting_point()

n = int(input())
ab = [list(map(int,input().split())) for i in range(n)]
count = 0
m = 10**10
for a,b in ab:
    if a > b:
        m = min(m,b)
    count += a
if m == 10**10:
    print(0)
else:
    print(count-m)

n=int(input())
INF=10**9+7
sum=0
m=INF
for i in range(n):
    a,b=map(int,input().split())
    sum+=a
    if a>b:
        m=min(m,b)
if m==INF:
    print(0)
else:
    print(sum-m)

n = int(input())
aa = 0
mi = 10 ** 10
flag = True
for _ in range(n):
    a, b = [int(i) for i in input().split()]
    aa += a
    if a != b:
        flag = False
    if a > b:
        mi = min(mi, b)
if flag:
    print((0))
else:
    print((aa-(mi if mi != 10**10 else 0)))


n = int(input())
ab = [list(map(int,input().split())) for i in range(n)]
count = 0
m = 10**10
for a,b in ab:
    if a > b:
        m = min(m,b)
    count += a
if m == 10**10:
    print(0)
else:
    print(count-m)

N=int(input())
A=[0 for i in range(N)]
B=[0 for i in range(N)]
for i in range(N):
    A[i],B[i]=list(map(int,input().split()))
X=[]
for i in range(N):
    if A[i]>B[i]:
        X.append(B[i])
if len(X)==0:
    print((0))
else:
    print((sum(A)-min(X)))


n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]

a, _ = list(zip(*ab))
sm = sum(a)

ans = 0
for a, b in ab:
    if a > b:
        ans = max(ans, sm - b)

print(ans)


def main():
  sum=0
  m=10**9+1
  for _ in range(int(input())):
    a,b=map(int,input().split())
    sum+=a
    if a>b:
      m=min(m,b)
  print(sum-m if m<=10**9 else 0)
main()

N = int(input())
a = []
b = []
for i in range(N):
    tmp1, tmp2 = list(map(int, input().split()))
    a.append(tmp1)
    b.append(tmp2)
count = 0
ls_l = [i for i in range(N) if a[i] > b[i]]
ls_m = [i for i in range(N) if a[i] == b[i]]
ls_s = [i for i in range(N) if a[i] < b[i]]

flg = 1
if len(ls_l) > 1:
    ls_l2 = [(b[i],i) for i in ls_l]
    ls_l2.sort()
    idx = ls_l2[0][1]
    count = sum(a)-b[idx]
elif len(ls_m) == N:
    count = 0
else:
    for idx in ls_m:
        count += a[idx]
    
    for idx in ls_s:
        count += a[idx]
    
    if flg==1:
        for idx in ls_l:
            count += a[idx]-b[idx]
print(count)


N=int(input())
x,y=0,0
A,B=[],[]
for i in range(N):
  x,y=map(int,input().split())
  A.append(x)
  B.append(y)
x=sum(A)
for i in range(N):
  if A[i]>B[i]:
    x=min(x,B[i])
print(sum(A)-x)


n = int(input())
c = 0
f = 0
m = 10000000000000000000000
for i in range(n):
    a,b = list(map(int, input().split()))
    c += a
    if a > b:
        m = min(b, m)
    if a != b:
        f = 1
print(((c-m)*f))


def main():
    n = int(input())
    ab = [list(map(int, input().split())) for _ in [0]*n]
    if all([a == b for a, b in ab]):
        print(0)
        return 0
    min_b = min([b for a, b in ab if a > b])
    sum_a = sum([a for a, b in ab])
    print(sum_a-min_b)


main()

N=int(input())
sum=0
min=10**9+1
for i in range(N):
  A,B=map(int,input().split())
  if A>B and min>B:
    min=B
  sum+=A
if min==10**9+1:
  print(0)
else:
  print(sum-min)

# https://betrue12.hateblo.jp/entry/2019/06/06/214607

N = int(input())
AB = (list(map(int, input().split())) for _ in range(N))

INF = 10 ** 9 + 10
s = 0
last = INF
for a, b in AB:
    if a > b:
        last = min(last, b)
    s += b

if last == INF:
    print((0))
else:
    print((s - last))


N = int(input())
A = []
B = []
for _ in range(N):
  a, b = list(map(int, input().split()))
  A.append(a)
  B.append(b)
  
if A == B:
  print((0))
else:
  M = sum(A)
  c = 10**10
  for a, b in zip(A, B):
    if a > b:
      c = min(c, b)
  print((M - c))


import numpy as np
import sys
N = int(input())
A = np.zeros(N, dtype=np.int64)
B = np.zeros(N, dtype=np.int64)
for i in range(N):
    A[i], B[i] = map(int,input().split())
if all(A == B):
    print(0)
    return
ans = A.sum() - B[A > B].min()
print(ans)

_,*t=open(0).read().split()
t=map(int,t)
s=0
m=1e18
for a,b in zip(t,t):
 s+=a
 if a>b:m=min(m,b)
print(max(0,s-m))


"""
すべて等しかったら0
それ以外は A > B を満たすものの内最小の黒以外取れる
"""

N = int(input())

ans = 0
l = []

for i in range(N):

    A,B = list(map(int,input().split()))

    ans += A

    if A > B:
        l.append(B)


if len(l) == 0:
    print((0))
else:
    l.sort()
    print((ans - l[0]))
    


n = int(input())
ans = 0
t = 10000000000000000
for i in range(n):
    a, b = list(map(int, input().split()))
    ans += a
    if a > b:
        t = min(t, b)
print((max(0, ans - t)))


import sys, math

input = sys.stdin.readline
N = int(input())
min_d = math.inf
sum_A = 0
for _ in range(N):
    a, b = list(map(int, input().split()))
    sum_A += a
    if a>b:
        min_d = min(min_d, b)
ans = max(0, sum_A-min_d)
print(ans)


n = int(input())
A, B = map(list,zip(*[map(int,input().split()) for i in range(n)]))
if A==B:
	print(0)
else:
	print(sum(A)-min([B[i] for i in range(n) if A[i] > B[i]]))

N = int(input())
m = 10**9
ans = 0
for i in range(N):
    A,B = map(int,input().split())
    ans += B
    if A > B:
        m = min(m,B)
if m == 10**9:
    print(0)
else:
    print(ans-m)

import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# A[i]>B[i]を満たす最小のB[i]を残して終える

N = I()
A,B = LIR(N,2)
min_ = 10**10
for i in range(N):
    if A[i] > B[i]:
        if min_ > B[i]:
            min_ = B[i]

if min_ == 10**10:
    print(0)
else:
    print(sum(A)-min_)

def solve():
    ans = 0
    N = int(input())
    miny = float('inf')
    ans = 0
    x = [0]*N
    y = [0]*N
    for i in range(N):
        x[i],y[i] = map(int, input().split())
    if x==y:
        return 0
    for i in range(N):
        ans += x[i]
        if x[i]>y[i]:
            miny = min(miny,y[i])
    ans -= miny
    return ans
print(solve())

import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N = I()
ans = 0
A,B = [],[]
for i in range(N):
    a,b = MI()
    A.append(a)
    B.append(b)

if A == B:
    print((0))
    return

m = sum(B)
for i in range(N):
    if A[i] > B[i]:
        m = min(m,B[i])

print((sum(B)-m))


import sys
input=sys.stdin.readline
n=int(input())
B=[]
ans=0
for i in range(n):
	a,b=map(int,input().split())
	ans+=a
	if a>b:
		B.append(b)
if len(B)==0:
	print(0)
else:
	print(ans-min(B))

n = int(input())
data = []
ans = 0
for i in range(n):
    data.append(list(map(int, input().split())))
    ans += data[i][1]
data.sort(key=lambda x: x[0] - x[1])
k = 10**10
if sum([abs(data[i][1] - data[i][0]) for i in range(n)]):
    for i in range(n):
        if data[i][0] > data[i][1]:
            k = min(k, data[i][1])
    print(ans-k)
else:
    print(0)

n = int(input())
ab = [tuple(map(int, input().split())) for _ in range(n)]

INF = 1 << 30
mx = INF
sm = 0
for i in range(n):
    a, b = ab[i]
    sm += a
    if a > b:
        mx = min(mx, b)
if mx == INF:
    print((0))
else:
    print((sm - mx))


n=int(input())
res = 10**10
s=0
l=[list(map(int,input().split())) for _ in range(n)]
for a,b in l:
    s += a
    if a>b:
        res = min(res,b)
if res==10**10:
    print((0))
else:
    print((s-res))


_,*t=open(0).read().split()
t=map(int,t)
s=0
m=1e18
for a,b in zip(t,t):
 s+=a
 if a>b:m=min(m,b)
print(max(0,s-m))

#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, A: "List[int]", B: "List[int]"):

    ma = INF
    mb = INF
    for a, b in zip(A, B):
        if a > b:
            if mb > b:
                ma = a
                mb = b
    if ma == INF:
        print((0))
    else:
        print((sum(A)-mb))

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]"
    B = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, A, B)


def __starting_point():
    main()

__starting_point()

N = int(input())
A = []
B = []
for _ in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)

if(A == B):
    print((0))
    return

ans = sum(A)
dis = 10**10
for a, b in zip(A, B):
    if a > b:
        dis = min(dis, b)
print((ans - dis))


n = int(input())
ab=[list(map(int,input().split())) for i in range(n)]

sm=0
bmin=10**10
for itm in ab:
    a,b=itm
    sm+=a
    if a>b:
        bmin=min(bmin,b)
if bmin==10**10:
    print(0)
else:
    print(sm-bmin)

import sys
input = sys.stdin.readline

n = int(input())
a = []
b = []


res = 0

for i in range(n):
    aa,bb = map(int,input().split())
    a.append(aa)
    b.append(bb)

def f(a,b):
    c = []
    d = []
    judge = True

    cnt = 0
    for i in range(len(a)):
        if a[i] != b[i]:
            judge = False
    if judge:
        return cnt,c,d
    
    for i in range(len(a)):
        if a[i] <= b[i]:
            cnt += b[i]
        else:
            c.append(a[i])
            d.append(b[i])
    if len(c) == 1 or max(d) == 0:
        return cnt,[],[]
    else:
        mi = min(d)
        flag = True
        pl_flag = True
        for i in range(len(c)):
            if d[i] == mi and flag:
                c[i] = d[i] + 1
                flag = False
            else:
                if pl_flag and d[i] > 0:
                    c[i] = d[i]-1
                    pl_flag = False
                else:
                    c[i] = d[i]
        return cnt,c,d

while True:
    plus,a,b = f(a,b)
    res += plus
    if a == []:
        print(res)
        break

N,*L = map(int, open(0).read().split())
A = [0]*N
B = [0]*N
n = 10**10
flag = False
for i,(a,b) in enumerate(zip(*[iter(L)]*2)):
  A[i] = a
  B[i] = b
  if a!=b:
    flag = True
  if a>b:
    n = min(n,b)
print(sum(A)-n if flag else 0)

n = int(input())
s = 0
minb = float("inf")
f = False
for _ in range(n):
  a, b = map(int, input().split())
  if a > b:
    f = True
    minb = min(b, minb)
  s += a
if f:
  print(s - minb)
else:
  print(0)

n=int(input())
a=[];b=[]

for _ in range(n):
    ai,bi=list(map(int, input().split()))
    a.append(ai);b.append(bi)

s=sum(a)
mn=s
for ai,bi in zip(a,b):
    if ai<=bi:
        continue
    mn=min(bi,mn)
print((s-mn))


n = int(input())
s = 0
minb = float("inf")
for _ in range(n):
  a, b = map(int, input().split())
  if a > b:
    minb = min(b, minb)
  s += a
if minb == float("inf"):
  print(0)
else:
  print(s - minb)

n=int(input())
t=0
m=1e18
f=0
for i in range(n):
  a,b=map(int,input().split())
  t+=a
  if a>b:m=min(m,b)
  if a!=b:f=1
if f:
  print(t-m)
else:
  print(0)

n=int(input())
s=0
c=10**10
for i in range(n):
  a,b=map(int,input().split())
  s=s+a
  if a>b:
    if b<c:
      c=b
if c==10**10:
  print(0)
else:
  print(s-c)

import sys
input = sys.stdin.readline
n = int(input())
A = [list(map(int,input().split())) for i in range(n)]

ans = 10**10
s = 0
eq = 0
for i in range(n):
    if A[i][0] == A[i][1]:
        eq += 1
    s += A[i][0]
    if A[i][0] > A[i][1]:
        ans = min(ans, A[i][1])

if eq == n:
    print(0)
else:
    print(s - ans)

import sys
n=int(input())

is_same=True
m=10**10
ans=0
for _ in range(n):
    a,b=map(int,input().split())
    if a!=b: is_same=False
    if a>b: m=min(m,b)
    ans+=a

ans-=m
print(ans if not is_same else 0)

import typing
import sys
import math
import collections
import bisect
import itertools
import heapq
import decimal
import copy
import operator

# sys.setrecursionlimit(10000001)
INF = 10 ** 20
MOD = 10 ** 9 + 7
# MOD = 998244353
# buffer.readline()


def ni(): return int(sys.stdin.readline())
def ns(): return list(map(int, sys.stdin.readline().split()))
def na(): return list(map(int, sys.stdin.readline().split()))
def na1(): return list([int(x)-1 for x in sys.stdin.readline().split()])


# ===CODE===

def main():
    n = ni()
    d = []
    x = []
    y = []
    s = []
    for _ in range(n):
        a, b = ns()
        c = a-b
        d.append([a, b, c])
        x.append(a)
        y.append(b)
        if c > 0:
            s.append(b)
    d.sort(key=lambda x: x[2])

    if x == y:
        print((0))
        return

    ans=sum(x)-min(s)
    print(ans)


def __starting_point():
    main()

__starting_point()

n=int(input())
rem=10**10
S=0
allissame=0
for i in range(n):
  a,b=list(map(int,input().split()))
  S+=a
  if a<b:
    allissame|=1
    continue
  elif a>b:
    if rem > b:
      rem=b
    allissame|=1
  else:pass
if not allissame:
  print((0))
else:
  print((S-rem))


import sys
input = sys.stdin.readline
N = int(input())
res = 0
t = float("inf")
c = 0
for _ in range(N):
  x, y = map(int, input().split())
  if x > y: t = min(y, t)
  if x == y: c += 1
  res += x
if c == N:
  print(0)
  return
print(res - t)

import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
A, B = list(zip(*[list(map(int, sys.stdin.readline().split())) for _ in range(N)]))

A = np.array(A, dtype=int)
B = np.array(B, dtype=int)
if np.all(A == B):
    print((0))
else:
    ans = A.sum() - B[A > B].min()
    print(ans)


n = int(input())
s = 0
f = False
m = 10**9
for i in range(n):
  a,b = map(int,input().split())
  s += a
  if a < b:
    f = True
  if a > b:
    f = True
    m = min(m,b)
if f:
  print(s-m)
else:
  print(0)

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=[None]*n
    B=[None]*n
    for i in range(n):
        A[i],B[i]=map(int,input().split())

    if(A==B):
        print(0)
        return
    print(sum(A)-min(b for a,b in zip(A,B) if(a>b)))
resolve()