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())
arrmon = list(map(int,input().split()))
arrhero = list(map(int,input().split()))

count = 0
i = 1
mon = arrmon[0]
hero = arrhero[0]
while i <= n:
  numin = min(mon,hero)
  count += numin
  hero -= numin
  mon = arrmon[i]
  
  numin = min(mon,hero)
  count += numin
  mon -= numin
  if i < n:
    hero = arrhero[i]
  
  i += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

# 重みがないのですべて小さい方から倒せると仮定する
cnt = 0
for i in range(N):
    cnt += min(B[i], A[i])
    diff = B[i] - A[i]
    if diff > 0:
        if A[i+1] < diff:
            cnt += A[i+1]
            A[i+1] = 0
        else:
            cnt += diff
            A[i+1] -= diff
        A[i] = 0
    else:
        A[i] -= B[i]
#print("A",A)
#print("B",B)
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())

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

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

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

print(suma)

Submission status: Accepted

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

Submission status: Accepted

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


def solve(N: int, A: "List[int]", B: "List[int]"):
    count = 0
    for i in range(N-1, -1, -1):
        if A[i+1] > B[i]:
            count += B[i]
        else:
            count += A[i+1]
            B[i] = B[i] - A[i+1]
            count += min(A[i], B[i])
            A[i] = max(A[i] - B[i], 0)
    print(count)
    return


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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

c, d = [0] * n, [0] * n
for i in range(n):
    c[i] = min(a[i] - d[i - 1], b[i]) if i != 0 else min(a[i], b[i])
    d[i] = min(a[i + 1], b[i] - c[i])
print(sum(c) + sum(d))

Submission status: Accepted

Solution code:
N = int(input())
As = [int(i) for i in input().split(" ")]
Bs = [int(i) for i in input().split(" ")]
ans = 0
for i in range(N):
    a = As[i]
    b = Bs[i]
    if a - b >= 0:
      ans += b
      As[i] = a - b
    else:
      rest = b - a
      ans += a
      As[i] = 0
      a2 = As[i + 1]
      if a2 - rest >= 0:
         As[i + 1] -= rest
         ans += rest
      else:
         As[i + 1] = 0
         ans += a2
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

N = int(input().strip())
As = list(map(int, input().strip().split(' ')))
Bs = list(map(int, input().strip().split(' ')))

count = 0
for i, b in enumerate(Bs):
    if As[i] >= b:
        count += b
    elif As[i] < b:
        if As[i+1] >= b - As[i]:
            count += b
            As[i+1] -= b - As[i]
        else:
            count += As[i] + As[i+1]
            As[i + 1] = 0
print(count)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
a=input().rstrip().split(" ")
b=input().rstrip().split(" ")
a[n]=int(a[n])
for i in range(n):
    a[i]=int(a[i])
    b[i]=int(b[i])
ans=0
for i in range(n):
    if(a[i] >b[i]):
        a[i]=a[i]-b[i]
        ans=ans+b[i]
       # print(ans)
        b[i]=0
    else:
        ans=ans+a[i]
        #print(ans)
        
        b[i]=b[i]-a[i]
        a[i]=0
        if(b[i] < a[i+1]):
            ans=ans+b[i]
           # print(ans)
            a[i+1]=a[i+1]-b[i]
            b[i]=0
        else:
            ans=ans+a[i+1]
            #print(ans)
          
            a[i+1]=0
print(ans)

Submission status: Accepted

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

ans = sum(A)

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

ans -= sum(A)

print(ans)

Submission status: Accepted

Solution code:
# ABC135c
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#C
import sys
#import math

n = int(input())

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
a = []
tm=0
for i in range(0,n+1):
	tm+=hoge[i]
	a.append(hoge[i])
#print(a)
#print(tm)


tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
b = []
for i in range(0,n):
	b.append(hoge[i])
#print(b)



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

tm2 = 0
for i in range(0,n+1):
	tm2+=a[i]
#print(a)
#print(tm2)
print(tm-tm2)

Submission status: Accepted

Solution code:
import sys

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

    rest = 0
    count = 0
    for a,b in zip(A,B):
        if rest > 0:
            count += min(a,rest)
            a -= rest
            if a <= 0:
                rest = b
                continue
        if a < b:
            rest = b-a
            count += a
        else:
            count += b
            rest = 0
    count += min(A[-1],rest)
    print(count)

if __name__=='__main__':
    main()

Submission status: Accepted

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


kill = 0
tmp = 0
for i in range(len(A)):
    if A[i] > tmp:
        A[i] = A[i] - tmp
        kill += tmp
    else:
        kill += A[i]
        A[i] = 0
    if B[i] > A[i]:
        tmp = B[i] - A[i]
        kill += A[i]
    else:
        tmp = 0
        kill += 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 n in range(N):
    d = B[n] - A[n]
    if d >= 0:
        ans += A[n]
        e = d - A[n+1]
        if e >= 0:
            ans += A[n+1]
            A[n+1] = 0
        else:
            A[n+1] -= d
            ans += d
    else:
        ans += B[n]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

surviver = As[0]
break_number = []

for i in range(N):
    monster_num = As[i+1] + surviver
    hero_num = Bs[i]
    if hero_num > monster_num:
        break_number.append(monster_num)
        surviver = 0
    else:
        break_number.append(hero_num)
        surviver = min(monster_num - hero_num, As[i+1])

print(sum(break_number))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0

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

print(count)

Submission status: Accepted

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


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

        kill = min(a[i], b[i])
        ans += kill
        a[i] -= kill
        b[i] -= kill

    return ans


if __name__ == '__main__':

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

    ans = solv(n, a, b)

    print(ans)

Submission status: Accepted

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

ans = 0
res = A[0]
for i in range(N):
  if (B[i] < res):
    #まるまる残る
    ans += B[i]
    res = A[i+1]
  else:
    ans += res
    b = B[i] - res
    res = A[i+1]
    if (b <= res):
      #一部倒せた
      ans += b
      res -= b
    else:
      #余った
      ans += res
      res = 0
print(ans)

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

Submission status: Accepted

Solution code:
n = int(input())
town = list(map(int,input().split()))
hero = list(map(int,input().split()))
before = sum(town)
for i in range(n):
  townhero = town[n-i] - hero[n-i-1]
  town[n-i] = max( 0, townhero )
  hero[n-i-1] = max( 0, -townhero )
  town[n-i-1] = max( 0, town[n-i-1] - hero[n-i-1] )
after = sum(town)
print( before - after)

Submission status: Accepted

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

mons = 0
for i in range(N):

    tmp = min(B[i], A[i])
    mons += tmp
    B[i] -= tmp

    tmp = min(B[i], A[i+1])
    mons += tmp
    A[i+1] -= tmp

print(mons)

Submission status: Accepted

Solution code:

N = int(input())
A_lst = list(map(int, input().split()))
B_lst = list(map(int, input().split()))

ans = 0

for i in range(N-1, -1, -1):
    if A_lst[i+1] >= B_lst[i]:
        ans += B_lst[i]
        A_lst[i+1] -= B_lst[i]
        B_lst[i] = 0
    else:
        ans += A_lst[i+1]
        B_lst[i] -= A_lst[i+1]
        A_lst[i+1] = 0

#    print("i=",i)
#    print(A_lst)
#    print(B_lst)
    
    if A_lst[i] >= B_lst[i]:
        ans += B_lst[i]
        A_lst[i] -= B_lst[i]
        B_lst[i] = 0
    else:
        ans += A_lst[i]
        B_lst[i] -= A_lst[i]
        A_lst[i] = 0

#    print("i=",i)
#    print(A_lst)
#    print(B_lst)
#    print()




print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
c = 0
ans = 0
for i in range(N):
    if A[i] - c <= 0:
        ans += A[i]
        c = B[i]
    elif A[i] - c <= B[i]:
        ans += A[i]
        c = B[i] - (A[i] - c)
    else:
        ans = ans + c + B[i]
        c = 0
ans += min(A[N], c)
print(ans)

Submission status: Accepted

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

for ai, bi in zip(a, b):
    if (prev + bi) > ai:
        res += ai
        tmp = ai - prev
        if tmp < 0:
            prev = bi
        else:
            prev = max(0, bi - tmp)
    else:
        res += prev + bi
        prev = 0

print(res)

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

n=int(input())
mon = list(map(int,input().split()))
kni = list(map(int,input().split()))
kill=0

for i in range(n):
    if kni[i]<=mon[i]:
        kill=kill+kni[i]
        mon[i]=mon[i]-kni[i]
    else:
        kill=kill+mon[i]
        if kni[i]-mon[i]<=mon[i+1]:
            kill=kill+kni[i]-mon[i]
            mon[i+1]=mon[i+1]-(kni[i]-mon[i])
        else:
            kill=kill+mon[i+1]
            mon[i+1]=0

print(kill)

Submission status: Accepted

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

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

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

    if 0 < A[i+1] <= B[i]:
        ans += A[i+1]
        B[i] = B[i] - A[i+1]
        A[i+1] = 0
    elif A[i+1] > B[i]:
        A[i+1] -= B[i]
        ans += B[i]
        B[i] = 0

print(ans)

Submission status: Accepted

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

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


main()

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
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 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

N=INT()
A=LIST()
B=LIST()

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

print(total-sum(A))

Submission status: Accepted

Solution code:
import sys

fin = sys.stdin.readline
N = [int(elem) for elem in fin().split()][0]
a_list = [int(elem) for elem in fin().split()]
b_list = [int(elem) for elem in fin().split()]


all_sum = 0
prev_b = 0

for i in range(len(a_list[:-1])):
    # 前の残りを引き算する
    current_a = a_list[i]
    prev_num = min(current_a, prev_b)
    current_a -= prev_num
    current_b = b_list[i]

    # 引き算する数
    current_num = min(current_a, current_b)
    # 残り
    prev_b = current_b - current_num
    # 総和
    all_sum += current_num + prev_num

all_sum += min(a_list[-1], prev_b)
print(all_sum)

Submission status: Accepted

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

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

  if flag :
    if a[i+1] > b[i] :
      a[i+1] -= b[i]
      cnt += b[i]
      b[i] = 0
    elif a[i+1] == b[i]:
      cnt += b[i]
      b[i] = 0
      a[i+1] = 0
    else :
      b[i] -= a[i+1]
      cnt += a[i+1]
      a[i+1] = 0
    flag = False

print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 27 20:48:33 2019

@author: ctiuser
"""

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

ans = 0
for i in range(N):
    tmp = B[i] - A[i]
    if tmp > 0:
        tmp2 = A[i+1] - tmp
        if tmp2 > 0:
            ans += B[i]
            A[i+1] = tmp2
        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 = list(map(int, input().split()))
B = list(map(int, input().split()))

kills = 0

for i in range(N):
    if B[i] <= A[i]:
        # 次無理
        kills += B[i]
    else:
        # 次OK
        kills += A[i]
        stock = B[i] - A[i]
        if stock < A[i + 1]:
            # 殲滅無理
            kills += stock
            A[i + 1] -= stock
        else:
            # 殲滅可能
            kills += A[i + 1]
            A[i + 1] = 0
print(kills)

Submission status: Accepted

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

main()

Submission status: Accepted

Solution code:
def main():
    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]
        elif a[i+1] >= b[i]-a[i]:
            ans += b[i]
            a[i+1] = a[i+1]-(b[i]-a[i])
        else:
            ans += a[i]+a[i+1]
            a[i+1] = 0
    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()))

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

Submission status: Accepted

Solution code:
import math
import fractions
import copy
import numpy as np
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
"""
#decomment when needed
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
"""
n = ip()
a  = iprow()
ans = sum(a)
b = iprow()
pl = 0
pr = n-1
if n==1:
    print(min(sum(b),sum(a)))
    exit(0)

while pl <= pr:
    if b[pl] - a[pl]>0:
        b[pl] -= a[pl]
        a[pl] = 0
    else:
        a[pl]-=b[pl]
        b[pl] = 0
    if b[pl]:
        if b[pl] - a[pl+1]>0:
            b[pl] = 0
            a[pl+1] = 0
        else:
            a[pl+1]-=b[pl]
            b[pl] = 0
    if b[pr] - a[pr+1] > 0:
        b[pr] -= a[pr + 1]
        a[pr+1] = 0
    else:
        a[pr+1] -= b[pr]
        b[pr] = 0
    if b[pr]:
        if b[pr] - a[pr] > 0:
            a[pr] = 0
            b[pr] = 0
        else:
            a[pr] -= b[pr]
            b[pr] = 0
    pl+=1
    pr-=1
print(ans-sum(a))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans_l = 0
rest = A[0]
for i in range(N):
    tmp = B[i]
    if rest < B[i]:
        ans_l += rest
        tmp -= rest
        if A[i+1] < tmp:
            ans_l += A[i+1]
            rest = 0
        else:
            ans_l += tmp
            rest = A[i+1] - tmp
    else:
        ans_l += B[i]
        rest = A[i+1]
ans_r = 0
A_r = A[::-1]
B_r = B[::-1]
rest = A_r[0]
for i in range(N):
    tmp = B_r[i]
    if rest < B_r[i]:
        ans_r += rest
        tmp -= rest
        if A_r[i+1] < tmp:
            ans_r += A_r[i+1]
            rest = 0
        else:
            ans_r += tmp
            rest = A_r[i+1] - tmp
    else:
        ans_r += B_r[i]
        rest = A_r[i+1]
print(max(ans_r, ans_l))

Submission status: Accepted

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

cnt = 0
for i in range(N):

    if b[i] <= a[i]:
        cnt += b[i]
    else:
        cnt += a[i]
        # 倒せる数を減らす
        b[i] -= a[i]
        # print(cnt)

        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())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

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

print(num)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
a_l = [int(_) for _ in input().split(' ')]
a_r = [_ for _ in a_l]

b = [int(_) for _ in input().split(' ')]

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

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

print(max(ans_l, ans_r))

Submission status: Accepted

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

def sig(n):
        if n>=0:
                return n
        else:
                return 0

sum_A = sum(A)
for i in range(N):
        hoge = B[i]
        A[i+1] = sig(A[i+1]-sig((B[i]-A[i])))
        A[i] = sig((A[i]-B[i]))

sum_remain = sum(A)
print(sum_A - sum_remain)

Submission status: Accepted

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

c = 0
leave = 0

for i, j in zip(a, b):
    atk = j + leave
    c += min(i, atk)
    if leave > i:
        leave = j
    elif leave + j > i:
        leave = j - (i - leave)
    else:
        leave = 0
print(c)

Submission status: Accepted

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

MONSTERS = A[:]
BRAVERS = B[:]

sum_monster = sum(MONSTERS)
#print(sum_monster)

for i, b in reversed(list(enumerate(BRAVERS))):
    if MONSTERS[i+1] >= b:
        MONSTERS[i+1] -= b
    else:
        b -= MONSTERS[i+1]
        MONSTERS[i+1] = 0
        if MONSTERS[i] >= b:
            MONSTERS[i] -= b
        else:
            MONSTERS[i] = 0
            
ans = sum_monster - sum(MONSTERS)
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

out = 0
for i in range(n):
    if a_list[i] < b_list[i]:
        val0 = a_list[i]
        out += val0
        b_list[i] -= val0
        #a_list[i] = 0
        if a_list[i+1] < b_list[i]:
            val1 = a_list[i+1]
            out += val1
            a_list[i+1] = 0
            #b_list[i] -= val1
        else:
            val1 = b_list[i]
            out += val1
            a_list[i+1] -= val1
    else:
        val0 = b_list[i]
        out += val0
        # b_list[i] = 0
        # a_list[i] -= val0

print(out)

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 0

for i in range(len(b)-1, -1, -1):

	taosu = min(b[i], a[i+1])
	ans += taosu
	a[i+1] -= taosu
	b[i] -= taosu

	taosu = min(b[i], a[i])
	ans += taosu
	a[i] -= taosu
	b[i] -= taosu

print(ans)