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



Sample input:
2
1 2
3 2


Sample output:
2

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

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

mod = 10**9 + 7

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

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

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

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

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return 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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

# 12

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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


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

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

    return


def main():

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:

"""
すべて等しかったら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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3


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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)
# Correct

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Contents   : AtCoder Regular Contest094 e問題 未完成
Author     : Kitaura Hiromi
LastUpdate : 20180420
Since      : 20180414
"""
inf = 10**20
# 最終的に, 1組以外はすべて0になる
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

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

#先手は同数以下の要素をひたすらデクリメントすることでアメと余裕分のターン数を稼げる。
#ABには先手が余裕ターン数を消費してアメを稼ぐ必要のある要素を入れる（＝後手の方が少ない要素を入れる）
for i in range(N):
  a,b=map(int,input().split())
  if a<=b:
    ans+=a
    turn+=(b-a)
  else:
    #AB.append([a,a-b]) <-間違い
    AB.append([a,b])
    

#turn=0で終了。AB[i][1]を選ぶ場合turn-=AB[i][1],その時turn>0ならばans+=AB[i][0],そうでなければans+=turnで終了。
#定義よりAB[i][1]の総和=turn,つまり、最後に選ぶべきもっとも小さなAB[i][0]を探せば良いか？
#つまり、ABの第１数を昇順、第２数を降順にして、AB[0]のみans+=AB[0][1],そこからans+=AB[i][0]か。

#いや、違うか（WAだった）。上の行コメントを噛み砕くと、「１つだけ-bしたa,残りは全てaを足す」ってこと。
#つまり、aの総和から１度だけbを引くってこと。１回だけなら最小の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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)