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

Submission status: Wrong Answer

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])
print(sum(a) - max(c) if len(c) else 0)

Submission status: Wrong Answer

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 b != 0 and (min_b == -1 or min_b > b):
      min_b = b
if d == 0:
  print 0 
else:
  print t - min_b

Submission status: Wrong Answer

Solution code:
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]
AB = sorted(AB, key=lambda x:x[1],reverse=True)
AB = sorted(AB, key=lambda x:x[0]-x[1])

dif = 0
ans = 0
for i in range(N):
    if AB[i][0] < AB[i][1]:
        ans += AB[i][1]
        dif += AB[i][1] - AB[i][0]
    else:
        if AB[i][0] - dif < AB[i][1]:
            ans += AB[i][1]
            dif = AB[i][1] -AB[i][0] + dif
        else:
            print(ans)
            exit()

Submission status: Wrong Answer

Solution code:
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]
AB = sorted(AB, key=lambda x:x[0]-x[1])

dif = 0
ans = 0
for i in range(N):
    if AB[i][0] < AB[i][1]:
        ans += AB[i][1]
        dif += AB[i][1] - AB[i][0]
    else:
        if AB[i][0] - dif < AB[i][1]:
            ans += AB[i][1]
            dif = AB[i][1] -AB[i][0] + dif
        else:
            print(ans)
            exit()

Submission status: Wrong Answer

Solution code:
N = int(input())
AB = [list(map(int, input().split())) for _ in range(N)]
AB = sorted(AB, key=lambda x:x[0]-x[1])

dif = 0
ans = 0
for i in range(N):
    if AB[i][0] < AB[i][1]:
        ans += AB[i][1]
        dif += AB[i][1] - AB[i][0]
    else:
        if AB[i][0] - dif < AB[i][1]:
            ans += AB[i][1]
            dif = AB[i][1] -AB[i][0] - dif
        else:
            print(ans)
            exit()

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
flag = True
a = []
b = []
for _ in range(n):
    ai, bi = map(int, input().split())
    if ai != bi:
        flag = False
    if ai <= bi:
        ans += bi
    else:
        a.append(ai)
        b.append(bi)

import numpy as np
a = np.array(a, dtype=int)
b = np.array(b, dtype=int)
rank = np.argsort(a)[::-1]
loss = (b - a)[rank]
gain = a[rank]
room = ans
for i in range(len(a)-1):
    if loss[i] <= room:        
        ans += gain[i]
        room -= loss[i]

print(0) if flag else print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
flag = True
for _ in range(n):
    a, b = map(int, input().split())
    if a != b:
        flag = False
    if a <= b:
        ans += b

print(0) if flag else print(ans)

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

Solution code:
# -*- coding: utf-8 -*-
"""
Contents   : AtCoder Regular Contest094 e問題 未完成
Author     : Kitaura Hiromi
LastUpdate : 20180420
Since      : 20180414
"""

# 最終的に, 1組以外はすべて0になる
import numpy as np
N = int(input())
queA = []
queB = []
for i in range(N):
    A, B = map(int, input().split(" "))
    queA.append(A)
    queB.append(B)

if np.allclose(queA, queB):
    print(0)
else:
    tmp = []
    for i, B in enumerate(queB):
        if queA[i] >= B:
            tmp.append(B)
    print(sum(queA) - min(tmp))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Contents   : AtCoder Regular Contest094 e問題 未完成
Author     : Kitaura Hiromi
LastUpdate : 20180420
Since      : 20180414
"""

# 最終的に, 1組以外はすべて0になる
import numpy as np
N = int(input())
queA = []
queB = []
for i in range(N):
    A, B = map(int, input().split(" "))
    queA.append(A)
    queB.append(B)

if np.allclose(queA, queB):
    print(0)
else:
    tmp = []
    for i, B in enumerate(queB):
        if queA[i] > B:
            tmp.append(B)
    print(sum(queA) - min(tmp))

Submission status: Wrong Answer

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

    c = 1e8
    d = 0
    A, B = [], []

    for _ in range(N):
        a, b = list(map(int, input().split(' ')))
        if a > b:
            c = min(c, b)

        A.append(a)
        B.append(b)

    if A == B:
        print(0)
    else:
        print(sum(A) - c)

if __name__ == '__main__':
    E()

Submission status: Wrong Answer

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

    c = 1e8
    d = 0
    A, B = [], []

    for _ in range(N):
        a, b = list(map(int, input().split(' ')))
        if a > b:
            if c > a - b:
                c = a - b
                d = b

        A.append(a)
        B.append(b)

    if A == B:
        print(0)
    else:
        print(sum(A) - d)

if __name__ == '__main__':
    E()

Submission status: Wrong Answer

Solution code:
def TozanGezan(a,b):
    if a==b:
        return 0
    
    AiGreaterthanBi = []
    for ai, bi in zip(a, b):
        AiGreaterthanBi.append(int(ai>=bi))
    
    filterdBi = []
    for bi, aiGTbi in zip(b, AiGreaterthanBi):
        if aiGTbi:
            filterdBi.append(bi*aiGTbi)
        
    return min(filterdBi)
        

def main():
    #1. input
    n = int(input())
    a = [0]*n
    b = [0]*n
    for i in range(n):
        a[i], b[i] = map(int, input().split())
    
    print(TozanGezan(a,b))
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = []
    B = []
    ans = 0
    for _ in range(N):
        a, b = map(int, input().split())
        if a < b:
            ans += b
        A.append(a)
        B.append(b)

    print(ans)

Submission status: Wrong Answer

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

#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]か。

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

Submission status: Wrong Answer

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 m > b:
    m = b
  if a != b:
    flag = True
if flag:
  print(s - m)
else:
  print(0)

Submission status: Wrong Answer

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

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 += a - b
    elif a < b:
        ans += a
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Contents   : AtCoder Regular Contest094 e問題
Author     : Kitaura Hiromi
LastUpdate : 20180414
Since      : 20180414
"""

# Aの行動
# Ai <= BiならAiを-1
# Ai > Biならそのまま. 他に操作するものがなければAiを-1
# Bの行動
# Ai < Bi ならAiを-1
# Ai >= Bi ならそのまま.

# 制約
# queA[i] >= 0, queB[i] >= 0
import numpy as np
def tozan_and_gezan():
    N = int(input())
    queA = []
    queB = []
    for i in range(N):
        A, B = map(int, input().split(" "))
        queA.append(A)
        queB.append(B)

    cnt = 0
    if np.allclose(queA, queB):
        print(cnt)
        return 0

    # queA[i] <= queB[i]なるqueA[i]を0にする.
    # queA[i] = 0か, queA[i] > queB[i]のみになる.
    for i in range(N):
        if queA[i] <= queB[i]:
            cnt += queA[i]
            queB[i] -= queA[i]
            queA[i] = 0

    if np.allclose(queA, queB):
        print(cnt)
        return 0

    # Aの行動は, queA[i] > queB[i]なるqueA[i]を-1するのみになる.
    # BはqueA[i] = 0なるqueB[i]を-1にして0にする行動をとる.
    # この行動がすべて終われば,終了.
    for i in range(N):
        if queA[i] == 0:
            cnt += queB[i]
            queB[i] = 0

    print(cnt)
    return 0

tozan_and_gezan()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Contents   : AtCoder Regular Contest094 e問題
Author     : Kitaura Hiromi
LastUpdate : 20180414
Since      : 20180414
"""

# Aの行動
# Ai <= BiならAiを-1
# Ai > Biならそのまま. 他に操作するものがなければAiを-1
# Bの行動
# Ai < Bi ならAiを-1
# Ai >= Bi ならそのまま.

# 制約
# queA[i] >= 0, queB[i] >= 0
import numpy as np

N = int(input())
queA = []
queB = []
for i in range(N):
    A, B = map(int, input().split(" "))
    queA.append(A)
    queB.append(B)

cnt = 0
if np.allclose(queA, queB):
    print(cnt)
else:
    # queA[i] <= queB[i]なるqueA[i]を0にする.
    # queA[i] = 0か, queA[i] > queB[i]のみになる.
    for i in range(N):
        if queA[i] <= queB[i]:
            cnt += queA[i]
            queB[i] -= queA[i]
            queA[i] = 0

    # Aの行動は, queA[i] > queB[i]なるqueA[i]を-1するのみになる.
    # BはqueA[i] = 0なるqueB[i]を-1にして0にする行動をとる.
    # この行動がすべて終われば,終了.
    for i in range(N):
        if queA[i] == 0:
            cnt += queB[i]
            queB[i] = 0

    print(cnt)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Contents   : AtCoder Regular Contest094 e問題
Author     : Kitaura Hiromi
LastUpdate : 20180414
Since      : 20180414
"""

# Aの行動
# Ai <= BiならAiを-1
# Ai > Biならそのまま. 他に操作するものがなければAiを-1
# Bの行動
# Ai < Bi ならAiを-1
# Ai >= Bi ならそのまま.

# 制約
# queA[i] >= 0, queB[i] >= 0
import numpy as np

N = int(input())
queA = []
queB = []
for i in range(N):
    A, B = map(int, input().split(" "))
    queA.append(A)
    queB.append(B)

cnt = 0
if np.allclose(A, B):
    print(cnt)
else:
    # queA[i] <= queB[i]なるqueA[i]を0にする.
    # queA[i] = 0か, queA[i] > queB[i]のみになる.
    for i in range(N):
        if queA[i] <= queB[i]:
            cnt += queA[i]
            queB[i] -= queA[i]
            queA[i] = 0

    # Aの行動は, queA[i] > queB[i]なるqueA[i]を-1するのみになる.
    # BはqueA[i] = 0なるqueB[i]を-1にして0にする行動をとる.
    # この行動がすべて終われば,終了.
    for i in range(N):
        if queA[i] == 0:
            cnt += queB[i]
            queB[i] = 0

    print(cnt)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Contents   : AtCoder Regular Contest094 e問題
Author     : Kitaura Hiromi
LastUpdate : 20180414
Since      : 20180414
"""

# Aの行動
# Ai <= BiならAiを-1
# Ai > Biならそのまま. 他に操作するものがなければAiを-1
# Bの行動
# Ai < Bi ならAiを-1
# Ai >= Bi ならそのまま.

# 制約
# queA[i] >= 0, queB[i] >= 0
import numpy as np

N = int(input())
queA = []
queB = []
for i in range(N):
    A, B = map(int, input().split(" "))
    queA.append(A)
    queB.append(B)

cnt = 0
if np.allclose(A, B):
    print(cnt)

# queA[i] <= queB[i]なるqueA[i]を0にする.
# queA[i] = 0か, queA[i] > queB[i]のみになる.
for i in range(N):
    if queA[i] <= queB[i]:
        cnt += queA[i]
        queB[i] -= queA[i]
        queA[i] = 0

# Aの行動は, queA[i] > queB[i]なるqueA[i]を-1するのみになる.
# BはqueA[i] = 0なるqueB[i]を-1にして0にする行動をとる.
# この行動がすべて終われば,終了.
for i in range(N):
    if queA[i] == 0:
        cnt += queB[i]
        queB[i] = 0

print(cnt)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
time = int(raw_input())
flag = True
count = 0
max_num = 0
max_num_distance = 0
for i in range(time):
    a,b = list(map(int,raw_input().split()))
    if a != b:
        flag = False
    if a > b:
        if a-b > max_num:
            max_num = a-b
            max_num_distance = a+b
        count += a+b
    else:
        count += a+b
if flag == True:
    print 0
else:
    print (count-max_num_distance+max_num)/2

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
time = int(raw_input())
flag = True
count = 0
for i in range(time):
    a,b = list(map(int,raw_input().split()))
    if a != b:
        flag = False
    if a > b:
        count += a-b
    else:
        count += a+b
if flag == True:
    print 0
else:
    print count/2

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

use_b = max_d = 0
for a,b in zip(A,B):
    if a-b > max_d:
        max_d = a-b
        use_b = b
print(sum(A) - use_b)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())

A = []
B = []

for i in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)

count = 0

for i in range(1, N):
    if A[i] != 0 and B[i] != 0:
        value = min(A[i], B[i])
        count += value
        A[i] -= value
        B[i] -= value

for i in range(1, N):
    if A[i] != 0 or np.argmax(A) == 0:
        for l in range(1, N):
            if B[i] != 0:
                value = min(A[i], B[l])
                count += value
                A[i] -= value
                B[l] -= value

for i in range(0, N):
    if A[i] != 0 or np.argmax(A) == 0:
        for l in range(1, N):
            if B[i] != 0:
                value = min(A[i], B[l])
                count += value
                A[i] -= value
                B[l] -= value

count += abs(A[0] - B[0])

print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())

A = []
B = []

for i in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)

count = 0

for i in range(1, N):
    if A[i] != 0 and B[i] != 0:
        value = min(A[i], B[i])
        count += value
        A[i] -= value
        B[i] -= value

for i in range(1, N):
    if A[i] != 0 or np.argmax(A) == 0:
        for l in range(1, N):
            if B[i] != 0:
                value = min(A[i], B[l])
                count += value
                A[i] -= value
                B[l] -= value

for i in range(0, N):
    if A[i] != 0 or np.argmax(A) == 0:
        for l in range(1, N):
            if B[i] != 0:
                value = min(A[i], B[l])
                count += value
                A[i] -= value
                B[l] -= value
print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())

A = []
B = []

for i in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)
    B.append(b)

count = 0

for i in range(1, N):
    if A[i] != 0 and B[i] != 0:
        value = min(A[i], B[i])
        count += value
        A[i] -= value
        B[i] -= value

for i in range(0, N):
    if A[i] != 0 or np.argmax(A) == 0:
        for l in range(1, N):
            if B[i] != 0:
                value = min(A[i], B[l])
                count += value
                A[i] -= value
                B[l] -= value
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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


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


def get_next_str():
    return input()


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


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


def solve():
    N = get_next_int()
    A, B = [], []
    diffs = []
    for i in range(N):
        a, b = get_next_ints()
        A.append(a)
        B.append(b)
        if a < b:
            diffs.append(b - a)
    if len(diffs) <= 0:
        print(0)
    else:
        max_diff = max(diffs)
        ans_diffs = []
        for i in range(N):
            a, b = A[i], B[i]
            if a < b or abs(a - b) < max_diff:
                ans_diffs.append(abs(b))
        ans = sum(ans_diffs)
        print(ans)

solve()

Submission status: Wrong Answer

Solution code:

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


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


def get_next_str():
    return input()


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


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


def solve():
    N = get_next_int()
    A, B = [], []
    diffs = []
    for i in range(N):
        a, b = get_next_ints()
        A.append(a)
        B.append(b)
        if a < b:
            diffs.append(b)
    ans = sum(diffs)
    print(ans)
solve()

Submission status: Wrong Answer

Solution code:
import numpy as np
INF = 100000000000000000
N = int(input())
A = []
B = []
C = []
ans = 0
res = 0
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
    C.append(b-a)
if A == B:
    print(ans)
else:
    A = np.array(A)
    B = np.array(B)
    C = np.array(C)
    for i in range(N):
        if C[i] >= 0:
            ans += B[i]
            res += B[i] - A[i]
            C[i] = -INF
        C[i] = -C[i]
    judge = True
    while judge:
        judge = False
        m = np.min(C)
        index = np.argmin(C)
        if m < res:
            ans += A[index]
            res -= C[index]
            C[index] = INF
            judge = True
    print(ans)

Submission status: Wrong Answer

Solution code:
INF = 100000000000000000
N = int(input())
A = []
B = []
C = []
ans = 0
res = 0
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
    C.append(b-a)

if A == B:
    print(ans)
else:
    for i in range(N):
        if C[i] >= 0:
            ans += B[i]
            res += B[i] - A[i]
            C[i] = -INF
        C[i] = -C[i]
    judge = True
    while judge:
        judge = False
        m = min(C)
        index = C.index(m)
        if m < res:
            ans += A[index]
            res -= C[index]
            C[index] = INF
            judge = True
    print(ans)

Submission status: Wrong Answer

Solution code:
INF = 100000000000000000
N = int(input())
A = []
B = []
C = []
ans = 0
res = 0
for i in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
    C.append(b-a)

if A is B:
    print(ans)
else:
    for i in range(N):
        if C[i] >= 0:
            ans += B[i]
            res += B[i] - A[i]
            C[i] = -INF
        C[i] = -C[i]
    judge = True
    while judge:
        judge = False
        m = min(C)
        index = C.index(m)
        if m < res:
            ans += A[index]
            res -= C[index]
            C[index] = INF
            judge = True
    print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
C=[0 for i in range(N)]
for i in range(N):
    A,B=map(int,input().split())
    C[i]=B-A
candy=0
while(True):
    candy+=1
    M=-10**9-1
    m=10**9+1
    argm=0
    argM=0
    for i in range(N):
        if C[i]<m:
            m=C[i]
            argm=i
        if C[i]>M:
            M=C[i]
            argM=i
    if M==0 and m==0:
        print(candy)
        break
    else:
        C[argM]-=1
        C[argm]+=1

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
diff = 0
sp = 0
d = []
e = []
for i in range(N):
    a, b = map(int, input().split())
    if a < b:
        diff += 1
    if a <= b:
        ans += b
        sp += b-a
    if a > b:
        d.append((a, a-b, b))
d.sort()
for a, f, b in d[:-1]:
    if sp >= f:
        sp -= f
        ans += a

if diff:
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
count = 0
num_list = []
b_min = 0
flag = 0
for i in range(n):
    a, b = map(int, input().split())
    if a < b:
        flag = 1
        b_min += b-a
        count += b
    elif a == b:
        count += a
    else:
        flag = 1
        num_list.append([a, b])

if flag == 0:
    count = 0
else:
    i = 0
    f = 0
    num_list = sorted(num_list, key=lambda x: x[0], reverse=True)
    while True:
        if b_min > num_list[i][0]:
            count += num_list[i][0]
            b_min -= num_list[i][0] - num_list[i][1]
            del num_list[i]
            f = 1
        else:
            f = 0
        if len(num_list) == 0 or i >= len(num_list)-1:
            break
        if f == 0:
            i += 1
        else:
            i = 0
print(count)

Submission status: Wrong Answer

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
n=i1()
c=0
d=0
x=0
for i in range(n):
 [a,b]=i2()
 if a<b:
    c+=1
 if a>b:
    d+=1
 x+=abs(a-b)
print(x+d-c)

Submission status: Wrong Answer

Solution code:
N=int(raw_input())
ans=0
AA=[]
BB=[]
for n in range(N):
    A,B=map(int, raw_input().split())
    AA.append(A)
    BB.append(B)
if AA!=BB:
    for i in range(N):
        A,B=AA[i],BB[i]
        if A<=B:
            ans+=B
print ans

Submission status: Wrong Answer

Solution code:
from copy import copy
from collections import defaultdict

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

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

ps = []
to_reduce = 0
for a,b in zip(A,B):
    if a > b:
        ps.append((a, b))
    elif a == 0:
        to_reduce += b

#print(ps)

dp = defaultdict(lambda: 0)
for a,b in ps:
    dp2 = copy(dp)
    for k,v in dp.items():
        if k + a > to_reduce: continue
        dp2[k+a] = v + b
    dp = dp2

gain = max(dp.values()) if dp else 0

print(ans + to_reduce + gain)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 0
same = 0
same_cnt = 0
max_delta = 0
max_delta_B = 0

s = 0
for i in range(N):
	A,B = map(int,input().split())
	s += A
	if A < B:		
		ans += B
	elif A == B:
		same += 1
		same_cnt += B
	else:
		max_delta_B = B if max_delta < A - B else max_delta_B 
		max_delta = max(max_delta,A - B)
if ans == 0 and same == N:
	print(0)
else:
	print(max(s - max_delta_B, (ans + same_cnt)))

Submission status: Wrong Answer

Solution code:
N=int(raw_input())
ans=0
for n in range(N):
    A,B=map(int, raw_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 = map(int, input().split())
    if a < b:
        ans += b
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(raw_input())
ans=0
for n in range(N):
    A,B=map(int, raw_input().split())
    if A<B:
        ans+=B
print ans

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 0
flag = True
abigger = 0
asum = 0
amin = float('inf')
for i in range(N):
  a, b = map(int, input().split())
  asum += a
  amin = min(amin, a)
  flag &= a == b
  if a <= b:
    ans += a
  elif a > b:
    abigger += 1
    ans += a - b

if flag:
  print(0)
elif abigger <= 1:
  print(ans)
else:
  print(asum - amin)

Submission status: Wrong Answer

Solution code:
n = int(input())
sol = 0
prvi, drugi = 0, 0
d1, d2 = 0, 0
for i in range(n):
    a, b = map(int, input().split())
    if a == b:
        continue
    elif a < b:
        sol += a
        d1 += b - a
    else:
        d2 += a - b
if d1 + d2 == 0:
    print(0)
else:
    assert d1 == d2
    print(sol + d1)

Submission status: Wrong Answer

Solution code:
from sys import exit, setrecursionlimit
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

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

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

N = read()

ans = 0

A = [0] * N; B = [0] * N
for i in range(N):
  (a, b) = reads()
  if a <= b:
    ans += a
  elif a > b:
    ans += a - b
  (A[i], B[i]) = (a, b)

if A == B:
  print(0)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit, setrecursionlimit
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

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

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

N = read()

ans = 0

for i in range(N):
  (a, b) = reads()
  if a < b:
    ans += a
  elif a > b:
    ans += a - b

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
sol = 0
prvi, drugi = 0, 0
bio = False
d1, d2 = 0, 0
for i in range(n):
    a, b = map(int, input().split())
    if a == b:
        sol += a
    elif a < b:
        bio = True
        sol += a
        d1 += b - a
    else:
        d2 += a - b
if d1 + d2 == 0:
    print(0)
else:
    assert d1 == d2
    print(sol + d1)

Submission status: Wrong Answer

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

def main():
    N = int(input())

    A = []
    B = []
    count = 0
    for i in range(N):
        a, b = list(map(int, input().split()))
        A.append(a)
        B.append(b)
 
    while True:
        if A == B:
            break
        a_max = 0
        a_index = 0
        b_min = 0
        b_index = 0

        for i in range(N):
            if A[i] is not 0:
                a_index = i
                if a_max < abs(A[i] - B[i]):
                    a_max = abs(A[i] - B[i])

            if B[i] is not 0:
                b_index = i
                if b_min > abs(A[i] - B[i]):
                    b_min =  abs(A[i] - B[i])

        A[a_index] -= 1
        B[b_index] -= 1
        count += 1
    
    print(count)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
count = 0
num_list = []
b_min = 0
flag = 0
for i in range(n):
    a, b = map(int, input().split())
    if a < b:
        flag = 1
        num_list.append([0, b-a])
        b_min += b-a
        count += b
    elif a == b:
        num_list.append([0, 0])
        count += a
    else:
        flag = 1
        num_list.append([a, b])

num_list = sorted(num_list, key=lambda x:x[0], reverse=True)
if flag == 0:
    count = 0
else:
    for i in range(n):
        if b_min > num_list[i][0]:
            count += num_list[i][0]
            b_min -= num_list[i][0]-num_list[i][1]
        if num_list[i][0] == 0:
            break
print(count)

Submission status: Wrong Answer

Solution code:
c = 0
for _ in range(int(input())):
    a, b = map(int, input().split())
    if a == b: continue
    if a > b: c += a - b - 1
    else: c += a
print(c if c == 0 else c + 1)

Submission status: Wrong Answer

Solution code:
n = int(input())
sol = 0
prvi, drugi = 0, 0
niz = []
bio = False
d = 0
for i in range(n):
    a, b = map(int, input().split())
    if a == b:
        sol += a
    elif a < b:
        bio = True
        sol += b
        prvi += b - a
    else:
        niz.append((a, b))
        d += a - b
if not bio and len(niz) == 0:
    print(0)
else:
    print(sol)

Submission status: Wrong Answer

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

#print solve([1,3], [2,2]) # (2, [1, 2])
#print solve([8,0,4], [3,1,8]) # (9, [3,0,0])
#print solve([3,3,1,1], [1,1,3,3]) # (7, [1, 0, 0, 0])
#print solve([4,3,1,1], [2,1,3,3]) # (8, [1, 0, 0, 0])
#print solve([2,2,3,1], [2,2,1,3]) # (7, [1, 0, 0, 0])
#print solve([2,2,2,0], [1,1,1,3]) # (5, [1, 0, 0, 0])
#print solve([1,1,1,0], [0,0,0,3]) # (3, [0, 0, 0, 0])
#print solve([1,2,1,0], [0,1,0,3]) # (4, [0, 0, 0, 0])
#print solve([1,2,2,0], [0,1,1,3]) # (5, [0, 0, 0, 0])

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

Submission status: Wrong Answer

Solution code:
import sys
N = int(input())
ans = 0
dif = 0
ar = []

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

    if a < b:
        ans += b
        dif += b - a
        ar.append([b-a, a])

if dif != 0:
    print(ans)
    sys.exit(0)

ars = sorted(ar, key=lambda x: (x[0], x[1]))
print(ans)
for i in range(len(ars)):
    if dif > ars[i][0]:
        ans += ars[i][1]
        dif -= ars[i][0]
    else:
        break

print(ans)