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: HARD

Solutions:
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
BIG = 10 ** 18
R = 10 ** 9 + 7

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


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)


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


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


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)


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)


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):
		if a - b > 0:
			c.append([b, a - b])
		else:
			ret += b
			W += b - a
	W -= 1
	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))


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)


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())
A = []
B = []
for i in range(N):
	Data = list(map(int, input().split()))
	A.append(Data[0])
	B.append(Data[1])
ma = 100000000000.0
for i in range(N):
	if A[i] > B[i]:
		if B[i] < ma:
			ma = B[i]
if ma == 100000000000.0:
	print(0)
else:
	print(sum(A) - ma)


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


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)


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)


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)


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


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)
	exit()
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 = [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)


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)


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)


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)


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())
(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 = [[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)


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)


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)


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


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 = [[int(i) for i in input().split()] for j in range(N)]
total = sum([sum(row) for row in ab]) // 2
diff = [row[1] for row in ab if row[0] > row[1]]
if len(diff) == 0:
	print(0)
else:
	print(total - min(diff))


N = int(input())
a = []
b = []
for i in range(N):
	(tmp1, tmp2) = 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())
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)


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


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)


N = int(input())
ans = 0
l = []
for i in range(N):
	(A, B) = 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())
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())
rem = 10 ** 10
S = 0
allissame = 0
for i in range(n):
	(a, b) = 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 math
import queue
import bisect
from collections import deque, defaultdict
import heapq as hpq
from sys import stdin, setrecursionlimit
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, *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)


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


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)


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


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)
	exit(0)
print(res - t)


n = int(input())
a = []
b = []
ku = []
for i in range(n):
	(tmp_a, tmp_b) = map(int, input().split())
	a.append(tmp_a)
	b.append(tmp_b)
if a == b:
	print(0)
	exit()
ans = 10 ** 9 + 7
for i in range(n):
	if a[i] - b[i] > 0:
		ans = min(b[i], ans)
print(sum(a) - ans)


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)


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


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())
M = 10 ** 9
C = M + 1
S = 0
for _ in range(N):
	(A, B) = map(int, input().split())
	S += A
	if B < A:
		C = min(B, C)
if M < C:
	C = S
print(S - C)


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


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())
sm = 0
mn = 10 ** 100
for i in range(n):
	(a, b) = map(int, input().split())
	if a > b:
		mn = min(mn, b)
	sm += a
print(max(0, sm - mn))


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))
	A = [int()] * N
	B = [int()] * N
	for i in range(N):
		A[i] = int(next(tokens))
		B[i] = int(next(tokens))
	solve(N, A, B)
main()


(n, *l) = 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])))


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)


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())
sum_a = 0
num = 100000000000.0
for i in range(N):
	(a, b) = list(map(int, input().split()))
	sum_a += a
	if a > b:
		num = min(num, b)
if num == 100000000000.0:
	print(0)
else:
	print(sum_a - num)


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)


import math
q = int(input())
cnt = 0
Issame = True
remainder = 10000000000.0
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)


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)


ans = 0
N = int(input())
(A, B) = zip(*list((list(map(int, input().split())) for _ in range(N))))
if A == B:
	print(0)
	exit()
Asum = sum(A)
ans = max((Asum - b for (a, b) in zip(A, B) if a > b))
print(ans)


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)


import typing
import sys
import math
import collections
import bisect
import itertools
import heapq
import decimal
import copy
import operator
INF = 10 ** 20
MOD = 10 ** 9 + 7

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

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


import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

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


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


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


import numpy as np
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()


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)
	exit(0)
print(res - t)


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


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


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:
		ans += b
		d -= diff
	else:
		ans += b
		temp = min(temp, b)
print(ans - temp)


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


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)


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


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())
ab = [list(map(int, input().split())) for _ in range(n)]
(a, _) = zip(*ab)
sm = sum(a)
ans = 0
for (a, b) in ab:
	if a > b:
		ans = max(ans, sm - b)
print(ans)


N = int(input())
(A, B) = ([], [])
d = float('inf')
ind = -1
for i in range(N):
	(a, b) = map(int, input().split())
	A.append(a)
	B.append(b)
	if a > b:
		if b < d:
			d = b
			ind = i
if d == float('inf'):
	print(0)
else:
	print(sum(A) - B[ind])


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)


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


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


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)


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n = int(readline())
data = list(map(int, read().split()))
tot = 0
same = 0
min_b = 10 ** 9
it = iter(data)
for (a, b) in zip(it, it):
	tot += a
	if a == b:
		same += 1
	elif a > b:
		min_b = min(min_b, b)
if same == n:
	print(0)
else:
	print(tot - min_b)


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


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)


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


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)


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


n = int(input())
cur = float('inf')
s = 0
for i in range(n):
	(ai, bi) = map(int, input().split())
	s += ai
	if ai > bi:
		cur = min(cur, bi)
if cur == float('inf'):
	print(0)
else:
	print(s - cur)


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)


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)


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())
S = 0
Ah = []
for _ in range(N):
	(A, B) = map(int, input().split())
	S += A
	if A > B:
		Ah.append(B)
if Ah:
	print(S - min(Ah))
else:
	print(0)


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


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


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


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)


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)


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


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)


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)


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)


import sys
input = sys.stdin.readline
N = int(input())
A = []
B = []
for i in range(N):
	(a, b) = map(int, input().split())
	A.append(a)
	B.append(b)
m = 10 ** 10
for i in range(N):
	if A[i] > B[i]:
		if m > B[i]:
			m = B[i]
if m == 10 ** 10:
	print(0)
else:
	print(sum(A) - m)


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)


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


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)


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)


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


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)


INF = float('inf')
(N, *AB) = map(int, open(0).read().split())
(A, B) = (AB[::2], AB[1::2])
sum_a = sum(A)
mi = min((b for (a, b) in zip(A, B) if a > b), default=sum_a)
print(sum_a - mi)


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)


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


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)


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


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


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)


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)


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


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)


def examC():
	ABC = LI()
	ABC.sort()
	judge = ABC[2] * 2 - ABC[1] - ABC[0]
	ans = judge // 2 + judge % 2 * 2
	print(ans)
	return

def examD():

	def judge(A, B):
		if A == B:
			return (A - 1) * 2
		cur = int((A * B) ** 0.5)
		if cur ** 2 == A * B:
			return (cur - 1) * 2 - 1
		if cur * (cur + 1) >= A * B:
			return cur * 2 - 2
		else:
			return cur * 2 - 1
	Q = I()
	ans = []
	for _ in range(Q):
		(a, b) = LI()
		ans.append(judge(a, b))
	for v in ans:
		print(v)
	return

def examE():
	N = I()
	ans = 0
	minc = inf
	for i in range(N):
		(a, b) = LI()
		ans += a
		if a > b:
			minc = min(minc, b)
	ans = max(ans - minc, 0)
	print(ans)
	return
import sys, copy, bisect, itertools, heapq, math
from heapq import heappop, heappush, heapify
from collections import Counter, defaultdict, deque

def I():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LSI():
	return list(map(str, sys.stdin.readline().split()))

def LS():
	return sys.stdin.readline().split()

def SI():
	return sys.stdin.readline().strip()
global mod, inf
mod = 10 ** 9 + 7
inf = 10 ** 18
examE()


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)


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


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)


n = int(input())
t = 0
m = 1e+18
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())
A = [0] * N
B = [0] * N
hoge = int(1e+18)
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(1e+18):
	print(0)
else:
	print(sum(B) - hoge)


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)


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)


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


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)


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


import sys
input = sys.stdin.buffer.readline
N = int(input())
issame = True
s = 0
B_min = 10 ** 9 + 7
for _ in range(N):
	(A, B) = map(int, input().split())
	s += B
	if issame:
		issame = A == B
	if A > B and B_min > B:
		B_min = B
print(0 if issame else s - B_min)


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


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


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


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