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)

    # 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]}

    min_index = 0
    houses.each do |house|
      min_new_crop_value = @growing_costs[houses[min_index/2]]
      min_index += 1

      local_cost = @growing_costs[house]
      left_cost, left_crop_house = calc_left_crop_cost(consumer_house: house,
                                                       stop_price: local_cost - min_new_crop_value)
      right_cost, right_crop_house = calc_right_crop_cost(consumer_house: house,
                                                          stop_price: local_cost - min_new_crop_value)

      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
      elsif left_cost == costs.min
        @fields_productions[left_crop_house] += 1
        change_moves_to_left(from: house, to: left_crop_house)
      elsif right_cost == costs.min
        @fields_productions[right_crop_house] += 1
        change_moves_to_right(from: house, to: right_crop_house)
      end
    end

    sum_prices
  end

  private

  def change_moves_to_left(from:, to:)
    (to...from).each do |road|
      if @right_moves[road] > 0
        @right_moves[road] -= 1
      else
        @left_moves[road] += 1
      end
    end
  end

  def change_moves_to_right(from:, to:)
    (from...to).each do |road|
      if @left_moves[road] > 0
        @left_moves[road] -= 1
      else
        @right_moves[road] += 1
      end
    end
  end

  def sum_prices
    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 += @left_moves[index] * cost
      total_cost += @right_moves[index] * cost
    end
    total_cost
  end

  def calc_left_crop_cost(consumer_house:, stop_price:)
    lower_cost = nil
    crop_house = nil

    road_cost = 0
    (0...consumer_house).each do |house|
      aux_house = consumer_house - 1 - house
      if @right_moves[aux_house] > 0
        road_cost -= @carrying_costs[aux_house]
      else
        road_cost += @carrying_costs[aux_house]
      end
      next unless @fields_productions[aux_house] < 2

      house_cost = road_cost + @growing_costs[aux_house]
      if lower_cost.nil? || house_cost < lower_cost
        lower_cost = house_cost
        crop_house = aux_house
      end

      break if road_cost > stop_price
    end

    [lower_cost, crop_house]
  end

  def calc_right_crop_cost(consumer_house:, stop_price:)
    lower_cost = nil
    crop_house = nil

    road_cost = 0
    ((consumer_house+1)...@number_of_houses).each do |house|
      if @left_moves[house-1] > 0
        road_cost -= @carrying_costs[house-1]
      else
        road_cost += @carrying_costs[house-1]
      end
      next unless @fields_productions[house] < 2

      house_cost = road_cost + @growing_costs[house]
      if lower_cost.nil? || house_cost < lower_cost
        lower_cost = house_cost
        crop_house = house
      end

      break if road_cost > stop_price
    end

    [lower_cost, crop_house]
  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