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 (Python):
N = int(input())
A = []
B = []
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

flag = 0
for i in range(N):
    if A[i] != B[i]:
        flag = 1
        break

s = sum(A)
if flag == 1:
    mini = 10**9+1
    for i in range(N):
        if A[i] > B[i]:
            mini = min(mini, B[i])
else:
    mini = s
print(s-mini)

Solution (Python):
import sys
input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))

INF = 10**10
N = int(input())
rans = INF
T = 0
for _ in range(N):
    a, b = inpl()
    T += a
    if a>b:
        rans = min(rans, b)
if rans != INF:
    print(T-rans)
else:
    print(0)

Solution (Python):
import sys, math, collections, heapq, itertools
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0: a, b = b, a % b
    return b
  
def solve():
    N = int(single_input())
    sum = 0
    smallestB = 10 ** 20
    for i in range(N):
        a, b = map(int, line_input())
        sum += a
        if a > b: smallestB = min(smallestB, b)
    print(max(0, sum - smallestB))
    return 0
  
if __name__ == "__main__":
    solve()

Solution (Python):
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]=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))

Solution (Python):
N = int(input())
ans = 0
mi = float("inf")
for _ in range(N):
    A, B = map(int, input().split())
    if A>B:
        mi = min(mi, B)
    ans += B
print(ans - mi if mi != float("inf") else 0)

Solution (Python):
n = int(input())
a = [0] * n
b = [0] * n

temp = 10 ** 10
for i in range(n):
    a[i], b[i] = map(int, input().split())
    if a[i] > b[i]:
        temp = min(temp, b[i])

if temp == 10 ** 10:
    print(0)
else:
    print(sum(a) - temp)

Solution (Python):



def solve(n, a, b):
    x = x0 = 10 ** 10
    for i in range(n):
        if b[i] < a[i]:
            x = min(x, b[i])

    if x == x0:
        return 0
    else:
        return sum(a) - x


def main():
    n = int(input())
    a = []
    b = []
    for _ in range(n):
        ai, bi = input().split()
        a.append(int(ai))
        b.append(int(bi))


    print(solve(n, a, b))


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())

A = []
B = []

for i in range(N):
  Data = list(map(int, input().split()))
  A.append(Data[0])
  B.append(Data[1])
  
ma = 10e10
for i in range(N):
  if A[i] > B[i]:
    if B[i] < ma:
      ma = B[i]

if ma == 10e10:
  print(0)
else:
  print(sum(A) - ma)

Solution (Python):
N = int(input())
sum_a = 0
num = 10e10
for i in range(N):
  a,b = list(map(int,input().split()))
  sum_a += a
  if a > b:
    num = min(num,b)

if num == 10e10:
  print(0)
else:
  print(sum_a-num)

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

Solution (Python):
N=int(input())
S=0
l=float("inf")
for i in range(N):
  a,b=map(int,input().split())
  S+=a
  if a>b:
    l=min(l,b)
print(max(S-l,0))

Solution (Python):
def inpl(): return [int(i) for i in input().split()]
N = int(input())
A, B = [], []
for _ in range(N):
    a, b = inpl()
    A.append(a)
    B.append(b)
C = [B[i] for i in range(N) if A[i] > B[i]]
if not C:
    c = sum(A)
else:
    c = min(C)
print(sum(A)-c)

Solution (Python):
n = int(input())
sum1 = 0
ti = float("inf")
for i in range(n):
  a,b = map(int,input().split())
  if a > b:
    ti = min(ti,b)
  sum1 += a
if ti == float("inf"):
  print(0)
else:
  print(sum1 - ti)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    N = int(input())
    A, B = [], []

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

    r = INF
    for a, b in zip(A, B):
        if a == 0 and b == 0:
            continue
        if b < a:
            r = min(r, b)

    if r == INF:
        print(0)
    else:
        print(total - r)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
m,num = 10**9+1,0
for _ in range(n):
	a,b = (int(i) for i in input().split())
	num+=a
	if a>b and m>b: m = b
if m==10**9+1: print(0)
else:  print(num-m)

Solution (Python):
N = int(input())
A = [0]*N
B = [0]*N
hoge = int(1e18)
for i in range(N):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b
    if a > b:
        hoge = min(hoge, b)

if hoge == int(1e18):
    print(0)
else:
    print(sum(B) - hoge)

Solution (Python):
N = int(input())
c = -1
s = 0
for i in range(N):
    ai,bi = map(int,input().split())
    s += ai
    if ai > bi:
        if c == -1:
            c = bi
        if bi < c:
            c = bi
if c == -1:
    print(0)
else:
    print(s-c)

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N = inp()
tmp = INF
S = 0
for i in range(N):
	A,B = inpl()
	S += A
	if A > B:
		tmp = min(B,tmp)

if tmp == INF:
	print(0)
else:
	print(S-tmp)

Solution (Python):
N = int(input())

b_min, isequal, total = 10**10, 0, 0
for _ in range(N):
    a, b = map(int, input().split())
    total += a
    if a > b:
        if b < b_min:
            b_min = b
    elif a == b:
        isequal += 1

if isequal == N:
    print(0)
else:
    print(int(total - b_min))

Solution (Python):
n = int(input())
aSum = 0
abSum = 0
bMin = float('inf')

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

    abSum += a
    if a > b:
        bMin = min(bMin, b)
    else:
        aSum += a

if aSum == abSum:
    print(0)
else:
    print(abSum - bMin)

Solution (Python):
n = int(input())

s = 0
m = 10**9+7
same = True
for _ in range(n):
    a,b = map(int, input().split())
    if a > b:
        m = min(m, b)
    s += a
    same &= a==b
if same:
    m = s
print(s-m)

Solution (Python):
n = int(input())
sum = 0
bmin = []
for _ in range(n):
    a, b = map(int,input().split())
    sum += a
    if a > b:
        bmin.append(b)
if len(bmin)==0:
    print(0)
else:
    print(sum - min(bmin))

Solution (Python):
N = int(input())
A, B = [], []
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
M = sum(A)

for i in range(N):
    if A[i] <= B[i]:
        B[i] -= A[i]
        A[i] = 0

ne = M
for i in range(N):
    if A[i] != 0 and B[i] < ne:
        ne = B[i]
print(M - ne)

Solution (Python):
N = int(input())
A, B = [], []
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
M = sum(A)

for i in range(N):
    if A[i] <= B[i]:
        A[i] = 0
        B[i] -= A[i]

ne = M
for i in range(N):
    if A[i] != 0 and B[i] < ne:
        ne = B[i]
print(M - ne)

Solution (Python):
n = int(input())
a, b, c = [], [], []
for i in range(n):
    ab = list(map(int, input().split()))
    a.append(ab[0])
    b.append(ab[1])
    if(ab[0] > ab[1]):
        c.append(ab[1])
if(len(c) == 0):
    print(0)
else:
    print(sum(a) - min(c))

Solution (Python):
import sys

n = int(raw_input())
t = 0
d = 0
min_b = -1
for i in range(n):
  a, b = map(int, raw_input().split())
  t += a
  if a < b:
    d += b - a
  elif a > b:
    if min_b == -1 or min_b > b:
      min_b = b
if d == 0:
  print 0 
else:
  print t - min_b

Solution (Python):
import math
q=int(input())
cnt=0
Issame=True
remainder=1e10
sum=0
for j in range(q):
  a,b=[int(i) for i in input().split()]
  Issame=Issame if a==b else False 
  if a>b:
    remainder=min(remainder,b)
  sum+=a
if Issame:
  print(0)
else:
  print(sum-remainder)

Solution (Python):
n = int(input())
s = 0
m = 1*10**9+1
for _ in range(n):
    ai, bi = map(int, input().split())
    s += ai
    if ai > bi:
        if m > bi:
            m = bi

print(0) if m == 1*10**9+1 else print(s-m)

Solution (Python):

"""
Contents   : AtCoder Regular Contest094 e問題 未完成
Author     : Kitaura Hiromi
LastUpdate : 20180420
Since      : 20180414
"""
inf = 10**20

N = int(input())
s = 0
m = inf
for i in range(N):
    a, b = map(int, input().split(" "))
    if a > b:
        if m > b:
            m = b
    s += a
if m == inf:
    print(0)
else:
    print(s - m)

Solution (Python):
inf = 10**20
n = int(input())
arr = []
s = 0
m = inf
for _ in range(n):
    a,b = ([int(x) for x in input().split()])
    if a > b and m > b:
        m = b
    s += a
if m == inf:
    print(0)
else:
    print(s - m)

Solution (Python):
def E():
    N = int(input())

    sum_ = 0
    c = -1

    for _ in range(N):
        a, b = list(map(int, input().split(' ')))
        if a > b:
            if c == -1:
                c = b
            elif b < c:
                c = b
            
        sum_ += a

    if c == -1:
        print(0)
    else:
        print(sum_ - c)
        
if __name__ == '__main__':
    E()

Solution (Python):
N=int(input())
ans=0
turn=0
AB=[]



for i in range(N):
  a,b=map(int,input().split())
  if a<=b:
    ans+=a
    turn+=(b-a)
  else:
    
    AB.append([a,b])
    








if turn==0:
  print(0)
else:
  AB=sorted(AB,key=lambda x:x[1])
  for i in range(len(AB)):
    if i==0:
      ans+=(AB[i][0]-AB[i][1])
    else:
      ans+=AB[i][0]
  print(ans)

Solution (Python):
import math

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

Solution (Python):
n = int(input())
s = 0
m = 10000000000
flag = False
for i in range(n):
  a,b = list(map(int,input().split()))
  s += a
  if a != 0 and a > b and m > b:
    m = b
  if a != b:
    flag = True
if flag:
  print(s - m)
else:
  print(0)

Solution (Python):
N = int(input())
c = -1
s = 0
for i in range(N):
    ai,bi = map(int,input().split())
    s += ai
    if ai > bi:
        if c == -1:
            c = bi
        if bi < c:
            c = bi
if c == -1:
    print(0)
else:
    print(s-c)

Solution (Python):


num = int(raw_input())
for_sum=0
for_min=10**11
is_same=True
for i in range(num):
	
	a, b = map(int, raw_input().split())
	for_sum+=a
	for_min=b if (a>b and for_min > b) else for_min
	is_same=is_same and (a==b)

print 0 if is_same else for_sum-for_min

Solution (Python):
n = int(input())
a,bm = [0 for i in range(n)],1234567890
flag = True
for i in range(n):
	a[i],b = map(int,input().split())
	if a[i] > b:
		flag = False
		bm = min(bm,b)
print(0 if flag else sum(a)-bm)

Solution (Python):
N = int(input())
A = []
B = []
for i in range(N):
    a,b = map(int,input().split())
    A.append(a)
    B.append(b)
if A == B:
    print(0)
    exit()

min_b = sum(A)
for a,b in zip(A,B):
    if a <= b: continue
    min_b = min(min_b, b)
print(sum(A) - min_b)

Solution (Python):
N = int(input())
A, B = [], []
for i in range(N):
    ai, bi = map(int, input().split())
    A.append(ai)
    B.append(bi)

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

bmin = float('inf')
for a, b in zip(A, B):
    if a > b and bmin > b:
        bmin = b

print(sum(A) - bmin)

Solution (Python):
N = int(input())
A = list()
B = list()
m = float('inf')
for i in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)

if A == B: print(0)
else:
    seriesSum = sum(A)
    for i in range(N):
        if A[i] > B[i]:
            m = min(m, B[i])
    print(seriesSum - m)

Solution (Python):
N = int(input())
ans = 0
m = INF = 10**9+7
d = []
for i in range(N):
    a, b = map(int, input().split())
    ans += a
    if a > b:
        m = min(m, b)
print(0 if INF == m else ans-m)

Solution (Python):
def get_next_int():
    return int(float(input()))


def get_next_ints(delim=" "):
    return tuple([int(float(x)) for x in input().split(delim)])


def get_next_str():
    return input()


def get_next_strs(delim=" "):
    return tuple(input().split(delim))


def get_next_by_types(*value_types, delim=" "):
    return tuple([t(x) for t, x in zip(value_types, input().split(delim))])


def solve():
    N = get_next_int()
    A, B = [], []
    diff_progression = False
    min_B = float('INF')
    for i in range(N):
        a, b = get_next_ints()
        A.append(a)
        B.append(b)
        if a != b:
            diff_progression = True
        if a > b:
            min_B = min(min_B, b)
    if not diff_progression:
        print(0)
    else:
        ans = sum(A) - min_B
        print(ans)

solve()

Solution (Python):
N = int(input())

same = True
sum_a = 0
min_b = None

for _ in range(N):
    a, b = map(int, input().split())
    
    sum_a += a
    
    if a != b:
        same = False
    
    if a > b and (min_b is None or b < min_b):
        min_b = b


if same:
    print(0)
    exit()


if min_b is None:
    min_b = 0

print(sum_a - min_b)

Solution (Python):
N = int(input())
A = []
B = []
C = []
ans = 0
equal = True
for i in range(N):
    t = tuple(map(int, input().split()))
    if t[0] != t[1]:
        equal = False
    A.append(t[0])
    B.append(t[1])
if A == B:
    print(ans)
else:
    for i in range(N):
        if A[i] > B[i]:
            C.append(B[i])
    print(sum(A) - min(C))

Solution (Python):
from sys import exit, setrecursionlimit
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N = read()

A = [0] * N; B = [0] * N
for i in range(N):
  (A[i], B[i]) = reads()
S = sum(A)

if A == B:
  print(0)
else:
  M = min(B[i] for i in range(N) if A[i] > B[i])
  print(S - M)

Solution (Python):
n = int(input())
sol = 0
niz = []
d1, d2 = 0, 0
for i in range(n):
    a, b = map(int, input().split())
    if a == b:
        sol += a
    elif a < b:
        sol += a
        d1 += b - a
    else:
        d2 += a - b
        niz.append(b)
if d1 + d2 == 0:
    print(0)
else:
    assert d1 == d2
    print(sol + d1 + sum(niz) - min(niz))

Solution (Python):
m = None
s = 0
for _ in range(int(input())):
    a, b = map(int, input().split())
    s += a
    if a == b: continue
    if a > b:
        if m is None or b < m: m = b
print(0 if m is None else s - m)

Solution (Python):
def solve(A,B):
  if A==B:
    return 0,A[:]
  a1 = 0
  for p1 in range(len(A)):
    if A[p1]>0:
      A[p1] -= 1
      a2 = 9999
      for p2 in range(len(B)):
        if B[p2]>0:
          B[p2] -= 1
          a,b = solve(A,B)
          a += 1
          if a<a2:
            a2 = a
            b2 = b
          B[p2] += 1
      if a2>a1:
        a1 = a2
        b1 = b2
      A[p1] += 1
  return a1,b2












N = input()
A,B = [0]*N, [0]*N
for i in range(N):
  A[i], B[i] = map(int, raw_input().split())
t = 9999999999
for i in range(N):
  if A[i]>B[i]:
    t = min(t, B[i])
if t==9999999999:
  print 0
else:
  print sum(B)-t

Solution (Python):

def main():
	n = int(input())
	nums = [list(map(int, input().split())) for _ in range(n)]
	a = [x[0] for x in nums]
	b = [x[1] for x in nums]

	if a == b:
		print(0)
		return 
	min_b = min([x[1] for x in nums if x[0] > x[1]])
	print(sum(a) - min_b)
	return 

if __name__ == '__main__':
	main()

Solution (Python):


import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq





def solve(f):
    n = f.read_int()
    diff = False
    mi = float('inf')
    s = 0
    res = 0
    for _ in xrange(n):
        a, b = f.read_int_list()
        s += a
        if a > b:
            diff = True
            mi = min(b, mi)

    if diff:
        return s - mi
    else:
        return 0


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case 
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Solution (Python):


import sys, math, copy




HUGE = 2147483647
HUGEL = 9223372036854775807
ABC = "abcdefghijklmnopqrstuvwxyz"

def main():
    n = int(input())
    an = []
    bn = []
    for i in range(n):
        a, b = map(int, input().split())
        an.append(a)
        bn.append(b)
    su = sum(an)
    assert sum(bn) == su

    if an == bn:
        return 0

    mi_b_smaller = HUGE
    for i in range(n):
        if an[i] > bn[i]:
            mi_b_smaller = min(mi_b_smaller, bn[i])

    return su - mi_b_smaller

print(main())

Solution (Python):
import numpy as np
N = int(raw_input())

lis = []
tot = 0
count = 0

for i in range(N):
    AB = map(int,raw_input().split())
    tot += AB[0]
    if AB[0] > AB[1]:
        lis.append(AB[1])

if len(lis) == 0:
    print 0

else:
    lis = np.array(lis)
    print tot - np.min(lis)

Solution (Python):
n = int(input())
min_b = float('inf')
sa = 0
for _ in range(n):
    a, b = map(int, input().split())
    if a - b > 0:
        min_b = min(min_b, b)
    sa += a
if min_b == float('inf'):
    print(0)
else:
    print(sa - min_b)

Solution (Python):

def main():
    n = int(input())
    xs, ys = [], []
    for _ in range(n):
        x, y = map(int, input().split())
        xs.append(x)
        ys.append(y)

    if xs == ys:
        print(0)
        return

    ans = 0
    capacity = 0
    weights, values = [], []
    for x, y in zip(xs, ys):
        if x <= y:
            ans += y
            capacity += y - x
        else:
            weights.append(x - y)
            values.append(y)

    unavailable = min(values)
    ans -= unavailable
    ans += sum(values)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
q = int(input())
a = []
b = []
smb = []

for i in range(q):
    t1, t2 = map(int,input().split(' '))
    a.append(t1)
    b.append(t2)
    if t1 > t2:
        smb.append(t2)

smb.sort()

if a == b:
    print(0)
else:
    b.sort()
    print(sum(b)-smb[0])

Solution (Python):
from sys import stdin
from itertools import repeat
def main():
    n = int(stdin.readline())
    dat = map(int, stdin.read().split(), repeat(10, 2 * n))
    p = []
    s = 0
    for i in xrange(n):
        if dat[i*2+1] < dat[i*2]:
            p.append(dat[i*2+1])
        s += dat[i*2]
    if not p:
        print 0
        return
    p.sort()
    print s - p[0]
main()

Solution (Python):
N = int(input())
A,B = zip(*(map(int,input().split()) for _ in range(N)))

c = min((b for a,b in zip(A,B) if a > b), default=None)
if c is None:
  print(0)
else:
  print(sum(A)-c)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    s = 0
    m = inf
    for _ in range(n):
        a,b = LI()
        if a > b and m > b:
            m = b
        s += a

    if m == inf:
        return 0
    return s - m


print(main())