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

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

print(to)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def calcRestNum(a, b, ret):
    if a < b:
        ret += a
        b -= a
        a = 0
    else:
        ret += b
        a -= b
        b = 0
    return [a, b, ret]

if __name__ == '__main__':
    N = int(input())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]

    ret = 0
    for n in reversed(range(N)):
        A[n + 1], B[n], ret = calcRestNum(A[n + 1], B[n], ret)
        A[n], B[n], ret = calcRestNum(A[n], B[n], ret)
    print(ret)

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


print(ans)

Submission status: Accepted

Solution code:
N = int(input())
M = list(map(int, input().split()))
Y = list(map(int, input().split()))
mtotal = sum(M)


for idx in range(len(Y)):
  if Y[idx] >= M[idx]:
    Y[idx] -= M[idx]
    M[idx] = 0
    M[idx+1] = max(M[idx+1] - Y[idx], 0)
  else:
    M[idx] -= Y[idx]
    Y[idx] = 0
    
  #print(M)
  #print(Y)

print(mtotal - sum(M))

Submission status: Accepted

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

first = sum(a)

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

print(first - sum(a))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]
def_Bsum = sum(B)
 
for i in range(N+1):
    if(i==0):                  #倒せたら０にする。最後に引いたら残るように
        if (A[i] < B[i]):
            B[i] -= A[i]
            A[i] = 0
        else:
            A[i] -= B[i]
            B[i] = 0
 
    if(0<i<N):
        if(A[i] < B[i-1]):
            B[i-1] -= A[i]
            A[i] = 0
        else:
            A[i] -= B[i-1]
            B[i-1] = 0
 
            if (A[i] < B[i]):
                B[i] -= A[i]
                A[i] = 0
            else:
                A[i] -= B[i]
                B[i] = 0
 
    if(i==N):
        if (A[i] < B[i-1]):
            B[i-1] -= A[i]
            A[i] = 0
        else:
            A[i] -= B[i-1]
            B[i-1] = 0
 
 
ans = def_Bsum - sum(B)

print(ans)

Submission status: Accepted

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

# print(A)
# print(B)
# print(P_true)
def_Bsum = sum(B)

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

    if(0<i<N):
        if(A[i] < B[i-1]):
            B[i-1] -= A[i]
            A[i] = 0
        else:
            A[i] -= B[i-1]
            B[i-1] = 0

            if (A[i] < B[i]):
                B[i] -= A[i]
                A[i] = 0
            else:
                A[i] -= B[i]
                B[i] = 0

    if(i==N):
        if (A[i] < B[i-1]):
            B[i-1] -= A[i]
            A[i] = 0
        else:
            A[i] -= B[i-1]
            B[i-1] = 0


ans = def_Bsum - sum(B)
# print(def_Bsum)
# print(sum(B))

print(ans)

Submission status: Accepted

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

ans1[0]=min(a[0],b[0])
ans2[0]=min(a[1],b[0]-min(a[0],b[0]))

for i in range(1,n):
    ans1[i]=min(a[i]-ans2[i-1],b[i])
    ans2[i]=min(a[i+1],b[i]-ans1[i])

print(sum(ans1)+sum(ans2))

Submission status: Accepted

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

o=0

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

print(o)

Submission status: Accepted

Solution code:
n = int(input())
li_a = list(map(int, input().split()))
li_b = list(map(int, input().split()))

li_a_sum = sum(li_a)
answer = 0


for idx in range(n):
    if li_a[idx] >= li_b[idx]:
        li_a[idx] -= li_b[idx]
        li_b[idx] = 0
    else:
        li_b[idx] = li_b[idx] - li_a[idx]
        li_a[idx] = 0

    if li_b[idx] > 0:
        if li_a[idx + 1] >= li_b[idx]:
            li_a[idx + 1] = li_a[idx + 1] - li_b[idx]
            li_b[idx] = 0
        else:
            li_b[idx] -= li_a[idx + 1]
            li_a[idx + 1] = 0

print(li_a_sum - sum(li_a))

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

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

c=0
for i in range(n):
      ci=min(a[i],b[i])
      c += ci
      b[i] -= ci
      ci1 = min(a[i+1],b[i])
      c += ci1
      a[i+1] -= ci1

print(c)

Submission status: Accepted

Solution code:
from collections import deque

def main():
    n = int(input())
    a = list(int(x) for x in input().split())
    b = list(int(x) for x in input().split())
    
    que = deque(a)
    ans = 0
    
    for i in b:
        q1 = que.popleft()
        if i >= q1:
            ans += q1
            q2 = que.popleft()
            if i-q1 >= q2:
                ans += q2
                que.appendleft(0)
            else: #i-q1 < q2
                ans += i-q1
                que.appendleft(q2-(i-q1))
        else: #i < q1
            ans += i
    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()))
B.append(0)

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

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

print(max-sum(b))

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

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

print(total-a[-1])

Submission status: Accepted

Solution code:
def City():
  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(len(B)):
    if A[i]<=B[i]:
      ans += A[i]
      B[i] -= A[i]
      A[i] = 0
    else:
      ans += B[i]
      A[i] -= B[i]
      B[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
  print(ans)
if __name__=="__main__":
  City()

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

print(sum([c - a for (c, a) in zip(C, A)]))

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    b = list(map(int, input().rstrip('\n').split()))
    cnt = 0
    for i in range(n):
        h = min(a[i], b[i])
        cnt += h
        a[i] -= h
        b[i] -= h
        h = min(a[i+1], b[i])
        cnt += h
        a[i+1] -= h
        b[i] -= h
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from fractions import gcd
from math import ceil,floor,sqrt
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
for i in range(n):
    tmp = min(a[i], b[i])
    ans += tmp
    b[i] -= tmp
    tmp = min(a[i+1], b[i])
    ans += tmp
    a[i+1] -= tmp

print(ans)

Submission status: Accepted

Solution code:
def func(N, A_i, B_i):
    ans = 0
    for i in range(N):
        ans += min(A_i[i], B_i[i]) + min(A_i[i+1], max(0, B_i[i] - A_i[i]))
        A_i[i+1] = max(0, A_i[i+1] - max(0, B_i[i] - A_i[i]))
    return ans

N = int(input())
A_i = list(map(int, input().split(" ")))
B_i = list(map(int, input().split(" ")))
print(func(N, A_i, B_i))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_s = list(map(int, input().split(' ')))
    b_s = list(map(int, input().split(' ')))
    ans = 0
    for i in range(n):
        if b_s[i] >= a_s[i]:
            ans += a_s[i]
            if (b_s[i] - a_s[i]) <= a_s[i+1]:
                ans += (b_s[i] - a_s[i])
                a_s[i+1] -= (b_s[i] - a_s[i])
            else:
                ans += a_s[i+1]
                a_s[i+1] = 0
        else:
            ans += b_s[i]
    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()))
b.append(0)
c = [0]*(n+2)
ans = 0
for i in range(n+1):
    if -a[i]+c[i]>=0:
        ans+=a[i]
        c[i+1] = b[i]
    else:
        if b[i]-a[i]+c[i]>=0:
            ans+=a[i]
            c[i+1] = b[i]-a[i]+c[i]
        elif b[i]-a[i]+c[i]<0:
            ans += b[i]+c[i]
print(ans)

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

Submission status: Accepted

Solution code:
N = int(input())
A_l = list(map(int, input().split()))
B_l = list(map(int, input().split()))

# 番号が若い勇者、番号が若い街から可能な限りモンスターを倒せばよい

killed_monsters = 0

for idx in range(N-1):
    killed_monsters += min(A_l[idx], B_l[idx])
    if B_l[idx] - A_l[idx] > 0:
        killed_monsters += min(B_l[idx] - A_l[idx], A_l[idx+1])
        A_l[idx+1] = max(0, A_l[idx+1] - (B_l[idx] - A_l[idx]))

killed_monsters += min(A_l[-2] + A_l[-1], B_l[-1])

print(killed_monsters)

Submission status: Accepted

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

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

print(answer)

Submission status: Accepted

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

ans=0
z=0

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

print(ans+min(z,A[N]))

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

    if ex != 0:
        if a[i+1] - ex <= 0:
            knock+=a[i+1]
            a[i+1]=0
        else :
            knock+=ex
            a[i+1]=a[i+1]-ex

print(knock)

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    lines = stdin.readlines()
    A = tuple([int(i) for i in lines[1].split()])
    B = tuple([int(i) for i in lines[2].split()])
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    lines = stdin.readlines()
    A = tuple(int(i) for i in lines[1].split())
    B = tuple(int(i) for i in lines[2].split())
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    lines = stdin.readlines()
    A = [int(i) for i in lines[1].split()]
    B = [int(i) for i in lines[2].split()]
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    lines = stdin.readlines()
    A = tuple(map(int, lines[1].split()))
    B = tuple(map(int, lines[2].split()))
    pre = A[0]
    ans = 0
    for a, b in zip(A[1:], B):
        tmp1 = pre if pre < b else b
        b -= tmp1
        tmp2 = a if a < b else b
        ans += tmp1 + tmp2
        pre = a - tmp2
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin


def main():
    lines = stdin.readlines()
    A = deque(map(int, lines[1].split()))
    B = deque(map(int, lines[2].split()))
    cnt = 0
    ans = 0
    while B:
        a = A.pop()
        b = B.pop()
        tmp = a if a < b else b
        ans += tmp
        if cnt % 2:
            A.append(a - tmp)
        else:
            B.append(b - tmp)
        cnt += 1
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
n=int(input())
A = list(map( int, input().split( )))
B = list(map( int, input().split( )))
#左から順に倒す
C = [0] * (n+1)
#左隣からの応援
ans = 0
for i in range(n):
    C[i+1] = min(max(B[i]+C[i]-A[i], 0),B[i])
    ans += min(A[i], B[i] + C[i])

ans += min(A[n], C[n])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

cities = list(map(int, input().split()))
savers = list(map(int, input().split()))

result = 0

for i in range(N):
  left = min(cities[i], savers[i])
  cities[i] -= left
  savers[i] -= left
  result += left
  
  right = min(cities[i + 1], savers[i])
  cities[i + 1] -= right
  savers[i] -= right
  result += right

print(result)

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

Submission status: Accepted

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

B.append(0)
punitives = []

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

    if B[-i-1] >= A[-i]:
        v = A[-i]
        A[-i] = 0
        B[-i-1] = B[-i-1]-v
    else:
        v = B[-i-1]
        A[-i] = A[-i]-v
        B[-i-1] = 0
    punitives.append(v)

if A[0] >= B[0]:
    w = B[0]
else:
    w = A[0]

punitives.append(w)
print(sum(punitives))

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

print(c)

Submission status: Accepted

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

Submission status: Accepted

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

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

monster_count = 0
next_saver = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
for i in range(n):
    res += min(A[i], B[i])
    if B[i] > A[i]:
        tmp = min(B[i] - A[i], A[i+1])
        res += tmp
        A[i+1] -= tmp
print(res)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
s=0
rd=0
for i in range(N):
    if a[i] < b[i]+rd:
        d = a[i]
        rd = b[i]+rd - a[i]
        rd = rd if rd < b[i] else b[i]
    else:
        d = b[i] + rd
        rd=0
    s+=d
s+=rd if rd < a[N] else a[N]
print(s)

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]
#         A[i] -= B[i]
#     else:
#         # 全て倒せる
#         B[i] -= A[i]
#         ans += A[i]
#         A[i] = 0
#         # 次の場所へ
#         if(A[i+1] > B[i]):
#             #次の場所はすべて倒せない
#             ans += B[i]
#             A[i+1] -= B[i]
#         else:
#             #次の場所もすべて倒せる
#             ans += A[i+1]
#             A[i+1] = 0
# =============================================================================

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

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

# =============================================================================
# if A[0] > B[0]:
#     ans += B[0]
# else:
#     # 全て倒せる（次の町を参照
#     B[0] -= A[0]
#     ans += A[0]
#     A[1] -= B[0]
#     ans += B[0]
# =============================================================================

for i in range(N):
    if(A[i] > B[i]):
        # 全て倒せない
        ans += B[i]
        A[i] -= B[i]
    else:
        # 全て倒せる
        B[i] -= A[i]
        ans += A[i]
        A[i] = 0
        # 次の場所へ
        if(A[i+1] > B[i]):
            #次の場所はすべて倒せない
            ans += B[i]
            A[i+1] -= B[i]
        else:
            #次の場所もすべて倒せる
            ans += A[i+1]
            A[i+1] = 0
#    print(A[i],B[i])
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(defeated_monster)

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

Submission status: Accepted

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

c = 0
for i in range(n):
  # 街のモンスター一部倒す
  if a[i] >= b[i]:
    c += b[i]
  else:
    # 街のモンスター全て倒す
    c += a[i]
    # 余力
    d = b[i] - a[i]
    # 隣街のモンスター一部倒す
    if a[i + 1] >= d:
      c += d
      a[i + 1] = a[i + 1] - d
    # 隣街のモンスター全て倒す
    else:
      c += a[i + 1]
      a[i + 1] = 0
      
print(c)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())

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

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

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

Submission status: Accepted

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

def calc():
    result = 0
    for i in range(N):
        x = min(A[i], B[i])
        A[i] -= x
        B[i] -= x
        result += x
        x = min(A[i + 1], B[i])
        A[i + 1] -= x
        B[i] -= x
        result += x
    return result

print(calc())

Submission status: Accepted

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

C = [0] * (N + 1)

B.append(0)
kill = 0

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

print(kill)

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

Submission status: Accepted

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

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

print(d_sum - sum(A))

Submission status: Accepted

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

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

print(total)

Submission status: Accepted

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

Submission status: Accepted

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

L=[-1 for i in range(N)]
R=[-1 for i in range(N)]

L[0]=min(A[0],B[0])
R[0]=min(A[1],B[0]-L[0])

for i in range(1,N):
    L[i]=min(A[i]-R[i-1],B[i])
    R[i]=min(A[i+1],B[i]-L[i])
    
print(sum(L)+sum(R))