Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }});

Farm Village

There is a village along a road. This village has $N$ houses numbered $1$ to $N$ in order along the road. Each house has a field that can make up to two units of the crop and needs just one unit of the crop. The total cost to distribute one unit of the crop to each house is the summation of carrying costs and growing costs.

The carrying cost: The cost to carry one unit of the crop between the $i$-th house and the ($i+1$)-th house is $d_i$. It takes the same cost in either direction to carry.

The growing cost: The cost to grow one unit of the crop in the $i$-th house's field is $g_i$.

Your task is to calculate the minimum total cost to supply one unit of the crop to each house.

Input

The input consists of a single test case formatted as follows.

$N$
$d_1$ ... $d_{N-1}$
$g_1$ ... $g_{N}$

The first line consists of an integer $N$ ($2 \leq 200,000$), which is the number of the houses. The second line consists of $N-1$ integers separated by spaces. The $i$-th integer $d_i$ ($1 \leq d_i \leq 10^9$, $1 \leq i \leq N-1$)represents the carrying cost between the $i$-th and the ($i+1$)-th houses. The third line consists of $N$ integers separated by spaces. The $i$-th integer $g_i$ ($1 \leq g_i \leq 10^9$, $1 \leq i \leq N$) represents the growing cost of the $i$-th house's field.

Output

Print the minimum cost to supply one unit of the crop to each house.

Sample Input 1

2
3
1 5

Output for Sample Input 1

5

Sample Input 2

3
100 100
1 2 3

Output for Sample Input 2

6

Sample Input 3

4
1 2 3
1 1 100 100

Output for Sample Input 3

12

Solution in Ruby (judge verdict: Wrong Answer)

class FarmVillage
  def initialize(number_of_houses:, carrying_costs:, growing_costs:)
    @number_of_houses = number_of_houses
    #@number_of_houses = 200000
    @carrying_costs = carrying_costs.dup
    #@carrying_costs = Array.new(@number_of_houses-1) {rand(1..100)}
    @growing_costs = growing_costs.dup
    #@growing_costs = Array.new(@number_of_houses) {rand(1..100)}
  end

  def calc_total_cost
    @fields_productions = Array.new(@number_of_houses, 0)

    @cheapest_left = Array.new(@number_of_houses) {{house: nil, total_cost: nil, carrying_cost: nil}}
    @cheapest_right = Array.new(@number_of_houses) {{house: nil, total_cost: nil, carrying_cost: nil}}
    @most_expensive_left = Array.new(@number_of_houses) {{house: nil, total_cost: nil}}
    @most_expensive_right = Array.new(@number_of_houses) {{house: nil, total_cost: nil}}
    left_accesses_carrying_cost = Array.new(@number_of_houses, 0)
    right_accesses_carrying_cost = Array.new(@number_of_houses, 0)
    furthest_left_accessor = Array.new(@number_of_houses)
    furthest_right_accessor = Array.new(@number_of_houses)
    left_moves = Array.new(@number_of_houses, 0)
    right_moves = Array.new(@number_of_houses, 0)

    left_distances = Array.new(@number_of_houses) {[]}
    right_distances = Array.new(@number_of_houses) {[]}


    aux_cheapest = nil
    (1...@number_of_houses).each do |house|
      if aux_cheapest.nil?
        aux_cheapest = {house: house - 1, total_cost: @growing_costs[house - 1] + @carrying_costs[house - 1], carrying_cost: @carrying_costs[house - 1]}
      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
          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

    aux_cheapest = nil
    (0...(@number_of_houses - 1)).reverse_each do |house|
      if aux_cheapest.nil?
        aux_cheapest = {house: house + 1, total_cost: @growing_costs[house + 1] + @carrying_costs[house], carrying_cost: @carrying_costs[house]}
      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
          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

    aux_most_expensive = nil
    (1...@number_of_houses).each do |house|
      if aux_most_expensive.nil?
        aux_most_expensive = {house: house - 1, total_cost: @growing_costs[house - 1] + @carrying_costs[house - 1]}
      else
        last_house_cost = aux_most_expensive[:total_cost] + @carrying_costs[house - 1]
        one_jump_cost = @growing_costs[house - 1] + @carrying_costs[house - 1]
        if last_house_cost >= one_jump_cost
          aux_most_expensive[:total_cost] += @carrying_costs[house - 1]
        else
          aux_most_expensive = {house: house - 1, total_cost: @growing_costs[house - 1] + @carrying_costs[house - 1]}
        end
      end
      @most_expensive_left[house] = aux_most_expensive.dup
    end

    aux_most_expensive = nil
    (0...(@number_of_houses - 1)).reverse_each do |house|
      if aux_most_expensive.nil?
        aux_most_expensive = {house: house + 1, total_cost: @growing_costs[house + 1] + @carrying_costs[house]}
      else
        last_house_cost = aux_most_expensive[:total_cost] + @carrying_costs[house]
        one_jump_cost = @growing_costs[house + 1] + @carrying_costs[house]
        if last_house_cost >= one_jump_cost
          aux_most_expensive[:total_cost] += @carrying_costs[house]
        else
          aux_most_expensive = {house: house + 1, total_cost: @growing_costs[house + 1] + @carrying_costs[house]}
        end
      end
      @most_expensive_right[house] = aux_most_expensive.dup
    end

    (0...@number_of_houses).each do |house|
      left_house = @cheapest_left[house][:house]
      left_cost = @cheapest_left[house][:total_cost]
      right_house = @cheapest_right[house][:house]
      right_cost = @cheapest_right[house][:total_cost]
      costs = []
      costs << @growing_costs[house]
      costs << left_cost unless left_cost.nil?
      costs << right_cost unless right_cost.nil?
      min_cost = costs.min
      if @growing_costs[house] == min_cost
        @fields_productions[house] += 1
      elsif left_cost == min_cost
        @fields_productions[left_house] += 1
        right_accesses_carrying_cost[left_house] += left_cost - @growing_costs[left_house]
        if furthest_right_accessor[left_house].nil? || house > furthest_right_accessor[left_house]
          furthest_right_accessor[left_house] = house
        end
        right_distances[left_house] << left_cost - @growing_costs[left_house]

      elsif right_cost == min_cost
        @fields_productions[right_house] += 1
        left_accesses_carrying_cost[right_house] += right_cost - @growing_costs[right_house]
        if furthest_left_accessor[right_house].nil? || house > furthest_left_accessor[right_house]
          furthest_left_accessor[right_house] = house
        end
        left_distances[right_house] << right_cost - @growing_costs[right_house]
      end
    end

    aux_moves = 0
    (0...@number_of_houses).each do |house|
      aux_moves = furthest_right_accessor[house] - house unless furthest_right_accessor[house].nil?
      right_moves[house] += aux_moves
      aux_moves -= 1 unless aux_moves.zero?
    end

    (0...@number_of_houses).reverse_each do |house|
      aux_moves = furthest_left_accessor[house] - house unless furthest_left_accessor[house].nil?
      left_moves[house] += aux_moves
      aux_moves -= 1 unless aux_moves.zero?
    end


    @cheapest_left = Array.new(@number_of_houses) {{house: nil, total_cost: nil, carrying_cost: nil}}
    @cheapest_right = 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

    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



    work_list = []

    @fields_productions.each_with_index do |fields, index|
      next if fields < 3
      work_list << [index, fields]
    end

    work_list.sort! {|a, b| b[1]<=>a[1]}

    work_list.each do |house, fields|
      (3..fields).each do


        left_crop = left_distances[house].first
        right_crop = right_distances[house].first

        left_move_left_crop = nil
        right_move_left_crop = nil
        left_move_right_crop = nil
        right_move_right_crop = nil

        unless left_crop.nil?
          unless @cheapest_left[house][:house].nil?
            left_move_left_crop = @cheapest_left[house][:total_cost]
            return_cost = @cheapest_left[house][:carrying_cost]
            left_move_left_crop -= ([left_crop, return_cost].min * 2)
          end

          unless @cheapest_right[house][:house].nil?
            right_move_left_crop = @cheapest_right[house][:total_cost]
          end
        end
        unless right_crop.nil?
          unless @cheapest_left[house][:house].nil?
            left_move_right_crop = @cheapest_left[house][:total_cost]
          end

          unless @cheapest_right[house][:house].nil?
            right_move_right_crop = @cheapest_right[house][:total_cost]
            return_cost = @cheapest_right[house][:carrying_cost]
            right_move_right_crop -= ([right_crop, return_cost].min * 2)
          end
        end

        costs = []
        costs << left_move_left_crop unless left_move_left_crop.nil?
        costs << right_move_left_crop unless right_move_left_crop.nil?
        costs << left_move_right_crop unless left_move_right_crop.nil?
        costs << right_move_right_crop unless right_move_right_crop.nil?
        min_cost = costs.min

        if left_move_left_crop == min_cost
          left_distances.shift
          new_crop_house = @cheapest_left[house][:house]
          @fields_productions[new_crop_house] += 1
          if @fields_productions[new_crop_house] > 1
            resettle_cheapest_left(cheap_house: new_crop_house)
            resettle_cheapest_right(cheap_house: new_crop_house)
          end
        elsif right_move_left_crop == min_cost
          left_distances.shift
          new_crop_house = @cheapest_right[house][:house]
          @fields_productions[new_crop_house] += 1
          if @fields_productions[new_crop_house] > 1
            resettle_cheapest_left(cheap_house: new_crop_house)
            resettle_cheapest_right(cheap_house: new_crop_house)
          end
        elsif left_move_right_crop == min_cost
          right_distances.shift
          new_crop_house = @cheapest_left[house][:house]
          @fields_productions[new_crop_house] += 1
          if @fields_productions[new_crop_house] > 1
            resettle_cheapest_left(cheap_house: new_crop_house)
            resettle_cheapest_right(cheap_house: new_crop_house)
          end
        elsif right_move_right_crop == min_cost
          right_distances.shift
          new_crop_house = @cheapest_right[house][:house]
          @fields_productions[new_crop_house] += 1
          if @fields_productions[new_crop_house] > 1
            resettle_cheapest_left(cheap_house: new_crop_house)
            resettle_cheapest_right(cheap_house: new_crop_house)
          end
        end

        @fields_productions[house] -= 1
      end
    end

    calc_final_cost
  end

  private

  def calc_final_cost
    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

  def resettle_cheapest_left(cheap_house:)
    return if cheap_house == @number_of_houses - 1
    aux_cheapest = @cheapest_left[cheap_house].dup
    if aux_cheapest[:house].nil?
      aux_cheapest = nil
      @cheapest_left[cheap_house+1] = {house: nil, total_cost: nil, carrying_cost: nil}
    else
      aux_cheapest[:total_cost] += @cheapest_left[cheap_house+1][:carrying_cost]
      aux_cheapest[:carrying_cost] += @cheapest_left[cheap_house+1][:carrying_cost]
      @cheapest_left[cheap_house+1] = aux_cheapest.dup
    end

    new_cheapest = nil
    ((cheap_house+2)...@number_of_houses).each do |house|
      if aux_cheapest.nil?
        aux_cheapest = {house: house - 1, total_cost: @growing_costs[house - 1] + @carrying_costs[house - 1], carrying_cost: @carrying_costs[house - 1]}
        next
      end
      break unless @cheapest_left[house][:house] == cheap_house

      unless new_cheapest.nil?
        new_cheapest[:total_cost] += @carrying_costs[house - 1]
        new_cheapest[:carrying_cost] += @carrying_costs[house - 1]
        @cheapest_left[house] = new_cheapest.dup
        next
      end

      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
        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]}
        new_cheapest = aux_cheapest.dup
      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
    if aux_cheapest[:house].nil?
      aux_cheapest = nil
      @cheapest_right[cheap_house-1] = {house: nil, total_cost: nil, carrying_cost: nil}
    else
      aux_cheapest[:total_cost] += @cheapest_right[cheap_house-1][:carrying_cost]
      aux_cheapest[:carrying_cost] += @cheapest_right[cheap_house-1][:carrying_cost]
      @cheapest_right[cheap_house-1] = aux_cheapest.dup
    end

    new_cheapest = nil
    (0...(cheap_house - 2)).reverse_each do |house|
      if aux_cheapest.nil?
        aux_cheapest = {house: house + 1, total_cost: @growing_costs[house + 1] + @carrying_costs[house], carrying_cost: @carrying_costs[house]}
        next
      end
      break unless @cheapest_right[house][:house] == cheap_house

      unless new_cheapest.nil?
        new_cheapest[:total_cost] += @carrying_costs[house]
        new_cheapest[:carrying_cost] += @carrying_costs[house]
        @cheapest_right[house] = new_cheapest.dup
        next
      end

      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
        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]}
        new_cheapest = aux_cheapest.dup
      end

      @cheapest_right[house] = aux_cheapest.dup
    end

  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