PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000083. Chọn hoa đẹp nhất (Flowers)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Chọn hoa đẹp nhất (Flowers)

Flowers

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn flowers arranged from left to right. Flower ii has height hih_i and beauty aia_i. All heights are distinct.

You may remove any flowers without changing the relative order of those remaining. Their heights, read from left to right, must form a strictly increasing sequence.

The value of a choice is the sum of the beauties of the remaining flowers. Find the maximum achievable sum.

Input

The first line contains nn.

The second line contains exactly nn heights h1,h2,…,hnh_1,h_2,\ldots,h_n.

The third line contains exactly nn beauties a1,a2,…,ana_1,a_2,\ldots,a_n, with aia_i belonging to the flower of height hih_i.

Output

Print the maximum total beauty of a valid set of remaining flowers.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; 1≤hi≤n1\le h_i\le n; 1≤ai≤1091\le a_i\le 10^9; all hih_i are pairwise distinct.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤hi≤n1\le h_i\le n; 1≤ai≤1091\le a_i\le 10^9; all hih_i are pairwise distinct.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤hi≤n1\le h_i\le n; 1≤ai≤1091\le a_i\le 10^9; all hih_i are pairwise distinct.

Examples

Input

4
3 1 4 2
10 20 30 40

Output

60

Explanation

The four (height, beauty) pairs are (3,10)(3,10), (1,20)(1,20), (4,30)(4,30), (2,40)(2,40).

Keeping flowers 22 and 44 produces heights 1<21<2 and beauty 20+40=6020+40=60. Keeping flowers 11 and 33 is also valid, but gives only 10+30=4010+30=40. Flowers 2,3,42,3,4 cannot all remain because heights 1,4,21,4,2 do not increase. The maximum is 60.