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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
ans=0
for i in range(N):
  a,b=map(int, input().split())
  if a<b and min(a,b)!=0:
    d=b-a
    ans+=d
    a-=d
    b-=d
  if a==0:
    ans+=b
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
for i in range(N):
  a,b = list(map(int,input().split()))
  if a<b:
    ans+=b
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
for i in range(N):
  a,b = list(map(int,input().split()))
  ans += abs(a-b)
  
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
for i in range(N):
  a,b = list(map(int,input().split()))
  if a<b:
    ans+=b
print(b)

Submission status: Wrong Answer

Solution code:
N = int(input())
flag = 1
ans = 0
for _ in range(N):
    a, b = map(int,input().split())
    if a <= b:
        ans += a
    else:
        ans += a - b
    if a != b:
        flag = 0
if flag:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
flag = 1
ans = 0
for _ in range(N):
    a, b = map(int,input().split())
    if a <= b:
        ans += b
    if a != b:
        flag = 0
if flag:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline

N = int(input())
delta_values = []
for _ in range(N):
    A, B = map(int, input().split())
    delta_values.append((B - A, B))

delta_values.sort(reverse=True)
d_sum = 0
v_sum = 0
for d, v in delta_values:
    d_sum += d
    if d_sum <= 0:
        print(v_sum)
        exit()
    v_sum += v

Submission status: Wrong Answer

Solution code:
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
max_b = 0
it = iter(data)
for a,b in zip(it,it):
    tot += a
    if(a==b):
        same += 1
    elif(a > b):
        max_b = max(max_b,b)

if(same==n):
    print(0)
else:
    print(tot-max_b)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
ans=0
min_b=200_000
for i in range(n):
	a,b=map(int,input().split())
	ans+=a
	if a>b:
		min_b=min(min_b,b)
print(max(ans-min_b,0))

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
ans=0
min_b=200_000
for i in range(n):
	a,b=map(int,input().split())
	ans+=a
	if a>=b:
		min_b=min(min_b,b)
print(ans-min_b)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
ans=0
min_b=200_000
for i in range(n):
	a,b=map(int,input().split())
	ans+=a
	if a>b:
		min_b=min(min_b,b)
print(ans-min_b)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
ans=0
for i in range(n):
	a,b=map(int,input().split())
	if a<b:
		ans+=b
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
ans=0
for i in range(n):
	a,b=map(int,input().split())
	ans+=abs(a-b)
print(ans)

Submission status: Wrong Answer

Solution code:
def slv(n,a,b):
    if a==b:return 0
    ret=0
    for ai,bi in zip(a,b):
        if ai<=bi:ret+=bi
    return ret

n=int(input())
a=[]
b=[]
for _ in range(n):
    i,j=map(int,input().split()) 
    a.append(i)
    b.append(j)
print(slv(n,a,b))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [list(map(int,input().split())) for _ in range(N)]
cnt = 0
flag = 0
for i in range(N):
    if A[i][0]!=A[i][1]:
        flag = 1
        break
if flag==0:
    print(cnt)
else:
    B = sorted([A[i][1] for i in range(N)])
    A = sorted([A[i][0] for i in range(N)])
    for i in range(N):
        if A[i]<=B[i]:
            cnt += B[i]
    print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab = [tuple(map(int, input().split())) for _ in range(n)]

mx = 0
sm = 0
for i in range(n):
    a, b = ab[i]
    sm += a
    if a >= b:
        mx = max(mx, b)
print(sm - mx)

Submission status: Wrong Answer

Solution code:
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()
    if a < b:
        ans += a
        A.append(0)
        B.append(b-a)
    else:
        A.append(a)
        B.append(b)

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

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

ans += sum(A)-A[i]+m
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
csum = 0
min_b = 10**5+1
for _ in range(n):
    a,b = map(int, input().split())
    csum += a
    if a >= b:
        min_b = min(min_b,b)

ans = csum-min_b
print(ans)


# a_down_sum = 0
# top_diff = 0
# for _ in range(n):
#     a,b = map(int, input().split())
#     if a < b:
#         a_down_sum += a
#     else:
#         top_diff = max(top_diff,a-b)

Submission status: Wrong Answer

Solution code:
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=0
        for i in range(N):
            if B[i]>=A[i]:
                ans+=B[i]
                
        print(ans)
            
        
    

main()

Submission status: Wrong Answer

Solution code:
N, *AB = [int(_) for _ in open(0).read().split()]
A, B = AB[::2], AB[1::2]
if all(a == b for a, b in zip(A, B)):
    print(0)
    exit()
ans = 0
for a, b in zip(A, B):
    if a <= b:
        ans += b
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a,b=[],[]
for i in range(n):x,y=map(int,input().split());a.append(x);b.append(y)
c=10**10
for i in range(n):
 if a[i]>b[i]:c=min(c,b[i])
print(sum(a)-c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def solve():
    ans = 0
    N = int(input())
    miny = float('inf')
    ans = 0
    for i in range(N):
        x,y = map(int, input().split())
        ans += x
        if x>=y:
            miny = min(miny,y)
    ans -= miny
    return ans
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
    ans = 0
    N = int(input())
    for i in range(N):
        x,y = map(int, input().split())
        if x<y:
            ans += y
    return ans
print(solve())

Submission status: Wrong Answer

Solution code:
ans = 0

for _ in range(int(input())):
    a,b = map(int,input().split())
    ans += abs(a-b)

print(ans)

Submission status: Wrong Answer

Solution code:
ans = 0
N = int(input())
for _ in range(N):
    a, b = map(int, input().split())
    if a < b:
        ans += b
print(ans)

Submission status: Wrong Answer

Solution code:
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)
if A==B:
  print(0)
  exit()
P=0
for i in range(N):
  if A[i]<=B[i]:
    P+=A[i]
    B[i]-=A[i]
    A[i]=0
for i in range(N):
  P+=max(0,B[i]-A[i])
print(P)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab=[list(map(int,input().split())) for i in range(n)]

ans=0
flg=False
for itm in ab:
    a,b=itm
    if a<=b:
        ans+=b
    if a!=b:
        flg=True
if flg:
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
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==0:
    print(0)
else:
    print(sum-m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
for i in range(n):
    a,b=map(int,input().split())
    ans+=abs(a-b)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
m = 10**6
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)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
t = 10000000000000
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))

Submission status: Wrong Answer

Solution code:
# ARC094E

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((a[i]-b[i], b[i]))
        m += b[i]
diff.sort()
c = 0
plus = 0
if ans==0:
    print(0)
else:
    for i,(d,val) in enumerate(diff):
        if c+d<ans:
            c += d
            plus += val
        else:
            break
    print(sum(a) - (m - plus))

Submission status: Wrong Answer

Solution code:
# ARC094E

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
for i in range(n):
    a[i],b[i] = map(int, input().split())
    if a[i]<b[i]:
        ans += a[i]
    elif a[i]==b[i]:
        same += a[i]
    else:
        m += b[i]
if ans==0:
    print(0)
else:
    print(sum(a) - m)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]

ab.sort(key=lambda x: x[0] - x[1])
sm = 0
sm_all = 0
for i, (a, b) in enumerate(ab):
    if a - b > 0:
        if sm + a - b < sm_all:
            sm += a - b
        else:
            break
    else:
        sm_all += b - a

ans = 0
for ea, _ in ab[:i]:
    ans += ea

ans += ab[i][0] - ab[i][1]

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]

ab.sort(key=lambda x: x[0] - x[1])
sm = 0
sm_all = 0
for i, (a, b) in enumerate(ab):
    if a - b > 0:
        if 2 * (sm + a - b) < sm_all:
            sm += a - b
        else:
            break
    else:
        sm_all += b - a

ans = 0
for ea, _ in ab[:i]:
    ans += ea

ans += ab[i][0] - ab[i][1]

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n)]

a, b = zip(*ab)

if a == b:
    print(0)
    exit()

ans = 0
for ae, be in zip(a, b):
    if ae <= be:
        ans += ae
    else:
        ans += ae - be

print(ans)

Submission status: Wrong Answer

Solution code:
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 = -1
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 = max(k, data[i][1])
    print(ans-k)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
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])
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]:
            ans -= data[i][1]
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
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])
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]:
            ans -= data[i][1]
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
data = []
for i in range(n):
    data.append(list(map(int, input().split())))
data.sort(key=lambda x: x[0] - x[1])
ans = 0
for i in range(n):
    if data[i][0] < data[i][1]:
        ans += data[i][0]
    else:
        break
ans += abs(data[-1][0] - data[-1][1])
print(ans)

Submission status: Wrong Answer

Solution code:
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
for diff, a, b in diff_a_b:
  if diff <= 0: # a <= b
    ans += b
    d -= diff
  else:
    if d > diff:
      d -= diff
      ans += b
    else: # d <= diff
      break

print(ans)

Submission status: Wrong Answer

Solution code:
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
for diff, a, b in diff_a_b:
  if diff <= 0: # a <= b
    ans += b
    d -= diff
  else:
    if d > diff:
      d -= diff
      ans += b
    else: # d <= diff
      ans += d
      break

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
num_eq = 0
for i in range(N):
  a, b = map(int, input().split())
  if b >= a:
    ans += b
  num_eq += a == b

if num_eq == N:
  print(0)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
for i in range(N):
  a, b = map(int, input().split())
  if b >= a:
    ans += b

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
AB=sorted([list(map(int,input().split())) for i in range(N)],key=lambda x:x[0]-x[1])
A,B=0,0
for a,b in AB:
    if a<b or a==b and A<B or a>b and a-b<B-A:
        A+=a;B+=b
print(B)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
rem=0
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)

Submission status: Wrong Answer

Solution code:
N,*L = map(int, open(0).read().split())
A = [0]*N
B = [0]*N
n = 10**10
for i,(a,b) in enumerate(zip(*[iter(L)]*2)):
  A[i] = a
  B[i] = b
  if a>b:
    n = min(n,b)
print(sum(A)-n)

Submission status: Wrong Answer

Solution code:
#!python3

import numpy as np


# input
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
    
    w = B[A >= B]
    ans = B[A < B].sum() + (w.sum() - w.min())
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a,b=[],[]
for i in range(n):
    aa,bb=map(int,input().split())
    a.append(aa)
    b.append(bb)
ans=0

for i in range(n):
    if a[i]<b[i]:
        ans+=a[i]
        b[i]-=a[i]
        a[i]=0

# print(a)
# print(b)

for i in range(n):
    if b[i]>a[i]:
        ans+=b[i]

print(ans)

Submission status: Wrong Answer

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, total - b[i])

if minans == total:
    print(0)
else:
    print(minans)

Submission status: Wrong Answer

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 = 10 ** 9 + 1
for i in range(N):
    if a[i] > b[i]:
        minans = min(minans, total - b[i])

if minans == 10 ** 9 + 1:
    print(0)
else:
    print(minans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = []
res = 0
c = 0
f = 1
for _ in range(N):
  x, y = map(int, input().split())
  if x != y: f = 0
  if x <= y:
    res += y
    c += y - x
    a.append([0, 0])
  else: a.append([x, y])
if f:
  print(0)
  exit(0)

print(res)

Submission status: Wrong Answer

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

ans = 0
for a, b in AB:
    if a < b:
        ans += b
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = []
res = 0
c = 0
f = 1
for _ in range(N):
  x, y = map(int, input().split())
  if x != y: f = 0
  if x <= y:
    res += y
    c += y - x
    a.append([0, 0])
  else: a.append([x, y])
if f:
  print(0)
  exit(0)
a.sort(key = lambda x: (x[1] - x[0]))
#print(a, c, res)
for i in range(N):
  if c == 0: break
  x, y = a[i]
  if x > 0:
    if y <= x:
      if x < c:
        c -= x
        a[i][0] = 0
      else:
        a[i][0] -= c
        c = 0
#print(a, c)
for i in range(N):
  x, y = a[i]
  if x < y: res += y - x
print(res)

Submission status: Wrong Answer

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 = 0
for a, b in AB:
    if a <= b:
        ans += b
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = []
res = 0
c = 0
f = 1
for _ in range(N):
  x, y = map(int, input().split())
  if x != y: f = 0
  if x <= y:
    res += y
    c += y - x
    a.append([0, 0])
  else: a.append([x, y])
if f:
  print(0)
  exit(0)
#print(a, c, res)
for i in range(N):
  if c == 0: break
  x, y = a[i]
  if x > 0:
    if y >= x:
      if x < c:
        c -= x
        a[i][0] = 0
      else:
        c = 0
        a[i][0] -= c
for i in range(N):
  x, y = a[i]
  if x < y: res += y - x
print(res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
A = [0] * N
B = [0] * N
for i in range(N):
    A[i], B[i] = map(int, input().split())
if A == B:
    print(0)
else:
    ans = 0
    for i in range(N):
        if A[i] <= B[i]:
            ans += B[i]
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
aa = 0
mi = 10 ** 9
for _ in range(n):
    a, b = [int(i) for i in input().split()]
    aa += a
    if a > b:
        mi = min(mi, b)
print(aa-mi)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
q=1
for _ in range(n):
    a,b=map(int,input().split())
    if a!=b: q=0
    if a<=b: ans+=a
    else: ans+=a-b
print(ans if q==0 else 0)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
a = []
b = []


res = 0
judge = True
for i in range(n):
    aa,bb = map(int,input().split())
    a.append(aa)
    b.append(bb)
    if aa != bb:
        judge = False

if judge:
    print(0)
else:
    for i in range(n):
        if a[i] <= b[i]:
            res += b[i]
    print(res)

Submission status: Wrong Answer

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())
    if aa < bb:
        res += bb

print(res)

Submission status: Wrong Answer

Solution code:
n=int(input())
rem=0
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)

Submission status: Wrong Answer

Solution code:
n=int(input())
rem=0
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:
    rem+=b
    allissame|=1
  else:rem+=b
if not allissame:
  print(0)
else:
  print(S-rem)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
t=0
for i in range(n):
    a,b=map(int,input().split())
    if a<=b:
        if a==b:
            t+=1
        ans+=a
    else:
        ans+=a-b
if t==n:
    ans=0
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
l=[]
x=0
for i in range(n):
  a,b=map(int,input().split())
  if a<b:
    ans+=b
    x+=b-a
  elif a==b:
    l.append(b)
l.sort(reverse=True)
print(ans+sum(l[:min(x,len(l))]))

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
for i in range(n):
  a,b=map(int,input().split())
  if a<b:
    ans+=b
print(ans)

Submission status: Wrong Answer

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: Wrong Answer

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

ans = 0
isSame = True
for A, B in ABs:
    if A <= B:
        ans += B
    if A != B:
        isSame = False

if isSame:
    ans = 0

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=0
for i in range(n):
  a,b=list(map(int,input().split()))
  s+=a
print(s-n)

Submission status: Wrong Answer

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(1)
    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: Wrong Answer

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

ans = 0
num = 10**9
if A != B:
    cnt = 0
    for i in range(N):
        if A[i] <= B[i]:
            ans += B[i]
        else:
            cnt += 1
            num = min(num, B[i])
        if cnt >= 2:
            ans = sum(A)-num
            break

print(ans)

Submission status: Wrong Answer

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

ans = 0
if A != B:
    cnt = 0
    for i in range(N):
        if A[i] <= B[i]:
            ans += B[i]
        else:
            cnt += 1
        if cnt >= 2:
            ans = sum(A)
            break

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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] and a[i]>0:
    ans = min(ans,b[i])
print(sum(b)-ans)

Submission status: Wrong Answer

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] and a[i]+b[i]>0:
    ans = min(ans,b[i])
print(sum(b)-ans)

Submission status: Wrong Answer

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: Wrong Answer

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: Wrong Answer

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 = 0
  d = -1
  for a, b in zip(A, B):
    if a - b > c:
      c = a - b
      d = b
  print(M - d)

Submission status: Wrong Answer

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 = 0
  d = -1
  for a, b in zip(A, B):
    if c < a - b:
      c = a - b
      d = b
  print(M - b)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
same=True
for i in range(n):
  a,b=map(int,input().split())
  if a<=b:
    ans+=b
  same&=a==b
print(0 if same else ans)

Submission status: Wrong Answer

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


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

    ma = INF
    mb = 0
    for a, b in zip(A, B):
        if a > b:
            if ma > a:
                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: Wrong Answer

Solution code:
n = int(input())

ans = 0
tmp = 0

for _ in range(n):
    a, b = map(int, input().split())
    if a < b:
        ans += b
    if a == b:
        tmp += a

if ans == 0:
    print(ans)
else:
    print(ans+tmp)

Submission status: Wrong Answer

Solution code:
n = int(input())

ans = 0

for _ in range(n):
    a, b = map(int, input().split())
    if a < b:
        ans += b

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

ans = 0
cnt = n

for _ in range(n):
    a, b = map(int, input().split())
    if a == b:
        cnt -= 1
    
    if a <= b:
        ans += a
    else:
        ans += (a-b)

print(ans if cnt != 0 else 0)

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
n=int(input())
t=0
m=1e18
for i in range(n):
  a,b=map(int,input().split())
  t+=a
  if a>=b:m=min(m,b)
print(t-m)

Submission status: Wrong Answer

Solution code:
n=int(input())
t=0
m=1e18
for i in range(n):
  a,b=map(int,input().split())
  t+=a
  if a>b:m=min(m,b)
print(t-m)

Submission status: Wrong Answer

Solution code:
N=int(input())
aBsum=[0,0]
ABsum=[0,0]
Absum=[0,0]
ans=0
for i in range(N):
  a,b=map(int,input().split())
  if a<b:
    aBsum[0]+=a
    aBsum[1]+=b
  elif a==b:
    ABsum[0]+=a
    ABsum[1]+=b
  else:
    Absum[0]+=a
    Absum[1]+=b
ans+=aBsum[0]
aBsum[1]-=aBsum[0]
aBsum[0]=0
if ans>0:
  ans+=ABsum[0]
  ABsum[1]=aBsum[1]
  aBsum[0]=0
  ans+=ABsum[1]
print(ans)

Submission status: Wrong Answer

Solution code:

import sys

N = int(input())

cab = []

AB = []

flag = True
for i in range(N):
    A,B = map(int,input().split())
    if A!=B:
        flag = False
    AB.append([A,B])

if flag:
    print (0)
    sys.exit()

cab = []
amari = 0
ans = 0

for i in range(N):

    if AB[i][0] <= AB[i][1]:
        ans += AB[i][0]
        amari += AB[i][1] - AB[i][0]

    else:
        cab.append([AB[i][0] - AB[i][1],-1 * AB[i][0],AB[i][1]])

cab.sort()

for i in range(len(cab)):

    if amari == cab[i][0]:
        ans += amari
        break
    else:
        amari -= cab[i][0]
        ans -= cab[i][1]

print (ans)