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

Submission status: Accepted

Solution code:
N = int(input())

a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

c = 0
bb = 0
for i in range(N):
    a = a_list[i]
    b = b_list[i]

    aa = a - bb
    if aa <= 0:
        c += a
        aa = 0
    else:
        c += bb

    if b <= aa:
        c += b
        bb = 0
    else:
        c += aa
        bb = b - aa

aa = a_list[-1] - bb
if aa <= 0:
    c += a_list[-1]
    aa = 0
else:
    c += bb

print(c)

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

Submission status: Accepted

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

def city_saver(a, b):
    monster = 0
    for i in range(len(b)):
        monster += min(a[i], b[i])
        if(a[i]<b[i]):
            monster += min(b[i]-a[i], a[i+1])
            a[i+1] -= min(b[i]-a[i], a[i+1])
    return monster

def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    print(city_saver(a, b))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
ans = 0
for i, v in enumerate(b_list):
  enemy = a_list[i]
  if enemy >= b_list[i]:
    a_list[i] -= b_list[i]
    ans += b_list[i]
    b_list[i] = 0
  else:
    a_list[i] = 0
    b_list[i] = b_list[i] - enemy
    ans += enemy
    enemy = a_list[i + 1]
    if a_list[i + 1] >= b_list[i]:
      a_list[i + 1] -= b_list[i]
      ans += b_list[i]
      b_list[i] = 0
    else:
      a_list[i + 1] = 0
      b_list[i] = b_list[i] - enemy
      ans += enemy
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
a_lst = list(map(int, input().split()))
b_lst = list(map(int, input().split()))
ans = 0

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pre = a[0]
ans = 0
for i in range(n):
        if pre + a[i+1]-b[i] < 0:
                ans += pre + a[i+1]
                pre = 0
        elif 0<= pre + a[i+1]-b[i] <= a[i+1]:
                ans += b[i]
                pre = pre + a[i+1]-b[i]
        else:
                ans += b[i]
                pre = a[i+1]
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 0
x = 0

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

print(ans)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def getlist():
    return list(map(int, input().split()))

N = int(input())
A = getlist()
B = getlist()

ans = 0
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]
    else:
        ans += B[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline

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

ans = 0
for i,b in enumerate(B):
    x = min(A[i], b)
    b -= x
    ans += x
    if b > 0:
        x = min(A[i+1], b)
        A[i+1] -= x
        ans += x
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
T=list(map(int,input().split()))
AB=list(map(int,input().split()))
ans=0

for i in range(n):
  ans+=min(T[i]+T[i+1],AB[i])
  T[i+1]=max(0,T[i+1]-max(0,AB[i]-T[i]))
  
print(ans)

Submission status: Accepted

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

pre_sum = sum(a)
limit = 0

for i in range(n):
  limit = b[i]
  if limit > a[i]:
    limit -= a[i]
    a[i] = 0
    if limit < a[i+1]:
      a[i+1] -= limit
      limit = 0
    else:
      a[i+1] = 0
      limit = 0
  else:
    a[i] -= limit
    limit = 0
    
result_sum = sum(a)
result = pre_sum - result_sum
print(result)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
for j in range(N):
    tmp=min(A[j],B[j])
    ans+=tmp
    B[j]-=tmp
    tmp=min(A[j+1],B[j])
    ans+=tmp
    A[j+1]-=tmp
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
cnt = 0 #モンスターの数
i = 0 

for _ 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] + A[i] - B[i]
    elif A[i]+A[i+1]<=B[i]:
        cnt += A[i] + A[i+1]
        A[i+1] = 0
    i += 1
print(cnt)

Submission status: Accepted

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

spare_win = now_win = spare = win = 0
ans = 0
for i in range(N):
    spare_win = 0
    if spare:
        spare_win = min(A[i], spare)
        spare = 0
    now_win = min( (A[i] - spare_win), B[i])
    spare = B[i] - now_win
    ans += (now_win + spare_win)
ans += min(A[N], spare)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
B = [int(b) for b in input().split()]
pow = 0
ans = 0
for i in range(N):
    ans += A[i] if pow >= A[i] else pow
    A[i] = 0 if pow >= A[i] else A[i] - pow
    ans += A[i] if A[i] <= B[i] else B[i]
    pow = B[i] - A[i] if B[i] >= A[i] else 0
ans += A[N] if pow >= A[N] else pow
print(ans)

Submission status: Accepted

Solution code:

N = int(input())
ai = list(map(int, input().split()))
bi = list(map(int, input().split()))

dair = {}
dbir = {}
for i in range(N):
    dair.setdefault(i, ai[N-i])
    dbir.setdefault(i, bi[N-1-i])

dair.setdefault(N, ai[0])
#print(dair)
#print(dbir)

ans = 0
for i in range(N):
    diff = dbir[i] - dair[i]
    if diff >= 0:
        ans += dair[i]
        diff2 = dair[i+1] - diff
        if diff2 >= 0:
            dair[i+1]=diff2
            ans += diff
        else:
            ans += dair[i+1]
            dair[i+1]=0
    else:
        ans += dbir[i]
    #print(ans)

print(ans)
#print(dair)
#print(dbir)

Submission status: Accepted

Solution code:
import sys

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

cnt = 0
for n in range(N):  
  ## 勇者に、とりあえず自分の村を討伐してもらう
  cnt += min(A[n],B[n])
  
  ## 勇者に余力があるなら次の村のモンスターも討伐してもらう
  if A[n] < B[n]:
    
    ## 勇者強すぎwww次の村のモンスター全部倒せちゃうパターン
    if A[n+1] <= (B[n] - A[n]):
      cnt += A[n+1]
      A[n+1] = 0
    ## 流石にそこまでは...パターン
    else:
      cnt += (B[n] - A[n])
      A[n+1] -= (B[n] - A[n])
  
print(cnt)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

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

print(killed)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())#街の数N+1、勇者がN
A=list(map(int,input().split()))#モンスターの数Ai
B=list(map(int,input().split()))#Niの勇者はモンスターの数Biまで倒せる
ans = 0
for i in range(N):
  #print(ans)
  if B[i] > A[i]:
    #print('a')
    B[i] -= A[i]
    ans += A[i]
    A[i] = 0
    if A[i+1] - B[i] > 0:
      #print('a-a')
      A[i+1] -= B[i]
      ans += B[i]
    else:
      #print('a-b')
      ans += A[i+1]
      A[i+1] = 0
  else:
    #print('b')
    ans += B[i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

a_arr = list(map(int, input().split()))
b_arr = list(map(int, input().split()))

a_left = a_arr[0]
b = b_arr[0]

counter = 0

for i in range(n):
    a_right = a_arr[i + 1]
    b = b_arr[i]
    killed = b if a_left > b else a_left
    b -= killed
    counter += killed
    killed = b if a_right > b else a_right
    a_left = a_right - killed
    counter += killed

print(int(counter))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(score)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

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

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


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

print(c)

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

Submission status: Accepted

Solution code:
n = int(input())

As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
a_ = 0
b_ = 0
count = 0

for i in range(n):
  a = As[i]
  b = Bs[i]
  
  if a >= b_:
    count += b_
    a = a - b_
  else:
    count += a
    a = 0
  
  if a >= b:
    count += b
    b_ = 0
  else:
    count += a
    b_ = b - a

count += min(As[-1], b_)
print(count)

Submission status: Accepted

Solution code:
n = int(input())
towns = [int(s) for s in input().split()]
heros = [int(s) for s in input().split()]
p = 0
for i in range(n):
  rest = heros[i] - towns[i]
  if rest >= 0:
    p += towns[i]
    trest = towns[i + 1] - rest
    if trest < 0:
      p += towns[i + 1]
      towns[i + 1] = 0
    else:
      towns[i + 1] = trest
      p += rest
  else:
    p += heros[i]
print(p)

Submission status: Accepted

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

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

beaten_monster = sum(A) - sum(C)
print(beaten_monster)

Submission status: Accepted

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

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

Submission status: Accepted

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

j = 0
r = 0
for i in range(len(B)):
  cnt = i
  j = i
  while j <= cnt+1:

    if  j == len(A):
      break

    if B[i] >= A[j]:
      B[i] -= A[j]
      r += A[j]
      A[j] = 0
      j+=1
    else:
      A[j] -= B[i]
      r +=  B[i]
      break
print(r)

Submission status: Accepted

Solution code:
n=int(input())
T=list(map(int,input().split()))
AB=list(map(int,input().split()))
ans=0

for i in range(n):
  ans+=min(T[i]+T[i+1],AB[i])
  T[i+1]=max(0,T[i+1]-max(0,AB[i]-T[i]))
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
ans=0
for i in range(n-1,-1,-1): #勇者を後ろから順にみていく
 if arr1[i+1]>=arr2[i]: #i番目の勇者がi+1番目の町のモンスターしか倒せないとき
   ans+=arr2[i]
   continue
 else: #i番目の勇者がi番目の町のモンスターも倒す余裕があるとき
   ans+=arr1[i+1]
   arr2[i]-=arr1[i+1]
   if arr1[i]>arr2[i]: #i番目の勇者がi番目の町のモンスターをすべて倒せないとき
     ans+=arr2[i]
     arr1[i]-=arr2[i] #i番目の勇者が倒せなかったモンスターの数を計算する
   else:  #i番目の勇者がi番目の町のモンスターもすべて倒せるとき
     ans+=arr1[i]
     arr1[i]=0
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] >= B[i]:
        count += B[i]
        continue
    else:
        count += A[i+1]
        power = B[i] - A[i+1]

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

print(count)

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
A = list(map(int,stdin.readline().rstrip().split()))
B = list(map(int,stdin.readline().rstrip().split()))
point = 0
fa = min(A[0],B[0])
fb = min(A[1],B[0]-fa)
point += fb
point += fa
for i in range(1,n):
    fc = min(A[i]-fb,B[i])
    point += fc
    fb = min(A[i+1],B[i]-fc)
    point += fb
print(point)

Submission status: Accepted

Solution code:
city=int(input())
a=input().split(" ")
b=input().split(" ")
monster=[]
hero=[]
for p in a:
    monster.append(int(p))
for q in b:
    hero.append(int(q))
max=0
for s in range (0,city):
    if monster[s]-hero[s]>=0:
        max+=hero[s]
    elif monster[s+1]+monster[s]-hero[s]>0:
        max+=hero[s]
        monster[s+1]-=hero[s]-monster[s]
    else:
        max+=monster[s+1]+monster[s]
        monster[s+1]=0
print(max)

Submission status: Accepted

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

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


def attack(i, j):
    global a
    global b

    if a[i] >= b[j]:
        a[i] -= b[j]
        b[j] = 0
    else:
        b[j] -= a[i]
        a[i] = 0


for i in range(n):
    attack(i, i)
    attack(i + 1, i)

print(total - sum(a))

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

Submission status: Accepted

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

cnt = 0
for n in range(N):
    killed = min(B[n], A[n])
    res = B[n] - killed
    killed2 = min(res, A[n + 1])
    A[n + 1] -= killed2
    cnt += killed + killed2

print (cnt)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

Solution code:
n = int(input())
mnstr = list(map(int,input().split()))
yshabl = list(map(int,input().split()))

cnt = 0

for i in range(-1,-1*len(yshabl)-1,-1):
	y = yshabl[i]
	m = mnstr[i]+mnstr[i-1]
	if m >= y:
		cnt += y
		if mnstr[i] >= y:
			pass
		else:
			mnstr[i-1] -= y - mnstr[i]
	else:
		cnt += m
		mnstr[i-1] = 0

print(cnt)

Submission status: Accepted

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

A_copy = [x for x in A]
B_copy = [y for y in B]

for i in range(N-1, -1, -1):
  if A_copy[i+1] >= B_copy[i]:
    A_copy[i+1] -= B_copy[i]
    B_copy[i] = 0
  else:
    B_copy[i] -= A_copy[i+1]
    A_copy[i+1] = 0
    if A_copy[i] > B_copy[i]:
      A_copy[i] -= B_copy[i]
      B_copy[i] = 0
    else:
      B_copy[i] -= A_copy[i]
      A_copy[i] = 0

    #A_copy[i+1] = max(A_copy[i+1]-B[i], 0)
    #B_copy[i] = max(B_copy[i]-A[i+1], 0)
  #if B_copy[i] > 0:
   # a = A_copy[i]
    #A_copy[i] = max(A_copy[i]-B_copy[i], 0)
    #B_copy[i] = max(B_copy[i]-a, 0)

n = sum(B) - sum(B_copy)
print(n)

Submission status: Accepted

Solution code:
n=int(input())
#m=int(input())
#r=[input() for _ in range(n)]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
for i in range(n):
    left=min(a[i],b[i])
    ans+=left
    a[i]-=left
    b[i]-=left
    right=min(a[i+1],b[i])
    ans+=right
    a[i+1]-=right
    b[i]-=right
print(ans)