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())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))
    score=0
    # pattern 1
    for i in range(n):
        k=min(A[i],B[i])
        score+=k
        A[i]-=k
        B[i]-=k
        if(B[i]>0):
            k=min(A[i+1],B[i])
            score+=k
            A[i+1]-=k
            B[i]-=k
    print(score)
resolve()

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]
    elif A[i+1] > (B[i] - A[i]):
        A[i+1] -= B[i] - A[i]
        ans += B[i]
    else:
        ans += A[i] + A[i+1]
        A[i+1] = 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()))
bf = sum(A_list)

for i in range(N):
    if B_list[i] >= A_list[i]:
        #順番注意
        A_list[i+1] = max(A_list[i] + A_list[i+1] - B_list[i], 0)
        A_list[i] = 0
    else:
        A_list[i] = A_list[i] - B_list[i]  

af = sum(A_list)
print(bf- af)

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


print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
AList = list(map(int, input().split()))
BList = list(map(int, input().split()))

enemy = 0
for i in range(N):
    # a: 自分の担当で倒せる数
    a = min([AList[i], BList[i]])
    # b: 自分の担当以上に倒せる数
    b = min([AList[i + 1], max([BList[i] - AList[i], 0])])
    AList[i + 1] = max([AList[i + 1] - b, 0])
    # 合計値
    enemy = enemy + a + b
print(enemy)

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!


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

ans = 0
rp = 0
for idx, a in enumerate(al):
    if idx == n:
        ans += min(rp, a)
    else:
        b = bl[idx]
        enemy = min(a,b+rp)
        ans += enemy
        rp = min(max(b - enemy + rp, 0), b)
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 = min(A_list[0], B_list[0])
remain_B = max(B_list[0] - A_list[0], 0)
for i in range(1, N ):
    c += min(remain_B, A_list[i])
    remain_A = max( A_list[i] - remain_B, 0 )
    c += min(remain_A, B_list[i])
    remain_B = max(B_list[i] - remain_A, 0 )
c += min(A_list[N], remain_B)
print(c)

Submission status: Accepted

Solution code:
n = int(input().strip())
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]
        b[i] -= a[i]
        a[i] = 0

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

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

Submission status: Accepted

Solution code:
n=int(input())
monsters=[int(i) for i in input().split()]
powers=[[int(i),0] for i in input().split()]
monsters.append(0)
powers.extend([[0,0],[0,0]])

breaks=[]
for i in range(n+1):
  if monsters[i]>powers[i][1]:
    attack=sum(powers[i])
    if monsters[i]>=attack:
      breaks.append(attack)
    else:
      breaks.append(monsters[i])
      powers[i+1][1]=attack-monsters[i]
                    
  else:
    powers[i+1][1]=powers[i][0]
    breaks.append(monsters[i])
    
print(sum(breaks))

Submission status: Accepted

Solution code:
n=int(input())
monsters=[int(i) for i in input().split()]
powers=[[int(i),0] for i in input().split()]
monsters.append(0)
powers.extend([[0,0],[0,0]])

breaks=[]
for i in range(n+1):
  if monsters[i]>powers[i][1]:
    
    if monsters[i]>=sum(powers[i]):
      breaks.append(sum(powers[i]))
    else:
      breaks.append(monsters[i])
      powers[i+1][1]=sum(powers[i])-monsters[i]
                    
  else:
    powers[i+1][1]=powers[i][0]
    breaks.append(monsters[i])
    
print(sum(breaks))

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
m = 0
cnt = 0
for i, a in enumerate(a_list):
    b = 0 if i == n else b_list[i]
    cnt += min(m+b, a)
    m = max(0, b - max(0, a-m))
print(cnt)

Submission status: Accepted

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

# 初期値設定
ans = 0
Ai = A[0]
Bi = B[0]

for i in range(N):
    # i番目の街のモンスターを倒す
    ans += min(Ai, Bi)

    # i + 1番目の街の残りモンスターを設定
    if Ai < Bi:
        ans += min(A[i + 1], Bi - Ai)
        Ai = A[i + 1] - min(A[i + 1], Bi - Ai)
    else:
        Ai = A[i + 1]

    # N番目の勇者を設定していなければ、i + 1番目の勇者を設定
    if i == (N - 1):
        break
    else:
        Bi = B[i + 1]

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

Submission status: Accepted

Solution code:
n = int(input())
an = list(map(int, input().split()))
bn = list(map(int, input().split()))



bsum = sum(bn)
for i in range(n):
    diff = an[i] - bn[i]
    if diff <= 0:
        diff = bn[i] - an[i]
        an[i] = 0
        bn[i] = diff
    else:
        an[i] = diff
        bn[i] = 0
    
    diff2 = an[i+1] - bn[i]
    if diff2 <= 0:
        diff2 = bn[i] - an[i+1]
        an[i+1] = 0
        bn[i] = diff2
    else:
        an[i+1] = diff2
        bn[i] = 0
ans = bsum - sum(bn)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
tmp_sum = sum(a)
a = list(reversed(a))
b = list(reversed(b))
for i in range(len(b)):
    tmp = b[i] - a[i]
    a[i] = max(a[i] - b[i], 0)
    a[i+1] = max(a[i+1] - max(tmp, 0),0)
res_sum = sum(a)
print(tmp_sum - res_sum)

Submission status: Accepted

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

sum_ = 0

for i in range(N):
    if(A[i]>B[i]):
        sum_ += B[i]
    else:
        sum_ += A[i]
        next_attack = min([B[i]-A[i],A[i+1]])
        A[i+1] -= next_attack
        sum_ += next_attack
print(sum_)

Submission status: Accepted

Solution code:
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think([3, 5, 2], [4, 5]), 9)

    def test_2(self):
        self.assertEqual(think([5, 6, 3, 8], [5, 100, 8]), 22)

    def test_3(self):
        self.assertEqual(think([100, 1, 1], [1, 100]), 3)


def solve():
    a, b = read()
    result = think(a, b)
    write(result)


def read():
    n = read_int(1)[0]
    a = read_int(n + 1)
    b = read_int(n)
    return a, b


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_float(n):
    return list(map(lambda x: float(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a, b):
    count = 0
    for index, value in enumerate(b):
        if a[index] + a[index + 1] <= value:
            count += a[index] + a[index + 1]
            a[index] = 0
            a[index + 1] = 0
        else:
            if a[index] <= value:
                count += a[index]
                value -= a[index]
                a[index] = 0
                if value > 0:
                    count += value
                    a[index + 1] -= value
            else:
                count += value
                a[index] -= value
    return count


def write(result):
    print(result)


if __name__ == '__main__':
    # unittest.main()
    solve()

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

Submission status: Accepted

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

cnt = 0
for i in range(0,N):
  if B[i]-A[i]<0:
    cnt += B[i]
    #print(cnt)
  else:
    if A[i+1]-(B[i]-A[i])<0:
      cnt += A[i+1]+A[i]
      A[i+1] = 0
      #print(cnt,1)
    else:
      cnt += B[i]
      A[i+1] -= (B[i]-A[i])
      #print(cnt,2)
    #print(A,B)
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
number_of_monsters =  list(map(int,input().split()))
feasible_beat =  list(map(int,input().split()))

start = sum(number_of_monsters)
for i in range(n):
    if number_of_monsters[i] < feasible_beat[i]:
        number_of_monsters[i+1] -= (feasible_beat[i] - number_of_monsters[i])
        number_of_monsters[i] = 0
        if number_of_monsters[i+1] < 0:
            number_of_monsters[i+1] = 0
    else:
        number_of_monsters[i] -= feasible_beat[i]
print(start-sum(number_of_monsters))

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n):
    b = max(B[i] - A[i], 0)
    A[i] = max(A[i]-B[i], 0)
    A[i+1] = max(A[i+1]-b, 0)

print(total-sum(A))

Submission status: Accepted

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

Submission status: Accepted

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

sumb = sum(b)

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

print(sumb - sum(b))

Submission status: Accepted

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

r=a[0]
sum=0
for i in range(n):
  if r + a[i+1] > b[i]:
    sum += b[i]
    r = a[i+1] - max(b[i]-r,0)
  else:
    sum += r + a[i+1]
    r = 0
print(sum)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

res = 0
for i in range(n):
    if al[i]<bl[i]:
        res += al[i]
        bl[i] -= al[i]
        al[i] = 0
    else:
        res += bl[i]
        al[i] -= bl[i]
        bl[i] = 0

    if bl[i]>0:
        if al[i+1]<bl[i]: 
            res += al[i+1]
            bl[i] -= al[i+1]
            al[i+1] = 0
        else:
            res += bl[i]
            al[i+1] -= bl[i]
            bl[i] = 0

print(res)

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
MONSTER = list(map(int, input().split()))
HERO = list(map(int, input().split()))

count = 0

for i in range(N):
    battle = min(HERO[i], MONSTER[i])
    count += battle
    HERO[i] -= battle
    MONSTER[i] -= battle

    if HERO[i] > 0:
        battle = min(HERO[i], MONSTER[i+1])
        count += battle
        HERO[i] -= battle
        MONSTER[i+1] -= battle


print(count)

Submission status: Accepted

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

cnt = 0
for i in range(n):
    monster = min(a[i], b[i])
    a[i] -= monster
    b[i] -= monster
    cnt += monster

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

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

SumA_old=sum(A)

P=[_ for _ in range(N)]

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

SumA_new=sum(A)

print(SumA_old-SumA_new)

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

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

Submission status: Accepted

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

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

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

print(sum)

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
c = a.copy()
b = list(map(int, input().split()))
for i in range(n):
    for j in range(2):
        if a[i+j] <= b[i]:
            b[i] -= a[i+j]
            a[i+j] = 0
        else:
            a[i+j] -= b[i]
            b[i] = 0
print(sum(c-a))

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

Submission status: Accepted

Solution code:
input()
monsters = [int(x) for x in input().split()]
hero = [int(x) for x in input().split()]
ans = 0
power_left = 0
for i, b in enumerate(hero[::-1],1):
    to_kill = min(monsters[-i], b+power_left)
    power_left = b - max(0, to_kill - power_left)
    ans += to_kill
ans += min(monsters[0], power_left)
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]:
    if B[i] >= A[i]+A[i+1]:
      sum += A[i]+A[i+1]
      A[i+1] = 0
    else:
      sum += B[i]
      A[i+1] -= B[i]-A[i]
  else:
    sum += B[i]
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    B = [int(x) for x in input().split()]

    ans = 0
    for idx in range(N):
        for inc in range(2):
            if A[idx+inc] <= B[idx]:
                ans += A[idx+inc]
                B[idx] -= A[idx+inc]
                A[idx+inc] = 0
            else:
                ans += B[idx]
                A[idx+inc] -= B[idx]
                B[idx] -= B[idx]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for i in range(N):
    if A[i] < B[i]:
        B[i] -= A[i]
        A[i] = 0
        A[i + 1] = max(0, A[i + 1] - B[i])
    else:
        A[i] -= B[i]
after_sum_A = sum(A)
print(sum_A - after_sum_A)

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

Submission status: Accepted

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

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

    if a <= b:
        ans += a
        A[i] = 0
        b -= a
    else:
        ans += b
        A[i] -= b
        b = 0

    if b > 0:
        if a2 <= b:
            ans += a2
            A[i+1] = 0
            b -= a2
        else:
            ans += b
            A[i+1] -= b
            b = 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 i in range(N):
    s = min(B[i],A[i])
    c += s
    B[i] = B[i]-s
    A[i] = A[i]-s
    t = min(B[i], A[i+1])
    c += t
    B[i] = B[i]-t
    A[i+1] = A[i+1]-t

print(c)

Submission status: Accepted

Solution code:
n = int(input())
monst = list(map(int,input().split()))
hero = list(map(int,input().split()))
attack = 0

for i in range(n):
    if monst[i] >= hero[i]:
        attack += hero[i]
    else:
        attack += monst[i]
        hero[i] = hero[i] - monst[i]
        if monst[i+1] > hero[i]:
            attack += hero[i]
            monst[i+1] = monst[i+1] - hero[i]
        else:
            attack +=  monst[i+1]
            monst[i+1] = 0
print(attack)

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
AB = list(map(int,read().split()))

A = AB[:N+1]; B = AB[N+1:]

B.append(0)
rest = 0
answer = 0
for a,b in zip(A,B):
    x = rest if rest < a else a
    answer += x; a -= x
    x = a if a<b else b
    answer += x; rest = b-x

print(answer)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(raw_input())
g=raw_input().split()
a=[int(x) for x in g]
g=raw_input().split()
b=[int(x) for x in g]
#n=len()
acc=0
for i in range(n):
	inc=min(a[i],b[i])
	a[i]-=inc
	b[i]-=inc
	acc+=inc
	inc=min(a[i+1],b[i])
	a[i+1]-=inc
	b[i]-=inc
	acc+=inc
print(acc)

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