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

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(index1 - index == 1):
		return min([enemy[index1], beat[index] - hoge(index, index)])
	else:
		return min([enemy[index] - hoge(index - 1, index), beat[index] ])
  
res = 0
for i in range(n):
	res += hoge(i, i) + hoge(i, i+1)
print(res)

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(index1 - index == 1):
		return min([enemy[index1], beat[index] - hoge(index, index)])
	else:
		return min([enemy[index] - hoge(index - 1, index), beat[index] ])
  
res = 0
for i in range(n):
	print(i)  
	res += hoge(i, i) + hoge(i, i+1)
print(res)

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(index1 - index == 1):
    return min([enemy[index1], beat[index] - hoge(index, index)])
  else:
    return min([enemy[index] - hoge(index - 1, index), beat[index] ])
  
res = 0
for i in range(n):
	print(i)  
	res += hoge(i, i) + hoge(i, i+1)
print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
count = 0
for i in range(n-1,-1,-1):
    # i+1の討伐
    if a[i+1] < b[i]:
        count += a[i+1]
        b[i] -= a[i+1]
        a[i+1] = 0
    else:
        count += b[i]
        a[i+1] -= b[i]
        b[i] = 0

    # iの討伐
    if a[i] < b[i]:
        count += a[i]
        b[i] -= a[i]
        a[i] = a
    else:
        count += b[i]
        a[i] -= b[i]
        b[i] = 0

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

def f(i,j):
    if i == 0 and j == 0:
        return min(a[0], b[0])
    elif i == j:
        return min(a[i] - f(i-1,i), b[i])
    else:
        return min(a[i+1], b[i] - f(i,i))
        
for i in range(n):
    ans += f(i, i)
    ans += f(i, i+1)

print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n = int(raw_input())
a = map(int,raw_input().split())
b = map(int,raw_input().split())

ta = [0]*(n+1)

max = 0
for i in range(n):
    for j in range (b[i]+1):
        if ta[i] > a[i]:
            ta[i]=a[i]

        if ta[i] == a[i]:
            ta[i+1] = b[i]-j
            if ta[i+1] > a[i+1]:
                ta[i+1] = a[i+1]
            break

        ta[i] = ta[i] + j

print sum(ta)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n = int(raw_input())
a = map(int,raw_input().split())
b = map(int,raw_input().split())

ta = [0]*(n+1)

max = 0
for i in range(n):
    for j in range (b[i]+1):
        if ta[i] >= a[i]:
            ta[i]=a[i]

        if ta[i] == a[i]:
            ta[i+1] = b[i]-j
            if ta[i+1] > a[i+1]:
                ta[i+1] = a[i+1]
            break

        ta[i] = ta[i] + j
        ta[i+1] = b[i]-j


print sum(ta)

Submission status: Runtime Error

Solution code:
#include <iostream>
using namespace std;
 
#define ll long long
 
int main(){
	int n,x,y;
	cin>>n;
	ll s=0;
	int a[n+1];
	for(int i=0;i<=n;i++){
		cin>>a[i];
	}
	for(int i=0;i<n;i++){
		cin>>x;
		y=min(x,a[i]);
		s+=y;
		x-=y;
		y=min(x,a[i+1]);
		s+=y;
		a[i+1]-=y;
	}
	cout<<s<<"\n";
}

Submission status: Runtime Error

Solution code:
n = int(inputs())

a = inputs()

b = inputs()

at = 0

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n = int(raw_input())
a = map(int,raw_input().split())
b = map(int,raw_input().split())

ta = [0]*(n+1)

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

        if ta[i] == a[i]:
            ta[i+1] = b[i]-j
            if ta[i+1] > a[i+1]:
                ta[i+1] = a[i+1]
            break

        ta[i] = ta[i] + j
        ta[i+1] = b[i]-j


print sum(ta)

Submission status: Runtime Error

Solution code:
raw_input = [int(i)for i in input().split()]
n = raw_input[0]
m = raw_input[1:len(raw_input)-2]
y = raw_input[-n:]
y += [0]
remainder = 0
total = 0
for i in range(n+1):
    y[i] += remainder
    total += min(y[i], m[i])
    remainder = max(y[i] - m[i], 0)
print(total)

Submission status: Runtime Error

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


N = input()
N = int(N)

A = []
A = input().split()
A = [int(n) for n in A]

B = []
B = input().split()
B = [int(n) for n in B]
B.append(0)

#print('N=',N)
#print('A=',A)
#print('B=',B)

monster = 0

for i in range(N+1) :
	if A[i] < B[i] :
		monster = monster + A[i]
		delta = B[i] - A[i]
		B[i+1] = B[i+1] + delta
	if A[i] >= B[i] :
		monster = monster + B[i]

print(monster)

Submission status: Runtime Error

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


N = input()
N = int(N)

A = []
A = input().split()
A = [int(n) for n in A]

B = []
B = input().split()
B = [int(n) for n in B]
B.append(0)

#print('N=',N)
#print('A=',A)
#print('B=',B)

monster = 0

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

print(monster)

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]
ans=0
mon1=a[0]
mon2=a[0]+a[1]
for i in range(n):
   if b[i] <= mon1:
      ans+=b[i]
      mon1=a[i+1]
      if i<=n-2:mon2=a[i+1]+a[i+2]
   elif mon1<b[i]<=mon2:
      ans+=b[i]
      mon1=a[i+1]-(b[i]-a[i])
      if i<=n-2:mon2=mon1+a[i+2]
   else:
      ans+=a[i]+a[i+1]
      if i<=n-2:mon2=a[i+2]
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
M = list(map(int, input().split()))
H = list(map(int, input().split()))

sum_m = 0
rest = 0
rest = max(0, H[1]-M[1])
sum_m += min(H[1], M[1])
for i in range(1, N):
  rest = max(0, H[i]+rest-M[i])
  if M[i] >= H[i]+rest:
    sum_m += H[i]+rest
  else:
    sum_m += M[i]
sum_m += min(rest, M[-1])

print(sum_m)

Submission status: Runtime Error

Solution code:
import copy

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

ans = 0
for i in range(0,n):
    if A[i] <= B[i]:
        ans += A[i]
        B[i] -= A[i] 
        if A[i+1] <= B[i]:
            ans += A[i+1]
1            A[i+1] -= 0
        else:
            ans += B[i]
            A[i+1] -= B[i]
    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()))

beaten_monster_num = 0

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

print(int(beaten_monster_num))

Submission status: Runtime Error

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

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

print(count)

Submission status: Runtime Error

Solution code:
n = raw_input[0]
m = raw_input[1:len(raw_input)-2]
y = raw_input[-2:]
y += [0]
remainder = 0
total = 0
for i in range(n+1):
    y[i] += remainder
    total += min(y[i], m[i])
    remainder = max(y[i] - m[i], 0)
    print(remainder)
print(total)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
n = int(input())
time = [list(map(int,input().split())) for i in range(n)]
monster=time[0]
power=time[1]
counter=0
for i in range(len(power)):
  cl1,cl2=monster[i],monster[i+1]
  if power[i]<max(cl1,cl2):
    counter=counter+power[i]
  else:
    maximum=max(cl1,cl2)
    counter=counter+maximum
    capacity=power[i]-maximum
    if capacity<min(cl1,cl2):
      counter=counter+capacity
    else:
      counter=counter+min(cl1,cl2)
print(counter)

Submission status: Runtime Error

Solution code:

N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

count = 0
while max(As) != 0 and max(Bs) != 0 :
    max_a = max(As)
    max_a_i = As.index(max_a)


    b1 = Bs[max_a_i - 1] if max_a_i > 0 else -1
    b2 = Bs[max_a_i] if (max_a_i < N -1) else -1

    count += 1
    if b1 >= b2:
        b1 -= 1
        Bs[max_a_i - 1] = b1
    else:
        b2 -= 1
        Bs[max_a_i] = b2
    As[max_a_i] = max_a - 1


print(count)

Submission status: Runtime Error

Solution code:
C:\acm\python\python.exe C:/acm/py/44.py
3
5 6 3 8
5 100 8
22
2
100 1 1
1 100
3
2
3 5 2
4 5
9

Submission status: Runtime Error

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

import sys

input = sys.stdin.readline

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

    def f(i):
        if i >= N:
            return 0

        n = 0
        m = min(A[i], B[i])
        n += m
        A[i] -= m
        B[i] -= m
        m = min(A[i+1], B[i])
        n += m
        A[i+1] -= m
        B[i] -= m
        return n + f(i+1)

    n = f(0)
    print(n)

main()

Submission status: Runtime Error

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

import sys

input = sys.stdin.readline

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

    def f(i):
        if i >= N:
            return 0

        n = 0
        m = min(A[i], B[i])
        n += m
        A[i] -= m
        B[i] -= m
        m = min(A[i+1], B[i])
        n += m
        A[i+1] -= m
        B[i] -= m
        return n + f(i+1)

    n = f(0)
    print(n)

main()

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
la=list(map(int,input().split()))

flag=0
for m,i in enumerate(la):
  ans+=min(i,l[m])+l[m+1]-max(0,l[m+1]-i+min(i,l[m]))
  l[m+1]=max(0,l[m+1]-i+min(i,l[m]))
print(ans)

Submission status: Runtime Error

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

import sys

input = sys.stdin.readline

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

    def f(i):
        if i == N:
            return 0

        n = 0
        m = min(A[i], B[i])
        n += m
        A[i] -= m
        B[i] -= m
        m = min(A[i+1], B[i])
        n += m
        A[i] -= m
        B[i] -= m
        return n + f(i+1)

    n = f(0)
    print(n)

main()

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

power = 0
monster = 0
if A[0] > B[0]:
  reslut = B[0]
  kurikoshi = 0
else:
  reslut = A[0]
  kurikoshi = B[0] - A[0]  

for i in range(1, N):
  if A[i] - kurikoshi > 0:
    nokori = A[i] - kurikoshi
    result += kurikoshi
    if nokori > B[i]:
      result += B[i]
      kurikoshi = 0
    else:
      result += nokori
      kurikoshi = B[i] - nokori
  elif A[i] - kurikoshi <= 0:
    result += A[i]
    kurikoshi = B[i]
if A[N] > kurikoshi:
  result += kurikoshi
  print(result)
else:
  result += A[N]
  print(result)

Submission status: Runtime Error

Solution code:
def  t(c,d):
    if(c>=d):
        return (d)
    else:
        return (d-c)
n=int(input())
l1=list(map(int,input().rstrip().split()))
l2=list(map(int,input().rstrip().split()))
s=0
g=0
k=0
for i in range(0,n+1):
    g=t(l1[i],l2[i])
    s=s+g
    l2[i+1]=g+l2[i+1]-k
    k=g
g=t(l1[n],l2[n-1])
s=s+g
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):
    print(a,b)
    if b[i] >= sum(a[i:i+2]):
        ans += sum(a[i:i+2])
        a[i+1] = 0
    elif a[i] >= b[i]:
        ans += b[i]
    elif a[i] < b[i]:
        ans += b[i]
        a[i+1] -= b[i] - a[i]
print(ans)

Submission status: Runtime Error

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

import sys

input = sys.stdin.readline

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

    def f(i):
        n = 0
        if i == N:
            return 0
        if B[i] >= A[i]:
            B[i] -= A[i]
            n += A[i]
            A[i] = 0
            n += min(A[i+1], B[i])
            A[i] -= min(A[i+1], B[i])
            B[i] -= min(A[i+1], B[i])
            return n + f(i+1)
        else:
            n += B[i]
            A[i] -= B[i]
            return n + f(i+1)

    n = f(0)
    print(n)

main()

Submission status: Runtime Error

Solution code:
def  t(c,d):
    if(c>=d):
        return (d)
    else:
        return (d-c)
n=int(input())
l1=list(map(int,input().rstrip().split()))
l2=list(map(int,input().rstrip().split()))
s=0
g=0
k=0
for i in range(0,n+1):
    g=t(l1[i],l2[i])
    s=s+g
    l2[i+1]=g+l2[i+1]-k
    k=g
g=t(l1[n],l2[n-1])
s=s+g
print(s)

Submission status: Runtime Error

Solution code:
n=int(input())
l1=list(map(int,input().rstrip().split()))
l2=list(map(int,input().rstrip().split()))
s=0
g=0
k=0
def  t(c,d):
    if(c>=d):
        return (d)
    else:
        return (d-c)
for i in range(0,n+1):
    g=t(l1[i],l2[i])
    s=s+g
    l2[i+1]=g+l2[i+1]-k
    k=g
g=t(l1[n],l2[n-1])
s=s+g
print(s)

Submission status: Runtime Error

Solution code:
n=int(input())
l1=list(map(int,input().rstrip().split()))
l2=list(map(int,input().rstrip().split()))
s=0
g=0
k=0
def  t(c,d):
    if(c>=d):
        return (d)
    else:
        return (d-c)
for i in range(0,n+1):
    g=t(l1[i],l2[i])
    s=s+g
    l2[i+1]=g+l2[i+1]-k
    k=g
g=t(l1[n],l2[n-1])
s=s+g

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


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


print(count)

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

count = 0
i = 0
a_idx = 0
b_idx = 0
a_num = a[a_idx]
b_num = b[b_idx]
while(b_idx < n and a_idx <= len(a)):
    
    a_num = a_num - 1
    
    if i == b_num:
        b_num = b[b_idx]
        b_idx += 1
        i = 0
    if a_num == 0:
        a_num = a[a_idx]
        a_idx += 1
    count += 1
    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()))
#print("A=",A)
#print("B=",B)

count = 0
for i in range(N+1):
    if i == 0:
        taosita = B[i]-max((B[i]-A[i]),0)
        count = taosita
        B[i]= B[i]-taosita
#        print("i=0")
    elif i!=N
        #1つ前の勇者
        taosita1 = B[i-1]-max((B[i-1]-A[i]),0)
        count =count + taosita1
        A[i] = A[i]-taosita1
        #今回の勇者
        taosita2 = B[i]-max((B[i]-A[i]),0)
        count =count + taosita2
        B[i]= B[i]-taosita2
    if i == N:
        #1つ前の勇者
        taosita3 = B[i-1]-max((B[i-1]-A[i]),0)
        count =count + taosita3
#        A[i] = A[i]-taosita1
print(count)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

        if a[i + 1] < d:
            count += a[i + 1]
            a[i + i] = 0
        else:
            count += d
            a[i + 1] -= d

    else:
        count += b[i]

print(count)

Submission status: Runtime Error

Solution code:
import numpy


def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    n = int(input())
    a_list = li()
    b_list = li()
    # i0_list = []
    # for a, b in zip(a_list[:n], b_list):
    #     i0_list.append(min(a, b))
    # i1_list = []
    # for a, b in zip(a_list[1:], b_list):
    #     i1_list.append(min(a, b))
    # i0_cum = numpy.cumsum(i0_list).tolist()
    # tmp = [0]
    # tmp.extend(i0_cum)
    # i0_cum = tmp
    # i1_cum = numpy.cumsum(i1_list[::-1]).tolist()
    # tmp = [0]
    # tmp.extend(i1_cum)
    # i1_cum = tmp[::-1]
    #
    # print(i0_cum)
    # print(i1_cum)
    # max_val = 0
    # for i0, i1 in zip(i0_cum, i1_cum):
    #     if i0 + i1 > max_val:
    #         max_val = i0 + i1
    # print(max_val)

    monster = 0
    for index in range(len(b_list)):
        monster_add = min(a_list[index], b_list[index])
        monster = monster + monster_add
        a_list[index] = a_list[index] - monster_add
        b_list[index] = b_list[index] - monster_add

        monster_add = min(a_list[index + 1], b_list[index])
        monster = monster + monster_add
        a_list[index] = a_list[index + 1] - monster_add
        b_list[index] = b_list[index] - monster_add
    print(monster)

# 尺取り的に全部求められる？
# iの場合とi+1の場合を全て計算して累積和を使えばいけそう

Submission status: Runtime Error

Solution code:
N = int(input())
k = 0
l = 0
for i in range(N+1):
  a = int(input())
  k += a
for i in range(N):
  b = int(input())
  l += b
if a >= b:
  print(k)
else:
  print(l)

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


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

if b[n-1] - a[n-1] > 0:
    count += a[n-1]
    rest = b[n-1] - a[n-1]
    if rest < a[n]:
        count += rest
    else:
        count += a[n]
else:
    count += b[i]


print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
k = 0
l = 0
for i in range(N+1):
    a = int(input())
    k += a
for i in range(N):
    b = int(input())
    l += b
if a >= b:
    print(k)
else:
    print(l)

Submission status: Runtime Error

Solution code:
N = int(input())
k = 0
l = 0
for i in range(N+1):
  a = int(input())
  k += a

for i in range(N):
  b = int(input())
  l += b

if a >= b:
  print(l)
elif:
  print(k)

Submission status: Runtime Error

Solution code:
ans = 0
for i in range(n):
    for j in range(i,i+2):
        tmp = b[i]
        b[i] -= a[j]        
        if b[i]<=0:
            ans += tmp
            a[j] -= tmp
            break
        else:
            ans += a[j]
            a[j] = 0
            
print(ans)

Submission status: Runtime Error

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

count = 0

if yuusya[0] >= teki[0]:
    yuusya[0] -= teki[0]
    count += teki[0]
    teki[0] = 0
else:
    count += yuusya[0]
    teki[0] -= yuusya[0]
for i in range(1, len(yuusya)):
    if yuusya[i - 1] > 0:
        if yuusya[i - 1] > teki[i]:
            count += teki[i]
            continue
        else:
            count += yuusya[i-1]
            teki[i]-=yuusya[i-1]
    if yuusya[i] >= teki[i]:
        yuusya[i] -= teki[i]
        count += teki[i]
        teki[i] = 0
    else:
        count += yuusya[i]
        teki[i] -= yuusya[i]
        yuusya[i] = 0
        

if yuusya[i] >= teki[i+1]:
    yuusya[i] -= teki[i+1]
    count += teki[i+1]
else:
    count += yuusya[i]
    teki[i] -= yuusya[i]
    yuusya[i] = 0

print(count)