Problem:


Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3



Sample input:
2
3 5 2
4 5


Sample output:
9

Submission status: Accepted

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

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

Submission status: Accepted

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

a = a[::-1]
b = b[::-1]
ans = 0
for i, bi in enumerate(b):
    ans += min(a[i], b[i])
    remain = max(0, b[i] - a[i])
    ans += min(a[i + 1], remain)
    a[i + 1] = max(0, a[i + 1] - remain)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    input=stdin.readline 

    n=int(input())
    A=[int(_) for _ in input().split()]
    sum_a=sum(A)
    B=[int(_) for _ in input().split()]
    for i in range(n):
        s=max(B[i]-A[i],0)
        A[i]-=min(A[i],B[i])
        A[i+1]-=min(A[i+1],s)
    print(sum_a-sum(A))

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline #文字列をsplit()しない場合s=s[:-2]が必要

    n=int(input())
    A=[int(_) for _ in input().split()]
    sum_a=sum(A)
    B=[int(_) for _ in input().split()]
    for i in range(n):
        s=max(B[i]-A[i],0)
        A[i]-=min(A[i],B[i])
        A[i+1]-=min(A[i+1],s)
    print(sum_a-sum(A))

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# Problem C - City Savers

# input
N = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

# initialization
max_monster = 0

# count
for i in range(N):
    b = b_list[i]
    if a_list[i]<=b:
        max_monster += a_list[i]
        b_list[i] -= a_list[i]
        a_list[i] = 0
    else:
        max_monster += b
        b_list[i] = 0

    b = b_list[i]
    if a_list[i+1]<=b:
        max_monster += a_list[i+1]
        a_list[i+1] = 0
    else:
        max_monster += b
        a_list[i+1] -= b_list[i]

# output
print(max_monster)

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

print(ans)

Submission status: Accepted

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

ans = 0
for i in range(N):
  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]:
      A[i+1] -= B[i]
      ans += B[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()))
count=0
for i in range(N):
	x=A[N-i]+A[N-1-i]
	count+=min(x,B[N-i-1])
	A[N-i-1]=max(0,A[N-i-1]-max(0,B[N-i-1]-A[N-i]))
print(count)

Submission status: Accepted

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

ans, mod = 0, 0

for i in range(N):

    if mod > A[i]:
        ans += A[i]
        A[i] = 0
    else:
        ans += mod
        A[i] -= mod
        mod = 0

    if B[i] > A[i]:
        ans += A[i]
        mod = B[i] - A[i]
    else:
        ans += B[i]
        mod = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

A.reverse()
B.reverse()

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

n = INT()
a = LIST()
b = LIST()
s = sum(a)
for i in range(n):
    if a[i] < b[i]:
        b[i] -= a[i]
        a[i] = 0
        a[i + 1] = max(0, a[i + 1] - b[i])
    else:
        a[i] -= b[i]
print(s - sum(a))

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    b = list(map(int,input().split()))
    a = 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:
            if a[i]>=b[i]+b[i+1]:
                ans+=b[i]+b[i+1]
                b[i+1]=0
            else:
                ans+=a[i]
                b[i+1] = b[i]+b[i+1]-a[i]
    print(ans)
if __name__ == "__main__":
    solve()

Submission status: Accepted

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

ans=0
for i in range(N):
    a0 = min(A[i], B[i])
    B[i] -= a0
    a1 = min(A[i+1], B[i])
    ans += a0 + a1
    A[i+1] -= a1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

if b_li[0]<b_li[-1]:
    a_li = a_li[::-1]
    b_li = b_li[::-1]

ans = 0
for i in range(0,n):
    attack = min(a_li[i],b_li[i])
    attack2 = min(a_li[i+1],b_li[i]-min(a_li[i],b_li[i]))
    a_li[i]-=attack
    a_li[i+1]-=attack2
    b_li[i]-=(attack+attack2)
    ans+=(attack+attack2)

print(ans)

Submission status: Accepted

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

goukei=0
for i in range(N):
    #print(goukei)
    if A[i]>=B[i]:
        goukei = goukei+B[i]

    elif A[i]+A[i+1]>=B[i]:
        A[i+1] = A[i+1] - (B[i]-A[i])
        goukei = goukei + B[i]

    else:
        goukei = goukei + A[i]+A[i+1]
        A[i+1] = 0


print(goukei)

Submission status: Accepted

Solution code:
n=int(input())
m=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=0

for i in range(n):
  if m[i]>c[i]:
    ans+=c[i]
  elif m[i]<=c[i]<m[i]+m[i+1]:
    ans+=c[i]
    m[i+1]=m[i]+m[i+1]-c[i]
  else:
    ans+=(m[i]+m[i+1])
    m[i+1]=0
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i in range(N):
    remain = B[i] - A[i]
    if remain > 0:
        ans += A[i]
        ans += min(remain, A[i+1])
        A[i+1] = max(0, A[i+1] - remain)
    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()))
count = 0
if N == 1:
  count += min(A[0]+A[1], B[0])
  print(count)
  exit(0)
mid_idx = N // 2
for idx in range(0, mid_idx):
  n_kill = min(A[idx], B[idx])
  count += n_kill
  A[idx] -= n_kill
  B[idx] -= n_kill
  n_kill = min(A[idx+1], B[idx])
  count += n_kill
  A[idx+1] -= n_kill
  B[idx] -= n_kill
  idx = -(idx+1)
  n_kill = min(A[idx], B[idx])
  count += n_kill
  A[idx] -= n_kill
  B[idx] -= n_kill
  n_kill = min(A[idx-1], B[idx])
  count += n_kill
  A[idx-1] -= n_kill
  B[idx] -= n_kill
if N % 2 != 0:
  count += min(A[mid_idx]+A[mid_idx+1], B[mid_idx])
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
A=nl()
B=nl()
sumMb=sum(A)

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

print(sumMb-sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
b_ls = list(map(int, input().split()))
before = sum(a_ls)

for i in range(n):
    target = a_ls[i:i+2]
    power = b_ls[i]
    if target[0] - power < 0:
        power -= target[0]
        target[0] = 0
        target[1] = max(0, target[1] - power)
    else:
        target[0] -= power
    a_ls[i:i+2] = target

after = sum(a_ls)
print(before - after)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import math
import collections
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n=I()
As=(LI())
Bs=(LI())
ans=0
#print(As)
#print(Bs)
for i in range(n):
    ans+=min(As[n-i]+As[n-1-i],Bs[n-1-i])
    As[n-i-1]=max(As[n-i-1]-max(0,Bs[n-1-i]-As[n-i]),0)
    #print(As)
    #print(ans)
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):
    min_ = min(A[i], B[i])
    A[i] -= min_
    B[i] -= min_
    ans += min_
    min_ = min(A[i+1], B[i])
    A[i+1] -= min_
    B[i] -= min_
    ans += min_


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

new_sumA = sum(A)

print(sumA - new_sumA)

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

Submission status: Accepted

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

Asum = sum(A)

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

print(Asum - sum(A))

Submission status: Accepted

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

dp = [0]*N #勇者が倒す数

for i in range(N):
  dp[i] = min(A[i], B[i])
  A[i] -= dp[i]
  B[i] -= dp[i]
  rem = A[i]
  if rem==0:
    tmp = min(A[i+1], B[i])
    dp[i] += tmp
    A[i+1] -= tmp
print(sum(dp))

Submission status: Accepted

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

Asum = sum(A)

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

print(Asum - sum(A))

Submission status: Accepted

Solution code:
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))

n = load()
A = load_list()
B = load_list()

cnt = 0
for i in range(n):
    diff = min(B[i], A[i])
    A[i] -= diff
    cnt += diff
    B[i] -= diff
    diff = min(B[i], A[i+1])
    A[i+1] -= diff
    cnt += diff
    B[i] -= diff
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):
    if b[i] > a[i]:
        total += a[i]
        if b[i]-a[i] > a[i+1]:
            total += a[i+1]
            a[i+1] = 0
        else:
            total += b[i] - a[i]
            a[i+1] -= b[i] - a[i]
    else:
        total += b[i]
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()]
sm = sum(a)
for i in range(n):
    tmp = a[i]
    a[i] = max(a[i]-b[i], 0)
    b[i] -= tmp - a[i]
    a[i+1] = max(a[i+1]-b[i], 0)

print(sm - sum(a))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input()) # 入力が1つ
# map(int, input().split()) # 入力が複数
m = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

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

Submission status: Accepted

Solution code:
heros = int(input())
monsters = list(map(int, input().split()))
capacity = list(map(int, input().split()))
killed = 0
for i in range(heros):
    left = min(monsters[i], capacity[i])
    killed += left
    capacity[i] -= left
    if capacity[i] > 0:
        left2 = min(monsters[i+1], capacity[i])
        killed += left2
        monsters[i+1] -= left2
print(killed)

Submission status: Accepted

Solution code:
n = int(input())
ns = list(map(int, input().split()))
bs = list(map(int, input().split()))
count = 0
for i in range(n):
    monster = min(ns[i], bs[i])
    count += monster
    bs[i] -= monster
    monster2 = min(ns[i+1], bs[i])
    count += monster2
    ns[i+1] -= monster2
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    N = int(input())
    A = input_nums()[::-1]
    B = input_nums()[::-1]
    B = [0] + B
    total = 0
    for i in range(N):
        # 後続の勇者が倒す
        mb = min(A[i], B[i])
        A[i] -= mb
        B[i] -= mb
        total += mb
        # 後続の勇者が倒し着れなかった場合
        if A[i] > 0:
            mb = min(A[i], B[i+1])
            A[i] -= mb
            B[i+1] -= mb
            total += mb
    mb = min(A[N], B[N])
    total += mb
    print(total)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
#pypyではscipy, numpyは使えない
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
#from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
#import numpy as np
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  N = int(readline())
  A = list(map(int, readline().split()))
  B = list(map(int, readline().split()))
  count = 0
  for i in range(N):
    tmp = min(A[i],B[i])
    count += tmp
    A[i] -= tmp
    B[i] -= tmp
    tmp = min(A[i+1],B[i])
    count += tmp
    A[i+1] -= tmp
    B[i] -= tmp
  print(count)
  
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
city_count = int( input() )
monster_list = list( map( int, input().split() ) )
hero_power_list = list( map( int, input().split() ) )

before_total_monsters = sum( monster_list )
# hero attach !
for i in range( city_count ):
  conflict = min( monster_list[i], hero_power_list[ i ] )
  monster_list[i] -= conflict
  hero_power_list[ i ] -= conflict
  conflict = min( monster_list[ i + 1 ], hero_power_list[ i ] )
  monster_list[ i + 1 ] -= conflict
  hero_power_list[ i ] -= conflict
monster_list[city_count] -= min( monster_list[city_count], hero_power_list[city_count-1] )

after_total_monsters = sum( monster_list )

print( before_total_monsters - after_total_monsters )

Submission status: Accepted

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

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

        dmg = min(B[i], A[i + 1])
        ans += dmg
        A[i + 1] -= dmg
        B[i] -= dmg
        
    print(ans)


resolve()

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
mons = list(map(int, input().split()))
yush = list(map(int, input().split()))

temp = sum(mons)

for i, yu in enumerate(yush):
    mo = mons[i]
    if (yu >= mo):
        mons[i] = 0
        yush[i] = yu - mo
        
        if (mons[i + 1] > yush[i]):
            mons[i + 1] -= yush[i]
        else:
            mons[i + 1] = 0
        
    else:
        mons[i] = mo - yu
ans = temp - sum(mons)
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):
    atk1 = min(A[i], B[i])
    atk2 = min(A[i + 1], B[i] - atk1)
    A[i + 1] -= atk2
    ans += atk1 + atk2

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

def main():
    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 += A[i]
            B[i] -= A[i]

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

            else:
                ans += B[i]
                A[i+1] -= B[i]
        else:
            ans += B[i]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()