Problem:


問題 1 

　n 分間にわたり，
トンネルの入口と出口で，
1分間に通過する車の数を数えたデータがある．
そのデータは，
全部で n+2 行からなり，
各行には次の内容が書かれている．

 
 第1行目には，正整数 n が書かれており，
      調査時間が n 分間であったことを表している．

 
 第2行目には，正整数 m が書かれており，
      調査開始時におけるトンネル内の車の台数が m であったことを表している．

 
 第(2+i)行目（ i = 1, 2, ... , n ) には，
      調査開始後 (i-1) 分経過した時点から i 分経過するまでの1分間に，
      入口を通過した車の台数と出口を通過した車の台数が
      １つの空白で区切られて書かれている．

調査開始後 j 分経過した時点 ( j=0, 1, 2, ... , n ) 
におけるトンネル内の車の台数を Sj とする．
Sj の最大値を出力しなさい．
また，
トンネル内の車の台数が負になることは考えられないので，
Sj が一度でも負になった場合は，
「エラー」の意味で 0 を出力しなさい．
ただし，
n は 10000 以下で，
トンネルの入口および出口を1分間に通過する車の台数は 100 以下である．

　出力ファイルにおいては，
出力の最後の行にも改行コードを入れること．

入出力例

入力例１

3
2
2 3
2 3
4 1

出力例１

3

入力例２

3
2
2 3
2 4
4 1

出力例２

0

入力例３

3
2
2 3
2 3
1 0

出力例３

2

問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
3
2
2 3
2 3
4 1


Sample output:
3

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=c=m=int(input())
for i in[0]*n:
    a,b=map(int,input().split())
    if c<0:ans=0;break
    c+=a-b
    ans=max(ans,c)

Submission status: Wrong Answer

Solution code:
n = int(input())
m = int(input())
for i in range(n):
    inn, out = map(int, input().split())
    m += inn - out 
    if m < 0:
        m = 0
        break
print(m)

Submission status: Wrong Answer

Solution code:
n = int(input())
m = int(input())
maxd = m
for i in range(n):
    inn, out = map(int, input().split())
    m += inn - out 
    if m > maxd:
        maxd = m
    elif m < 0:
        m = 0
        break
print(maxd)

Submission status: Wrong Answer

Solution code:
n = input()
m = input()
for j in range(n):
  (i, o) = map(int, raw_input().split())
  m += i - o
  if m < 0:
    print 0
    quit()
print m

Submission status: Accepted

Solution code:
n = int(input())
m = int(input())

cars_in = m
max_cars_in = cars_in

is_negative = False

for i in range(n) :
    a, b = map(int, input().split())
    cars_in = cars_in + a - b
    max_cars_in = max(max_cars_in, cars_in)

    if cars_in < 0 :
        is_negative = True
        
if is_negative :
    print(0)
else :
    print(max_cars_in)

Submission status: Accepted

Solution code:
n = int(input())

s = m = int(input())

for i in range(n):
  in_car, out_car = map(int, input().split())
  
  m += in_car - out_car
  
  if m < 0:
    s = 0
    break
  else:
    s = max(s, m)

print(s)

Submission status: Accepted

Solution code:
n = input()
m = input()
ans = m
for i in range(n):
    a, b = map(int, raw_input().split())
    m += a - b
    ans = max(ans, m)
    if m < 0: print 0; break
else: print ans

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n=int(input())
m=int(input())
ans=c=m
for i in range(n):
    a,b=[int(a) for a in input().split()]
    if c<0:
        continue
    c+=a-b
    if ans<c:
        ans=c
    if c<0:
        ans=0
print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    # ??????????????\???
    duration = int(input())     # ????????????(???)
    cars_in_tunnel = int(input())  # ??????????????????????????°
    data = []                   # 1????????¨????????\???????????????
    for _ in range(duration):
        data.append([int(x) for x in input().split(' ')])

    # ???????????????
    max_cars = cars_in_tunnel   # ???????????????????????????????????§??°
    error_detected = False      # ??????????????????????????°???????????????????????£???????????????
    for car_in, car_out in data:
        cars_in_tunnel += car_in
        cars_in_tunnel -= car_out
        if cars_in_tunnel < 0:
            error_detected = True
            break
        else:
            max_cars = max(max_cars, cars_in_tunnel)

    # ???????????¨???
    if error_detected is False:
        print(max_cars)
    else:
        print(0)

Submission status: Accepted

Solution code:
n=int(input())
ans=c=m=int(input())
for i in[0]*n:
    a,b=map(int,input().split())
    if c<0:ans=0;break
    c+=a-b
    ans=max(ans,c)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
n, m = int(input()), int(input())
B, S = 0, m
d = deque([0]*m)
for _ in range(n):
    cars = list(map(int, input().split()))
    for _ in range(cars[0]): d.append(0)
    try:
        for _ in range(cars[1]): d.pop()
    except IndexError:
        B = 1
    S = max(S, len(d))
print(0 if B else S)

Submission status: Accepted

Solution code:
n, m = int(input()), int(input())
B, S = 0, m
d = [0]*m
for _ in range(n):
    cars = list(map(int, input().split()))
    for _ in range(cars[0]): d.append(0)
    try:
        for _ in range(cars[1]): d.pop()
    except IndexError:
        B = 1
    S = max(S, len(d))
print(0 if B else S)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
m = int(input())
S_max = m
for i in range(n):
    a, b = map(int, input().split())
    m += a - b
    if m < 0:
        print(0)
        break
    S_max = max(S_max, m)
else:
    print(S_max)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=s=int(input())
b=1
for _ in[0]*n:
 i,o=map(int,input().split())
 s+=i-o
 if s<0:b=0;break
 if a<s:a=s
print(a*b)

Submission status: Accepted

Solution code:
import sys
input()
a=s=int(input())
b=1
for e in sys.stdin:
 i,o=map(int,e.split())
 s+=i-o
 if s<0:b=0;break
 if a<s:a=s
print(a*b)

Submission status: Accepted

Solution code:
import sys
input()
a=s=int(input())
for e in sys.stdin:
 i,o=map(int,e.split())
 s+=i-o
 if s<0:a=0;break
 if a<s:a=s
print(a)

Submission status: Accepted

Solution code:
n = int(input())
m = int(input())
maxd = m
for i in range(n):
    inn, out = map(int, input().split())
    m += inn - out 
    if m > maxd:
        maxd = m
    elif m < 0:
        maxd = 0
        break
print(maxd)

Submission status: Accepted

Solution code:
n = int(input()) #time
m = int(input()) 
a = [] #input
s = [m] #result

for i in range(n):
    b = input().split( )
    a.append(b)

for i in range(n):
    m = m + int(a[i][0]) - int(a[i][1])
    s.append(m)

if(min(s) < 0): 
    print(0)
else:
    print(max(s))

Submission status: Accepted

Solution code:
n = int(input())
m = int(input())

ans = m
for i in range(n):
  a, b = map(int,input().split())
  m += (a - b)
  if m < 0:
    ans = 0
    break
  elif m > ans:
    ans = m
print(ans)

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  m = int(input())
  ans = m
  for i in range(n):
    a, b = map(int,input().split())
    m += (a - b)
    if m < 0:
      ans = 0
      break
    elif m > ans:
      ans = m
  print(ans)
main()

Submission status: Accepted

Solution code:
n = int(input())
m = int(input())
S = m
S_max = S
for _ in range(n):
    i, o = map(int, input().split())
    S += i - o
    if S < 0:
        print(0)
        exit(0)
    elif S_max < S:
        S_max = S
print(S_max)

Submission status: Accepted

Solution code:
# coding=utf-8

if __name__ == '__main__':
    N = int(input())
    M = int(input())
    S = M
    for i in range(N):
        enter, exit = map(int, input().split())
        M += (enter-exit)
        if M < 0:
            print(0)
            break
        S = max(S, M)
    else:
        print(S)

Submission status: Accepted

Solution code:
# AOJ 0587: Tunnel
# Python3 2018.6.30 bal4u

n = int(input())
ans = m = int(input())
for i in range(n):
	a, b = map(int, input().split())
	m += a-b
	if m < 0: ans = 0
	if ans > 0 and m > ans: ans = m
print(ans)

Submission status: Accepted

Solution code:
n = input()
m = input()
result = m
for i in range(n):
	a, b = map(int, raw_input().split())
	m += a - b
	if m < 0:
		print 0
		break
	result = max(result, m)
else:
	print result

Submission status: Accepted

Solution code:
n = input()
m = input()
a = m
for j in range(n):
  (i, o) = map(int, raw_input().split())
  m += i - o
  if m < 0:
    print 0
    quit()
  a = max(a, m)
print a

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
m = int(input())

count = m
for i in range(n):
    a, b = map(int, input().split())
    m = m + a - b
    if m < 0:
      count = 0
      break
    elif m > count: 
      count = m
print(count)

Submission status: Accepted

Solution code:
N=int(input())
M=int(input())
k=[]
for _ in range(N):
    k.append(list(map(int,input().split())))

S=[0 for _ in range(N+1)]
S[0]=M

for i in range(1,N+1):
    S[i]=S[i-1]+k[i-1][0]-k[i-1][1]

S.sort()
if S[0]<0:
    print(0)
else:
    print(S[-1])

Submission status: Accepted

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

s = m
M = m

for i in range(n):
    s += x[i][0] - x[i][1]
    if s < 0:
        M = 0
        break
    M = max(M,s)
print(M)

Submission status: Accepted

Solution code:
m=int(input())
n=int(input())
car_list=[n]
c=n
for i in range(m):
    a,b=map(int,input().split())
    c+=a-b
    car_list.append(c)

if min(car_list)<0:
    print(0)
else:
    print(max(car_list))

Submission status: Accepted

Solution code:
n = int(input())
m = int(input())
sm = m; C = []
for i in range(n):
    C.append(list(map(int, input().split())))
for i in C:
    m += i[0] - i[1]
    if m < 0:
        print(0)
        exit()
    sm = max(m, sm)
print(sm)

Submission status: Accepted

Solution code:
n = int(input())
S = list()
S.append(int(input()))
flag = 0
for i in range(n):
    a, b = map(int, input().split())
    daisu = S[i] +a -b
    if daisu < 0:
        print(0)
        flag = 1
        break
    S.append(daisu)
if flag == 0:
    print(max(S))

Submission status: Accepted

Solution code:
n = int(input())
m = int(input())
lines = [list(map(int, input().split())) for _ in range(n)]
# print(lines)
subs = [m]
adds = m

for i, line in enumerate(lines):
    adds += line[0] - line[1]
    # print(adds)
    if adds < 0:
        adds = 0
        break
    else:
        subs.append(adds)

if adds == 0:
    print(adds)
else:
    print(max(subs))
# print(subs)
# adds = 0
# for i in range(len(subs)):
#     adds += subs[i]
#     if adds < 0:
#         answer = 0
#         break
#     else:
#         answer = adds
# print(answer)

Submission status: Accepted

Solution code:
n = int(input())  # n分間
m = int(input())  # トンネル内車両数の初期値
mmax = m  # 最大値保持用
A = []  # 出入り台数のリスト [[a1, b1],[a2, b2],,] a:入り, b:出
for i in range(n):
	A.append(list(map(int, input().split())))

# print(A)

# リストを回して、ドンネル内の車両台数を計算し、最大値を更新する
for a in A:
	m = m + a[0] - a[1]
	if m < 0:
		mmax = 0
		break
	if m > mmax:
		mmax = m

# 出力部
print(mmax)

Submission status: Accepted

Solution code:
n = int(input())
m = int(input())
j = [list(map(int,input().split())) for _ in range(n)]
ans = m
for x in j:
    m += x[0]-x[1]
    ans = max(ans,m)
    if m < 0:
        ans= 0
        break
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
M = int(input())
L = [list(map(int,input().split())) for _ in range(N)]

ans = M
for l in L:
    M = M + l[0] - l[1]
    if M < 0:
        print(0)
        exit()
    ans = max(ans, M)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
M = int(input())
S = M
c = M
for i in range(N):
    a, b = map(int, input().split())
    c = c + a - b
    if c < 0:
        S = 0
        break
    else:
        S = max(S, c)

print(S)

Submission status: Accepted

Solution code:
import sys
n = int(input())
m = int(input())

s = [m]
for i in range(1, n+1):
    a, b = map(int, input().split())
    m = m + a - b
    if m < 0:
        print(0)
        sys.exit(0)
    s.append(m)
print(max(s))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
m = int(input())

ans = m
flag = False
for _ in range(n):
    a, b = map(int, input().split())
    if flag:
        continue
    m += a - b
    if m < 0:
        flag = True
        continue
    if m > ans:
        ans = m

if not flag:
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
n = int(input())
m = int(input())
dat = [input().split(" ") for i in range(n)]
s = [m]
for i in range(n):
    m += (int(dat[i][0])-int(dat[i][1]))
    if m < 0:
        print(0)
        sys.exit()
    s.append(m)
print(max(s))