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:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

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

mod = 1000000007

if __name__ == "__main__":
     n= int(sys.stdin.readline())
     a= list(map(int, sys.stdin.readline().split()))
     b= list(map(int, sys.stdin.readline().split()))

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

Submission status: Wrong Answer

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

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

mod = 1000000007

if __name__ == "__main__":
     n= int(sys.stdin.readline())
     a= list(map(int, sys.stdin.readline().split()))
     b= list(map(int, sys.stdin.readline().split()))

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

Submission status: Wrong Answer

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

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

mod = 1000000007

if __name__ == "__main__":
     n= int(sys.stdin.readline())
     a= list(map(int, sys.stdin.readline().split()))
     b= list(map(int, sys.stdin.readline().split()))

     ans = 0
     for i in range(n):
          ans += min(a[i]+a[i+1], b[i])
          if a[i] < b[i]:
               a[i+1] -=  max(0, (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):
	if a[i] >= b[i]:
		ans += b[i]
	else:
		ans += a[i]
		x = b[i] -a[i]
		if x >= a[i+1]:
			ans += a[i+1]
		else:
			ans += x
			a[i+1] = a[i+1] -x
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, e in enumerate(b):
    rem = e
    if e > a[i]:
        ans += a[i]
        rem = e - a[i]
    else:
        ans += e
        a[i] -= e
        continue

    if rem > a[i + 1]:
        ans += a[i + 1]
        a[i + 1] = 0
    else:
        ans += rem
        a[i] -= rem

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
city = list(map(int,input().split(' ')))
hero = list(map(int,input().split(' ')))

ans = 0
for i, ihero in enumerate(hero):
    point = hero[i]
    imonster =city[i]
    i1monster =city[i+1]
    
    if point > imonster:
        point= point - imonster
        ans += imonster
        imonster=0
    if point <= imonster:
        ans +=point
        imonster -= point
        point = 0
    if n ==1:
        break
    if point > i1monster:
        point= point - i1monster
        ans += i1monster
        i1monster=0
    if point <= i1monster:
        ans +=point
        i1monster -= point
        point = 0

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
city = list(map(int,input().split(' ')))
hero = list(map(int,input().split(' ')))

ans = 0
for i, ihero in enumerate(hero):
    point = hero[i]
    imonster =city[i]
    i1monster =city[i+1]
    
    if point > imonster:
        point= point - imonster
        ans += imonster
        imonster=0
    if point <= imonster:
        ans +=point
        imonster -= point
        point = 0
    if point > i1monster:
        point= point - i1monster
        ans += i1monster
        i1monster=0
    if point <= i1monster:
        ans +=point
        i1monster -= point
        point = 0

print(ans)

Submission status: Wrong Answer

Solution code:

import numpy as np
N = int(input())

A_ls = np.array(list(map(int,input().split(" "))))
B_ls = np.array(list(map(int,input().split(" "))))

S = 0

for i,b in enumerate(B_ls):
    if b > A_ls[i]:
        A_ls[i] == 0
        b -= A_ls[i]
        S += A_ls[i]
        if A_ls[i+1] >= b:
            A_ls[i+1] -=b
            S+=b
        else:
            A_ls[i+1] = 0
            S+=(A_ls[i+1])
    else:
        S += b

print(S)

Submission status: Wrong Answer

Solution code:
N = int(input())    #勇者の数
monster_list = list(map(int,input().split()))       #モンスターの数
cap_list = list(map(int,input().split()))       #倒せる数

count = 0

for i in range(N):
    winnum = monster_list[i] - cap_list[i]
    if winnum >= 0:      #モンスターの数のほうが多い場合
        count += cap_list[i]

    elif monster_list[i+1] + winnum < 0:       #i+1でもモンスターの数が少ない場合
        count += monster_list[i] + monster_list[i+1]
        monster_list[i+1] = 0
    
    elif monster_list[i+1] + winnum > 0:
        count += monster_list[i] - winnum
        monster_list[i+1]  = monster_list[i+1] + winnum

print(count)

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

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

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
inputsA = input().split()
inputsB = input().split()
A = [int(n) for n in inputsA]
B = [int(n) for n in inputsB]
enemy = 0
attack = 0
dif = 0

enemy += A[0]
temp = A[0] - B[0]
if temp > 0:
    dif += temp

for i in range(1, N):
    enemy += A[i]
    attack += B[i-1]

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

enemy += A[N]
attack += B[N-1]
temp = A[N] - B[N-1]
if temp > 0:
    dif += temp

result = enemy - dif
if result > attack:
    result = attack

print(result)

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, e in enumerate(b):
    rem = e
    if e > a[i]:
        ans += a[i]
        rem = e - a[i]
    else:
        ans += e
        a[i] -= e
        continue

    if rem > a[i + 1]:
        ans += a[i + 1]
    else:
        ans += rem
        a[i] -= rem

print(ans)

Submission status: Wrong Answer

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

r = a[0]
ans = sum(a)
for i in range(n):
    tmp = max(0, b[i] - a[i])
    a[i+1] = max(0, a[i+1]-tmp)
if a[0]>b[0]: ans -= a[0]-b[0]
print(ans-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
         
for i in range(0,N):
      left = A[i] - B[i]
      ans += min(A[i],B[i])
         
      if left < 0:
          right = A[i+1] - abs(left)
          ans += min( A[i+1],abs(left))
          A[i+1] = right

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

print(c)

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:
        if b[i] >= a[i] + a[i+1]:
            ans += a[i] + a[i+1]
        else:
            ans += b[i]
            a[i+1] = a[i+1] + 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]:
        ans += b[i]
    else:
        if b[i] > a[i] + a[i+1]:
            ans += a[i] + a[i+1]
        else:
            ans += b[i]
            a[i+1] = a[i+1] + a[i] - b[i]

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

c=b[:]
d=np.argsort(b)
e=d[-1::-1]
ans=0
for i in range(len(b)):
    f=b[e[i]]
    if f>=a[e[i]+1]:
        ans+=a[e[i]+1]
        f-=a[e[i]+1]
        a[e[i]+1]=0
    else:
        ans+=f
        a[e[i]+1]-=f
        f=0
    if f>=a[e[i]]:
        ans+=a[e[i]]
        a[e[i]]=0
    else:
        ans+=f
        a[e[i]]-=f
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

c=b[:]
d=np.argsort(b)
e=d[-1::-1]
ans=0
for i in range(len(b)):
    f=b[e[i]]
    if f>=a[e[i]+1]:
        ans+=a[e[i]+1]
        f-=a[e[i]+1]
    else:
        ans+=f
        f=0
    if f>=a[e[i]]:
        ans+=a[e[i]]
    else:
        ans+=f
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
H = [int(a) for a in input().split()]

if (N == 1):
    print('Yes')
    exit()
hmax=H[0]
for i in range(N - 1):  
    if (hmax - H[i + 1]) > 1:
        print('No')
        exit()
    hmax = max(hmax, H[i + 1])
print('Yes')

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)

res = 0
for i in range(N+1):
    res += min(A[i], B[i])
    B[i+1] += max(B[i]-A[i], 0)

print(res)

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

print(o)

Submission status: Wrong Answer

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

tmp = 0
res = 0
for i in range(N):
    res += min(A[i], B[i]+tmp)
    tmp = max(B[i]+tmp-A[i], 0)

res += min(tmp, A[N])

print(res)

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()]
# print(B)
num = 0
# 最初の街への対処
num += min(A[0],B[0])
B[0] = max(0, B[0]-A[0])

# 最後の街への対処
num += min(A[-1],B[-1])
B[-1] = max(0, B[-1]-A[-1])
# print(B, num)
for i in range(1, N):
    power = B[i]+B[i-1]
    tmp = min(A[i],power)
    num += tmp
    B[i-1] = max(0, B[i-1]-A[i]) 
    B[i] += min(0, B[i-1]-A[i])
    # print(B, num)
print(num)

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

num = 0
# 最初の街への対処
num += min(A[0],B[0])
B[0] = max(0, B[0]-A[0])

# 最後の街への対処
num += min(A[-1],B[-1])
B[-1] = max(0, B[-1]-A[-1])

for i in range(1, N):
    power = B[i]+B[i-1]
    num += min(A[i],power)
    B[i-1] = max(0,power-A[i])
    B[i] -= B[i-1]
print(num)

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)

res = 0

for i in range(N + 1):
    if a[i] >= b[i]:
        res += b[i]
    if a[i] < b[i]:
        res += a[i]
        b[i + 1] += b[i] - a[i]

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))

total = 0
i = -1
print(i, total, monster, hero)

for i in range(N + 1):
    if i == 0:
        if monster[0] < hero[0]:
            total += monster[0]
            hero[0] -= monster[0]
            monster[0] = 0
        else:
            total += hero[0]
            monster[0] -= hero[0]
            hero[0] = 0
            
    elif i < N:
        if monster[i] < hero[i - 1]:
            total += monster[i]
            hero[i - 1] -= monster[i]
            monster[i] = 0
        else:
            total += hero[i - 1]
            monster[i] -= hero[i - 1]
            hero[i - 1] = 0
            
            if monster[i] < hero[i]:
                total += monster[i]
                hero[i] -= monster[i]
                monster[i] = 0
            else:
                total += hero[i]
                hero[i] = 0
                monster[i] -= hero[i]
    else:
        if monster[N] < hero[N - 1]:
            total += monster[N]
        else:
            total += hero[N - 1]

print(total)

Submission status: Wrong Answer

Solution code:
N = int(input())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))

total = 0
i = -1
print(i, total, monster, hero)

for i in range(N + 1):
    if i == 0:
        if monster[0] < hero[0]:
            total += monster[0]
            hero[0] -= monster[0]
        else:
            total += hero[0]
            hero[0] = 0
    elif i < N:
        if monster[i] < hero[i - 1]:
            total += monster[i]
        else:
            total += hero[i - 1]
            monster[i] -= hero[i - 1]
            
            if monster[i] < hero[i]:
                total += monster[i]
                hero[i] -= monster[i]
            else:
                total += hero[i]
                hero[i] = 0
    else:
        if monster[N] < hero[N - 1]:
            total += monster[N]
        else:
            total += hero[N - 1]
    print(i, total, monster, hero)
print(total)

Submission status: Wrong Answer

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

v = 0
for i in range(n):
  v += min(heros[i], monsters[i] + monsters[i+1])

print(min(v, sum(monsters)))

Submission status: Wrong Answer

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

v = 0
for i in range(n):
  v += min(heros[i], monsters[i] + monsters[i+1])

print(v)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

bb = 0
m = 0
for i, _ in enumerate(a):
    if a[i] > bb:
        ai = a[i] - bb
        m = m + bb
    else:
        ai = 0
        m = m + a[i]

    if i + 1 <= len(b):
        if ai > b[i]:
            m = m + b[i]
        else:
            m = m + ai
            bb = b[i] - ai
    
print(m)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

sum_M = []
for i in range(0, N+1):
 if i == 0:
  p1 = max(0, Y[0] - M[0])
  sum_M.append(min(M[0], Y[0]) ) 
 elif i < N:
  sum_M.append(min(M[i], Y[i] + p1) )
  p1 = min(Y[i], max(0, Y[i] - (M[i] + p1)) )
 else:
  sum_M.append(min(M[i], p1) )
  p1 = max(0, p1 - M[i])  

print(sum(sum_M))

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

sum_M = []
for i in range(0, N+1):
 if i == 0:
  p1 = max(0, Y[0] - M[0])
  sum_M.append(min(M[0], Y[0]) ) 
 elif i < N:
  sum_M.append(min(M[i], Y[i] + p1) )
  p1 = max(0, Y[i] - M[i] + p1)
 else:
  sum_M.append(min(M[i], p1) )
  p1 = max(0, p1 - M[i])  

print(sum(sum_M))

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

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

print (S - sum(A))

Submission status: Wrong Answer

Solution code:
# 要は減少してはいけない。イコールまでは大丈夫
n = int(input())
h = [int(x) for x in input().split()]

flg=0
flg_0 = 0
for i in range(0,len(h)-1):
    tmp = h[i] - h[i+1]
    if(tmp >= 2)&(flg!=1):
        print("No")
        flg=1
    elif(tmp ==1):
        h[i] -= 1
        if(h[i-1]-h[i] >= 1)&(h[i] != 0)&(flg!=1):
            print("No")
            flg=1
            
if(flg!=1):
    print("Yes")

Submission status: Wrong Answer

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

n_defeat=0

i_enemy = a[-2]
i_hero = b[-1]
remain_enemy = a[-1]

for i in range(n-2, -1, -1):
    if i_enemy + remain_enemy > i_hero:
        n_defeat += i_hero
        remain_enemy = max(0, i_enemy + remain_enemy- i_hero)
        i_enemy = a[i]
    else:
        n_defeat += i_enemy + remain_enemy
        remain_enemy = 0
        i_enemy = a[i]

    i_hero = b[i]

n_defeat += min(remain_enemy+i_enemy, i_hero)

print(n_defeat)

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):
    Ai=max(A[i],A[i+1])
    if Ai<B[i]:
        mon+=(A[i]+A[i+1])
        A[i+1]=0
    else:
        mon+=B[i]
print(mon)

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

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]
    if A[i+1] < B[i]:
      count += A[i+1]
    else:
      count += B[i]
  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()))
c=0
for i in range(n-1):
  if a[i]<=b[i]:
    c+=a[i]
    d=b[i]-a[i]
    b[i+1]+=d
  else:
    c+=b[i]
if b[n-1]>=a[n-1]:
  c+=a[n-1]
  x=b[n-1]-a[n-1]
  c+=min(a[n],x)
else:
  c+=b[n-1]
print(c)

Submission status: Wrong Answer

Solution code:
import sys
 
n=int(input())
a=input().split() 
b=input().split() 
kill=0
stack=0

for i in range(n):
  if int(b[i])<=int(a[i])+int(a[i+1]):
    kill = kill+int(b[i])
  else:
    kill = kill+int(a[i])+int(a[i+1])
  
  
print(kill)

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()]
t=0

print(a)
print(b)

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

print(t)

Submission status: Wrong Answer

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

for i in range(0,n):
    check1 = (L[i]+L[i+1])
    check2 = check1 - L2[i]
    if check2 < 0:
        L[i+1] = 0
        count = count + check1
    else:
        L[i+1] = check2
        count = count + L2[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=count+b[i]
    continue
  else:
    count=count+a[i]
    b[i]=b[i]-a[i]

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

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

    if(a[i+1]>=b[i]):
      count=count+b[i]
      continue
    else:
      count=count+a[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):
    a0 = min(A[i], B[i])
    B[i] -= a0

    a1 = min(A[i+1], B[i])
    B[i] -= a1

    ans += (a0 + a1)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
if a[0] <= b[0]:
    ans = a[0]
    count = b[0] - a[0]
else:
    ans = b[0]
    count = 0
for i in range(1,n):
    if count + b[i] >= a[i]:
        ans = ans + a[i]
        count = count + b[i] - a[i]
    else:
        ans = ans + count + b[i]
        count = 0
if a[n] <= count:
    ans = ans + a[n]
else:
    ans = ans + count
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()))


cnt = 0
for i in range(n):
  if a_list[i] <= b_list[i]:
    cnt += a_list[i]
    b_list[i] -= a_list[i]
    a_list[i] = 0
    if a_list[i+1] <= b_list[i]:
      cnt += a_list[i+1]
      a_list[i] = 0
    else:
      cnt += b_list[i]
      a_list[i+1] -= b_list[i]
  else:
    cnt += b_list[i]
    a_list[i] -= b_list[i]
    b_list[i] = 0
print(cnt)

Submission status: Wrong Answer

Solution code:
num = int(input())
c = input().split()
h = input().split()

for i in range(len(c)):
  c[i] = int(c[i])
for i in range(len(h)):
  h[i] = int(h[i])
  
sumc = sum(c)
for i in range(len(h)):
  c[i] -= h[i]
  h[i] = abs(c[i])
  if c[i] < 0:
    c[i]=0
  if h[i] > 0:
    c[i+1] -= h[i]
    if c[i+1] < 0:
      c[i+1]=0
      
sumedc = sum(c)
print(sumc-sumedc)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

n = int(input())
town_list = [int(x) for x in input().split(' ')]
saver_list = [int(x) for x in input().split(' ')] + [0]

count = 0

for i, town in enumerate(town_list):
    if town_list[i] >= saver_list[i]:
        count += saver_list[i]
    else:
        count += town_list[i]
        if not i+1 == len(saver_list):
            saver_list[i+1] += saver_list[i] - town_list[i]

print(count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

score += min(remain, a[-1])
print(score)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

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

Submission status: Wrong Answer

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

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

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

		M = A[i] + A[i+1] + M
		A[i] = 0
		A[i+1] = 0

	else:

		M = B[i] + M
		A[i] = 0
		A[i+1] = B[i] - A[i]

print(M)

Submission status: Wrong Answer

Solution code:
n=int(input())
al=list(map(int, input().split()))
bl=list(map(int, input().split()))

ap=al[0]
i=0
sum=0
for a in al[1:]:
    y = bl[i]

    m1 = min([ap, y])
    sum += m1

    y1 = y - m1
    if y1 < 0:
        y1 = 0

    m2 = min([a, y1])
    sum += m2

    ap = a
    i += 1

print(sum)

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

Submission status: Wrong Answer

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

defeat = 0
pow_rest = 0

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

defeat += min([pow_rest, a[n]])

print(defeat)

Submission status: Wrong Answer

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

defeat = 0
pow_rest = 0

for i in range(n):
  defeat += min([a[i], b[i]+pow_rest])
  if b[i]+pow_rest-a[i] > 0 :
    pow_rest = b[i]+pow_rest-a[i]

defeat += min([pow_rest, a[n]])

print(defeat)

Submission status: Wrong Answer

Solution code:
n = int(input())
an, bn = [list(map(int, input().split())) for i in [0, 1]]
m = 0
for i in range(n):
    m += min(bn[i], an[i])
    if an[i] < bn[i]:
        r = bn[i] - an[i]
        m += min(an[i+1], r)
        an[i+1] -= r if an[i+1] > r else 0
print(m)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

ans = 0

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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