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

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

print(beated_monsters)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    mon=0
    for i,x in enumerate(b):
        mon = mon+min(a[i],x)
        tmp=x-a[i]
        if tmp>0:
            u = min(a[i+1],tmp)
            mon = mon+u
            a[i+1] = a[i+1]-u
    print(mon)


if __name__ == "__main__":
    resolve()

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

Submission status: Accepted

Solution code:
def c_city_savers(N, A, B):
    ans = 0
    for i in range(N):
        if A[i] >= B[i]:
            ans += B[i]
            A[i] -= B[i]
            B[i] = 0
            continue
        else:
            ans += A[i]
            B[i] -= A[i]
            A[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
    return ans

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
print(c_city_savers(N, A, 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(1, n + 1):
    if b[i - 1] < a[i - 1]:
        ans += b[i - 1]
    else:
        ans += a[i - 1]
        b[i - 1] -= a[i - 1]
        if b[i - 1] > a[i]:
            ans += a[i]
            a[i] = 0
        else:
            ans += b[i - 1]
            a[i] -= b[i - 1]
print(ans)

Submission status: Accepted

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

res = 0
for i in range(N):
    x = min(A[-1-i], B[-1-i])
    res += x
    A[-1-i] -= x
    y = min(A[-1-i], B[-2-i])
    res += y
    A[-1-i] -= y
    B[-2-i] -= y

x = min(A[0], B[0])
res += x
print(res)

Submission status: Accepted

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

count = 0
residual = A[0]

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

Submission status: Accepted

Solution code:
N = int(input())
As = [int(ai) for ai in input().split(' ')]
Bs = [int(bi) for bi in input().split(' ')]
S = sum(As)

As = As[::-1]
Bs = Bs[::-1]

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

print(S-sum(As))

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
carry = 0
for i in range(N+1):
    #print(carry, B[i], A[i])
    cnt += min(A[i], carry+B[i])
    carry = max(0, B[i]-max(0,A[i]-carry))
    #print(cnt, '\n')

print(cnt)

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):
	#print(A,B)
	num1 = min(B[i],A[i])
	B[i] -= num1
	A[i] -= num1
	#print(A,B)
	num2 = min(B[i],A[i+1])
	B[i] -= num2
	A[i+1] -= num2
	total += num1+num2
print(total)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    ai = input().split(' ')
    bi = input().split(' ')

    count = 0
    for i in range(n):
        a0 = int(ai[i])
        b0 = int(bi[i])
        if b0 <= a0:
            count += b0
            continue
        else:
            count += a0
            a1 = int(ai[i+1])
            count += min(b0 - a0, a1)
            ai[i + 1] = str(max(a1 - (b0 - a0), 0))
    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(' ')))
#print(A)
#print(B)

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

#print(A)
#print(B)
print(defeated_num)

Submission status: Accepted

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

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

count = 0
for i in range(len(a)) : 
	count += _a[i] - a[i]
print(count)

Submission status: Accepted

Solution code:
import numpy as np
import math
import sys
sys.setrecursionlimit(10000000)

def main():
    N = int(input())
    A = [int(c) for c in input().split()]
    B = [int(c) for c in input().split()]
    cnt = 0
    for i in range(N):
        each_cnt = min(A[i], B[i])
        A[i] -= each_cnt
        B[i] -= each_cnt
        cnt += each_cnt
        if B[i] != 0:
            each_cnt = min(A[i + 1], B[i])
            A[i + 1] -= each_cnt
            B[i] -= each_cnt
            cnt += each_cnt
        else:
            continue
    print(cnt)
            


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

# N <= 10^5
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]

ans = 0

for i in range(N):
    if A[i] > B[i]:
        ans += B[i]
    else:
        B[i] -= A[i]
        ans += 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:
#!/usr/bin/env python3
#ABC135 C

import sys
import math
import bisect
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
mod = 10**9 + 7

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    
    ans = 0
    rem = 0

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

    print(ans)

        
    





main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import defaultdict, deque
import math
from math import factorial
import fractions
import re

BIGNUM = 10 ** 9 + 7

def main():
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    num = 0
    for i in range(N):
        idx = (N - 1) - i
        B = Bs[idx]
        A_prev = As[idx+1]
        A = As[idx]
        num_prev_monster = min([A_prev, B])
        B -= num_prev_monster
        num_monster = min([A, B])
        As[idx] -= num_monster
        num += num_prev_monster + num_monster

    print(num)

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

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

print(res)

Submission status: Accepted

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

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

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

print(max(ans1, ans2))

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
import re
from collections import deque
import sys
import math
input = sys.stdin.readline


def int_raw():
    return int(input())


def ss_raw():
    return input().split()


def ints_raw():
    return tuple(map(int, ss_raw()))


DIV = 10**9+7


def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret


def main():
    N = int_raw()
    As = ints_raw()
    Bs = ints_raw()
    ans = 0
    resB = 0
    for i in range(N):
        Bi = Bs[i]
        Ai = As[i] 
        ans += min(Bi+resB,Ai)

        resB = max(Bi-max(Ai-resB,0),0)

    return ans+min(resB,As[-1])
print(main())

Submission status: Accepted

Solution code:
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
# 一次元累積和
from itertools import accumulate
from bisect import bisect_left, bisect_right

import re
# import numpy as np
# from scipy.misc import comb

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e18)
MOD = int(1e9)+7 # 10^9 + 7

# W行H列, x[W][H]
def create_grid(W, H, value = 0):
    return [[ value for _ in range(H)] for _ in range(W)]

########

N = i_inpl()
A = l_inpl()
B = l_inpl()

ans = 0

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

print(ans)
# N = i_inpl()
# p = l_inpl()

# l = list(range(1, N+1))

# cnt = 0
# for pi, li in zip(p, l):
#     if pi != li:
#         cnt += 1

# if cnt == 2 or cnt == 0:
#     print("YES")
# else:
#     print("NO")

Submission status: Accepted

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

x = 0
for i in range(n):
    y = b[i] - a[i]
    if (y <= 0): #余力なし
        x += b[i]
    else:
        x += a[i]
        if (a[i+1] < y): #過剰勇者
            x += a[i+1]
            a[i+1] = 0
        else:
            x += y
            a[i+1] -= y

print(x)

Submission status: Accepted

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

pre = 0
ans = 0
for i, a in enumerate(A):
    if i == N:
        ans += min(pre, a)
        break
    if a <= pre:
        ans += a
        pre = B[i]
    elif pre < a < pre + B[i]:
        ans += a
        pre = B[i]+pre - a
    else:
        ans += pre + B[i]
        pre = 0

print(ans)

Submission status: Accepted

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

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

    kill += x1 + x2

print(kill)

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

        ans += min(A[i+1], B[i])
        A[i+1] = max(0, A[i+1] - B[i])
    else:
        ans += B[i]
        A[i] -= B[i]
print(ans)

Submission status: Accepted

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

cnt=0

for i in range(n):
    for j in range(2):
        x=a[i+j]
        y=b[i]
        m=min(x,y)
        b[i]-=m
        a[i+j]-=m
        cnt+=m

print(cnt)

Submission status: Accepted

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

cnt = 0
beaten = min(a[0], b[0])
b[0] -= beaten
cnt += beaten
for i in range(1, len(b)):
    beaten = min(a[i], b[i-1])
    a[i] -= beaten
    cnt += beaten
    beaten = min(a[i], b[i])
    b[i] -= beaten
    cnt += beaten
beaten = min(a[len(a)-1], b[len(b)-1])
cnt += beaten

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
T=0;
p=input().rstrip().split(' ')
q=input().rstrip().split(' ')
for i in range(0,len(q)):
    A=int(q[i])
    B=int(p[i])
    C=int(p[i+1])
    if A>=B:
        T+=B;
        A=A-B;
        if(A>=C):
            T+=C;
            p[i+1]=0;
        else:
            p[i+1]=int(p[i+1])-A;
            T+=A;
    else:
        T+=A;
print(T)

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int,raw_input().split())
b = map(int,raw_input().split())
ans = 0
for i in range(n):
  temp = min(a[i],b[i])
  a[i] -= temp
  b[i] -= temp
  ans += temp
  temp = min(a[i+1],b[i])
  a[i+1] -= temp
  b[i]   -= temp
  ans += temp
print ans

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
ans=0
for i in range(N):
  if A[i]>=B[i]:
    ans+=B[i]
  else:
    ans+=A[i]
    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:
from sys import stdin

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

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

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
for i in range(n):
    if b[i]<a[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())
mosters = list(map(int, input().split()))
heros = list(map(int, input().split()))
cnt = 0

for i in range(N):
    if heros[i] <= mosters[i]:
        cnt += heros[i]
    else:
        cnt += mosters[i]
        if (heros[i] - mosters[i]) >= mosters[i+1]:
            cnt += mosters[i+1]
            mosters[i+1] = 0
        else:
            cnt += heros[i] - mosters[i]
            mosters[i+1] -= (heros[i] - mosters[i])

print(cnt)

Submission status: Accepted

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

cnt = 0

cnt += min(a[0], b[0])
b[0] -= min(a[0], b[0])

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

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

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
num = 0
for i in range(N):
    left = min(A[i], B[i])
    num += left
    B[i] -= left
    right = min(A[i+1], B[i])
    num += right
    A[i+1] -= right
print(num)

Submission status: Accepted

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

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

ans = 0
for i in range(N):
    added = min(As[i], Bs[i])
    Bs[i] -= added
    added2 = min(As[i + 1], Bs[i])
    As[i + 1] -= added2
    ans += added + added2
print(ans)

Submission status: Accepted

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

dead = 0
yuusya_zan = 0
for i, yuusya in enumerate(b):
    monster = a[i]

    if yuusya_zan >= monster:
        dead += monster
        monster = 0
    else:
        dead += yuusya_zan
        monster -= yuusya_zan

    if yuusya >= monster:
        yuusya -= monster
        dead += monster
        yuusya_zan = yuusya
    else:
        dead += yuusya
        yuusya_zan = 0

monster = a[n]
if yuusya_zan >= monster:
    dead += monster
    monster = 0
else:
    dead += yuusya_zan
    monster -= yuusya_zan

print(dead)

Submission status: Accepted

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

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

    else:
        ans += b
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
alist = list(map(int, input().split()))
blist = list(map(int, input().split()))

taos=0
for x in range(n):
    if blist[x]>=alist[x]:
        taos+=alist[x]
        blist[x]=blist[x]-alist[x]
        alist[x]=0
        if blist[x]>=alist[x+1]:
            taos+=alist[x+1]
            alist[x+1]=0
        else:
            taos+=blist[x]
            alist[x+1]-=blist[x]
    else:
        taos+=blist[x]
        alist[x]=alist[x]-blist[x]

print(taos)

Submission status: Accepted

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

counter = 0
for i in range(n):
    # i-th
    _counter = min(a[i], b[i])
    a[i] -= _counter
    counter += _counter

    left_b = b[i] - _counter
    if left_b > 0:
        __counter = min(left_b, a[i + 1])
        a[i + 1] -= __counter
        counter += __counter

print(counter)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0] * (n + 1)
for h in range(n):
    k = min(b[h], a[h])
    c[h] = c[h] + k
    a[h] = a[h] - k
    #print("h:{} a:{} b:{} k:{} c:{}".format(h, a, b, k, c))
    k = min(b[h] - k, a[h + 1])
    c[h + 1] = c[h + 1] + k
    a[h + 1] = a[h + 1] - k
    #print("h:{} a:{} b:{} k:{} c:{}".format(h, a, b, k, c))
print(sum(c))

Submission status: Accepted

Solution code:
import copy
import sys
sys.setrecursionlimit(4100000)

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

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

Submission status: Accepted

Solution code:

N = int(input())
As =   list(map(int,input().split(" ")))
Bs =   list(map(int,input().split(" ")))
ans = 0
for i in range(N):
    ans += min(As[i],Bs[i]) #i町での討伐数
    Bs[i] = max(Bs[i] - As[i] , 0 ) #勇者iの余力
    ans +=min(As[i + 1],Bs[i]) #i + 1町での討伐数
    As[i + 1] = max(As[i + 1] - Bs[i], 0)
    # if As[i] <= Bs[i]: #全員倒せた
    #     ans += As[i]
    #     Bs[i] -= As[i]
    #     if As[i + 1] <= Bs[i]:
    #         ans += Bs[i]
    #
    #
    #     As[i + 1] = max ( As[i + 1] - Bs[i] ,0)
    #
    # else: #全員は倒せなかった
    #     ans += Bs[i]

print(ans)

Submission status: Accepted

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

cnt = 0

for i, b in enumerate(B):

    if b >= A[i]:
        cnt += A[i]
        b -= A[i]

        if b >= A[i+1]:
            cnt += A[i+1]
            A[i+1] = 0
        else:
            cnt += b
            A[i+1] -= b
    else:
        cnt += b
print(cnt)

Submission status: Accepted

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    killed = 0
    remain = A[0]
    for i, b in enumerate(B):
        target = remain + A[i + 1]
        power = B[i]
        killed += min(target, power)
        remain = max(min(target - power, A[i + 1]), 0)
    print(killed)


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):
    for j in range(2):
        m = min(a[i + j], b[i])
        ans += m
        a[i + j] -= m
        b[i] -= m
print(ans)

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

    print(cnt)
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()))
    count = 0

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def readInts():
  return [int(s) for s in input().split(" ")]

def main():
  N = readInts()[0]
  A = readInts()
  B = readInts()

  ans = 0
  carry = 0
  B.append(0)
  for i, b in enumerate(B):
    n = min(A[i], carry)
    restA = A[i] - n
    m = min(restA, b)
    ans += n + m
    carry = b - m
  print(ans)

if __name__ == "__main__":
  main()

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l1=LI()
  l2=LI()

  ans=0
  for i in range(n):
    x=l2[i]
    y=l1[i]
    if y<=x:
      ans+=y
      x-=y

      if l1[i+1]<=x:
        ans+=l1[i+1]
        l1[i+1]=0
      else:
        ans+=x
        l1[i+1]-=x

    else:
      ans+=x

  return ans

# main()
print(main())

Submission status: Accepted

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

carry = 0
ans = 0
for i in range(N+1):
    if A[i] <= carry:
        ans += A[i]
        carry = B[i]
    else:
        A[i] -= carry
        ans += carry
        if A[i] >= B[i]:
            ans += B[i]
            carry = 0
        else:
            ans += A[i]
            carry = 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()))
tmpmonster =0
tmphuman = 0
killnum = 0
for i in range(n):
  #step1
  if b[i] >= a[i]:
    killnum += a[i]
    b[i] -= a[i]
  else:
    killnum += b[i]
    b[i] = 0
  #step2
  if b[i] >= a[i+1]:
    killnum += a[i+1]
    a[i+1] = 0
  else:
    killnum += b[i]
    a[i+1] -= b[i]
print (killnum)

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