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))

    exit()



m = sum(B)

for i in range(N):

    if A[i] > B[i]:

        m = min(m,B[i])



print((sum(B)-m))

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))

    exit()



s = sum(B)

m = s

for i in range(N):

    if A[i] > B[i]:

        m = min(m,B[i])



print((s-m))

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))
    exit()
m = sum(B)
for i in range(N):
    if A[i] > B[i]:
        m = min(m, B[i])
print((sum(B) - m))

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))
    exit()
s = sum(B)
m = s
for i in range(N):
    if A[i] > B[i]:
        m = min(m, B[i])
print((s - m))

- -sys.setrecursionlimit(10**7) - - -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())  # 空白なし -m = sum(B) +s = sum(B) +m = s -print((sum(B) - m)) +print((s - m))