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

#CC0000063. Chọn những bông hoa đẹp nhất (Q - Flowers)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chọn những bông hoa đẹp nhất (Q - Flowers)

Q - Flowers

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn flowers in a row. Flower ii has height hih_i and beauty aia_i. Heights are a permutation of 1,…,n1,\ldots,n. Remove some flowers, keeping the original order, so that remaining heights are strictly increasing and their total beauty is maximized.

Input

The first line contains nn; the second contains h1,…,hnh_1,\ldots,h_n; the third contains a1,…,ana_1,\ldots,a_n.

Output

Print the maximum possible total beauty.

Subtasks

Original constraints: 1≤n≤2⋅1051\le n\le2\cdot10^5; heights form a permutation of 1,…,n1,\ldots,n; 1≤ai≤1091\le a_i\le10^9.

  • Subtask 1 (20 points): 1≤n≤81\le n\le8; hh is a permutation of 1,…,n1,\ldots,n; 1≤ai≤501\le a_i\le50.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le3000; hh is a permutation of 1,…,n1,\ldots,n; 1≤ai≤1000001\le a_i\le100000.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le200000; hh is a permutation of 1,…,n1,\ldots,n; 1≤ai≤10000000001\le a_i\le1000000000.

Examples

Input

4
3 1 4 2
10 20 30 40

Output

60

Explanation

Keep flowers 22 and 44, whose heights are 1,21,2 and beauties are 20,4020,40. Their total beauty is 6060.