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)
    @imports = Array.new(@number_of_houses, nil)

    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, local_crop_house = calc_local_crop_cost(house: house)
      left_cost, left_crop_house = calc_left_import_cost(consumer_house: house)
      right_cost, right_crop_house = calc_right_import_cost(consumer_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
        total_cost += local_cost
        @fields_productions[local_crop_house] += 1
        @imports[house] = local_crop_house
      elsif left_cost == costs.min
        total_cost += left_cost
        @fields_productions[left_crop_house] += 1
        @imports[house] = left_crop_house
      elsif right_cost == costs.min
        total_cost += right_cost
        @fields_productions[right_crop_house] += 1
        @imports[house] = right_crop_house
      end
    end

    total_cost
  end

  private

  def calc_local_crop_cost(house:)
    cost = nil
    new_crop_house = nil
    if @fields_productions[house] < 2
      cost = @growing_costs[house]
      new_crop_house = house
    else
      export_houses = @imports.each_index.select {|i| @imports[i] == house}
      export_house_a = export_houses.first
      export_house_b = export_houses.last

      export_house_a_cost = nil
      new_crop_house_a = nil
      if export_house_a < house
        export_house_a_cost, new_crop_house_a = calc_left_import_cost(consumer_house: export_house_a)
      else
        export_house_a_cost, new_crop_house_a = calc_right_import_cost(consumer_house: export_house_a)
      end
      export_house_a_cost -= calc_carrying_cost(house, export_house_a) unless export_house_a_cost.nil?

      export_house_b_cost = nil
      new_crop_house_b = nil
      if export_house_b < house
        export_house_b_cost, new_crop_house_b = calc_left_import_cost(consumer_house: export_house_b)
      else
        export_house_b_cost, new_crop_house_b = calc_right_import_cost(consumer_house: export_house_b)
      end
      export_house_b_cost -= calc_carrying_cost(house, export_house_b) unless export_house_b_cost.nil?

      if !export_house_a_cost.nil?
        cost = export_house_a_cost
        new_crop_house = new_crop_house_a
      end

      if !export_house_b_cost.nil?
        if cost.nil? || export_house_b_cost < cost
          cost = export_house_b_cost
          new_crop_house = new_crop_house_b
        end
      end
    end
    [cost, new_crop_house]
  end

  def calc_left_import_cost(consumer_house:)
    calc_import_cost(consumer_house: consumer_house,
                     carrying_costs: @carrying_costs,
                     fields_productions: @fields_productions,
                     growing_costs: @growing_costs)
  end

  def calc_right_import_cost(consumer_house:)
    import_cost, import_house = calc_import_cost(consumer_house: @number_of_houses-1-consumer_house,
                                                 carrying_costs: @carrying_costs.reverse,
                                                 fields_productions: @fields_productions.reverse,
                                                 growing_costs: @growing_costs.reverse)
    import_house = @number_of_houses-1-import_house unless import_house.nil?

    [import_cost, import_house]
  end

  def calc_import_cost(consumer_house:, carrying_costs:, fields_productions:, growing_costs:)
    total_carrying_cost = 0
    total_carrying_costs = Array.new(consumer_house)
    (0...consumer_house).reverse_each do |house|
      total_carrying_cost += carrying_costs[house]
      total_carrying_costs[house] = total_carrying_cost
    end

    last_crop_house = nil
    crop_houses = Array.new(consumer_house)
    costs = Array.new(consumer_house)
    (0...consumer_house).each do |house|
      if fields_productions[house] < 2
        last_crop_house = house
      end

      if !last_crop_house.nil?
        crop_houses[house] = last_crop_house
        costs[house] = total_carrying_costs[last_crop_house] + growing_costs[last_crop_house]
      end
    end

    import_cost = nil
    import_house = nil
    costs.each_with_index do |cost, index|
      if !cost.nil?
        if import_cost.nil? || cost < import_cost
          import_cost = cost
          import_house = crop_houses[index]
        end
      end
    end

    [import_cost, import_house]
  end

  def calc_carrying_cost(house_a, house_b)
    house_a, house_b = house_b, house_a if house_a > house_b
    @carrying_costs[house_a...house_b].inject(:+)
  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