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=input().split(" ")
b=input().split(" ")
A=[]
B=[]
for i in a:
    A.append(int(i))
for i in b:
    B.append(int(i))
count=0
roof=0
for i in range(len(A)-1):
    enemy=A[i]
    if(roof>0):
        if(enemy>roof):
            count+=roof
            enemy-=roof
        else:
            count+=enemy
            enemy=0
            roof=0
    if(enemy<B[i]):
        count+=enemy
        roof=B[i]-enemy
    else:
        count+=B[i]
enemy=A[-1]
if(roof>0):
    if(enemy>roof):
        count+=roof
        enemy-=roof
    else:
        count+=enemy

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

A_0 = A_list.pop(0)
#print(A_0)
#print(A_list)
A_list[0] = A_list[0] + A_0
#print(A_list)

taos = 0
nokori = 0
keep = 0

for a, b in zip(A_list, B_list):
  if (a + nokori) >= (b + keep) :
    taos += (b + keep)
    nokori = (a + nokori) - (b + keep)
    keep = 0
  else:
    taos += (a + nokori)
    nokori = 0
    keep = (b + keep) - (a + nokori)
  #print(taos, nokori, keep)
    
print(taos)

Submission status: Wrong Answer

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

#dp[i] = dp[i-1]

target = a[0]
ans = 0

for i in range(n):
#    print("\n# i: " + str(i))

    if b[i] > target:
#        print("# Terminate all!")
        ans += target
        res = b[i] - target
        ans += min(res, a[i+1])
        target = res - a[i+1]
        if target < 0:
            target = 0

#        print("# ans: " + str(ans))
    else:
#        print("# Do my best...")
        ans += b[i]
        target = a[i+1]
#        print("# ans: " + str(ans))

print(ans)

Submission status: Wrong Answer

Solution code:
import copy
N = int(input())
liA = list(map(int,input().split()))
liB = list(map(int,input().split()))
liC = copy.deepcopy(liA)
for i in range(len(liB)):
    if liA[i] >= liB[i]:
        liA[i] = liC[i]-liB[i]
    elif liB[i] - (liA[i]+liA[i+1]) <= 0:
        liA[i+1] = liC[i+1] + liA[i] - liB[i]
        liA[i] = 0
    else:
        liA[i] = 0
        liA[i+1] = 0
print(sum(liC)-sum(liA))

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

totalMonster = 0
kurikoshi = 0
for i in range(N + 1):
    if i == N:
        if kurikoshi >= A[i]:
            totalMonster = totalMonster + A[i]
        else:
            totalMonster = totalMonster + kurikoshi
        break

    if kurikoshi >= A[i]:
        totalMonster = totalMonster + A[i]
        kurikoshi = B[i]
    elif kurikoshi + B[i] >= A[i]:
        totalMonster = totalMonster + A[i]
        kurikoshi = kurikoshi + B[i] - A[i]
    else:
        totalMonster = kurikoshi + B[i]
        kurikoshi = 0

print(totalMonster)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

killed = 0
attack = 0
for i in range(0, n + 1):
  if i != n:
  	attack += heros[i]
  if (monsters[i] - attack) < 0:
    killed += monsters[i]
    attack -= monsters[i]
  elif (monsters[i] - attack) > 0:
    killed += attack
    attack = 0
  elif (monsters[i] - attack) == 0:
    killed += monsters[i]
    attack = 0

print(killed)

Submission status: Wrong Answer

Solution code:

def main():
	N = int(input()) 
	A = [int(f) for f in input().split()]
	B = [int(f) for f in input().split()]
	
	m = 0
	for i in range(N):
		A[i] = A[i] - B[i]
		m += B[i]
		if A[i] < 0:
			B[i] = -A[i]
			m += A[i]
			A[i] = 0
		if B[i] > 0:
			A[i+1] = A[i+1] - B[i]
			m += B[i]
			if A[i+1] < 0:
				B[i] = -A[i+1]
				m += A[i+1]
				A[i+1] = 0
	
	print(m)

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

A_0 = A_list.pop(0)
#print(A_0)
#print(A_list)
A_list[0] = A_list[0] + A_0
#print(A_list)

taos = 0
nokori = 0
keep = 0

for a, b in zip(A_list, B_list):
  if (a + nokori) >= (b + keep) :
    taos += (b + keep)
    nokori = (a + nokori) - (b + keep)
    keep = 0
  else:
    taos += (a + nokori)
    nokori = 0
    keep = b - a
  #print(taos, nokori, keep)
    
print(taos)

Submission status: Wrong Answer

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

count = 0
for num in range(N):
    if B[num] > A[num] + A[num + 1] :
        count += A[num] + A[num + 1]
        A[num + 1] = 0
    else:
        count += B[num]
        A[num + 1] = max(A[num] + A[num + 1] - B[num], 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):
    if a[i] > b[i]:
        total += b[i]
    else:
        if a[i+1] >= b[i] - a[i]:
            a[i+1] -= (b[i] - a[i])
            total += b[i]
        else:
            a[i+1] == 0
            total += a[i] + a[i+1]
print(total)

Submission status: Wrong Answer

Solution code:
N = int(input()) 
B= [int(x) for x in input().split()]
A= [int(x) for x in input().split()]
all_ene = sum(B)
c = 0
for i in range(N):
  if B[i] >= A[i]:
    B[i] = B[i]-A[i]
  else:
    B[i] = 0
    B[i+1] = max(0,B[i+1]+B[i]-A[i])
    
print(all_ene-sum(B))

Submission status: Wrong Answer

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

for i in range(len(a)-2,-1,-1):
    c = c + a[i]
    d = c
    c = max(c-b[i],0)
    enemy = enemy + d-c
    
print(enemy)

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()]
b.append(0)
d=[0]*(n+2)
for i in range(n):
    d[i+1]=min(a[i]+a[i+1],b[i]+d[i])
print(d)

Submission status: Wrong Answer

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

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

totalMonster = 0
kurikoshi = 0
for i in range(N + 1):
    if i == N:
        if kurikoshi >= A[i]:
            totalMonster = totalMonster + A[i]
        else:
            totalMonster = totalMonster + kurikoshi
        break

    if kurikoshi >= A[i]:
        totalMonster = totalMonster + A[i]
        kurikoshi = B[i]
    elif kurikoshi + B[i] >= A[i]:
        totalMonster = totalMonster + A[i]
        kurikoshi = kurikoshi + B[i] - A[i]
    else:
        totalMonster = kurikoshi + B[i]
print(totalMonster)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.reverse()
B.reverse()
ans = 0
t = A[0]
for a, b in zip(A[1:], B):
    t += a
    m = min(t, b)
    ans += m
    t -= m if t - a <= b else 0

print(ans)

Submission status: Wrong Answer

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

if As[0]>=Bs[0]:
    cnt = Bs[0]
    yo = 0
else:
    cnt = As[0]
    yo = min(Bs[0],Bs[0]-As[0])
    
for i in range(1,N):
    if As[i]-yo>=Bs[i]:
        cnt += Bs[i]
        yo = 0
    else:
        cnt += As[i]
        yo = min(Bs[i],Bs[i]-(As[i]-yo))
        
cnt += min(As[N], yo)
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
rev_a = list(reversed(a))
rev_b = list(reversed(b))
stock = 0
getted = 0

for i in range(n):
  #Be careful that rev_a size is N+1 !
  if stock > 0:
    if rev_a[i] >= stock:
      getted += stock
      rev_a[i] = rev_a[i] - stock
    else:
      getted += rev_a[i]
      rev_a[i] = 0
    stock = 0
  
  if rev_a[i] >= rev_b[i]:
    getted += rev_b[i]
  else:
    getted += rev_a[i]
    stock = rev_b[i] - rev_a[i]
  
  if i == n-1:
    getted += stock

print(getted)

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):
  p=B[i]
  
  #town1
  if p<=A[i]:
    p=0
    A[i]-=p
  else:
    p-=A[i]
    A[i]=0
  #town2
  if p!=0:
    if A[i+1]<p:
      p-=A[i+1]
      A[i+1]=0
    else:
      p=0
      A[i+1]-=p
      
  C+=B[i]-p
  
print(C)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in 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 += A[i+1]
            A[i+1] = 0
        else:
            ans += B[i]
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
list_a=list(map(int, input().split()))  #リスト入力
list_b=list(map(int, input().split()))  #リスト入力

count = 0
list_b.append(0)
yuu_amari = 0
for i in range(n):
    if list_a[i] > yuu_amari:
        mon_now = list_a[i] - yuu_amari
    else:
        mon_now = 0
    yuu_now = list_b[i]
    count += min(mon_now, yuu_now)
    
    if yuu_now - mon_now > 0:
        yuu_amari = yuu_now - mon_now
        
        count += min(list_a[i+1], yuu_amari)
print(count)

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

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

print(enemy)

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] > b[i]:
        total += b[i]
    else:
        if a[i+1] >= b[i] - a[i]:
            a[i+1] -= (b[i] - a[i])
            total += b[i]
        else:
            total += a[i] + a[i+1]
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
tmp = 0
B.append(0)
for i in range(N + 1):
    if B[i] + tmp >= A[i]:
        ans += A[i]
        tmp = B[i] - A[i] + tmp
    else:
        ans += tmp + B[i]
        tmp = 0
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()]

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) and count != sum(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: 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]
        ans += min(b[i] - a[i], a[i + 1])
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
prev = 0
count = 0
for i, a in enumerate(A):
  if i >= N:
    if A[i] < prev:
      count += A[i]
      continue
    else:
      count += prev
      continue

  if prev >= a:
    count += a
    prev = B[i]
    continue
  
  if prev + B[i] >= a:
    count += a
    temp_a = a - prev
    prev = B[i] - temp_a
    continue
  
  if a > B[i] :
    count += B[i]
    prev =0
  
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] >= 0):
        a[i] -= b[i]
        count += b[i]
    else:
        b[i] -= a[i]
        count += a[i]
        a[i] = 0
    
    if(a[i+1] - b[i] >= 0):
        a[i+1] -= b[i]
        count += b[i]
    else:
        # b[i] -= a[i+1]
        count += a[i+1]
        a[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()))
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: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
Mod = 1000000007



def main(): #startline-------------------------------------------
    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+1]+A[i],B[i])
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================




def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Wrong Answer

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

total_mons = 0
c_over = 0

if B[0] < A[0]:
    total_mons += B[0]
else:
    total_mons += A[0]
    c_over = B[0] - A[0]


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

if c_over < A[N]:
    total_mons += c_over
else:
    total_mons += A[N]
print(total_mons)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
N =int(input())
A= np.array( list(map(int,input().split())))
B= np.array( list(map(int,input().split())))
result = 0
for i in range(1,N+1):
    if B[N-i]> A[N-i+1]:
        result+= A[N-i+1]
        B[N-i] +=-A[N-i+1]
        if B[N-i]>A[N-i]:
            result+=A[N-i]
            B[N-i] +=-A[N-i]
        else:
            result+= B[N-i]
            A[N-i]+=-B[N-i]
    else:
        result+= B[N-i]
        A[N-i+1]+=-B[N-i]
print(result)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

N = int(input())

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

B = np.sort(B)[::-1]

AA = np.zeros(N)
for i in range(N):
    AA[i] = A[i] + A[i+1]

#print('AA')
#print(AA)
#print(np.argsort(AA))
#print(np.argsort(AA)[::-1])
#/print(AA)
#print('B')
#print(B)
#print(B[np.argsort(AA)[::-1]])

B = B[np.argsort(AA)[::-1]]

B = np.append(B,0)
B = np.append(B,0)
summ = 0


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

Submission status: Wrong Answer

Solution code:
from enum import Enum
from queue import Queue
import collections

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

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

for i in range(N):
#    print('i=%d'%(i))
    if A[i] >= B[i]:
        kill += B[i]
#        print(kill)
        A[i] -= B[i]
        B[i] = 0
    else:#A[i] < B[i]
        tmp = A[i]
        kill += tmp
#        print(kill)
        B[i] -= tmp
        if A[i+1] >= B[i]:
            kill += B[i]
#            print(kill)
            A[i+1] -= B[i]
        else:#A[i+1] < B[i]
            tmp = A[i+1]
            kill += tmp
            B[i] -= tmp


print(kill)

Submission status: Wrong Answer

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

counter=0
for i in range(N):
    a=A[i]+A[i+1]
    b=B[i]
    if a<=b:
        counter +=a
        A[i+1]=0
    elif a-b>A[i]:
        counter +=b
        A[i+1]=a-b
    else:
        counter +=b
print(counter)

Submission status: Wrong Answer

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

if As[0]>=Bs[0]:
    cnt = Bs[0]
    yo = 0
else:
    cnt = As[0]
    yo = Bs[0]-As[0]
    
for i in range(1,N):
    if As[i]-yo>=Bs[i]:
        cnt += Bs[i]
        yo = 0
    else:
        cnt += As[i]
        yo = min(Bs[i],Bs[i]-(As[i]-yo))
        
cnt += min(As[N], yo)
print(cnt)

Submission status: Wrong Answer

Solution code:
def INT_(n): return int(n)-1
def MI(): return map(int, input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_, input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n: int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split())) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')


def main():
    N = I()
    A = LI()
    B = LI()

    total = 0

    res = 0
    for i in range(N):
        monster = A[i] - res
        if monster >= B[i]:
            total += B[i] + res
            res = 0
        else:
            total += A[i]
            res = B[i] - monster
    total += min(res, A[N])
    print(total)


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())) +[0]

miss = 0
ex = 0
target = 0

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

print(sum(a) - miss)

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

t = 0

for i in range(n):
    if i != n-1:
    #自分の担当箇所に全力投球
        a[i+1] -= max(0,(b[i]-a[i])) #残ったらその分を隣に回す
        t += min(b[i],a[i]) + max(0,(b[i]-a[i]))
        #print(i,t)
    else:
        if b[i] < a[i]:
            t += b[i]
        else:
            t += (a[i] + min((b[i]-a[i]),a[i+1]))#残りと最後のやつの比較

        #print(i,t)

        #t += min(b[n-1],)
print(t)

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 reversed(range(0, N)):
    diff = A[i+1] - B[i]
    if(diff >= 0):
        count += B[i]
    else:
        B[i] -= A[i+1]
        count += A[i+1]
    diff = A[i] - B[i]
    if(diff >= 0):
        A[i] -= B[i]
        count += B[i]
    else:
        count += A[i]
        A[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):
    ab = a[i] - b[i]
    
    if ab < 0:
        ans += a[i]
        b[i] -= a[i]
        a[i] = 0
        b[i] = abs(ab)
        ab = a[i+1] - b[i]
        
        if ab <= 0:
            ans += a[i+1]
            a[i+1] = 0
            b[i] = abs(ab) 
        else:
            ans += b[i]
           
    elif ab == 0:
        ans += b[i]
        a[i] = 0
        b[i] = 0
        
    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)

c = 0

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

Submission status: Wrong Answer

Solution code:
import sys
def main():
    input = sys.stdin.readline

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

    res = 0
    for i in range(N):
        a = A[i]
        b = B[i]

        if a >= b:
            res += b
            A[i] = A[i] - b
        elif a < b:
            res += a
            A[i] = A[i] - a
            a_nx = A[i + 1]
            b -= a
            if a_nx >= b:
                res += b
                A[i + 1] = A[i + 1] - b
            elif a_nx < b:
                res += a_nx
                A[i + 1] = A[i + 1] - a_nx
    print(res)

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

count = 0
rem = 0
for i in range(N):
    if A[i] >= B[i] + rem:
        count += B[i] + rem
        rem = 0
    elif A[i] <= rem:
        count += A[i]
        rem = B[i]
    else:
        count += A[i]
        rem = B[i] +rem - A[i]
        
    print(str(i)+"回目")
    print(count)
        
if A[N] < rem:
    count += A[N]
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
for i in range(N):
  sum1 = A[i]+A[i+1]
  ans += min(B[i],sum1)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = list(int(_) for _ in input().split()) 
b = list(int(_) for _ in input().split()) 

cnt = min(a[0],b[0])
if b[0] > a[0]:
	tmp = b[0] - a[0]
else:
	tmp = 0

for i in range(1,n):

	if tmp >= a[i]:
		tmp =  b[i]
		cnt += a[i]
	elif b[i]+tmp >= a[i]:
		tmp = b[i]+tmp - a[i]
		cnt += a[i]
	else:
		tmp = 0
		cnt += b[i]+tmp

if tmp > 0:
	cnt += min(a[n],tmp)

print(cnt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

b = B[0]
a = b - A[0]
if a > 0:
    ans += A[0]
    work = A[1] - a
    if work <= 0:
        ans += A[1]
        work = float("INF")
    else:
        ans += a
else:
    ans += b
    work = float("INF")

for i in range(1, N):
    b = B[i]
    a = b - min(A[i], work)
    if a > 0:
        ans += min(A[i], work)
        work = A[i + 1] - a
        if work <= 0:
            ans += A[i + 1]
            work = float("INF")
        else:
            ans += a
    else:
        ans += b
        work = float("INF")

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

for i in range(N+1):
  target = A[i] - can_kill
  if i != N:
    if target <= B[i]:
      count += A[i]
      can_kill = B[i] - target
    else:
      count += B[i]+can_kill
      can_kill = 0
  else:
    if A[i] > can_kill:
      count += can_kill
    else:
      count += 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()))
Mon = 0
for i in range(N):
    C = A[i] + A[i+1]
    BB = B[i]
    if C >= BB:
        Mon += BB
        A[i] = 0
    else:
        Mon += C
        C2 = B[i] - A[i]
        A[i] = A[i+1]-C2
print(Mon)

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

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(reversed(list(map(int,input().split()))))
brr = list(reversed(list(map(int,input().split()))))

cur=0
ans=0
for i in range(n):
    if cur >= arr[i]:
        ans+=arr[i]
        a=0
    else:
        ans+=cur
        a=arr[i]-cur
    if brr[i] >= a:
        ans+=a
        cur=brr[i]-a
    else:
        ans+=brr[i]
        cur=0
ans+=min(arr[0],cur)
print(ans)

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

r_monster = 0
r_yusya = 0
monster_cnt = 0

if A[-1] > B[-1]:
    monster_cnt += B[-1]
    B[-1] = 0
else:
    monster_cnt += A[-1]
    B[-1] -= A[-1]
    A[-1] = 0
for a, b in zip(reversed(A[:-1]), reversed(B)):
    if a+r_monster > b+r_yusya:
        monster_cnt += b+r_yusya
        r_yusya = 0
        r_monster = min(a+r_monster - b - r_yusya, a)
    else:
        monster_cnt += a+r_monster
        r_yusya = min(b+r_yusya - a - r_monster, b)
        r_monster = 0

print(monster_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 i != N-1:
#    cnt += min(A[i], B[i])
    if A[i] >= B[i]:
      cnt += B[i]
    else:
      cnt += A[i]
      # 貫通する数
      kantuu = B[i] - A[i]
      # A[i+1]のうち、倒す数
      taosu = max(A[i+1], kantuu)
      cnt += taosu
      A[i+1] -= taosu
  else:
    cnt += min((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()))

cnt = 0
tmp = 0
for i in range(N):
    tmp_mon = max(A[i] - tmp, 0)
    cnt += min(B[i], tmp_mon) + tmp
    if B[i] > tmp_mon:
        tmp = B[i] - tmp_mon
    elif B[i] <= tmp_mon:
        tmp = 0

cnt += min(A[-1], tmp)

print(cnt)

Submission status: Wrong Answer

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

total = 0
m = 0
for i in range(n):
    if a[i] >= (b[i] + m):
        m = 0
        total += (b[i] + m)
    else:
        total += a[i]
        m = min(b[i] - a[i] + m, b[i])
total += min(m, a[-1])
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):
    mons = a[i]+a[i+1]
    hero = b[i]
    if hero <= mons:
        ans += hero
    else:
        ans += mons

print(ans)