Problem:


Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3



Sample input:
2
3 5 2
4 5


Sample output:
9

Submission status: Runtime Error

Solution code:
N = int(input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
L = 0
for i in range(N):
  l = min(A[i], B[i])
  r = min(A[i + 1], B[i])
  A[i + 1] -= r
  L += l + r
print(L)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = 0

for i in range(n):
  c += min(a[i],b[i])
  tmp = min(a[i+1],b[i]-min(a[i],b[i]))
  c += tmp
  if tmp == a[i+1]:
    del a[i+1]
    a.insert(i+1,0)
  else:
    del a[i+1]
    a.insert(i+1,a[i+1]+a[i]-b[i])

print(c)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

MaxValue = 0
if N == 1:
  MaxValue = min(B[0], A[0]+A[1])
  print(MaxValue)
else:
  for i in range(N):
    if A[i] < B[i]:
      #MaxValue += A[i]
      if A[i+1] < B[i] - A[i]:
        MaxValue += A[i+i] + A[i]
        A[i+1] = 0
      else:
        temp = B[i] - A[i]
        MaxValue += B[i]
        A[i+1] -= temp
    else:
      MaxValue += B[i]
  print(MaxValue)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

MaxValue = 0
if N == 1:
  MaxValue = min(B[0], A[0]+A[1])
  print(MaxValue)
else:
  for i in range(N):
    if A[i] < B[i]:
      MaxValue += A[i]
      if A[i+1] < B[i] - A[i]:
        MaxValue += A[i+i]
        A[i+1] = 0
      else:
        temp = B[i] - A[i]
        MaxValue += temp
        A[i+1] -= temp
    else:
      MaxValue += B[i]
  print(MaxValue)

Submission status: Runtime Error

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

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

killed = 0

for i in range(n):
    if b[i] <= a[i]:
        killed += b[i]
        continue
    else:
        b[i] -=	a[i]
        killed += a[i] + min(a[i+1], b[i])
	a[i+1] = max(0, a[i+1]-b[i])

print(killed)

Submission status: Runtime Error

Solution code:
a = int(input())
b = list(map(int,input()))
c = list(map(int,input()))

ans = 0
for i in range(a):
  ans += min(b[i],c[i])
  c[i] -= min(b[i],c[i])
  ans += min(b[i + 1], c[i])

print(ans)

Submission status: Runtime Error

Solution code:
2
100 1 1
1 100

Submission status: Runtime Error

Solution code:

N = int(input())
A = input().split()
B = input().split()
for i in range(N+1):
    A[i] = int(A[i])
for i in range(N):
    B[i] = int(B[i])

Ans = 0

if (B[0]-A[0] > 0):
    if (B[0]-A[0] < A[1]):
        yojo = B[0]-A[0]
        Ans += A[0]
        chinpo = True
    else:
        yojo = B[1]
        Ans += A[0]
        chinpo = False
        #A[0]+for以下
else:
    yojo = 0
    Ans += B[0]
    chinpo = True
    #B[0]+for以下

for i in range(1,N-1):
    if chinpo:
        yoB = yojo + B[i]
        if yoB >= A[i]:
            yojo = yoB - A[i]
            Ans += A[i]
            if yojo >= A[i+1]:
                chinpo = False
        else:
            Ans += B[i]
            yojo = 0
    else:
        Ans += A[i]
        yojo = B[i]
        chinpo = True

if yojo > A[N]:
    Ans += A[N]
else:
    if yojo + B[N-1] > A[N]:
        Ans += A[N]
    else:
        Ans += yojo + B[N-1]

print(Ans)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding:utf-8 -*-

def city_saver(a, b):
    monster = 0
    for i in range(len(b)):
        monster += min(a[i], b[i])
        if(a[i]<b[i]):
            monster += min(b[i]-a[i], a[i+1])
            a[i+1] -= min(b[i]-a[i], a[i+1])
    return monster

def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    print(city_saver(a, b))

if __name__ == "__main__":
    try:
    main()

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding:utf-8 -*-

def city_saver(a, b):
    monster = 0
    for i in range(len(b)):
        monster += min(a[i], b[i])
        if(a[i]<b[i]):
            monster += min(b[i]-a[i], a[i+1])
            a[i+1] -= min(b[i]-a[i], a[i+1])
    return monster

def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    city_saver(a, b)

if __name__ == "__main__":
    try:
    main()

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding:utf-8 -*-

def city_saver(a, b):
    monster = 0
    for i in range(len(b)):
        monster += min(a[i], b[i])
        if(a[i]<b[i]):
            monster += min(b[i]-a[i], a[i+1])
            a[i+1] -= min(b[i]-a[i], a[i+1])
    return monster

def main():
    n = int(input())
    a = map(int, input().split())
    b = map(int, input().split())

if __name__ == "__main__":
    try:
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
b.append(0)
count = 0
for i in range(n+1):
  if a[i] >= b[i]:
    count += b[i]
  else:
    count += b[i]
    a[i+1] = a[i+1] - b[i] + a[i]
print(count)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
totl=min(0,a[0]-b[0])
for i in range(n):
  totl+=min(0,a[i+1]-b[i]-b[i+1])
    
print(sum(b)+totl)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if a[0]-b[0]<0:
  totl=a[0]-b[0]
else:
  totl=0
for i in range(n):
  if b[i]+b[i+1]>a[i+1]:
    totl+=a[i+1]-b[i]-b[i+1]
    
print(sum(b)+totl)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if a[0]-b[0]<0:
  totl=a[0]-b[0]
for i in range(n):
  if b[i]+b[i+1]>a[i+1]:
    totl+=a[i+1]-b[i]-b[i+1]
    
print(sum(b)+totl)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
totl=0
for i in range(n):
  m=min(b[i],max(a[i],a[i+1]))
  totl+=m
  del a[a.index(m)]
print(totl)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=a.sort(reverse=True)
d=b.sort(reverse=True)
totl=0
for i in range(n):
  totl+=min(c[i],d[i])
print(totl)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a_=a.sort(reverse=True)
b_=b.sort(reverse=True)
totl=0
for i in range(n):
  totl+=min(a_[i],b_[i])
print(totl)

Submission status: Runtime Error

Solution code:
n = int(input()
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0

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

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = 0
for i in range(0,n+1):
  if a[i] > b[i]:
    c = c + b[i]
  elif a[i] <= b[i]:
    c = c + a[i]
    b[i] = b[i] - a[i]
    if b[i] < a[i+1]:
      c = c + b[i]
      a[i+1] = a[i+1] - b[i]
    else:
      c = c + a[i+1]
      a[i+1] = 0
print(c)

Submission status: Runtime Error

Solution code:
n=int(input())
m=list(map(int,input().split()))
h=list(map(int,input().split()))
a=0
for i in range(N):
  if h[i]<m[i]:
    a+=h[i]
  else:
    a+=m[i]
    r=h[i]-m[i]
    s=min(m[i+1],r)
    a+=s
    m[i+1]-=s
print(a)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
cnt=0


for i in range(n):
  if(b[i]<=a[i]):
    cnt+=b[i]
    a[i]-=b[i]
  elif(b[i]>a[i] and a[i]+a[i+1]>b[i]):
    cnt+=a[i]
    cnt+=b[i]-a[i]
    a[i+1]-=b[i]-a[i]
    a[i]=0
  else:
    cnt+=a[i]+a[i+1]
    a[i]=0
    a[i+1]=0
    
  print(a)
    
print(cnt)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
cnt=0


for i in range(n):
  if(b[i]<=a[i]):
    cnt+=b[i]
    a[i]-=b[i]
  if(b[i]>a[i] and a[i]+a[i+1]>b[i]):
    cnt+=a[i]
    cnt+=b[i]-a[i]
    a[i+1]-=b[i]-a[i]
    a[i]=0
  else:
    cnt+=a[i]+a[i+1]
    a[i]=0
    a[i+1]=0
    
  print(a)
    
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

C = A.copy()
for i in range(N):
    if C[i] - B[i] > 0:
        C[i] -= B[i]
    else:
        C[i+1] = C[i+1] - (B[i] - C[i]) if C[i+1] - (B[i] - C[i]) > 0 else 0
        C[i] = 0
    # print(C)

beaten_monster: int = sum(A) - sum(C)
print(beaten_monster)

Submission status: Runtime Error

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
A = list(map(int,stdin.readline().rstrip().split()))
B = list(map(int,stdin.readline().rstrip().split()))
point = 0
fa = min(A[0],B[0])
fb = min(A[1],B[0]-f00)
point += fb
point += fa
for i in range(1,n):
    fc = min(A[i]-fb,B[i])
    point += fc
    fb = min(A[i+1],B[i]-fc)
    point += fb
print(point)

Submission status: Runtime Error

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
A = list(map(int,stdin.readline().rstrip().split()))
B = list(map(int,stdin.readline().rstrip().split()))
point = 0
f00 = min(A[0],B[0])
f12 = min(A[1],B[0]-f00)
point += f12
point += f0
for i in range(1,n):
    f11 = min(A[i]-f12,B[i])
    point += f11
    f12 = min(A[i+1],B[i]-f11)
    point += f12
print(point)

Submission status: Runtime Error

Solution code:
import sys
data = sys.stdin.readline().split('\n')
twn = int(data[0]) + 1
ysh = int(data[0])
yshabl = list(map(int,data[2].split()))
mnstr = list(map(int,data[1].split()))

cnt = 0

for i in range(-1,-1*len(yshabl)-1,-1):
	y = yshabl[i]
	m = mnstr[i]+mnstr[i-1]
	if m >= y:
		cnt += y
		mnstr[i-1] -= y - mnstr[i]
	else:
		cnt += m
		mnstr[i-1] = 0

print(cnt)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
b.append(0)
i=0
count=0
while True:
    if i==n+1:
        break
    elif a[i]>b[i]:
        i+=1
        count+=min(a[i],b[i])
    else:
        count+=min(a[i],b[i])
        c=b[i]-a[i]
        count+=min(a[i+1],c)
        if c>a[i+1]:
            a[i+1]=0
            i+=1
        else:
            a[i+1]-=c
            i+=1
print(count)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
i=0
count=0
while True:
    count+=min(a[i],b[i])
    if i==n:
        break
    elif a[i]>b[i]:
        i+=1
    else:
        c=b[i]-a[i]
        count+=min(a[i+1],c)
        if c>a:
            a[i+1]=0
            i+=1
        else:
            a[i+1]-=c
            i+=1
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split(" ")))
B = list(map(int,input().split(" ")))
ans = 0
for i in range(N):
    if A[i] >= B[i]:
        ans += B[i]
    else: 
        if B[i] >= (A[i] + A[i+1]):
            ans += (A[i] + A[i+1])
            A[i+1] = 0
        
        elif B[i] < (A[i] + A[i+1]):
            print(ans)
            ans += B[i]
            print(ans)
            A[i+1] = (A[i] + A[i+1]) - B[i]
    print(A,ans)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split(" ")))
B = list(map(int,input().split(" ")))
a = sum(A)
ans = 0
for i in range(N):
    tmp = A[i]+A[i+1]-B[i]
    ans = ans + max(0,tmp-A[i+1])
    A[i+1] = min(A[i+1],tmp)
    print(A)

print(a - ans)

Submission status: Runtime Error

Solution code:
n=int(input())
monsters=list(map(int, input().split()))
battle=list(map(int, input().split()))
remain=0
win=0
for i in range(n):
    if remain != 0:
        win += remai
        battle[i] -= remain
        remain = 0
    if monsters[i] < battle[i]:
        win += monsters[i]
        monsters[i+1] -= battle[i] - monsters[i]
    else:
        win += battle[i]
        remain = monsters[i] - battle[i]
print(win)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 0
for i in range(N):
  ans += min(a[i]+a[i+1],b[i])
  A[i+1] -= max(0,min(A[i+1],B[i]-A[i]))
print(ans)

Submission status: Runtime Error

Solution code:

import numpy as np
N = int(input())

A_ls = np.array(list(map(int,input().split(" "))))
B_ls = np.array(list(map(int,input().split(" "))))

S = 0

for i,b in B_ls:
    if b > A_ls[i]:
        A_ls[i] == 0
        b -= A_ls[i]
        S += A_ls[i]
        if A_ls[i+1] >= b:
            A_ls[i+1] -=b
            S+=b
        else:
            A_ls[i+1] = 0
            S+=(A_ls[i+1])
    else:
        S += b

print(S)

Submission status: Runtime Error

Solution code:

import numpy as np
N = int(input())

A_ls = np.array(list(map(int,input().split(" "))))
B_ls = np.array(list(map(int,input().split(" "))))

S = 0

for i,a in A_ls:
    if a > B_ls[i]:
        B_ls[i] == 0
        a -= B_ls[i]
        S += B_ls[i]
        if B_ls[i+1] >= a:
            B_ls[i+1] -=a
            S+=a
        elif:
            B_ls[i+1] = 0
            S+=(B_ls[i+1])
    elif:
        S += a

print(S)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
for i in range(n):
  if b[i] > a[i]:
    ans += a[i]
    b[i] -= a[i]
    if b[i] >= a[i+1]:
      ans += a[i+1]
      a[i+1] = 0
    else:
      ans += b[i]
      a[i+i] -= b[i]
  elif b[i] == a[i]:
    ans += a[i]
  else:
    ans += b[i]

print(ans)

Submission status: Runtime Error

Solution code:
N = input()
A = list((map(int,input().split()))
B = list(map(int,input().split()))

ans = 0
         
for i in range(0,N):
    left = A[i] - B[i]
	ans += min(A[i],B[i])
         
    if left < 0:
		right = A[i+1] - abs(left)
    	ans += min( A[i+1],abs(left))
		A[i+1] = right

print(ans)

Submission status: Runtime Error

Solution code:
def solve(string):
    n, *ab = map(int, string.split())
    a, b = ab[:n + 1], ab[n + 1:]
    _sum = sum(a)
    for i, _b in enumerate(b[::-1]):
        if a[n - i] < _b:
            _b -= a[n - i]
            a[n - i] = 0
            a[n - i - 1] = max(0, a[n - i - 1] - _b)
        else:
            a[n - i] -= _b
    return str(_sum - sum(a))


if __name__ == '__main__':
    n = int(input())
    print(solve('{}\n'.format(n) + '\n'.join([input() for _ in range(n)])))

Submission status: Runtime Error

Solution code:
n = int(input())
monsters = list(map(int, input().split()))
heros = list(map(int, input().split()))

def f(i, j):
  if i == j == 0:
    return min(monsters[i], heros[i])
  if i == j - 1:
    return min(monsters[i+1], heros[i] - f(i, i))
  if i == j:
    return min(monsters[i] - f(i-1, i), heros[i])

s = 0
for i in range(n):
	s += f(i, i) + f(i, i+1)
print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
monsters = list(map(int, input().split()))
heros = list(map(int, input().split()))

def f(i, j):
  if i == j == 1:
    return min(monsters[i], heros[i])
  if i == j - 1:
    return min(monsters[i+1], heros[i] - f(i, i)
  if i == j:
	return min(monsters[i] - f(i-1, i), heros[i])

s = 0
for i in range(1, 1+n):
	s += f(i, i) + f(i, i+1)

Submission status: Runtime Error

Solution code:
N = int(input())

M = list(map(int, input().split()))

Y = list(map(int, input().split()))

sum_M = []
for i in range(0, N+1):
 if i == 0:
  p1 = max(0, Y[0] - M[0])
  sum_M.append(min(M[0], Y[0]) ) 
 elif i < N:
  sum_M.append(min(M[i], Y[i] + p1) )
  p0 = 
  p1 = min(Y[i], max(0, Y[i] - (M[i] + p1)) )
 else:
  sum_M.append(min(M[i], p1) )
  p1 = max(0, p1 - M[i])  

print(sum(sum_M))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

kill = 0

if A[0] >= B[0]:
  kill += B[0]
  B[0] = 0
else:
  kill += A[0]
  B[0] -= A[0]

for i in range(N):
  if A[i+1] <= B[i]:
    kill += A[i+1]
  else:
    A[i+1] -= B[i]
    kill += B[i]
    if A[i+1] >= B[i+1]:
      kill += B[i+1]
      B[i+1] = 0
    else:
      kill += A[i+1]
      B[i+1] -= A[i+1]

print(kill)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

kill = 0

if A[0] >= B[0]:
  kill += B[0]
  B[0] = 0
else:
  kill += A[0]
  B[0] -= A[0]

for i in range(N):
  if A[i+1] <= B[i]:
    kill += A[i+1]
  else:
    A[i+1] -= B[i]
    kill += B[i]
    if A[i+1] >= B[i+1]:
      kill += B[i+1]
      B[i+1] = 0
    else:
      kill += A[i+1]
      B[i+1] -= A[i+1]

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(x) for x in input.split()]
B = [int(x) for x in input.split()]

tot = 0
for i in range(N):
    if A[i] <= B[i]:
        tot += A[i]
        B[i] -= A[i]
        A[i] = 0
    else:
        tot += B[i]
        B[i] = 0
        A[i] = A[i] - B[i]
    if B[i] >= 0:
        if A[i+1] <= B[i]:
            tot += A[i+1]
            A[i+1] = 0
            B[i] -= A[i+1]
        else:
            A[i+1] -= B[i]
            tot += B[i]
            B[i] = 0

print(tot)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=0
for i in range(n-1):
  if a[i]<=b[i]:
    c+=a[i]
    d=b[i]-a[i]
    b[i+1]+=d
  else:
    c+=b[i]
if b[n-1]=>a[n-1]:
  c+=a[n-1]
  x=b[n-1]-a[n-1]
  c+=min(a[n],x)
else:
  c+=b[n-1]
print(c)

Submission status: Runtime Error

Solution code:
N = int(input())
An = map(int,input().split())
Bn = map(int,input().split())
ans=0
for b in range(len(Bn)):
    left = min(An[b],Bn[b])
    ans+=left
    An[b]-=left
    Bn[b]-=left
    right = min(An[b+1],Bn[b])
    ans+=right
    An[b+1]-=right
    Bn[b]-=right
print(ans)

Submission status: Runtime Error

Solution code:

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
x = min(A[0],B[0])
ans += x
for j in range(N-1):
    i = j+1
    x = min(A[i],B[i-1]-x)
    ans += x
    x = min(A[i]-x,B[i])
    ans += x
i = i+1
x = min(A[i],B[i-1]-x)
ans += x
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
x = min(A[0],B[0])
ans += x
for j in range(N-1):
    i = j+1
    x = min(A[i],B[i-1]-x)
    ans += x
    x = min(A[i]-x,B[i])
    ans += x
i = i+1
x = min(A[i],B[i-1]-x)
ans += x

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0
x = 0
ans += min(A[0],B[0])

for j in range(N-1):
    i = j+1
    x = min(A[i],B[i-1]-x)
    ans += x
    x = min(A[i]-x,B[i])
    ans += x
i = i+1
x = min(A[i],B[i-1]-x)
ans += x
print(ans)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    n = int(input())
    al = [int(a) for a in input().split()]
    bl = [int(b) for b in input().split()]

    score = 0
    while len(bl) > 0:
        a = al.pop()
        b = bl.pop()

        if a >= b:
            score += b
        else:
            score += a
            b -= a
            a2 = al.pop()

            if a2 > b:
                score += b
                a2 -= b
                al.append(a2)
            else:
                score += a2
    print(score)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
res = 0
for i in range(n):
    res += min(a[i], b[i])
    b[i] = max(0, b[i] - a[i])
    res += min(b[i], a[i + 1])
    a[i + 1] = max(0, a[i + 1] - b[i])
print(res)

Submission status: Runtime Error

Solution code:
for i, b in enumerate(B):
    if b <= A[i]:
        r += b
    else:
        b -= A[i]
        r += A[i]
        if b <= A[i+1]:
            A[i+1] -= b
            r += b
        else:
            r += A[i+1]
            A[i+1]=0
            
print(r)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))

result = 0
sum_a = sum(a)
for i in range(n):
    if a[i] <= b[i]:
        b[i] -= a[i]
        a[i] = 0
        if a[i+1] <= b[i]:
            a[i+1] = 0
        else:
            a[i+1] -= b[i]
    else:
        a[i] -= b[i]
    print('a',a)
    print('b',b)
print(sum_a-sum(a))

Submission status: Runtime Error

Solution code:
 N = int(input())
 monstors = [int(i) for i in input().split()]
 heros = [int(i) for i in  input().split()]
 init_monstor = sum(monstors)

 for i in range(N):
     if monstors[i] >= heros[i]:
         monstors[i] -= heros[i]
         heros[i] = 0
     else:
         heros[i] -= monstors[i]
         monstors[i] = 0
     if monstors[i+1] >= heros[i]:
         monstors[i+1] -= heros[i]
         heros[i] = 0
     else:
         heros[i] -= monstors[i+1]
         monstors[i+1] = 0
 print(init_monstor - sum(monstors))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [0] * (N + 1)
B = [0] * N
for i in range(N):
    A[i], B[i] = map(int, input().split())
 
defeated_all = 0
for i in range(N):
  if B[i] <= A[i]:
    defeated = B[i]
    if B[i+1] <= (A[i] - defeated):
      defeated += B[i+1]
    else:
      defeated += A[i] - defeated
  else:
    defeated = A[i]
print(defeated_all)

Submission status: Runtime Error

Solution code:

Copy
Copy
N = int(input())
A = [int(i) for i in input.split()]
B = [int(i) for i in input.split()]
 
defeated_all = 0
for i in range(N):
  if B[i] <= A[i]:
    defeated = B[i]
    if B[i+1] <= (A[i] - defeated):
      defeated += B[i+1]
    else:
      defeated += A[i] - defeated
  else:
    defeated = A[i]
  defeated_all += defeated
print(defeated_all)
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
 
defeated_all = 0
for i in range(N):
  if B[i] <= A[i]:
    defeated = B[i]
    if B[i+1] <= (A[i] - defeated):
      defeated += B[i+1]
    else:
      defeated += A[i] - defeated
  else:
    defeated = A[i]
  defeated_all += defeated
print(defeated_all)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(i) for i in input.split()]
B = [int(i) for i in input.split()]
 
defeated_all = 0
for i in range(N):
  if B[i] <= A[i]:
    defeated = B[i]
    if B[i+1] <= (A[i] - defeated):
      defeated += B[i+1]
    else:
      defeated += A[i] - defeated
  else:
    defeated = A[i]
  defeated_all += defeated
print(defeated_all)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(i) for i in input.split()]
B = [int(i) for i in input.split()]

defeated_all = 0
for i in range(N):
  if B[i] <= A[i]:
    defeated = B[i]
    if B[i+1] <= (A[i] - defeated):
      defeated += B[i+1]
    else:
      defeated += A[i] - defeated
  else:
    defeated = A[i]
  defeated_all += defeated

Submission status: Runtime Error

Solution code:
N = int(input())
p = input()
list1 = list(map(int,p.split()))

list2 = []
for i in range(1,N + 1):
    list2.append(i)

if list1 == list2:
    print("YES")
    exit()
for i in range(0,N):
    if list1[i] != i+1:
        if i+1 == list1[list1[i]-1]:
            list1[list1[i]-1] = list1[i]
            list1[i] = i+1
            

        if list1 == list2:
            print("YES")
            exit()
        else:
            print("NO")
            exit()

Submission status: Runtime Error

Solution code:
n = int(input())
A=[]
for x in input().split():
    A.append(int(x))

B=[]
for x in input().split():
    B.append(int(x))

B.append(int(0))    

TOT = sum ( A )

print ( A )

for i in range (n) :
    KILL = min ( A[i], B[i] )
    A [i] = A [i] - KILL
    A [i+1] = A[i+1] - min (A[i+1], B[i] - KILL)
    print ( A )    
    
print ( TOT - sum ( A ) )

Submission status: Runtime Error

Solution code:
n = int(input())
monster = list(map(int,input().split()))
brave = list(map(int,input().split()))
ans = 0
for i in range(n):
  if i == 0:
    kill = min(monster[0],brave[0])
    ans += kill 
    brave -= kill
  else:
    kill = min(monster[i],brave[i])
    ans += kill
    brave[i] -= kill
    kill = min(monster[i+1],brave[i])
    ans += kill
    monster -= kill
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
monsters = list(map(int, input().split()))
savers = list(map(int, input().split()))

def _solve(m, s):
  if len(s)==2:
    return min(s[0], m[0]) + min(s[-1], m[-1]) + min(max(s[0]-m[0], 0) + max(s[-1]-m[-1], 0), m[1])
  elif len(s)==1:
    return min(m[0]+m[1], s[0])
  
  s[1] += max(s[0]-m[0], 0)
  s[-2] += max(s[-1]-m[-1], 0)
  
  return min(s[0], m[0]) + min(s[-1], m[-1]) + _solve(m[1:-1], s[1:-1])

print(_solve(monsters, savers))

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=c=0
for i in range(N+1):
    a=A[i]
    b=B[i]
    ans+=min(a,c)
    a=max(a-c,0)
    ans+=min(a,b)
    c=max(b-a,0)
print(ans)

Submission status: Runtime Error

Solution code:
#coding: UTF-8

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

Ans = 0

for num in range(N):
    Ans += min(A[num], B[num])
    if A[num] => B[num]:
        pass
    else: # A[num] < B[num]
        B[num] -= A[num]
        Ans += min(A[num+1], B[num])
        if A[num+1] => B[num]:
            A[num+1] -= B[num]
        else:
            A[num+1] = 0
print(Ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
input=sys.stdin.readline
def intinput():return map(int,input().split())


n=int(input())
a=list(intinput())
b=list(intinput())
ans=0
print(a)
for i in range(n):
    if a[i]+a[i+1]<b[i]:
        ans+=a[i]+a[i+1]
        a[i+1]=0
        print(a,0)
    else:
        ans+=b[i]
        a[i+1]-=0 if a[i]-b[i]>0 else b[i]-a[i]
        print(a,1)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
mosnter_list = list(map(int, input().split()))
braber_list = list(map(int, input().split()))
pos_num = 0
braber_list.append(0)

rem_num = 0
for a, b in zip(moster_list, braber_list):
    if a >= b + rem_num:
        pos_num += b + rem_num
        rem_num = 0
    elif rem_num > a:
        pos_num += a
        rem_num = b
    else:
        pos_num += a
        rem_num = b + rem_num - a
        
print(pos_num)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))

ans=0
for i in range(n):
    if a[i]=<b[i]:
        ans+=a[i]
        ans+=min(a[i+1],b[i]-a[i])
        a[i+1]=max(a[i+1]-(b[i]-a[i]),0)
    else:
        ans+=b[i]
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
s=0
y=0
for i in range(n+1):
    v=max(0,a[i]-y)
    if v<=b[i]:
        s+=a[i]
        y=b[i]-v
    elif v>b[i]:
        s+=b[i]+y
        y=0
print(s)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
count=0

def battle(a,ai,b):
  if a+ai <= b:
    ai=0
    count=a+ai
  elif a<=b:
    ai=ai-(b-a)
    count=b
  else:
    ai=ai
    count=b
  result ai,count
  
for i in range(n):
  c=0
  a[i+1],c=battle(a[i],a[i+1],b[i])
  count += c

print(count)

Submission status: Runtime Error

Solution code:
N = int( input() )
A, B = list( map(lambda x: int(x), input().split(" ")) ), list( map(lambda x: int(x), input().split(" ")) )
#A = monsters. B = number of to be able to kill monsters.殺せる数

def kill_monsters(Ai, Bi):

    if Ai <= 0 or Bi <= 0:
        return (Ai, Bi)
    else:
        return kill_monsters(Ai-1, Bi-1)


def main(local_A, local_B, lim):

    for i in range(lim):
        local_A[i], local_B[i] = kill_monsters(local_A[i], local_B[i])

        if 0 < local_B[i]:
            local_A[i+1], local_B[i] = kill_monsters(local_A[i+1], local_B[i])

    return [local_A, local_B]


print( sum(A) - sum(main(A, B, N)[0]) )

Submission status: Runtime Error

Solution code:
N = int( input() )
A, B = list( map(lambda x: int(x), input().split(" ")) ), list( map(lambda x: int(x), input().split(" ")) )
#A = monsters. B = number of to be able to kill monsters.殺せる数

def kill_monsters(Ai, Bi):

    if Ai <= 0 or Bi <= 0:
        return (Ai, Bi)
    else:
        return kill_monsters(Ai-1, Bi-1)


def main(local_A, local_B, i, lim):
    local_A[i], local_B[i] = kill_monsters(local_A[i], local_B[i])

    if 0 < local_B[i]:
        local_A[i+1], local_B[i] = kill_monsters(local_A[i+1], local_B[i])

    if lim <= i:
        return [local_A, local_B]

    return main(local_A, local_B, i+1, lim)

print( sum(A) - sum(main(A, B, 0, N-1)[0]) )

Submission status: Runtime Error

Solution code:
while(True):
  n=int(input())
  mns=list(map(int,input().split()))
  hrs=list(map(int,input().split()))
  mk=0
  for i in range(n):
    if(hrs[i]>=mns[i]+mns[i+1]):
      mk+=mns[i]+mns[i+1]
      mns[i]=0
      mns[i+1]=0
    else:
      if(hrs[i]<=mns[i]):
        mk+=hrs[i]
      else:
        mk+=hrs[i]
        mns[i+1]-=(hrs[i]-mns[i])
  print(mk)

Submission status: Runtime Error

Solution code:
import sys
import math
INPUT = sys.stdin.readline
#入力関数
def SORO_INT(): return int(INPUT())
def MULT_INT_LIST(): return list(map(int, INPUT().split()))
def MULT_INT_MAP(): return map(int, INPUT().split())
def SORO_STRING(): return INPUT()
def MULT_STRING(): return INPUT().split()

def is_integer_num(n):
    if isinstance(n, int):
        return True
    if isinstance(n, float):
        return n.is_integer()
    return False
N = SORO_INT()
A = MULT_INT_LIST()
B = MULT_INT_LIST()
B.append(0)
count=0

for i in range(0,N+1):
    t = B[i]-A[i]
    #print(t)
    #print(count)
    if t>=0:
        B[i+1]+=t
        count+=A[i]
    else:
        count+=B[i]

t = B[N]-A[N]

if t<0:
    t = -t
    count+=t
    print(count)
else:
    count+=A[N]
    print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
enemy = list(map(int, input().split()))
beat = list(map(int, input().split()))

def hoge(index, index1):
	if(index == 0 and index1 == 0):
		return min([enemy[0], beat[0]])
	elif(index >= 0 and index1 - index == 1):
		return min([enemy[index1], beat[index] - hoge(index, index)])
	elif(index >= 1 and index1 == index):
		return min([enemy[index] - hoge(index - 1, index), beat[index] ])
	else:
		print(index, index1)
res = 0
for i in range(n):
	res += hoge(i, i) + hoge(i, i+1)
print(res)