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

cnt=0

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

Submission status: Wrong Answer

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

enemy = [0]*(n+1)
kurikoshi = 0
for i in range(n+1):
  enemy[i] = min(a[i], b[i]+kurikoshi)
  kurikoshi = max(0, b[i]+kurikoshi-enemy[i])

print(sum(enemy))

Submission status: Wrong Answer

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

enemy = [0]*(n+2)
kurikoshi = 0
for i in range(n+1):
  if i < n:
    enemyi = min(a[i], b[i]+kurikoshi)
  else:
    enemyi = min(a[i], kurikoshi)
  enemy[i] = enemyi
  
  if i < n:
    kurikoshi = max(0, b[i]+kurikoshi-enemyi)
print(sum(enemy))

Submission status: Wrong Answer

Solution code:
def resolve():
    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]) #i番目で倒せる数
        ans += min(A[i+1], B[i]-min(A[i],B[i])) #i+1番目で倒せる数
    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
from collections import defaultdict as D
from functools import reduce
import numpy as np
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353    

n = I()
a = LI()
b = LI()

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

Submission status: Wrong Answer

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

res = 0

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

print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
AL = list(map(int, input().split()))
BL = list(map(int, input().split()))
c = 0

for i in range(n):
  if AL[i] < BL[i]:
    d = BL[i] - AL[i]
    if AL[i+1] < d:
      c = AL[i] + AL[i+1] + c
      AL[i+1] = 0
    else:
      c = AL[i] + d + c
      AL[i+1] = AL[i+1] - d
  else:
    c = BL[i]
print(c)

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import deque
N = int(input())
A_list = np.array(list(map(int, input().split())))
B_list = np.array(list(map(int, input().split())))
monsters = A_list.sum()
A_list = np.append(0, A_list)
B_list = np.append(0, B_list)
B_list = np.append(B_list,0)
#配列を作って、そこにAi-Bi-(max(0,Bi-1-Ai-1)をつっこんでいく
#i番目の町で倒されたあとの怪獣の数をカウントしていくイメージ
#最後に配列のマイナスを全て0に統一して、最初の怪獣数との差分を出せば、合計討伐数が算出される

beated_list = np.zeros(N+1)

for i in range(1,N+2):
    beated_list[i-1] = A_list[i]-B_list[i]-max(0, B_list[i-1]-A_list[i-1])
    A_list[i] = max(0, A_list[i]-(B_list[i-1]-A_list[i-1]))
beated_list = np.where(beated_list < 0, 0, beated_list)
rest_monsters = beated_list.sum()
ans = int(monsters - rest_monsters)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import deque
N = int(input())
A_list = np.array(list(map(int, input().split())))
B_list = np.array(list(map(int, input().split())))
monsters = A_list.sum()
A_list = np.append(0, A_list)
B_list = np.append(0, B_list)
B_list = np.append(B_list,0)
#配列を作って、そこにAi-Bi-(max(0,Bi-1-Ai-1)をつっこんでいく
#i番目の町で倒されたあとの怪獣の数をカウントしていくイメージ
#最後に配列のマイナスを全て0に統一して、最初の怪獣数との差分を出せば、合計討伐数が算出される

beated_list = np.zeros(N+1)

for i in range(1,N+2):
    beated_list[i-1] = A_list[i]-B_list[i]-max(0, B_list[i-1]-A_list[i-1])
    A_list[i] = max(0, A_list[i]-(B_list[i-1]-A_list[i-1]))
    print(beated_list)
beated_list = np.where(beated_list < 0, 0, beated_list)
rest_monsters = beated_list.sum()
ans = int(monsters - rest_monsters)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import deque
N = int(input())
A_list = np.array(list(map(int, input().split())))
B_list = np.array(list(map(int, input().split())))
monsters = A_list.sum()
A_list = np.append(0, A_list)
B_list = np.append(0, B_list)
B_list = np.append(B_list,0)
#配列を作って、そこにAi-Bi-(max(0,Bi-1-Ai-1)をつっこんでいく
#i番目の町で倒されたあとの怪獣の数をカウントしていくイメージ
#最後に配列のマイナスを全て0に統一して、最初の怪獣数との差分を出せば、合計討伐数が算出される

beated_list = np.zeros(N+1)

for i in range(1,N+2):
    beated_list[i-1] = A_list[i]-B_list[i]-max(0, B_list[i-1]-A_list[i-1])
    A_list[i] -= max(0, B_list[i-1]-A_list[i-1])
beated_list = np.where(beated_list < 0, 0, beated_list)
rest_monsters = beated_list.sum()
ans = int(monsters - rest_monsters)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import deque
N = int(input())
A_list = np.array(list(map(int, input().split())))
B_list = np.array(list(map(int, input().split())))
monsters = A_list.sum()
A_list = np.append(0, A_list)
B_list = np.append(0, B_list)
B_list = np.append(B_list,0)
#配列を作って、そこにAi-Bi-(max(0,Bi-1-Ai-1)をつっこんでいく
#i番目の町で倒されたあとの怪獣の数をカウントしていくイメージ
#最後に配列のマイナスを全て0に統一して、最初の怪獣数との差分を出せば、合計討伐数が算出される

beated_list = np.zeros(N+1)

for i in range(1,N+2):
    beated_list[i-1] = A_list[i]-B_list[i]-max(0, B_list[i-1]-A_list[i-1])
    A_list[i] -= max(0, B_list[i-1]-A_list[i-1])
beated_list = np.where(beated_list < 0, 0, beated_list)
rest_monsters = beated_list.sum()
ans = monsters - rest_monsters
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)

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

Submission status: Wrong Answer

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

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

print(defeat_monster)

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):
    t = min(a[i], b[i])
    cnt += t
    a[i] -= t
    b[i] -= t

    s = min(a[i+1], b[i])
    cnt += s
    a[i] -= s
    b[i] -= s

print(cnt)

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):
    t = min(a[i], b[i])
    cnt += t
    b[i] -= t

    s = min(a[i+1], b[i])
    cnt += s
    b[i] -= s

print(cnt)

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

ans = 0

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

ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
N,A,B=open(0).read().rstrip().split('\n')
N=int(N)
A=list(map(int,A.split()))
B=list(map(int,B.split()))
A.sort(reverse=True)
B.sort(reverse=True)
hunt=0
for i in range(N):
    if A[i]>=B[i]:
        hunt+=B[i]
    elif A[i]+A[i+1]>=B[i]:
        hunt+=B[i]
        suplus=B[i]-A[i]
        A[i+1]=A[i+1]-suplus
    else:
        hunt+=A[i]+A[i+1]
        A[i+1]=0
print(hunt)

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()]
cnt = 0
for i in range(n):
    dm = min(a[i], b[i])
    a[i] -= dm; b[i] -= dm; cnt += dm
    dmn = min(a[i + 1], b[i])
    a[i] -= dmn; b[i] -= dmn; cnt += dmn
print(cnt)

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()]
cnt = 0
for i in range(n):
    cnt += min(a[i], b[i])
    b[i] -= min(a[i], b[i])
    cnt += min(a[i + 1], b[i])
    a[i] -= min(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()))
cnt = 0
for i in range(n):
    if a[i] >= b[i]:
        cnt += b[i]
    else:
        cnt += a[i]
        b[i] -= a[i]
        cnt += min(b[i],a[i+1])
        a[i+1] += b[i] - a[i]
    #print("i=",i,a,b)
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
C = tuple(map(int,input().split()))
B = list(map(int,input().split()))
A = list(C)
for i in range(N)[::-1]:
    if A[i+1] > B[i]:
        A[i+1] -= B[i]
    elif B[i] < A[i]+A[i+1]:
        A[i+1] = 0
        A[i] -= B[i] - A[i+1]
    else:
        A[i] = 0
        A[i+1] = 0
ans = 0
for j in range(N+1):
    ans += C[j]-A[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
for i in range(n):
    ans += min(b[i], a[i]+a[i+1])
    a[i+1] = max(0, a[i+1]-(b[i]-a[i]))

print(ans)

Submission status: Wrong Answer

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

for i in range(len(A)-1):
    t = A[i] + A[i+1]
    l.append(t)

for j in range(len(B)):
    if l[j] >= B[j]:
        ans += B[j]
    else:
        ans += l[j]

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

N = int(sys.stdin.readline())
As = [int(x) for x in sys.stdin.readline().split(" ")]
Bs = [int(x) for x in sys.stdin.readline().split(" ")]

result = 0
for i in range(0, N):
    i = (N - 1) - i
    B = Bs[i]

    right = As[i + 1]
    right_result = min(B, right)
    result += right_result
    B -= right_result

    left = As[i]
    left_result = min(B, left)
    result += left_result
    B -= left_result

print(result)

Submission status: Wrong Answer

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

ans = 0
rest = 0
for a, b in zip(B, A):
  b_ = b - rest
  if b_ > a:
    rest = 0
    ans += rest + a
  elif b_ > 0:
    rest = a - b_
    ans += b
  elif b_ <= 0:
    rest = a
    ans += b
b = A[-1]
if rest > b:
  ans += b
else:
  ans += rest
print(ans)

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 A[i] != 0:  # 担当の街
        if B[i] >= A[i]:
            B[i] -= A[i]
            count += A[i]
            A[i] = 0
        else:
            A[i] -= B[i]
            count += B[i]
    if A[i+1] <= B[i]:  # 隣の街
        count += A[i+1]
        A[i+1] = 0
    else:
        A[i+1] -= B[i]
        count += B[i]

print(count)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 15 05:49:43 2020

@author: Dell
"""

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

Submission status: Wrong Answer

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

remain=max(0,B[0]-A[0])
mons=min(A[0],B[0])
for n in range(1,N):
    mons+=min(remain+B[n],A[n])
    remain=max(B[n]+remain-A[n],0)
mons+=min(remain,A[N])
print(mons)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    alis = list(map(int, input().split()))
    blis = list(map(int, input().split()))
    ans = 0

    for i in range(n-1, -1, -1):
        if blis[i] <= alis[i+1]:
            ans += blis[i]
        elif blis[i] < alis[i+1] + alis[i]:
            ans += blis[i]
            alis[i] = blis[i] - alis[i+1] 
        elif blis[i] >= alis[i+1] + alis[i]:
            ans += alis[i] + alis[i+1]
            alis[i] = 0
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    alis = list(map(int, input().split()))
    blis = list(map(int, input().split()))
    ans = 0

    for i in range(n-1, -1, -1):
        if blis[i] <= alis[i+1]:
            ans += alis[i+1]
        elif blis[i] < alis[i+1] + alis[i]:
            ans += blis[i]
            alis[i] = blis[i] - alis[i+1] 
        elif blis[i] >= alis[i+1] + alis[i]:
            ans += alis[i] + alis[i+1]
            alis[i] = 0
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
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])
    c += a[i+1] - max(a[i+1] - max(b[i]-a[i], 0), 0)
    a[i+1] = max(a[i+1] - max(b[i]-a[i], 0), 0)
if a[n-1] >= b[n-1]:
    print(c)
else:
    c += min(a[n], b[n-1]-a[n-1])
    print(c)

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], B[i])
    B[i] = max(0, B[i] - A[i + 1])
    cnt += min(A[i], B[i])
    A[i] = max(0, A[i] - 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()))

print(A,B)
add = 0
for i in range(len(B)):
    if B[i] > A[i]:
        add += A[i]
        B[i] -=  A[i]
        if A[i+1] > B[i]:
            add+=B[i]
            A[i+1] -= B[i]
        else:
            add += A[i+1]
            A[i+1] = 0
    else:
        add+=B[i]
        B[i] = 0
    #print(A,B,add)
    
#add += min(A[-1],B[-1])
print(add)

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

Submission status: Wrong Answer

Solution code:
dat = []
n = int(input())
for i in range(2):
    std_in = list(map(int, input().split()))
    dat.append(std_in)

b_res = 0  # 倒せる数のi+1番目の都市への繰越
killed = 0  # 累計で倒した数
a_last = dat[0][n]  # N+1番目の都市のモンスター数

for i, z in enumerate(zip(dat[0], dat[1])):
    a = z[0]
    b = z[1] + b_res  # i番目の都市で倒せる数とi-1番目の都市からの繰越の合計
    killed += min(a, b)  # モンスター数と倒せる数の小さい方
    b_res = max(b - a, 0)  # 倒せる数のi+1番目の都市への繰越（使い切ったら0）

# N+1番目の都市の計算
killed += min(a_last, b_res)
print(killed)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = 0
    tail = A[N]
    idx = N-1
    while B:
        brave = B.pop()
        if brave >= tail + A[idx]:
            ans += tail + A[idx]
            tail = 0
        else:
            ans += brave
            tail = tail+A[idx]-brave
        idx -= 1
    print(ans)



    
if '__main__' == __name__:
    resolve()

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

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    for i in range(n):
        for j in range(i, i+2):
            if a[j] >= b[i]:
                a[j] -= b[i]
                ans += b[i]
                break
            else:
                ans += a[j]
                b[i] -=  a[j]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    t = 0
    for i in range(n):
        for j in range(i, i+2):
            if a[j] >= b[i]:
                a[j] -= b[i]
                ans += b[i]
                break
            else:
                ans += a[j]
                a[j] = 0
                b[i] -=  a[j]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

def main():
    ans = 0
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    t = 0
    for i in range(n):
        for j in range(i, i+2):
            if a[j] >= b[i]:
                ans += b[i]
                break
            else:
                ans += a[j]
                b[i] -=  a[j]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())

a = list(map(int, input().split()))
b = list(map(int, input().split()))
total = 0
cnt = 0

for i in range(n):
    if a[i] - (b[i] - cnt) >= 0:
        total += b[i] - cnt
        cnt = min(b[i], a[i] - (b[i] - cnt))
    else:
        total += a[i]
        cnt = max(-b[i], a[i] - (b[i] - cnt))

if cnt < 0:
    total += min(a[n], abs(cnt))

print(total)

Submission status: Wrong Answer

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

ans = sum(a)
monster = 0
advantage = [0]

for i in range(n):
    if b[i] - a[i] >= 0:
        if advantage[i] - a[i] >= 0:
            advantage.append(b[i])
        else:
            a[i] -= advantage[i]
            advantage.append(b[i] - a[i])
    elif (advantage[i] + b[i]) - a[i] >= 0:
        if advantage[i] -a[i] >= 0:
            advantage.append(b[i])
        elif advantage[i] + b[i] - a[i] >= 0:
            advantage.append(b[i] -a[i])
    elif advantage[i] + b[i] - a[i] < 0:
            monster += abs(advantage[i] + b[i] - a[i])
            advantage.append(0)
    else:
        monster += abs(b[i] - a[i])
        advantage.append(0)

if advantage[-1] - a[-1] >= 0:
    print(ans -monster)
else:
    print(ans -monster + advantage[-1] -a[-1])

Submission status: Wrong Answer

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

ans = sum(a)
monster = 0
advantage = [0]

for i in range(n):
    if b[i] - a[i] >= 0:
        if advantage[i] - a[i] >= 0:
            advantage.append(b[i])
        else:
            a[i] -= advantage[i]
            advantage.append(b[i] - a[i])
    elif (advantage[i] + b[i]) - a[i] >= 0:
        if advantage[i] -a[i] >= 0:
            advantage.append(b[i])
        elif advantage[i] + b[i] - a[i] >= 0:
            advantage.append(b[i] -a[i])
        else:
            monster += advantage[i] + b[i] - a[i]
            advantage.append(0)
    else:
        monster += abs(b[i] - a[i])
        advantage.append(0)

if advantage[-1] - a[-1] >= 0:
    print(ans -monster)
else:
    print(ans -monster + advantage[-1] -a[-1])

Submission status: Wrong Answer

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

ans = 0
monster = 0
advantage = [0]

for i in range(n):
    if b[i] - a[i] >= 0:
        if n >= 1:
            advantage.append(advantage[i] + b[i] - a[i] -advantage[i -1])
        else:
            advantage.append(b[i] - a[i])
    elif (advantage[i] + b[i]) - a[i] >= 0:
        if advantage[i] -a[i] >= 0:
            advantage.append(b[i])
        elif advantage[i] + b[i] - a[i] >= 0:
            advantage.append(b[i] -a[i])
        else:
            monster += advantage[i] + b[i] - a[i]
            advantage.append(0)
    else:
        monster += abs(b[i] - a[i])
        advantage.append(0)

if advantage[-1] - a[-1] >= 0:
    print(sum(a) -monster)
else:
    print(sum(a) -monster + advantage[-1] -a[-1])

Submission status: Wrong Answer

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

ans = 0
monster = 0
advantage = [0]

for i in range(n):
    if b[i] - a[i] >= 0:
        if n >= 2:
            advantage.append(advantage[i] + b[i] - a[i] -advantage[i -1])
        else:
            advantage.append(b[i] - a[i])
    elif (advantage[i] + b[i]) - a[i] >= 0:
        if advantage[i] -a[i] >= 0:
            advantage.append(b[i])
        elif advantage[i] + b[i] - a[i] >= 0:
            advantage.append(b[i] -a[i])
        else:
            monster += advantage[i] + b[i] - a[i]
            advantage.append(0)
    else:
        monster += abs(b[i] - a[i])
        advantage.append(0)

if advantage[-1] - a[-1] >= 0:
    print(sum(a) -monster)
else:
    print(sum(a) -monster + advantage[-1] -a[-1])

Submission status: Wrong Answer

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

ans = 0
monster = 0
advantage = [0]

for i in range(n):
    if b[i] - a[i] >= 0:
        advantage.append(advantage[i] + b[i] - a[i])
    elif (advantage[i] + b[i]) - a[i] >= 0:
        if advantage[i] -a[i] >= 0:
            advantage.append(b[i])
        elif advantage[i] + b[i] - a[i] >= 0:
            advantage.append(b[i] -a[i])
        else:
            monster += advantage[i] + b[i] - a[i]
            advantage.append(0)
    else:
        monster += abs(b[i] - a[i])
        advantage.append(0)

if advantage[-1] - a[-1] >= 0:
    print(sum(a) -monster)
else:
    print(sum(a) -monster + advantage[-1] -a[-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 A[i] > B[i]:
        cnt += B[i]
        A[i] -= B[i]
        B[i] = 0
        
    else:
        cnt += A[i]
        A[i] = 0
        B[i] -= A[i]
        
            
    if A[i+1] > B[i]:
        cnt += B[i]
        A[i+1] -= B[i]
        B[i] = 0
        
    else:
        cnt += A[i+1]
        A[i+1] = 0
        B[i] -= A[i+1]
        
        
       
cnt += min(A[-1],B[-1]) 


print(cnt)

Submission status: Wrong Answer

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

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

A[-1] = max(A[-1]-B[-1],0)


print(cnt_mons - sum(A))

Submission status: Wrong Answer

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

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

print(cnt_mons - sum(A))

Submission status: Wrong Answer

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

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

print(cnt_mons - sum(A))

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):
    if A[i] != 0:
        if A[i] <= B[i]:
            total += A[i]
            B[i] -= A[i]
            A[i] = 0
        else:
            total += B[i]
            A[i] -= B[i]
            B[i] = 0
    if B[i] != 0:
        if A[i+1] <= B[i]:
            total += A[i+1]
            B[i] -= A[i+1]
            A[i] = 0
        else:
            total += B[i]
            A[i+1] -= B[i]
            B[i] = 0

print(total)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

count = 0
plus = 0

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

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

n = readInt()
a = readInts()
b = readInts()
b.append(0)
b.append(0)

s = sum(a)

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

print(s-sum(a))

Submission status: Wrong Answer

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

ans = 0
if len(A) == 1:
    print(min(A, B))
    exit()

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()))
Ans = 0
for i in range(N):
    if A[i] + A[i+1] < B[i]:
        Ans += A[i]
        A[i+1] = 0
    elif A[i] >= B[i]:
        Ans += B[i]
    else:
        Ans += B[i]
        A[i+1] -= B[i] - A[i]
print(Ans)