class FarmVillage
  def initialize(number_of_houses:, carrying_costs:, growing_costs:)
    @number_of_houses = number_of_houses
    @carrying_costs = carrying_costs.dup
    @growing_costs = growing_costs.dup
  end

  def calc_total_cost
    total_cost = 0

    @fields_productions = Array.new(@number_of_houses, 0)
    @left_moves = Array.new(@number_of_houses-1, 0)
    @right_moves = Array.new(@number_of_houses-1, 0)

    settle_cheapest_left
    settle_cheapest_right

    # order houses index by growing cost
    houses = (0...@number_of_houses).zip(@growing_costs).sort {|a, b| a[1] <=> b[1]}.map {|t| t[0]}

    houses.each do |house|
      local_cost = @growing_costs[house] unless @fields_productions[house] == 2
      left_cost = @cheapest_left[house][:total_cost]
      left_crop_house = @cheapest_left[house][:house]
      right_cost = @cheapest_right[house][:total_cost]
      right_crop_house = @cheapest_right[house][:house]

      costs = []
      costs << local_cost unless local_cost.nil?
      costs << left_cost unless left_cost.nil?
      costs << right_cost unless right_cost.nil?

      if local_cost == costs.min
        @fields_productions[house] += 1
        if @fields_productions[house] == 2
          resettle_cheapest_left(cheap_house: house)
          resettle_cheapest_right(cheap_house: house)
        end
      elsif left_cost == costs.min
        @fields_productions[left_crop_house] += 1
        if @fields_productions[left_crop_house] == 2
          resettle_cheapest_left(cheap_house: left_crop_house)
          resettle_cheapest_right(cheap_house: left_crop_house)
        end
      elsif right_cost == costs.min
        @fields_productions[right_crop_house] += 1
        if @fields_productions[right_crop_house] == 2
          resettle_cheapest_left(cheap_house: right_crop_house)
          resettle_cheapest_right(cheap_house: right_crop_house)
        end
      end
    end

    sum_prices
  end

  private

  def settle_cheapest_left
    @cheapest_left = Array.new(@number_of_houses) {{house: nil, total_cost: nil, carrying_cost: nil}}

    aux_cheapest = nil
    (1...@number_of_houses).each do |house|
      if aux_cheapest.nil?
        if @fields_productions[house - 1] < 2
          aux_cheapest = {house: house - 1, total_cost: @growing_costs[house - 1] + @carrying_costs[house - 1], carrying_cost: @carrying_costs[house - 1]}
        else
          next
        end
      else
        last_house_cost = aux_cheapest[:total_cost] + @carrying_costs[house - 1]
        one_jump_cost = @growing_costs[house - 1] + @carrying_costs[house - 1]
        if last_house_cost < one_jump_cost || @fields_productions[house - 1] > 1
          aux_cheapest[:total_cost] += @carrying_costs[house - 1]
          aux_cheapest[:carrying_cost] += @carrying_costs[house - 1]
        else
          aux_cheapest = {house: house - 1, total_cost: @growing_costs[house - 1] + @carrying_costs[house - 1], carrying_cost: @carrying_costs[house - 1]}
        end
      end
      @cheapest_left[house] = aux_cheapest.dup
    end
  end

  def settle_cheapest_right
    @cheapest_right = Array.new(@number_of_houses) {{house: nil, total_cost: nil, carrying_cost: nil}}
    aux_cheapest = nil
    (0...(@number_of_houses - 1)).reverse_each do |house|
      if aux_cheapest.nil?
        if @fields_productions[house + 1] < 2
          aux_cheapest = {house: house + 1, total_cost: @growing_costs[house + 1] + @carrying_costs[house], carrying_cost: @carrying_costs[house]}
        else
          next
        end
      else
        last_house_cost = aux_cheapest[:total_cost] + @carrying_costs[house]
        one_jump_cost = @growing_costs[house + 1] + @carrying_costs[house]
        if last_house_cost < one_jump_cost || @fields_productions[house + 1] > 1
          aux_cheapest[:total_cost] += @carrying_costs[house]
          aux_cheapest[:carrying_cost] += @carrying_costs[house]
        else
          aux_cheapest = {house: house + 1, total_cost: @growing_costs[house + 1] + @carrying_costs[house], carrying_cost: @carrying_costs[house]}
        end
      end
      @cheapest_right[house] = aux_cheapest.dup
    end
  end

  def resettle_cheapest_left(cheap_house:)
    return if cheap_house == @number_of_houses - 1
    aux_cheapest = @cheapest_left[cheap_house].dup
    ((cheap_house+1)...@number_of_houses).each do |house|
      break if !@cheapest_left[house][:house].nil? && @cheapest_left[house][:house] != cheap_house

      if aux_cheapest[:house].nil?
        if @fields_productions[house - 1] < 2
          aux_cheapest = {house: house - 1, total_cost: @growing_costs[house - 1] + @carrying_costs[house - 1], carrying_cost: @carrying_costs[house - 1]}
        else
          next
        end
      else
        last_house_cost = aux_cheapest[:total_cost] + @carrying_costs[house - 1]
        one_jump_cost = @growing_costs[house - 1] + @carrying_costs[house - 1]
        if last_house_cost < one_jump_cost || @fields_productions[house - 1] > 1
          aux_cheapest[:total_cost] += @carrying_costs[house - 1]
          aux_cheapest[:carrying_cost] += @carrying_costs[house - 1]
        else
          aux_cheapest = {house: house - 1, total_cost: @growing_costs[house - 1] + @carrying_costs[house - 1], carrying_cost: @carrying_costs[house - 1]}
        end
      end
      @cheapest_left[house] = aux_cheapest.dup
    end
  end

  def resettle_cheapest_right(cheap_house:)
    return if cheap_house == 0
    aux_cheapest = @cheapest_right[cheap_house].dup
    (0...(cheap_house - 1)).reverse_each do |house|
      break if !@cheapest_right[house][:house].nil? && @cheapest_right[house][:house] != cheap_house

      if aux_cheapest[:house].nil?
        if @fields_productions[house + 1] < 2
          aux_cheapest = {house: house + 1, total_cost: @growing_costs[house + 1] + @carrying_costs[house], carrying_cost: @carrying_costs[house]}
        else
          next
        end
      else
        last_house_cost = aux_cheapest[:total_cost] + @carrying_costs[house]
        one_jump_cost = @growing_costs[house + 1] + @carrying_costs[house]
        if last_house_cost < one_jump_cost || @fields_productions[house + 1] > 1
          aux_cheapest[:total_cost] += @carrying_costs[house]
          aux_cheapest[:carrying_cost] += @carrying_costs[house]
        else
          aux_cheapest = {house: house + 1, total_cost: @growing_costs[house + 1] + @carrying_costs[house], carrying_cost: @carrying_costs[house]}
        end
      end
      @cheapest_right[house] = aux_cheapest.dup
    end
  end

  def sum_prices
    debt = 0
    paths = Array.new(@number_of_houses-1, 0)
    (0...(@number_of_houses-1)).each do |house|
      debt += 1
      debt -= @fields_productions[house]
      paths[house] = debt.abs
    end

    total_cost = 0
    (0...@number_of_houses).each do |house|
      total_cost += @growing_costs[house] * @fields_productions[house]
    end
    @carrying_costs.each_with_index do |cost, index|
      total_cost += paths[index] * cost
    end
    total_cost
  end
end

number_of_houses = gets.to_i
carrying_costs = gets.split.map(&:to_i)
growing_costs = gets.split.map(&:to_i)

farm_village = FarmVillage.new(number_of_houses: number_of_houses,
                               carrying_costs: carrying_costs,
                               growing_costs: growing_costs)

puts farm_village.calc_total_cost