CodeEff/ECCO huggingface dataset:
# Original solution
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=998244353
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 LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())

def main():
  p=20010
  h,n=LI()
  a=[]
  b=[]

  for _ in range(n):
    x,y=LI()
    a.append(x)
    b.append(y)

  dp=[[inf]*p for _ in range(n+1)]
  dp[0][0]=0

  for i in range(n):
    for j in range(p-5):
      dp[i+1][j]=min(dp[i+1][j],dp[i][j])
      if j+a[i]<p:
        dp[i+1][j+a[i]]=min(dp[i+1][j+a[i]],dp[i+1][j]+b[i])

  # print(dp)
  ans=inf
  for i in range(h,20010):
    ans=min(ans,dp[n][i])
  return ans

# main()
print((main()))


# Optimized solution
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=998244353
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 LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())

def main():
  p=10005
  h,n=LI()
  a=[]
  b=[]

  for _ in range(n):
    x,y=LI()
    a.append(x)
    b.append(y)

  dp=[[inf]*(p+1) for _ in range(n+1)]
  dp[0][0]=0

  for i in range(n):
    for j in range(p):
      dp[i+1][j]=min(dp[i+1][j],dp[i][j])
      if j+a[i]<p:
        dp[i+1][j+a[i]]=min(dp[i+1][j+a[i]],dp[i+1][j]+b[i])
      else:
        dp[i+1][p]=min(dp[i+1][p],dp[i+1][j]+b[i])

  # print(dp)
  ans=inf
  for i in range(h,p+1):
    ans=min(ans,dp[n][i])
  return ans

# main()
print((main()))
# Original solution
H,N=list(map(int,input().split()))

A=[]
B=[]

for i in range(N):
    a,b=list(map(int,input().split()))
    A.append(a)
    B.append(b)

DP=[[1000000000]*(H+max(A)+1) for i in range(N+1)]


for i in range(N):
    DP[i+1][A[i]]=B[i]


for i in range(N):
    for h in range(H+max(A)+1):
        DP[i+1][h]=min(DP[i+1][h],DP[i][h])
        if h-A[i]>=0:
            DP[i+1][h]=min(DP[i+1][h-A[i]]+B[i],DP[i+1][h])

print((min(DP[N][H:])))

# Optimized solution
H,N=list(map(int,input().split()))

A=[]
B=[]

for i in range(N):
    a,b=list(map(int,input().split()))
    A.append(a)
    B.append(b)

DP=[1000000000]*(H+max(A)+1)
DP[0]=0


for i in range(N):
    DP[A[i]]=B[i]

for i in range(N):
    for h in range(A[i],H+max(A)+1):
        DP[h]=min(DP[h-A[i]]+B[i],DP[h])

print((min(DP[H:])))
# Original solution
h, n = [int(i) for i in input().split()]
a, b = [0]*n, [0]*n

eff = 0
eff_index = 0
for i in range(n):
    a[i], b[i] = [int(_) for _ in input().split()]
    if eff < a[i]/b[i]:
        eff = a[i]/b[i]
        eff_index = i

max_kaisuu = - (-h//a[eff_index])

ans = float('inf')
H = max_kaisuu * b[eff_index]
dp = [[0] * (H+1) for _ in range(n+1)]
# dp[i][j] i番目までの魔法で消費魔力がjを超えないように選んだときのダメージの最大値
for i in range(n):
    for j in range(H+1):
        if j < b[i]:# i番目の魔法消費b[i]がjを超えているので，入れる可能性がない

            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max(dp[i][j], dp[i+1][j-b[i]]+a[i])# 入れ場合と入れない場合を比較

        if dp[i+1][j] >= h:
            ans = min(ans, j)
            H = j
            continue
print(ans)


# Optimized solution
h, n = [int(i) for i in input().split()]
a, b = [0]*n, [0]*n

eff = 0
eff_index = 0
for i in range(n):
    a[i], b[i] = [int(_) for _ in input().split()]

max_a = max(a)

ans = float('inf')
H = h + max_a
dp = [float('inf')] * (H+1)
# dp[i] iのダメージを与える最小消費魔力
dp[0] = 0
for i in range(H+1):
    for j in range(n):
        if i >= a[j]:
            dp[i] = min(dp[i], dp[i-a[j]]+b[j])

    if i >= h:
        ans = min(ans, dp[i])
print(ans)
# Original solution
def e_crested_ibis_vs_monster():
    H, N = [int(i) for i in input().split()]
    Damage, Cost = [], []
    for _ in range(N):
        a, b = [int(i) for i in input().split()]
        Damage.append(a)
        Cost.append(b)

    # dp[i][j]: 最初の i 種類の魔法でモンスターに j ダメージ与える場合の消費魔力の最小値
    dp = [[float('inf')] * (H + 1) for _ in range(N + 1)]
    dp[0][0] = 0
    for i in range(N):
        for j in range(H + 1):
            # i 番目の魔法を使わない場合
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
            # i 番目の魔法を使う場合
            dp[i + 1][min(j + Damage[i], H)] = min(dp[i + 1][min(j + Damage[i], H)],
                                                   dp[i + 1][j] + Cost[i])
    return dp[N][H]

print((e_crested_ibis_vs_monster()))

# Optimized solution
def e_crested_ibis_vs_monster():
    H, N = [int(i) for i in input().split()]
    Monsters = [[int(i) for i in input().split()] for j in range(N)]

    # dp[i][j]: 最初の i 種類の魔法でモンスターに j ダメージ与える場合の消費魔力の最小値
    # としたとき、更新順序を工夫することで添字 i をなくせる
    dp = [0] + [float('inf')] * H
    for a, b in Monsters:
        for j in range(H + 1):
            nj = min(j + a, H)
            dp[nj] = min(dp[nj], dp[j] + b)
    return dp[H]

print((e_crested_ibis_vs_monster()))
# Original solution
h, n = list(map(int, input().split()))
attack = [None] * n
cost = [None] * n
for i in range(n):
    a, c = list(map(int, input().split()))
    attack[i] = a
    cost[i] = c
dp = [0] + [float('inf')] * h
for i in range(1, h + 1):
    for j in range(n):
        dp[i] = min(dp[i], cost[j] + dp[max(0, i - attack[j])])
print((dp[h]))

# Optimized solution
from sys import stdin

h, n = list(map(int, stdin.readline().split()))
attack = [None] * n
cost = [None] * n
for i in range(n):
    a, c = list(map(int, stdin.readline().split()))
    attack[i] = a
    cost[i] = c
dp = [0] + [float('inf')] * h
for i in range(1, h + 1):
    for j in range(n):
        dp[i] = min(dp[i], cost[j] + dp[max(0, i - attack[j])])
print((dp[h]))
# Original solution
###### Importing Libraries ######
import math
import operator as op
from functools import reduce
from fractions import Fraction as frac
#################################
##### User defined function #####

############ NCR ###############
def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, list(range(n, n-r, -1)), 1)
    denom = reduce(op.mul, list(range(1, r+1)), 1)
    return numer / denom
    
############ READ ###############
def rd(p):
    if(p==0):
        return eval(input())
    if(p==1):
        return int(eval(input()))
    if(p==2):
        return list(map(int,input().split()))
    if(p==3):
        return list(map(int,input().split()))


############ DSU ###############
global par
# par=[-1 for i in range(100001)]
# t=rd(1)
def find(u):
    if(par[u]<0):
        return u
    else:
        return find(par[u])
def uni(x,y):
    u=find(x)
    v=find(y)
    if u==v:
        return
    if(par[u]<par[v]):
        par[u]+=par[v]
        par[v]=u
    else:    
        par[v]+=par[u]
        par[u]=v
################################        
        

t=1
for term in range(1,t+1):
    h,n=rd(2)
    P=[rd(3) for i in range(n)]
    x=max(a for a,b in P)
    DP=[0 for i in range(h+x)]
    for i in range(1,h+x):
        DP[i]=min(DP[max(i-a,0)]+b for a,b in P)
    print((DP[h]))
    
    
    
    
    
    
    
    
    
    
    
    

# Optimized solution
###### Importing Libraries ######
import math
import operator as op
from functools import reduce
from fractions import Fraction as frac
#################################
##### User defined function #####

############ NCR ###############
def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, list(range(n, n-r, -1)), 1)
    denom = reduce(op.mul, list(range(1, r+1)), 1)
    return numer / denom
    
############ READ ###############
def rd(p):
    if(p==0):
        return eval(input())
    if(p==1):
        return int(eval(input()))
    if(p==2):
        return list(map(int,input().split()))
    if(p==3):
        return list(map(int,input().split()))


############ DSU ###############
global par
# par=[-1 for i in range(100001)]
# t=rd(1)
def find(u):
    if(par[u]<0):
        return u
    else:
        return find(par[u])
def uni(x,y):
    u=find(x)
    v=find(y)
    if u==v:
        return
    if(par[u]<par[v]):
        par[u]+=par[v]
        par[v]=u
    else:    
        par[v]+=par[u]
        par[u]=v
################################        
        

t=1
for term in range(1,t+1):
    h,n=rd(2)
    P=[list(map(int,input().split())) for i in range(n)]
    x=max(a for a,b in P)
    DP=[0]*(h+x)
    for i in range(1,h+x):
        DP[i]=min(DP[i-a]+b for a,b in P)
    print((DP[h]))
# Original solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, N, *AB = list(map(int, read().split()))

    magic = [0] * N
    for i, (a, b) in enumerate(zip(*[iter(AB)] * 2)):
        magic[i] = (a, b)

    dp = [[INF] * (H + 1) for _ in range(N + 1)]
    for i in range(N + 1):
        dp[i][0] = 0

    for i in range(N):
        a, b = magic[i]
        for j in range(H + 1):
            dp[i + 1][j] = min(dp[i][j], dp[i + 1][max(j - a, 0)] + b)

    print((dp[N][H]))
    return


if __name__ == '__main__':
    main()


# Optimized solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, N, *AB = list(map(int, read().split()))

    dp = [INF] * (H + 1)
    dp[0] = 0

    for a, b in zip(*[iter(AB)] * 2):
        for i in range(H + 1):
            dp[i] = min(dp[i], dp[max(i - a, 0)] + b)

    print((dp[H]))
    return


if __name__ == '__main__':
    main()
# Original solution
import sys

read = sys.stdin.buffer.read
INF = 1 << 60


def main():
    H, N, *AB = list(map(int, read().split()))

    dp = [INF] * (H + 1)
    dp[0] = 0

    for a, b in zip(*[iter(AB)] * 2):
        for i in range(H + 1):
            if dp[i] > dp[max(i - a, 0)] + b:
                dp[i] = dp[max(i - a, 0)] + b

    print((dp[H]))
    return


if __name__ == '__main__':
    main()


# Optimized solution
import sys

read = sys.stdin.read
INF = 1 << 60


def main():
    H, N, *AB = list(map(int, read().split()))

    dp = [INF] * (H + 1)
    dp[0] = 0

    for a, b in zip(*[iter(AB)] * 2):
        for i in range(H + 1):
            if i >= a:
                if dp[i] > dp[i - a] + b:
                    dp[i] = dp[i - a] + b
            else:
                if dp[i] > dp[0] + b:
                    dp[i] = dp[0] + b

    print((dp[H]))
    return


if __name__ == '__main__':
    main()
# Original solution
INF = 10 ** 9

def main():
    H, N = list(map(int, input().split()))
    dp = [INF] * (H+1)
    dp[0] = 0
    for _ in range(N):
        a, b = list(map(int, input().split()))
        for j in range(H):
            nj = min(j+a, H)
            dp[nj] = min(dp[nj], dp[j]+b)

    print((dp[H]))

    
if __name__ == "__main__":
    main()

# Optimized solution
import sys
readline = sys.stdin.readline
INF = 10 ** 8


def main():
    H, N = list(map(int, readline().rstrip().split()))
    dp = [INF] * (H + 1)  # HPを減らすのに必要な最小のMP
    dp[0] = 0

    for _ in range(N):
        hp, mp = list(map(int, readline().rstrip().split()))

        for i in range(H):
            j = min(i+hp, H)
            dp[j] = min(dp[j], dp[i] + mp)

    print((dp[-1]))


if __name__ == '__main__':
    main()
# Original solution
h,n=list(map(int,input().split()))
a,b=[],[]
for i in range(n):
  aa,bb=list(map(int,input().split()))
  a.append(aa)
  b.append(bb)
inf=float("inf")
dp=[[inf]*(h+1) for i in range(n+1)]
dp[0][0]=0
for i in range(n):
  for j in range(h+1):
    dp[i+1][j]=min(dp[i+1][j],dp[i][j])
    dp[i+1][min(h,j+a[i])]=min(dp[i+1][j]+b[i],dp[i+1][min(h,j+a[i])])
    
print((dp[n][h]))

# Optimized solution
h,n=list(map(int,input().split()))
a,b=[],[]
for i in range(n):
  aa,bb=list(map(int,input().split()))
  a.append(aa)
  b.append(bb)
inf=float("inf")
dp=[[inf]*(h+1) for i in range(n+1)]
dp[0][0]=0
for i in range(n):
  for j in range(h+1):
    dp[i+1][j]=min(dp[i+1][j],dp[i][j])    
    dp[i+1][min(j+a[i],h)]=min(dp[i+1][j]+b[i],dp[i+1][min(j+a[i],h)])
print((dp[n][h]))
# Original solution
h,n=list(map(int,input().split()))
a,b=[],[]
for i in range(n):
  aa,bb=list(map(int,input().split()))
  a.append(aa)
  b.append(bb)
inf=float("inf")
dp=[[inf]*(h+1) for i in range(n+1)]
dp[0][0]=0
for i in range(n):
  for j in range(h+1):
    dp[i+1][j]=min(dp[i+1][j],dp[i][j])    
    dp[i+1][min(j+a[i],h)]=min(dp[i+1][j]+b[i],dp[i+1][min(j+a[i],h)])
print((dp[n][h]))


# Optimized solution
import sys
input = sys.stdin.readline

h, n = list(map(int, input().split()))
a = [0]*n
b = [0]*n
for i in range(n):
    a[i] ,b[i] = list(map(int, input().split()))

inf = float("inf")
# dp[i][j]:i-1番目までの魔法で体力をj減らした時の消耗魔力の最大値
dp = [[inf]*(h+2) for i in range(n+1)]
dp[0][0] = 0

for i in range(n):
    for j in range(h+1):
        dp[i+1][j] = min(dp[i+1][j],dp[i][j])
        dp[i+1][min(j+a[i],h)] = min(dp[i+1][min(j+a[i],h)],dp[i+1][j]+b[i])

print((dp[n][h]))
# Original solution
h,n = list(map(int, input().split()))
x = [list(map(int, input().split())) for i in range(n)]

#dp[v]:体力をv減らす時の最小魔力
dp=[10**20 for i in range(h+1)]
dp[0]=0

for i in range(h+1):
    for j in range(n):
        if i-x[j][0]<0:
            dp[i] = min(dp[i], dp[0] + x[j][1])

        else:
            dp[i]=min(dp[i],dp[i-x[j][0]]+x[j][1])
print((dp[-1]))


# Optimized solution
h,n= list(map(int, input().split()))
a= [list(map(int, input().split())) for i in range(n)]

dp=[float('inf')]*(h+1)
dp[0]=0
for i in range(n):
    x=a[i][0]
    y=a[i][1]
    for j in range(h+1):
        if j>=x:
            dp[j]=min(dp[j-x]+y,dp[j])
        else:
            dp[j]=min(y,dp[j])

print((dp[-1]))
# Original solution
h,n=list(map(int,input().split()))
a,b=[],[]
for i in range(n):
    a_sub,b_sub=list(map(int,input().split()))
    a.append(a_sub)
    b.append(b_sub)
inf=10000000000000
ma=max(a)
dp=[inf]*(h+1+ma)
dp[0]=0
for i in range(n):
    x=[a[i],b[i]]
    for j in range(h):
        if dp[j]!=inf:
            if j+x[0]<=h+ma:
                dp[j+x[0]]=min(dp[j+x[0]],dp[j]+x[1])
                if j+x[0]>=h:
                    dp[h]=min(dp[h],dp[j]+x[1])
            else:
                break
print((dp[h]))


# Optimized solution
h,n=list(map(int,input().split()))
ab=[list(map(int,input().split())) for i in range(n)]
ab.sort(reverse=True)
inf=1000000000
ma=ab[0][0]
dp=[inf]*(h+1+ma)
dp[0]=0
for i in range(n):
    x=ab[i]
    for j in range(h):
        if dp[j]!=inf:
            if j+x[0]<=h+ma:
                if j+x[0]>=h:
                    dp[h]=min(dp[h],dp[j]+x[1])
                else:
                    dp[j+x[0]]=min(dp[j+x[0]],dp[j]+x[1])
            else:
                break
print((dp[h]))
# Original solution
def DP():
  H,N,*T = list(map(int,open(0).read().split()))
  
  #dpテーブルの設定と初期化
  dp = [0] + [10 ** 9] * H
  
  #各魔法について
  for d,c in zip(*[iter(T)] * 2):
    #d,cはそれぞれ魔法ダメージと魔法のコスト
    #dpテーブルの各HPについて
    for j in range(H + 1):
      k = max(j - d,0)
      #ダメージjを与える時のコストについて、
      #魔法iを新たに使った場合と、既存の最小コストを比較し更新する
      l = dp[k] + c
      dp[j] = min(l,dp[j])
  
  print((dp[-1]))
  
DP()

# Optimized solution
def main():
    h,n,*t=list(map(int,open(0).read().split()))
    d=[0]+[10**18]*h
    for a,b in zip(*[iter(t)]*2):
        for j in range(h+1):
            k=j-a
            if k<0:k=0
            c=d[k]+b
            if c<d[j]:d[j]=c
    print((d[h]))
main()
# Original solution
h, n = list(map(int, input().split()))
c = [0]*n
m = [0]*n

mc = 0
for i in range(n):
    c[i], m[i] = list(map(int, input().split()))
    if mc < c[i]:
        mc = c[i]
    
dp = [10**10]*(h+mc)
dp[0] = 0

for i in range(1, h + mc):
    for j in range(n):
        dp[i] = min(dp[i-c[j]] + m[j], dp[i])
        
print((min(dp[h:])))

# Optimized solution
h, n = list(map(int, input().split()))
c = [0]*n
m = [0]*n

mc = 0
for i in range(n):
    c[i], m[i] = list(map(int, input().split()))
    if mc < c[i]:
        mc = c[i]
    
dp = [10**10]*(h+mc)
dp[0] = 0

for i in range(1, h + mc):
    dp[i] = min(dp[i-a] + b for a, b in zip(c, m))
        
print((min(dp[h:])))
# Original solution
import sys
input = sys.stdin.readline
inf = 10**9
H, N = list(map(int, input().split()))
AB = []
for n in range(N):
    a, b = list(map(int, input().split()))
    AB.append((a, b))
AB.sort(reverse=True, key=lambda x: x[0])
dp = [inf] * (H + 10 ** 4)
dp[0] = 0
for a, b in AB:
    for h in range(H):
        if dp[h] != inf:
            tmp = dp[h] + b
            if tmp < dp[h + a]:
                dp[h + a] = tmp
print((min(dp[H:])))


# Optimized solution
inf = 10 ** 9


def main():
    H, N = list(map(int, input().split()))
    AB = []
    for n in range(N):
        a, b = list(map(int, input().split()))
        AB.append((a, b))
    AB.sort(reverse=True, key=lambda x: x[0])
    dp = [inf] * (H + 10 ** 4)
    dp[0] = 0
    for a, b in AB:
        for h in range(H):
            # if dp[h] == inf:
            #     continue
            # else:
            #     dp[h+a] = min(dp[h]+b, dp[h+a])
            if dp[h] != inf:
                tmp = dp[h] + b
                if tmp < dp[h + a]:
                    dp[h + a] = tmp
    print((min(dp[H:])))


main()
# Original solution
inf = 10 ** 9


def main():
    H, N = list(map(int, input().split()))
    AB = []
    for n in range(N):
        a, b = list(map(int, input().split()))
        AB.append((a, b))
    AB.sort(reverse=True, key=lambda x: x[0])
    dp = [inf] * (H + 10 ** 4)
    dp[0] = 0
    for a, b in AB:
        for h in range(H):
            # if dp[h] == inf:
            #     continue
            # else:
            #     dp[h+a] = min(dp[h]+b, dp[h+a])
            if dp[h] != inf:
                tmp = dp[h] + b
                if tmp < dp[h + a]:
                    dp[h + a] = tmp
    print((min(dp[H:])))


main()


# Optimized solution
def main():
    inf = 10**9
    H, N = list(map(int, input().split()))
    AB = []
    for n in range(N):
        a, b = list(map(int, input().split()))
        AB.append((a, b))
    AB.sort(reverse=True, key=lambda x: x[0])
    dp = [inf] * (H + 10 ** 4)
    dp[0] = 0
    for a, b in AB:
        for h in range(H):
            if dp[h] != inf:
                tmp = dp[h] + b
                if tmp < dp[h + a]:
                    dp[h + a] = tmp
    print((min(dp[H:])))


main()
# Original solution
import sys
sys.setrecursionlimit(10**9)

INF = 10**20
def main():
    H,N = list(map(int,input().split()))
    A,B = [],[]
    for _ in range(N):
        a,b = list(map(int,input().split()))
        A.append(a)
        B.append(b)

    tp = 3 * 10**4
    dp = [INF] * tp
    dp[0] = 0
    minim = INF
    for h in range(tp):
        for i in range(N):
            dp_i = min(h-A[i],H)
            dp[h] = min(dp[h],dp[dp_i]+B[i])
            if h >= H:
                minim = min(minim,dp[h])


    print(minim)
    # assert False


if __name__ == "__main__":
  main()

# Optimized solution
import sys
sys.setrecursionlimit(10**9)

INF = 10**20
def main():
    H,N = list(map(int,input().split()))
    A,B = [],[]
    for _ in range(N):
        a,b = list(map(int,input().split()))
        A.append(a)
        B.append(b)

    tp = 3 * 10**4
    dp = [INF] * tp
    dp[0] = 0
    ans = INF
    for h in range(tp):
        minim = INF
        for i in range(N):
            dp_i = h-A[i]
            if dp_i >= 0 and dp[dp_i] != INF:
                minim = min(minim,dp[dp_i]+B[i])
                # print(dp[h-A[i]]+B[i])

        if minim != INF:
            dp[h] = minim
            if h >= H:
                ans = min(ans,dp[h])


    print(ans)


if __name__ == "__main__":
  main()
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
count=0
for i in range(n):
  if b[i]-a[i]>0:
    count+=a[i]
    if a[i+1]>(b[i]-a[i]):
      a[i+1]-=(b[i]-a[i])
      count+=(b[i]-a[i])
    elif a[i+1]<=b[i]-a[i]:
      count+=a[i+1]
      a[i+1]=0
  elif b[i]<=a[i]:
    count+=b[i]   
print(count)

# Optimized solution
n=int(eval(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:
    if b[i]<=a[i]+a[i+1]:
      ans+=b[i]
      a[i+1]-=(b[i]-a[i])
      if a[i+1]<0:
        a[i+1]=0
    else:
      if b[i]>a[i]+a[i+1]:
        ans+=a[i]+a[i+1]
        a[i+1]=0
print(ans)
# Original solution
R = lambda: list(map(int, input().split()))
n = int(eval(input()))
a, b = R(), R()
r = 0
for i in range(n):
    x = min(a[i], b[i])
    b[i] -= x
    y = min(a[i + 1], b[i])
    a[i + 1] -= y
    r += x + y
print(r)


# Optimized solution
R = lambda: list(map(int, input().split()))
n = int(eval(input()))
a, b = R(), R()
r = 0
for i in range(n):
    x = min(a[i], b[i])
    y = min(a[i + 1], b[i] - x)
    a[i + 1] -= y
    r += x + y
print(r)
# Original solution
R = lambda: list(map(int, input().split()))
eval(input())
a, b = list(R()), R()
r = 0
for i, u in enumerate(b):
    x = min(a[i], u)
    y = min(a[i + 1], u - x)
    a[i + 1] -= y
    r += x + y
print(r)


# Optimized solution
R = lambda: list(map(int, input().split()))
eval(input())
a, b = list(R()), R()
r = y = 0
for u, v, w in zip(b, a, a[1:]):
    x = min(v - y, u)
    y = min(w, u - x)
    r += x + y
print(r)
# Original solution
from itertools import tee

R = lambda: list(map(int, input().split()))
eval(input())
(a, b), c = tee(R()), R()
next(b)
r = y = 0
for u, v, w in zip(c, a, b):
    x = min(v - y, u)
    y = min(w, u - x)
    r += x + y
print(r)


# Optimized solution
R = lambda: list(map(int, input().split()))
eval(input())
a = list(R())
r = y = 0
for u, v, w in zip(R(), a, a[1:]):
    x = min(v - y, u)
    y = min(w, u - x)
    r += x + y
print(r)
# Original solution
R = lambda: list(map(int, input().split()))
eval(input())
a = list(R())
def f():
    y = 0
    for u, v, w in zip(R(), a, a[1:]):
        x = min(v - y, u)
        y = min(w, u - x)
        yield x + y
print((sum(f())))


# Optimized solution
R = lambda: list(map(int, input().split()))
eval(input())
a = list(R())
def f():
    s = 0
    for x, y, z in zip(R(), a, a[1:]):
        t = min(x, y - s)
        s = min(z, x - t)
        yield s + t
print((sum(f())))
# Original solution
n = int(eval(input()))
a_lis = list(map(int,input().split()))
b_lis = list(map(int,input().split()))

ans = 0

for i in range(n):
	if b_lis[i] <= a_lis[i]:
		ans += b_lis[i]
	else:                                     
		ans += a_lis[i]
		b_lis[i] -= a_lis[i]
		if b_lis[i] <= a_lis[i+1]:
			ans += b_lis[i]
			a_lis[i+1] -= b_lis[i]
		else:
			ans +=  a_lis[i+1]
			a_lis[i+1] = 0
print(ans)

# Optimized solution
n = int(eval(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]
		x = b[i] -a[i]
		if x >= a[i+1]:
			ans += a[i+1]
			a[i+1] = 0
		else:
			ans += x
			a[i+1] = a[i+1] -x
print(ans)
# Original solution
# ABC135C - City Savers
# greedy algorithm
def main():
    n = int(eval(input()))
    A = list(map(int, input().rstrip().split()))
    B = tuple(map(int, input().rstrip().split()))
    ans = 0
    for i in range(n):
        cur1 = min(A[i], B[i])
        cur2 = min(A[i + 1], B[i] - cur1)
        A[i] -= cur1
        A[i + 1] -= cur2
        ans += cur1 + cur2
    print(ans)


if __name__ == "__main__":
    main()

# Optimized solution
# ABC135C - City Savers
# greedy algorithm
def main():
    n = int(eval(input()))
    A = tuple(map(int, input().rstrip().split()))
    B = tuple(map(int, input().rstrip().split()))
    ans, a1 = 0, A[0]
    for a2, b in zip(A[1:], B):
        cnt1 = min(a1, b)
        cnt2 = min(a2, b - cnt1)
        ans += cnt1 + cnt2
        a1 = a2 - cnt2  # a2 becomes a1 in next turn
    print(ans)


if __name__ == "__main__":
    main()