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: Accepted

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: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

sum = 0

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

print(sum)

Submission status: Accepted

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

fl=[];fnex=[]
total=0

fl.append(min(al[0],bl[0]))
total+=fl[0]

fnex.append(min(al[1],bl[0]-fl[0]))
total+=fnex[0]
#print(fl)

#print(min(al[1],bl[0]-fl[0]))

for i in range(2,n+1):
  #i がi番目のmonsterを倒す
  fii=min(al[i-1]-fnex[i-2],bl[i-1])
  fl.append(fii)
  total+=fii
  #print(i)
  #print(fii)
  #i が(i+1)番目のmonsterを倒す
  fnex.append(min(al[i],bl[i-1]-fii))
  total+=fnex[i-1]
  #print(min(al[i],bl[i-1]-fii))
  
print(total)

Submission status: Accepted

Solution code:
i = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
d = 0
e = 0
z = 0
y = 0

for f in range(i):
    d = b[f] - a[f]
    if d > 0:
        z = z + a[f]
        y = a[f+1]
        a[f+1] = a[f+1] - d
        if a[f+1] <= 0:
            a[f+1] = 0
            z = z + y
        else:
            z = z + d
    elif d < 0:
        z = z + b[f]
    else:
        z = z + a[f]
print(z)

Submission status: Accepted

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

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

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

    ans += min(A[i], B[i])
    ex = max(0, B[i] - A[i])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a_li = list(map(int, input().split()))
b_li = list(map(int, input().split()))

ans = 0
for i in range(1, n+1):
  if a_li[-i] < b_li[-i]:
    ans += a_li[-i]
    b_li[-i] -= a_li[-i]
  else:
    ans += b_li[-i]
    b_li[-i] = 0
    
  if a_li[-(i+1)] < b_li[-i]:
    ans += a_li[-(i+1)]
    a_li[-(i+1)] = 0
  else:
    ans += b_li[-i]
    a_li[-(i+1)] -= b_li[-i]
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

     print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for idx in range(len(a)-1):
    if a[idx] > b[idx]:
        count += b[idx]
    else:
        count += a[idx]
        if a[idx+1] > b[idx] - a[idx]:
            a[idx+1] -= (b[idx] - a[idx])
            count += (b[idx] - a[idx])
        else:
            count += a[idx+1]
            a[idx+1] = 0
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(count)

Submission status: Accepted

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 + 1] -= rem

print(ans)

Submission status: Accepted

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
    city[i] =imonster
    city[i+1] =i1monster    
    

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = int(input())

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

S = 0

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

print(S)

Submission status: Accepted

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]:
        b -= A_ls[i]
        S += A_ls[i]
        if A_ls[i+1] >= b:
            A_ls[i+1] -=b
            S+=b
        else:
            S+=A_ls[i+1]
            A_ls[i+1] = 0 
    else:
        S += b

print(S)

Submission status: Accepted

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

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

print(result)

Submission status: Accepted

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: Accepted

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

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

print(ans)

Submission status: Accepted

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)
          if right < 0: right = 0
          ans += A[i+1] - right
          A[i+1] = right

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
a_lis = list(map(int,input().split()))
b_lis = list(map(int,input().split()))

ans = 0

for i in range(n):
	if b_lis[i] <= a_lis[i]:
		ans += b_lis[i]
	else:                                     
		ans += a_lis[i]
		b_lis[i] -= a_lis[i]
		if b_lis[i] <= a_lis[i+1]:
			ans += b_lis[i]
			a_lis[i+1] -= b_lis[i]
		else:
			ans +=  a_lis[i+1]
			a_lis[i+1] = 0
print(ans)

Submission status: Accepted

Solution code:
def solve(string):
    n, *ab = map(int, string.split())
    a, b = ab[:n + 1], ab[n + 1:]
    _sum = sum(a)
    for i, _b in enumerate(b[::-1]):
        diff = min(a[n - i], _b)
        _b -= diff
        a[n - i] -= diff
        a[n - i - 1] = max(0, a[n - i - 1] - _b)
    return str(_sum - sum(a))


if __name__ == '__main__':
    n = int(input())
    print(solve('{}\n'.format(n) + '\n'.join([input() for _ in range(2)])))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(string):
    n, *ab = map(int, string.split())
    a, b = ab[:n + 1], ab[n + 1:]
    _sum = sum(a)
    for i, _b in enumerate(b[::-1]):
        if a[n - i] < _b:
            _b -= a[n - i]
            a[n - i] = 0
            a[n - i - 1] = max(0, a[n - i - 1] - _b)
        else:
            a[n - i] -= _b
    return str(_sum - sum(a))


if __name__ == '__main__':
    n = int(input())
    print(solve('{}\n'.format(n) + '\n'.join([input() for _ in range(2)])))

Submission status: Accepted

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

ans = 0
for idx in range(len(B)):
    if A[idx] < B[idx]:
        add = B[idx] - A[idx]
        save = A[idx + 1]
        A[idx + 1] -= add
        A[idx + 1] = max(A[idx + 1], 0)
        ans += A[idx] + (save - A[idx + 1])
    else:
        ans += B[idx]

print(ans)

Submission status: Accepted

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

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

print (ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(res)

Submission status: Accepted

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

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

print(total)

Submission status: Accepted

Solution code:
city = int(input())
monster = list(map(int, input().split()))
hero = list(map(int, input().split()))
cnt = 0
for i, m in enumerate(monster):
    if i == 0:
        if m <= hero[i]:
            cnt = m
            hero[i] -= m
        else:
            cnt = hero[i]
            hero[i] = 0
    elif i <= len(hero) - 1:
        if m <= hero[i-1]:
            cnt += m
        elif m <= hero[i] + hero[i-1]:
            cnt += m
            hero[i] -=  m - hero[i-1]
        else: # m > hero[i] + hero[i-1]
            cnt += hero[i] + hero[i-1]
            hero[i] = 0
    else: # i > len(hero) - 1
        if m <= hero[i-1]:
            cnt += m
        else:
            cnt += hero[i-1]
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]

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

Submission status: Accepted

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

count = 0
r = 0
for i in range(N):
    a = int(As[i])
    b = int(Bs[i])
    count += min(a, r)
    a2 = max(a - r, 0)
    count += min(a2, b)
    r = max(b - a2, 0)

count += min(int(As[N]), r)
print(count)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]
rem = 0
tot=0
for i in range(N):
    tot=tot+min(A[N-i],B[N-i-1])
    rem = max(B[N - i - 1] - A[N - i], 0)
    tot = tot+min(rem, A[N-i-1])
    A[N - i - 1] = max(A[N - i - 1] - rem, 0)
print(tot)

Submission status: Accepted

Solution code:
def run_c():
    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 = a[i] - c
        if d - b[i] <= 0:
            o += a[i]
            c = b[i] - d if d >= 0 else b[i]
        else:
            o += c + b[i]
            c = 0

    print(o)

if __name__ == "__main__":
    run_c()

Submission status: Accepted

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

pow_rest = 0
defeat = 0

for i in range(n):
    x = min([a[i], b[i]])
    defeat += x
    y = min([a[i+1], b[i]-x])
    defeat += y
    a[i+1] -= y

print(defeat)

Submission status: Accepted

Solution code:
N=int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
num = 0
for i in range(N):
    num += min(A[i],B[i])
    B[i] = max(0, B[i]-A[i])
    num += min(A[i+1], B[i])
    A[i+1] = max(0, A[i+1]-B[i])
print(num)

Submission status: Accepted

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

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

Submission status: Accepted

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

res = 0
for i in range(N):
    res += min(a[i], b[i])
    if a[i] < b[i]:
        t = min(b[i] - a[i], a[i + 1])
        a[i + 1] -= t
        res += t

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
a_li = list(map(int, input().split()))
b_li = list(map(int, input().split()))
ans = 0
for i in range(n):
    if i >= 1 and b_li[i-1]>0:
        ans += min(a_li[i], b_li[i-1])
        a_li[i] = max(0, a_li[i] - b_li[i-1])

    if b_li[i] > 0:
        temp = min(a_li[i], b_li[i])
        ans += temp
        a_li[i] = max(0, a_li[i] - b_li[i])
        b_li[i] -= temp


    
ans += min(a_li[n], b_li[n-1])
a_li[i] = max(0, a_li[n] - b_li[n-1])
print(ans)

Submission status: Accepted

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

total = 0
i = -1

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: Accepted

Solution code:
def solve():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    org_a = sum(a)
    for i in range(n):
        amari = b[i] - a[i]
        a[i] = max(0, a[i] - b[i])
        if amari > 0:
            a[i + 1] = max(a[i + 1] - amari, 0)
    return org_a - sum(a)


if __name__ == "__main__":
    print(solve())

Submission status: Accepted

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

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

res = 0

for i in range(N):
  m1 = min(B[i], A[i])
  res += m1
  A[i] -= m1
  B[i] -= m1
  m2 = min(B[i], A[i+1])
  res += m2
  A[i + 1] -= m2
  
print(res)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
B.append(0)
amari = 0
i = 0
ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = int(input())
A = [int(v) for v in input().split()]
B = [int(v) for v in input().split()]

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

    if i < N:
        v = min(A[i], B[i])
        A[i] -= v
        cnt += v
        B[i] -= v
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
t = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
ans = 0
for i in range(N):
  if t[i] >= b[i]:
    ans += b[i]
  else:
    ans += t[i]
    b[i] = b[i] - t[i]
    if t[i+1] >= b[i]:
      ans += b[i]
      t[i+1] = t[i+1] - b[i]
    else:
      ans += t[i+1]
      t[i+1] = 0
print(ans)

Submission status: Accepted

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: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
ans = 0
for i in range(N):
  a1 = A[i]
  a2 = A[i+1]
  b = B[i]
  A[i] = A[i]-min(a1,b)
  ans += min(a1,b)
  b = b-min(a1,b)
  A[i+1] = A[i+1]-min(a2,b)
  ans += min(a2,b)
print(ans)

Submission status: Accepted

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]
    if a[i]<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)