Lines Game

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

Consider the following game about removing NN straight line segments on the plane. The segments are numbered from 11 to NN. The ii-th segment connects points (0,i)(0, i) and (1,p_i)(1, p\_i), where the numbers p_1,p_2,,p_Np\_1, p\_2, \ldots, p\_N are a permutation of numbers from 11 to NN. Your are also given NN positive integers v_1,v_2,,v_Nv\_1, v\_2, \ldots, v\_N. On each step, you can select any segment ii which is not removed yet, pay v_iv\_i dollars, and then remove the ii-th segment and all segments intersecting with it. Note that you MUST remove all segments which intersect with ii-th segment.

The purpose of this game is to remove all segments while spending the minimum possible sum of dollars. Please answer how much you need to spend when you play the game with best strategy.

입력

The input consists of three lines. The first line contains an integer NN. The second line consists of NN integers p_1,p_2,,p_Np\_1, p\_2, \ldots, p\_N. The third line consists of NN integer v_1,v_2,,v_Nv\_1, v\_2, \ldots, v\_N.

출력

Output only one number indicating the minimum possible sum of dollars required to remove all segments.

제한

  • 1N1051 \le N \le 10^5
  • p_i\langle p\_i \rangle is a permutation of 1,2,,N1, 2, \ldots, N
  • 1v_i2×1041 \le v\_i \le 2 \times 10^4