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

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

l = []
for i in range(n):
    l.append(a[i+1] + a[i])

ans = 0
for j in range(n):
    if l[j] - b[j] >= 0:
        ans += b[j]
    else:
        ans += l[j]

print(ans)

Submission status: Wrong Answer

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

ans = 0

m = A[-1]+A[-2]
i = 0
j = -2
while i != N*-1:
    i -= 1
    j -= 1
    if m > B[i]:
        m -= B[i]
        ans += B[i]
    else:
        ans += m
        m = 0
    try:
        m += A[j]
    except:
        break

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
f=lambda:map(int,input().split())
input()
p,*A=f()
B=f()
s=0
for a,b in zip(A,B):
  t=min(b,a+p)
  p=a-max(0,t-b)
  s+=t
print(s)

Submission status: Wrong Answer

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

# In[9]:


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


# In[11]:


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


# In[ ]:

Submission status: Wrong Answer

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

count = 0
for i in range(n):
    count += min(a[i], b[i])
    b[i] = max(b[i]-a[i], 0)

    count += min(a[i+1], b[i])
    b[i] = max(b[i] - a[i+1], 0)
    a[i+1] = max(a[i+1] - b[i], 0)

print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(N):
  if B[i]<=A[i]:
    A[i]-=B[i]
  else:
    A[i+1]=max(0, A[i+1]-B[i]-A[i])
    A[i]=0
print(pre-sum(A))

Submission status: Wrong Answer

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

for i in range(N):
  if B[i]<=A[i]:
    A[i]-=B[i]
  else:
    A[i+1]=max(0, B[i]-A[i])
    A[i]=0
print(sum(A)-pre)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cnt = 0
beforeKill = 0
for i in range(n):
    kill1 = min(A[i], B[i] - beforeKill)
    kill2 = max(min(A[i+1], B[i]-kill1), 0)
    cnt += kill1 + kill2
    beforeKill = kill2
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split())) #len(A) = n+1
B = list(map(int, input().split())) #len(B) = n

def f(X,Y):
  count = 0
  for i in range(n):
    if Y[i] <= X[i]:
      X[i+1] = X[i+1]
      count += Y[i]
    elif X[i] < Y[i] < X[i]+X[i+1]:
      X[i+1] = X[i]+X[i+1] - Y[i]
      count += Y[i]
    elif X[i] + X[i+1] <= Y[i] :
      X[i+1] == 0
      count += X[i]+X[i+1]
  return(count)
print(max(f(A,B),f(A[-1::-1],B[-1::-1])))

Submission status: Wrong Answer

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
answer_list = [0]*N

for i in range(N):
    temp = B_list[i]

    for j in range(i, i+2):
        if temp - A_list[j] <= 0:
            answer_list[i] += temp
            A_list[j] -= temp
            break
        else:
            answer_list[i] += A_list[j]
            temp -= A_list[j]

print(sum(answer_list))

Submission status: Wrong Answer

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
answer_list = [0]*N

for i in range(N):
    temp = B_list[i]

    for j in range(i, i+2):
        if temp - A_list[j] <= 0:
            answer_list[i] += temp
            break
        else:
            answer_list[i] += A_list[j]
            temp -= A_list[j]

print(sum(answer_list))

Submission status: Wrong Answer

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
answer_list = [0]*N

for i in range(N):
    temp = B_list[i]

    for j in range(i, i+2):
        if temp - A_list[j] < 0:
            answer_list[i] += temp
            break
        else:
            answer_list[i] += A_list[j]
            temp -= A_list[j]

print(sum(answer_list))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split())) #len(A) = n+1
B = list(map(int, input().split())) #len(B) = n

def f(X,Y):
  i = 0
  if Y[0] <= X[0]:
    i += Y[0]
    X = X[1:n+1]
  elif X[0] < Y[0] <= X[0]+X[1]:
    i += Y[0]
    X[1] = X[1] - (Y[0] - X[0])
    X = X[1:n+1]
  else:
    i += X[0] + X[1]
    X = X[1:n+1]
    X[0] = 0
  if Y[-1] <= X[-1]:
    X = X[0:n-1]
    i += Y[-1]
  elif X[-1] < Y[-1] <= X[-1] + X[-2]:
    i += Y[-1]
    X[-2] = X[-2] - (Y[-1] - X[-1])
    X = X[0:n-1]
  else:
    i += X[-1] + X[-2]
    X = X[0:n-1]
    X[-1] = 0
  Y = Y[1:n-1]
  return([X,Y,i])

ans = 0
while True:
  if len(B) == 0:
    print(ans)
    exit()
  elif len(B) == 1:
    if A[0] + A[1] <= B[0]:
      print(ans + A[0]+A[1])
      exit()
    else:
      print(ans + B[0])
      exit()
  else:
    X = f(A,B)
    ans += X[2]
    A = X[0]
    B = X[1]

Submission status: Wrong Answer

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]
    else :
        cnt += a[i]
        c = b[i] - a[i]
        if c <= a[i+1]:
            cnt += c
            a[i+1] -= c
        else :
            cnt += a[i+1]
print(cnt)

Submission status: Wrong Answer

Solution code:

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

M = []
for i in range(N):
    M.append(A[i] + A[i+1])
    
ans = 0
for i in range(N):
    ans += min(M[i], B[i])
    
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
apple = list(map(int, input().split()))
orange = list(map(int, input().split()))
count = 0
cat = []
for i in range(n):
    cat.append(apple[i] + apple[i + 1])
for i, j in zip(orange, cat):
    count += min(i, j)
print(count)

Submission status: Wrong Answer

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

before_sumA=sum(A)
before_sumB=sum(B)
for i in range(N):
    if A[i]>=B[i]:
        R=A[i]
        A[i]-=B[i]
        B[i]=0
    elif A[i]+A[i+1]>=B[i]:
        R=A[i]
        A[i]=0
        B[i]-=R
        A[i-1]-=B[i]
        B[i]=0
    else:
        R=A[i]+A[i+1]
        A[i]=0
        A[i+1]=0
        B[i]-=R

after_sumA=sum(A)
after_sumB=sum(B)
#print(before_sumA-after_sumA)
print(before_sumB-after_sumB)

Submission status: Wrong Answer

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

before_sum=sum(A)
for i in range(N):
    if A[i]>=B[i]:
        R=A[i]
        A[i]-=B[i]
        B[i]-=R
    elif A[i]+A[i+1]>=B[i]:
        R=A[i]
        A[i]=0
        B[i]-=R
        A[i-1]-=B[i]
    else:
        A[i]=0
        A[i+1]=0

after_sum=sum(A)

print(before_sum-after_sum)

Submission status: Wrong Answer

Solution code:
import math
import re
import copy
import sys

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

for i in range(n):
    c += min(a[i],b[i])
    if a[i] < b[i]:
        a[i+1] -= b[i]-a[i]
        c += b[i]-a[i]

print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#整数の入力
N = int(input())
#スペース区切りの整数の入力リスト
A_list = list(map(int, input().split()))
#スペース区切りの整数の入力リスト
B_list = list(map(int, input().split()))

count = 0
for i in range(N):
  # もしBよりAの方が大きい場合
  # A[i]しか倒せない
  if(A_list[i] >= B_list[i]):
    count = count + A_list[i]
  # A[i+1]も倒せるけど
  # A[i+1]に残る場合
  elif((B_list[i] - A_list[i]) < A_list[i+1]):
    count = count + (B_list[i] - A_list[i])
    A_list[i+1] = A_list[i+1] - (B_list[i] - A_list[i])
  # A[i+1]も全部倒せる場合
  else:
    count = count + A_list[i+1]
    A_list[i+1] = 0
    
print(count)

Submission status: Wrong Answer

Solution code:
#整数の入力
N = int(input())
#スペース区切りの整数の入力リスト
A_list = list(map(int, input().split()))
#スペース区切りの整数の入力リスト
B_list = list(map(int, input().split()))

count = 0
for i in range(N):
  if(A_list[i] >= B_list[i]):
    count = count + B_list[i]
  elif(A_list[i] - B_list[i] <= A_list[i+1]):
    count = count + A_list[i] - B_list[i]
    A_list[i+1] = A_list[i+1] - (A_list[i] - B_list[i])
  else:
    count = A_list[i+1]
    A_list[i+1] = 0
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
total = 0
for i in range(N): #0 -> N-2, N-1
    # if i==0:
    #     total += min(A[N], B[N-1])
    #     B[N-1] -= min(A[N], B[N-1])
    #     A[N] -= min(A[N], B[N-1])
    #     if B[N-1] <= 0:
    #         pass
    #     else:
    #         total += min(A[N-1], B[N-1])
    #         B[N-1] -= min(A[N-1], B[N-1])
    #         A[N-1] -= min(A[N-1], B[N-1])

    total += min(A[i], B[i])
    B[i] -= min(A[i], B[i])
    A[i] -= min(A[i], B[i])
    if B[i] <= 0:
        continue
    else:
        total += min(A[i+1], B[i])
        B[i] -= min(A[i+1], B[i])
        A[i+1] -= min(A[i+1], B[i])

print(total)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
total = 0
for i in range(N-1): #0 -> N-2, N-1
    if i==0:
        total += min(A[N], B[N-1])
        B[N-1] -= min(A[N], B[N-1])
        A[N] -= min(A[N], B[N-1])
        if B[N-1] <= 0:
            pass
        else:
            total += min(A[N-1], B[N-1])
            B[N-1] -= min(A[N-1], B[N-1])
            A[N-1] -= min(A[N-1], B[N-1])

    total += min(A[i], B[i])
    B[i] -= min(A[i], B[i])
    A[i] -= min(A[i], B[i])
    if B[i] <= 0:
        continue
    else:
        total += min(A[i+1], B[i])
        B[i] -= min(A[i+1], B[i])
        A[i+1] -= min(A[i+1], B[i])

print(total)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
B2 = B.copy()
total = 0
total_2 = 0
for i in range(N): #0 -> N-1
    total += min(A[i], B[i])
    B[i] -= min(A[i], B[i])
    if B[i] <= 0:
        break
    else:
        total += min(A[i+1], B[i])
        B[i] -= min(A[i+1], B[i])

for i in range(N-1, -1, -1): #N-1 -> 0
    total_2 += min(A[i], B2[i])
    B2[i] -= min(A[i], B2[i])
    if B2[i] <= 0:
        break
    else:
        total_2 += min(A[i+1], B2[i])
        B2[i] -= min(A[i+1], B2[i])

print(max(total, total_2))

Submission status: Wrong Answer

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

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

print(total)

Submission status: Wrong Answer

Solution code:
# 街を基準に考える
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0

for i in range(n):
    # 勇者が倒したあとに街に残っているモンスター
    tmp = a[i] - b[i]
    
    # n+1番目のモンスターにまで到達
    if tmp < 0 and -tmp <= a[i+1]:
        ans += b[i]
        a[i+1] += tmp
    # n+1番目のモンスターも駆逐
    elif tmp < 0 and -tmp > a[i+1]:
        ans += a[i]+a[i+1]
        a[i+1] = 0
    # n番目のモンスターで手一杯
    else:
        ans += b[i]

Submission status: Wrong Answer

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

ans = 0

kill_1 = 0
kill_2 = 0

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

Submission status: Wrong Answer

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

ans = 0

kill_1 = 0
kill_2 = 0

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    num = int(input())        
    a_list = list(map(int, input().split()))
    a = sum(a_list)
    b_list = list(map(int, input().split()))

    for i in range(num):
        if a_list[i] >= b_list[i]:
            a_list[i] -= b_list[i]
            b_list[i] = 0
        else:
            b_list[i] -= a_list[i]
            a_list[i] = 0
            if a_list[i+1] >= b_list[i]:
                a_list[i+1] -= b_list[i]
            else:
                a_list[i+1] = 0
        

    print(a - sum(a_list))
    print(a_list)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# For taking integer inputs.
def inp():
    return(int(input()))


# For taking List inputs.
def inlist():
    return(list(map(int, input().split())))


# For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
def instr():
    s = input()
    return(list(s[:len(s)]))


# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))


N = inp()
A = inlist()
B = inlist()
res = 0
b = 0
for i in range(N):

    b += B[i]
    s = A[i] + A[i+1]

    if b < s:
        res += b

        if b > A[i]:
            b -= A[i]
            A[i] = 0
        else:
            A[i] -= b
            b = 0

        if A[i+1] < b:
            b -= A[i+1]
            A[i+1] = 0
        else:
            A[i+1] -= b
            b = 0
    else:
        # b sum er theke boro
        res += s
        b = b - s
        A[i] = A[i+1] = 0

print(res)

Submission status: Wrong Answer

Solution code:
# For taking integer inputs.
def inp():
    return(int(input()))


# For taking List inputs.
def inlist():
    return(list(map(int, input().split())))


# For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
def instr():
    s = input()
    return(list(s[:len(s)]))


# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))


N = inp()
A = inlist()
B = inlist()
killed = 0
res = 0
b = 0
for i in range(N):

    b += B[i]
    s = A[i] + A[i+1]

    if b < s:
        res += b
        temp = A[i] - b
        if temp < 0:
            A[i] = 0
            b = temp * -1
        else:
            A[i] = temp
            b = 0
        temp = A[i+1] - b
        if temp < 0:
            A[i+1] = 0
            b = temp * -1
        else:
            A[i+1] = temp
            b = 0
    else:
        res += s
        b = b - s
        A[i] = A[i+1] = 0

print(res)

Submission status: Wrong Answer

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 += b[i]
    elif b[i] <= a[i] + a[i+1]:
        a[i+1] = a[i+1] - a[i] + b[i]
        ans += b[i]
    else:
        ans += a[i] + a[i+1]
        a[i+1] = 0
print(ans)

Submission status: Wrong Answer

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 += b[i]
    elif b[i] < a[i] + a[i+1]:
        a[i+1] = a[i+1] - a[i] + b[i]
        ans += b[i]
    else:
        ans += a[i] + a[i+1]
        a[i+1] = 0
print(ans)

Submission status: Wrong Answer

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 += b[i]
    elif b[i] < a[i] + a[i+1]:
        a[i+1] = a[i] - b[i]
        ans += b[i]
    else:
        ans += a[i] + a[i+1]
        a[i+1] = 0
print(ans)

Submission status: Wrong Answer

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 += b[i]
    elif b[i] <= a[i] + a[i+1]:
        a[i+1] -= a[i] - b[i]
        ans += b[i]
    else:
        ans += a[i] + a[i+1]
        a[i+1] = 0
print(ans)

Submission status: Wrong Answer

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:
        ans += A[i]
        B[i] -= A[i]
        if A[i + 1] >= B[i]:
            ans += B[i]
            A[i + 1] -= B[i]
        else:
            ans += A[i + 1]
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0]*n
for i in range(n):
    if a[i] + a[i+1] >= b[i]:
        c[i] = b[i]
    else:
        c[i] = a[i] + a[i+1]
        a[i+1] = 0
print(sum(c))

Submission status: Wrong Answer

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 B[i] >= A[i] + A[i+1]:
        count += A[i] + A[i+1]
        A[i+1] = 0
    else:
        A[i+1] = A[i+1] - B[i]
        count += B[i]

print(count)

Submission status: Wrong Answer

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 B[i] >= A[i] + A[i+1]:
        count += A[i] + A[i+1]
        A[i+1] = 0
    else:
        count += B[i]

print(count)

Submission status: Wrong Answer

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 B[i] > A[i] + A[i+1]:
        count += A[i] + A[i+1]
        A[i+1] = 0
    else:
        count += B[i]

print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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],b[i])
    ans += min(a[i+1],b[i]-min(a[i],b[i]))

print(ans)

Submission status: Wrong Answer

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

ans = min(A[0], B[0])
rest = B[0] - ans
  
for i in range(N):
  #rest+B[i+1]でとA[i+1]をどれだけ倒せるか
  curr = min(A[i+1], rest + B[i+1])
  rest = max(0, rest + B[i+1] - curr)
  ans += curr
print(ans)

Submission status: Wrong Answer

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

ans = min(A[0], B[0])
rest = B[0] - ans
  
for i in range(N):
  #rest+B[i+1]でとA[i+1]をどれだけ倒せるか
  curr = min(A[i+1], rest + B[i+1])
  rest = min(0, rest + B[i+1] - curr)
  ans += curr
print(ans)

Submission status: Wrong Answer

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

ans = min(A[0], B[0])
rest = B[0] - ans
  
for i in range(N):
  #rest+B[i+1]でとA[i+1]をどれだけ倒せるか？
  curr = min(A[i+1], rest + B[i+1])
  rest = min(0, B[i+1] - curr)
  ans += curr
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

p = 0
ans = 0
for enemy, power in zip(A, B):
    enemy -= p
    ans += p
    if enemy <= power:
        p = power - enemy
        ans += enemy
    else:
        ans += power
        p = 0
ans += min(p, A[-1])

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10000000)
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue

# import fractions

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()
    B = il()

    ret = 0
    for n in range(N):
        ret += min(A[n] + A[n + 1], B[n])
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input(""))
list1=list(map(int,input("").split(" ")))
list2=list(map(int,input("").split(" ")))
count=0
rem=0

listr=[]

for i in range(len(list2)):
    listr.append(list2[i]-list1[i])

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

for i in range(len(list2)):
    if(list2[i]-list1[i]>0):
        count+=list1[i]
    else:
        count+=list2[i]
rem=list2[len(list1)-2]-list1[len(list1)-2]
if((rem)>0):
    if((rem-list1[len(list1)-1])>0):
        count+=list1[len(list1)-1]
    else:
        count+=rem
print(count)

Submission status: Wrong Answer

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

ans = 0
e = 0
for i, v in enumerate(b):
  if a[i] >= e:
      ans += e
      a[i] -= e
  else:
      ans += a[i]
      a[i] = 0
  if a[i] >= v:
      ans += v
  else:
      ans += a[i]
      e = v - a[i]
ans += min(a[-1], e)
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
t = a[-1]
count = 0

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

print(count)

Submission status: Wrong Answer

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A = IL()
A_copy = A+[]
B = IL()

ans1 = 0
ans2 = 0

for i in range(N):
  if A[i] >= B[i]:
    ans1 += B[i]
  else:
    ans1 += A[i]
    if A[i+1]>B[i]-A[i]:
      ans1 += B[i]-A[i]
      A[i+1] -= B[i]-A[i]
    else:
      ans1 += A[i+1]
      A[i+1] = 0
      
A_copy = A_copy[::-1]
B = B[::-1]
for i in range(N):
  if A[i] >= B[i]:
    ans2 += B[i]
  else:
    ans2 += A[i]
    if A[i+1]>B[i]-A[i]:
      ans2 += B[i]-A[i]
      A[i+1] -= B[i]-A[i]
    else:
      ans2 += A[i+1]
      A[i+1] = 0
print(max(ans1,ans2))