Problem: 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?

Submission attempts (in order):

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
yo = 0
chinpo = True

for i in range(N):
    if chinpo:
        Ans += min(A[i],B[i])
        if B[i] > A[i]:
            yo = B[i] - A[i]
            chinpo = False
    else:
        #Ans += min(A[i],B[i]+yo)
        chinpo = True
        if yo >= A[i]:
            Ans += A[i]
            yo = B[i]
            chinpo = False
        elif B[i]+yo >= A[i]:
            yo = B[i]+B[i-1]-A[i-1]
            chinpo = False

if yo > A[N]:
    Ans += A[N]
else:
    Ans += yo

print(Ans)


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
        
else:
    yojo = 0
    Ans += B[0]
    chinpo = True
    

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)

　

#n = int(input())
#L = list(map(int, input().split()))
import math
import sys
import heapq
import fractions
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]:
        if A[i] >= B[i+1]+B[i]:
            ans += B[i]+B[i+1]
            B[i+1] = 0
        else:
            B[i+1] -= A[i]-B[i]
            ans += A[i]
    else:
        ans += A[i]

print(ans)

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
yo = 0
chinpo = True

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

if not chinpo:
    if B[N-1]+B[N-2]-A[N-2]-A[N-1] >= A[N]:
        Ans += A[N]
    else:
        Ans += B[N-1]+B[N-2]-A[N-2]-A[N-1]

print(Ans)


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]+A[1]
        chinpo = False
        
else:
    yojo = 0
    Ans += B[0]
    chinpo = True
    

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]:
                Ans += A[i+1]
                chinpo = False
        else:
            Ans += B[i]
            yojo = 0
    else:
        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)

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
yo = 0

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

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

print(Ans)



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

Ans = 0

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

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

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

print(Ans)

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
chinpo = True

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

print(Ans)


from sys import stdin

#s = input()
#S = []

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
yo = 0
chinpo = True

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

if yo > A[N]:
    Ans += A[N]
else:
    Ans += yo

print(Ans)


#n = int(input())
#L = list(map(int, input().split()))
import math
import sys
import heapq
import fractions
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ans = 0

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

print(ans)

#n = int(input())
#L = list(map(int, input().split()))
import math
import sys
import heapq
import fractions
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]:
        if B[i] >= A[i+1]+A[i]:
            ans += A[i]+A[i+1]
            A[i+1] = 0
        else:
            A[i+1] -= B[i]-A[i]
            ans += B[i]
    else:
        ans += B[i]

print(ans)

Verdicts: Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Accepted