gets
monsters = gets.split.map(&:to_i)
heros = gets.split.map(&:to_i) + [0]

count = 0
prev = 0
heros.zip(monsters).each{|h, m|
    count += [m, prev + h].min
    if prev > m
        prev = h
    elsif prev + h > m
        prev = prev + h - m
    else
        prev = 0
    end
}
puts count
