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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
monstors = [int(i) for i in input().split()]
heros = [int(i) for i in input().split()]
init_monstor = sum(monstors)

for i in range(N):
    if monstors[i] >= heros[i]:
        monstors[i] -= heros[i]
        heros[i] = 0
    else:
        heros[i] -= monstors[i]
        monstors[i] = 0
    if monstors[i+1] >= heros[i]:
        monstors[i+1] -= heros[i]
        heros[i] = 0
    else:
        heros[i] -= monstors[i+1]
        monstors[i+1] = 0
print("{}".format(init_monstor - sum(monstors)))

Submission status: Accepted

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

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

    tmp = B[i+1]-C[i]
    if(tmp>0):
        cnt+=C[i]
        B[i+1]-=C[i]
        C[i]=0
        
    else:
        cnt+=B[i+1]
        C[i]-=B[i+1]
        B[i+1]=0

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())

A = [int(z) for z in input().split()]
B = [int(w) for w in input().split()]

defeat = 0

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

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

print(score)

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    towns = list(map(int,input().split()))
    braves = list(map(int,input().split()))

    count = 0
    for b in range(n-1, -1, -1):
        if towns[b+1] > braves[b]:
            count = count + braves[b]
            towns[b+1] = towns[b+1] - braves[b]
            braves[b] = 0
            
        else:
            count = count + towns[b+1]
            braves[b] = braves[b] - towns[b+1]
            towns[b+1] = 0

        if braves[b]:
            if towns[b] > braves[b]:
                count = count + braves[b]
                towns[b] = towns[b] - braves[b]
                braves[b] = 0
                
            else:
                count = count + towns[b]
                braves[b] = braves[b] - towns[b]
                towns[b] = 0

    print(count)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT()
    A = LIST()
    B = LIST()
    rest = 0
    count = 0
    for i in range(N):
        if A[i] <= rest:
            count += A[i]
            tmp = 0
        else:
            count += rest
            tmp = A[i] - rest
        if tmp <= B[i]:
            count += tmp
            rest = B[i] - tmp
        else:
            count += B[i]
            rest = 0
    if A[-1] <= rest:
        count += A[-1]
    else:
        count += rest
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

defeat = 0
for i in range(n-1, -1, -1):
  if monsters[i+1] >= heros[i]:
    defeat += heros[i]
  else:
    defeat += monsters[i+1]
    heros[i] -= monsters[i+1]
    if monsters[i] >= heros[i]:
      defeat += heros[i]
      monsters[i] -= heros[i]
    else:
      defeat += monsters[i]
      monsters[i] = 0
      
print(defeat)

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

print(ans)

Submission status: Accepted

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

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

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]:
            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())
mati = list(map(int,input().split()))
yuu = list(map(int,input().split()))
monster = 0
for i in range(N,0,-1):
    x = mati[i] + mati[i-1]
    if x <= yuu[i-1]:
        monster += x
        mati[i],mati[i-1] = 0,0
    else:
        monster += yuu[i-1]
        if yuu[i-1] >= mati[i]:
            mati[i-1] += -(yuu[i-1]-mati[i])
            mati[i] = 0
        else:
            mati[i] = mati[i] - yuu[i-1]
print(monster)

Submission status: Accepted

Solution code:
import numpy as np

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


result = 0

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

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
N = int(raw_input())
A = [int(i) for i in raw_input().split()]
B = [int(i) for i in raw_input().split()]

rest = [0] * N
score = [0] * (N + 1)

for i in range(0, N + 1):
    if i == 0:
        if A[i] < B[i]:
            score[i] = A[i]
            rest[i] = B[i] - A[i]
        else:
            score[i] = B[i]
            rest[i] = 0
    elif i == N:
        score[i] = min([A[i], rest[i - 1]])
    else:
        if (A[i] - rest[i - 1]) < B[i]:
            score[i] = A[i]
            rest[i] = min([B[i] - A[i] + rest[i - 1], B[i]])
        else:
            score[i] = B[i] + rest[i - 1]
            rest[i] = 0
total = 0
for i in score:
    total = total + i

print total

Submission status: Accepted

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

B.append(0)
k = 0
answer = 0

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

Submission status: Accepted

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


def count(A, B):
    ret = 0
    for i in range(N - 1, -1, -1):
        m = min(A[i + 1], B[i])
        B[i] -= m
        ret += m
        m = min(B[i], A[i])
        A[i] -= m
        ret += m
    return ret


print(max(count(A, B), count(A[::-1], B[::-1])))

Submission status: Accepted

Solution code:
n = int(input())
li_city = list(map(int,input().split()))
li_hero = list(map(int,input().split()))
score = 0
for i in range (n):
  if li_hero[i] >= li_city[i] + li_city[i + 1]:
    score += li_city[i] + li_city[i + 1]
    li_city[i + 1] = 0
  elif li_hero[i] >= li_city[i]:
    score += li_hero[i]
    li_city[i + 1] -= li_hero[i] - li_city[i]
  else:
    score += li_hero[i]
print(score)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
cn = int(input())
enm = input().split(" ")
brv = input().split(" ")
taoshita = int(0)

for i in range(0,cn):
    taoshita = taoshita + min(abs(int(brv[i])),abs(int(enm[i])))  
    enm[i] = int(enm[i]) - int(brv[i])
    if int(enm[i]) < 0:
        brv[i] = abs(int(enm[i]))
        enm[i] = 0
        if (int(enm[i+1]) - int(brv[i])) < 0:
            taoshita = taoshita + int(enm[i+1])
            enm[i+1] = 0
        if (int(enm[i+1]) - int(brv[i])) >= 0:
            enm[i+1] = int(enm[i+1]) - int(brv[i])
            taoshita = taoshita + int(brv[i])
print(taoshita)

Submission status: Accepted

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

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

cnt += a[n] if amari >= a[n] else amari

print(cnt)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
n=int(input())
aa=list(map(int,input().split()))
bb=list(map(int,input().split()))
s = 0
for i,b in enumerate(bb):
    t = min(aa[i]+aa[i+1], b)
    s += t
    if aa[i] >= t:
        aa[i] -= t
    else:
        t -= aa[i]
        aa[i] = 0
        aa[i+1] -= t
print(s)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
n=0
m=0
for i in range(N):
	n=n+int(min(A[i]+A[i+1],B[i]))
	if A[i]<=B[i] and A[i]+A[i+1]>=B[i]:
		A[i+1]=A[i+1]-(B[i]-A[i])
	elif A[i]<=B[i] and A[i]+A[i+1]<=B[i]:
		A[i+1]=0
for i in reversed(range(N)):
	m=m+int(min(A[i]+A[i+1],B[i]))
	if A[i+1]<=B[i] and A[i+1]+A[i]>=B[i]:
		A[i]=A[i]-(B[i]-A[i+1])
	elif A[i+1]<=B[i] and A[i+1]+A[i]<=B[i]:
		A[i]=0
print(max(m,n))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = B[:] + [0]

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

Submission status: Accepted

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

ans = B[:] + [0]

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

Submission status: Accepted

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

count = 0

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

        if a[i+1] >= b[i]:
            a[i+1] -= b[i]
            count += b[i]
            b[i] = 0
            continue
        
        else:
            b[i] -= a[i+1]
            count += a[i+1]
            a[i+1] = 0
            continue

print(count)

Submission status: Accepted

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

C_kill = 0
try:
    for i in range(N):
        C_kill = C_kill + min(A[i], B[i])
        B[i] = max(0, B[i]-A[i])
        C_kill = C_kill + min(A[i+1], B[i])
        A[i+1] = max(0, A[i+1] - B[i])
except:
    print("ERROR")

print(C_kill)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

N = int(input())
A = [int(v) for v in input().split()]
B = [int(v) for v in input().split()]
s = [0] * (N + 1)

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

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

Submission status: Accepted

Solution code:
n = int(input())
monsters = [int(value) for value in input().split()]
heros = [int(value) for value in input().split()]

#倒すことのできるモンスターの数
defeatable_monsters = 0

#リストを逆順にする
rev_monsters = monsters[::-1]
rev_heros = heros[::-1]

for offset, hero in enumerate(rev_heros):
    if hero > rev_monsters[offset]:
        defeatable_monsters += rev_monsters[offset]
        hero -= rev_monsters[offset]
        rev_monsters[offset] = 0
    else:
        defeatable_monsters += hero
        rev_monsters[offset] -= hero
        hero = 0
        continue

    if hero > rev_monsters[offset+1]:
        defeatable_monsters += rev_monsters[offset+1]
        hero -= rev_monsters[offset+1]
        rev_monsters[offset+1] = 0
    else:
        defeatable_monsters += hero
        rev_monsters[offset+1] -= hero
        hero = 0


print(defeatable_monsters)

Submission status: Accepted

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

B=[]
for x in input().split():
    B.append(int(x))

B.append(int(0))    

TOT = sum ( A )

for i in range (n) :
    KILL = min ( A[i], B[i] )
    A [i] = A [i] - KILL
    A [i+1] = A[i+1] - min (A[i+1], B[i] - KILL)
    
print ( TOT - 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 index, brave in enumerate(b):
    ans += min(a[index] + a[index+1], brave)
    a[index+1] -= max(0, brave - a[index])
    if a[index+1] < 0:
        a[index+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 = []

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

print(sum(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, b in enumerate(B):
    if A[i] >= b:
        ans += b
    else:
        ans += A[i]
        diff = b - A[i]
        if A[i+1] >= diff:
            ans += diff
            A[i+1] -= diff
        else:
            ans += max(A[i+1], 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()))
ans = 0
for i in range(N):
    ans_tmp = min(A[i],B[i])
    ans += ans_tmp
    A[i] = A[i] - ans_tmp
    B[i] = B[i] - ans_tmp
    ans_tmp = min(A[i+1],B[i])
    ans += ans_tmp
    A[i+1] = A[i+1] - ans_tmp
    B[i] = B[i] - ans_tmp
print(ans)

Submission status: Accepted

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

import copy
cnt=0
BB = copy.deepcopy(B) + ([[0,0],[0,0]])
for i, ai in enumerate(A):
    if sum(BB[i]) > ai:
        cnt += ai
        if BB[i][0] > ai:
            BB[i+1][0] += BB[i][1]
        else:
            BB[i+1][0] += sum(BB[i]) - ai
    else:       
        cnt += sum(BB[i])

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
m = list(map(int, input().split()))
s = list(map(int, input().split()))
atk = 0
for i, e in enumerate(s):
    rem = e
    am = m[i]
    am2 = m[i+1]
    m[i] = max(0, am - rem)
    rem -= am - m[i]
    m[i+1] = max(0, am2 - rem)
    rem -= am2 - m[i+1]
    atk += e - rem
print(atk)

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int,input().split()))
B_list = list(map(int,input().split()))
kill_cnt=0

for i in range(N):
  if A_list[i] + A_list[i+1] <= B_list[i]:
    kill_cnt += (A_list[i] + A_list[i+1])
    A_list[i]=0
    A_list[i+1]=0
  else:
    kill_cnt += B_list[i]
    if A_list[i]>=B_list[i]:
      A_list[i]=A_list[i]-B_list[i]
    else:
      A_list[i+1] = A_list[i+1] - (B_list[i]-A_list[i])
      
print(kill_cnt)

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):
    n_defeat = min(A[i], B[i])
    A[i] -= n_defeat
    B[i] -= n_defeat
    count += n_defeat
    n_defeat = min(A[i + 1], B[i])
    A[i + 1] -= n_defeat
    B[i] -= n_defeat
    count += n_defeat

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

Submission status: Accepted

Solution code:
n = int(input())
monster = list(map(int, input().split()))
brave = list(map(int, input().split()))

cnt = 0

for i in range(n):
    kill1 = min(monster[i], brave[i])
    monster[i] -= kill1
    brave[i] -= kill1

    kill2 = min(monster[i+1], brave[i])
    monster[i+1] -= kill2
    brave[i] -= kill2

    cnt += (kill1+kill2)

print(cnt)

Submission status: Accepted

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

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

ans += min(prev,A[N])

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

    ans += min(a, b)

    if b > a:
        ans += min(a_next, (b-a))
        a_next -= min(a_next, (b-a))

    a = a_next

print(ans)

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 range(N):
    a = a_list[i]
    b = b_list[i]
    if a >= b:
        cnt += b
    else:
        cnt += a
        kill = min(a_list[i+1], b-a)
        cnt += kill
        a_list[i+1] -= kill
        
print(cnt)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]


def main():

  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)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

def main():

  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)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
for i, b in enumerate(b_list):
    res += min(a_list[i], b)
    res += min(a_list[i+1], b-min(a_list[i],b))
    a_list[i+1] -= min(b-min(a_list[i],b), a_list[i+1])
print(res)

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin

N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
B = [int(_) for _ in stdin.readline().rstrip().split()]

ans = 0

for i, b in enumerate(B):
    ans += min(A[i], b)
    b -= min(A[i], b)
    ans += min(A[i+1], b)
    A[i+1] -= min(b, A[i+1])

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

print(count)

Submission status: Accepted

Solution code:
n = int(input())
monsters = [int(x) for x in input().rstrip("\n").split(" ")]
damages = [int(x) for x in input().rstrip("\n").split(" ")]
total_monsters = sum(monsters)
result = 0
remain = monsters[n]
for i in range(n-1,-1,-1):
  
  damage = damages[i]
  monster = monsters[i]
  #print(i, remain,damage, monster, result)
  if damage <= remain:
    result += damage
    remain = monster
  else:
    damage -= remain
    result += remain
    if monster >= damage:
      remain = monster - damage
      result += damage
    else:
      result += monster
      remain = 0
print(result)

Submission status: Accepted

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