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

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

Submission status: Accepted

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

now = A[0]
ret = 0
for i in range(n):
    x = B[i]

    if now <= x:
        x -= now
        ret += now
        now = A[i + 1]

        if x <= 0:
            continue
        
        if now <= x:
            ret += now
            now = 0
        else:
            now -= x
            ret += x
    
    else:
        ret += x
        now = A[i + 1]

print(ret)

Submission status: Accepted

Solution code:
num = int(input())

town = [int(x) for x in input().split()]

fighter = [int(x) for x in input().split()]

ans = 0

for i in reversed(range(num)):
  if town[i+1] >= fighter[i]:
    ans += fighter[i]
    continue
  else:
    ans += town[i+1]
    amari = fighter[i] - town[i+1]
    
  if town[i] >= amari:
    ans += amari
    town[i] -= amari
  else:
    ans += town[i]
    town[i] = 0

print(ans)

Submission status: Accepted

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


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

if b[n-1] - a[n-1] > 0:
    count += a[n-1]
    rest = b[n-1] - a[n-1]
    if rest < a[n]:
        count += rest
    else:
        count += a[n]
else:
    count += b[n-1]


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

if A[0] > B[0]:
  result = B[0]
  kurikoshi = 0
else:
  result = A[0]
  kurikoshi = B[0] - A[0]  

for i in range(1, N):
  if A[i] - kurikoshi > 0:
    nokori = A[i] - kurikoshi
    result += kurikoshi
    if nokori > B[i]:
      result += B[i]
      kurikoshi = 0
    else:
      result += nokori
      kurikoshi = B[i] - nokori
  elif A[i] - kurikoshi <= 0:
    result += A[i]
    kurikoshi = B[i]
if A[N] > kurikoshi:
  result += kurikoshi
  print(result)
else:
  result += A[N]
  print(result)

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

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

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline())
a = [int(i) for i in sys.stdin.readline().split()]
b = [int(i) for i in sys.stdin.readline().split()]

total = 0
for i, v in enumerate(b):
    m1 = min(v, a[i])

    total += m1
    a[i] -= m1

    if m1 == v:
        continue
    v -= m1

    m2 = min(v, a[i+1])
    total += m2
    a[i+1] -= m2
print(total)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

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


print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
aArr = input().split(" ")
bArr = input().split(" ")

aArr= [int(i) for i in aArr]
bArr= [int(i) for i in bArr]

res = 0

for i in range(N):
  if(bArr[i] < aArr[i]):
    res += bArr[i]
  else:
    res += aArr[i]
    if((bArr[i] - aArr[i]) < aArr[i+1]):
      res += bArr[i] - aArr[i]
      aArr[i+1] -= bArr[i] - aArr[i]
    else:
      res += aArr[i+1]
      aArr[i+1] = 0

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]", B: "List[int]"):
    sum = 0
    for i in range(N):
        cap = B[i]
        if cap <= A[i]:
            sum += cap
            # A[i] = A[i] - B[i]
        else:
            sum += A[i]
            cap = cap - A[i]

            if cap <= A[i+1]:
                sum += cap
                A[i+1] = A[i+1] - cap
            else:
                sum += A[i+1]
                A[i+1] = 0

    print(sum)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N+1) ]  # type: "List[int]"
    B = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A, B)

if __name__ == '__main__':
    main()

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)]
counter=0
for i in range(N):  
    counter+=min(A[N-i]+A[N-1-i],B[N-1-i]) 
    if B[N-1-i]>A[N-i]:       
      A[N-1-i]=max(A[N-1-i]-B[N-1-i]+A[N-i],0)
print(counter)

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

Submission status: Accepted

Solution code:
LARGE = 10 ** 9 + 7


def solve_a(test=False, a=2, b=16):

    if not test:
        a, b = map(int, input().split())
    if (b - a) % 2 == 0:
        print((a + b) // 2)
    else:
        print("IMPOSSIBLE")


def solve_b(test=False, n=5, p_list=[5, 2, 3, 4, 1]):

    if not test:
        n = int(input())
        p_list = list(map(int, input().split()))

    r = 0
    for i in range(n):
        if p_list[i] != i + 1:
            r += 1
    if r == 0 or r == 2:
        print("YES")
    else:
        print("NO")


def solve_c(test=False, n=2, a_list=[3, 5, 2], b_list=[4, 5]):

    if not test:
        n = int(input())
        a_list = list(map(int, input().split()))
        b_list = list(map(int, input().split()))

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

    print(res)


def solve_d(test=False, s='??2??5'):

    if not test:
        s = input()

    l = len(s)
    res_list_new = [0] * 13
    res_list_old = [0] * 13
    zero_mod = int(s.replace('?', '0')) % 13
    res_list_new[zero_mod] += 1

    for i in range(1, l + 1):
        if s[-i] == '?':
            for k in range(13):
                res_list_old[k] = res_list_new[k] % LARGE
            base_mod = pow(10, i-1, 13)
            for j in range(1, 10):
                for k in range(13):
                    res_list_new[(k + base_mod * j) % 13] += res_list_old[k]

    print(res_list_new[5] % LARGE)


# solve_a(test=True, a=2, b=16)
# solve_a(test=True, a=0, b=3)
# solve_a(test=True, a=998244353, b=99824435)

# solve_b(test=True, n=5, p_list=[5, 2, 3, 4, 1])
# solve_b(test=True, n=5, p_list=[2, 4, 3, 5, 1])
# solve_b(test=True, n=7, p_list=[1, 2, 3, 4, 5, 6, 7])

# solve_c(test=True, n=2, a_list=[3, 5, 2], b_list=[4, 5])
# solve_c(test=True, n=3, a_list=[5, 6, 3, 8], b_list=[5, 100, 8])
# solve_c(test=True, n=2, a_list=[100, 1, 1], b_list=[1, 100])

# solve_d(test=True, s='??2??5')
# solve_d(test=True, s='?44')
# solve_d(test=True, s='7?4')
# solve_d(test=True, s='?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???')

solve_c()

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

    if taosu < 0:
        A[i+1] = A[i+1]+taosu
        if A[i+1] < 0:
            A[i+1] = 0
    c += taosita

print(c)

Submission status: Accepted

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

print(k)

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

print(cnt)

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

Submission status: Accepted

Solution code:
# 整数の入力
City = int(input())
# スペース区切りの整数の入力
Mons = [int(a) for a in input().split()]
# スペース区切りの整数の入力
Blave = [int(b) for b in input().split()]


slash = 0


for i in range(City):
  if Blave[i] > Mons[i]:
    slash += Mons[i]
    Sword = Blave[i] - Mons[i]
    if Sword > Mons[i+1]:
      slash += Mons[i+1]
      Mons[i+1] = 0
    else :
      slash += Sword
      Mons[i+1] -= Sword
  else:
    slash += Blave[i]


print('{}'.format(slash))

Submission status: Accepted

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

total = 0

for i in range(N):
    if(a_li[i] <= b_li[i]):
        total = total + a_li[i]
        b_li[i] = b_li[i] - a_li[i]
        if(a_li[i+1] <= b_li[i]):
            total = total + a_li[i+1]
            a_li[i+1] = 0
        else:
            total = total + b_li[i]
            a_li[i+1] = a_li[i+1] - b_li[i]
    else:
        total = total + b_li[i]

print(total)

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(len(B)):
  remain = A[i]-B[i]
  if remain >= 0:
    ans += B[i]
  else:
    if i <= N:
      #A[i+1]を超えない分のremainだけ有効
      remain = max(remain,-A[i+1])
      A[i+1] += remain
      ans += (A[i]-remain)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split())) + [0]
rest = 0
ans = 0
for i in range(N):
  a = A[i]
  b = B[i]
  if a > rest:
    ar = a - rest
    ans += rest
  else:
    ar = 0
    ans += a
    
  if b > ar:
    rest = b-ar
    ans += ar
  else:
    rest = 0
    ans += b

if A[-1] > rest:
  temp = rest
else:
  temp = A[-1]

ans += temp

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
monsters = list(reversed(list(map(int,input().split()))))
savers = list(reversed(list(map(int,input().split()))))
kotae = 0
nokori = monsters[0]

for k,i in enumerate(savers):
    a = nokori + monsters[k+1]
    if i >= a:
        kotae = kotae + a
        nokori = 0
    else:
        kotae = kotae + i
        if i >= nokori:
            nokori = a - i
        else:
            nokori = a - nokori
print(kotae)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

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()]
c = 0
for i in range(n):
    left = min(a[i], b[i])
    b[i] -= left
    c += left
    right = min(a[i+1], b[i])
    a[i+1] -= right
    c += right
print(c)

Submission status: Accepted

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

p = 0
ans = 0
for ai, bi in zip(a, b):
    ans += min(-p, ai)
    n = max (ai + p, 0)
    ans += min(n, bi)
    p = min(n - bi, 0)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

  else:
    d += a[i]
    b[i] = b[i] - a[i]
    
    if a[i+1] - b[i] >= 0:
      d += b[i]
      a[i+1] = a[i+1] - b[i]
      
    else:
      d += a[i+1]
      a[i+1] = 0
      
      
print(d)

Submission status: Accepted

Solution code:
def simu(i, j, c):
    if A[i] >= B[j]:
        q = B[j]
        A[i] -= B[j]
        B[j] = 0
    else:
        q = A[i]
        B[j] -= A[i]
        A[i] = 0
        if not c:
            q += simu(i + 1, j, True)
    return q


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

p = 0
for i in range(N):
    p += simu(i, i, False)

print(p)

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

Submission status: Accepted

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

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):
  for j in range(2):
    tmp = min(A[i+j],B[i])
    count += tmp
    A[i+j] -= tmp
    B[i] -= tmp
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()]

M = 0

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

    else:
        M += B[i]

print(M)

Submission status: Accepted

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

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

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 = a[i+1] - res
        if target < 0:
            target = 0

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

print(ans)

Submission status: Accepted

Solution code:
import copy
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.append(0)
tmp = copy.copy(b)
ans = 0
for i in range(0, n):
    ans += min(a[i], b[i])
    b[i + 1] += min(max(b[i] - a[i], 0), tmp[i])
ans += min(a[n], b[n])
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):
    x = A[i]-B[i]
    if x>=0:
        sum = sum + B[i]
    else:
        sum = sum + A[i]
        B[i] = B[i]-A[i]
        x = A[i+1]-B[i]
        if x>=0:
            sum = sum + B[i]
            A[i+1]=A[i+1]-B[i]
        else:
            sum = sum + A[i+1]
            A[i+1]=0

print(sum)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

count = 0
am = 0
for i, b in enumerate(B):
    a = A[i]-am
    if a < 0:
        count += A[i]
        a = 0
    else:
        count += A[i]-a
    n = a-b
    if n < 0:
        count += a
        am = b-a
    else:
        count += a-n
        am = 0
i += 1
a = A[i]-am
if a < 0:
    count += A[i]
else:
    count += A[i]-a
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
''' 
------------------------
author : iiou16
------------------------
'''

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

    count = 0
    nokori = 0

    for i in range(len(A)):
        mon = A[i]
        if nokori >= mon:
            count += mon
            mon = 0
        else:
            count += nokori
            mon -= nokori
        if i < N:
            if B[i] >= mon:
                count += mon
                nokori = B[i] - mon
            else:
                count += B[i]
                nokori = 0
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
res = 0
for i in range(n+1):
    if i < n:
        res += b[i]
    ans += min(res, a[i])
    if i < n:
        res = min(max(res - a[i], 0), b[i])

print(ans)

Submission status: Accepted

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

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

print(battle)

Submission status: Accepted

Solution code:
n = int(input())
aarr = list(map(int, input().split()))
barr = list(map(int, input().split()))

tmparr = aarr[:]
count = 0

for i in range(n):
    if tmparr[i] >= barr[i]:
        count += barr[i]
        tmparr[i] -= barr[i]
    else:
        count += tmparr[i]
#        tmparr[i] = 0
        if tmparr[i+1] >= barr[i] - tmparr[i]:
            tmparr[i+1] -= barr[i] - tmparr[i]
            count += barr[i] - tmparr[i]
        else:
            count += tmparr[i+1]
            tmparr[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()))

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

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

Solution code:
#!/usr/bin/env python

n = int(input())
p = input().split()
q = input().split()
a = []
b = []
a_start = 0
a_end = 0

for i in p:
    a.append(int(i))
for i in q:
    b.append(int(i))
for i in a:
    a_start += i

for i,j in enumerate(b):
    if a[i] > b[i]:
        a[i] -= b[i]
    else:
        b[i]   -= a[i]
        a[i]    = 0
        if a[i+1] > b[i]:
            a[i+1] -= b[i]
            b[i] = 0
        else:
            a[i+1] = 0
            b[i] -= a[i+1]

for i in a:
    a_end += i

print(a_start - a_end)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
aLst = list(map(int, input().split()))
bLst = list(map(int, input().split()))

ans = 0

for i in range(N):
    tmp = aLst[i] - bLst[i]
    if tmp < 0:
        if aLst[i + 1] - (bLst[i] - aLst[i]) <= 0:
            ans += aLst[i] + aLst[i + 1]
            aLst[i + 1] = 0
        else:
            ans += bLst[i]
            aLst[i + 1] = aLst[i + 1] - (bLst[i] - aLst[i])
    else:
        ans += bLst[i]
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()]

mon = 0
length = len(B) - 1

for i in range(len(B)):
	j = length - i
	if B[j] >= A[j+1]:
		mon += A[j+1]
		B[j] -= A[j+1]
		A[j+1] = 0
		if B[j] >= A[j]:
			mon += A[j]
			A[j]=0
		else:
			mon += B[j]
			A[j]-=B[j]
	else:
		mon += B[j]
		B[j] = 0
		A[j+1] -= B[j]

print(mon)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

ans=0
for i in range(n):
    if b[i]>c[i]:
        
        rem=b[i]-c[i]
        
        ans+=c[i]+min(rem,c[i+1])
        c[i+1]=max(c[i+1]-rem,0)
    else:
        ans+=b[i]
print(ans)

Submission status: Accepted

Solution code:

N=int(input())
M=list(map(int,input().split()))
S=list(map(int,input().split()))
K=[0 for i in range(N)]
for i in range(N):
  if M[i]>=S[i]:
    K[i]=S[i]
  else:
    if M[i+1]>=S[i]-M[i]:
      M[i+1]-=S[i]-M[i]
      K[i]=S[i]
    else:
      K[i]=M[i]+M[i+1]
      M[i+1]=0
print(sum(K))

Submission status: Accepted

Solution code:
import numpy as np

N=int(input())
A=np.array(input().split(),dtype=np.int32)
B=np.array(input().split(),dtype=np.int32)

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

print(result)

Submission status: Accepted

Solution code:
n=int(input())
Ai=list(map(int,input().split()))
Bi=list(map(int,input().split()))

ans=0
for i in range(n):
    b=Bi[i]
    if i<n-1:
        if Ai[i]>=b:
            ans+=b
        else:
            ans+=Ai[i]
            b-=Ai[i]
            if Ai[i+1]>=b:
                ans+=b
                Ai[i+1]-=b
            else:
                ans+=Ai[i+1]
                Ai[i+1]=0
    else:
        ans+=min(Ai[i]+Ai[i+1], b)


print(ans)

Submission status: Accepted

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
        yo = 0
    else:
        cnt += As[i]
        yo = min(Bs[i],Bs[i]-(As[i]-yo))
    
        
cnt += min(As[N], yo)
print(cnt)