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())
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)
  exit()
print(d-s)

Solution (Python):
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)
    sys.exit()
ans = A.sum() - B[A > B].min()
print(ans)

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

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

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

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

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

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

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return 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)

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

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return 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)

Solution (Python):
n = int(input())
INF = 10**9
csum = 0
min_b = INF
for _ in range(n):
    a,b = 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)

Solution (Python):
n = int(input())
INF = 10**9
csum = 0
min_b = INF
for _ in range(n):
    a,b = 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)

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

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

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return 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()

Solution (Python):
'''
研究室PCでの解答
'''
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 = 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

if __name__ == '__main__':
    main()

Solution (Python):

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

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

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

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

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

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

Solution (Python):
n = int(input())
ans = []
sum_a = 0
for i in range(n):
    a, b = 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)

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

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

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

Solution (Python):
import os

import sys
import numpy as np

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

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


N = int(sys.stdin.buffer.readline())
A, B = list(zip(*[map(int, sys.stdin.buffer.readline().split()) for _ in range(N)]))
if A == B:
    print(0)
    exit()

A = np.array(A, dtype=int)
B = np.array(B, dtype=int)
ans = B.sum() - B[A > B].min()
print(ans)

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

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

Solution (Python):
N=int(input())
num=0
result=0
min=10000000000
for i in range(N):
  a,b=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)

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

Solution (Python):


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

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

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

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

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

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)

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

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

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

Solution (Python):


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)


if __name__ == "__main__":
    main()

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





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

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

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

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

A, B = zip(*AB)

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

ans = sum(A)

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

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

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

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

Solution (Python):


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 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)
    exit()
ans = sm - mn
print(ans)

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

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

Solution (Python):
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); exit()

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

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

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

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

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

Solution (Python):
N = int(input())
A = [0]*N
B = [0]*N
for i in range(N):
    a,b = 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)

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

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

Solution (Python):
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:
  M = sum(A)
  c = 10**10
  for a, b in zip(A, B):
    if a > b:
      c = min(c, b)
  print(M - c)

Solution (Python):

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)


if __name__ == '__main__':
    main()

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

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

Solution (Python):

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

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

Solution (Python):
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)
  exit()
ans = 0
diff = 10 ** 9
for a,b in ab:
  ans += b
  if a > b:
    diff = min(diff, b)
print(ans-diff)

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

if __name__ == '__main__':
    examE()

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

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

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

Solution (Python):


N = int(input())
AB = (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)

Solution (Python):
_,*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))

Solution (Python):
_,*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))

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

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

Solution (Python):
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 = 10**9+10
for i in range(N):
    if B[i] < A[i]:
        ans = min(ans,B[i])
print(sum(B) - ans)

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

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

Solution (Python):
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(*[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)

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

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))

def main():
    n = int(input())
    a, b = [], []
    minb = 10**10
    for _ in range(n):
        _a, _b = map(int, input().split())
        a.append(_a)
        b.append(_b)
        if _a != 0 and _b < _a:
            minb = min(minb, _b)
    if a == b:
        print(0)
    else:
        print(sum(a) - minb)


if __name__ == '__main__':
    main()

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

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

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

Solution (Python):
N = int(input())
ans = 0
A = []
B = []
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
if A == B:
    print(0)
    exit()
b_min = 10**10
for a, b in zip(A, B):
    if a>b:
        b_min = min(b_min, b)
print((sum(A)+sum(B))//2-b_min)

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

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
import numpy as np

N = int(input())
AB = np.array([input().split() for _ in range(N)], dtype=np.int64)

A,B = AB.T
select = np.where(A > B)[0]
if len(select) == 0:
    answer = 0
else:
    answer = A.sum() - B[select].min()
print(answer)

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

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