#P4437. [HNOI/AHOI2018] 排列

[HNOI/AHOI2018] 排列

Description

Given nn integers a1,a2,,an,0aina_1, a_2, \dots, a_n, 0 \le a_i \le n, and nn integers w1,w2,,wnw_1, w_2, \dots, w_n. Call a permutation ap1,ap2,,apna_{p_1}, a_{p_2}, \dots, a_{p_n} of a1,a2,,ana_1, a_2, \dots, a_n a valid permutation of a1,a2,,ana_1, a_2, \dots, a_n if and only if it satisfies: for any kk and any jj, if jkj \le k, then apjpka_{p_j} \ne p_k. (In other words: for any kk and any jj, if pk=apjp_k = a_{p_j}, then k<jk < j.) Define the weight of this valid permutation as wp1+2wp2++nwpnw_{p_1} + 2 w_{p_2} + \dots + n w_{p_n}.

You need to find the maximum weight among all valid permutations. If no valid permutation exists, output 1-1.

The sample explanations include examples of valid and invalid permutations.

Input Format

The first line contains an integer nn.

The next line contains nn integers, denoting a1,a2,,ana_1, a_2, \dots, a_n. The following line contains nn integers, denoting w1,w2,,wnw_1, w_2, \dots, w_n.

Output Format

Output a single integer representing the answer.

3 
0 1 1 
5 7 3 
32
3 
2 3 1 
1 2 3 
-1
10 
6 6 10 1 7 0 0 1 7 7 
16 3 10 20 5 14 17 17 16 13 
809

Hint

Sample Explanation 1

For a1=0,a2=1,a3=1a_1 = 0, a_2 = 1, a_3 = 1, its permutations are:

  • a1=0,a2=1,a3=1a_1 = 0, a_2 = 1, a_3 = 1, this is a valid permutation, and its weight is 1×5+2×7+3×3=281\times 5 + 2\times 7 + 3\times 3 = 28;
  • a2=1,a1=0,a3=1a_2 = 1, a_1 = 0, a_3 = 1, this is an invalid permutation because ap2a_{p_2} equals p2p_2;
  • a1=0,a3=1,a2=1a_1 = 0, a_3 = 1, a_2 = 1, this is a valid permutation, and its weight is 1×5+2×3+3×7=321\times 5 + 2\times 3 + 3\times 7 = 32;
  • a3=1,a1=0,a2=1a_3 = 1, a_1 = 0, a_2 = 1, this is an invalid permutation because ap1a_{p_1} equals p2p_2;
  • a2=1,a3=1,a1=0a_2 = 1, a_3 = 1, a_1 = 0, this is an invalid permutation because ap1a_{p_1} equals p3p_3;
  • a3=1,a2=1,a1=0a_3 = 1, a_2 = 1, a_1 = 0, this is an invalid permutation because ap1a_{p_1} equals p3p_3.

Therefore, the maximum weight is 3232.

Sample Explanation 2

For a1=2,a2=3,a3=1a_1 = 2, a_2 = 3, a_3 = 1, its permutations are:

  • a1=2,a2=3,a3=1a_1 = 2, a_2 = 3, a_3 = 1, this is an invalid permutation because ap1a_{p_1} equals p2p_2;
  • a2=3,a1=2,a3=1a_2 = 3, a_1 = 2, a_3 = 1, this is an invalid permutation because ap1a_{p_1} equals p3p_3;
  • a1=2,a3=1,a2=3a_1 = 2, a_3 = 1, a_2 = 3, this is an invalid permutation because ap1a_{p_1} equals p3p_3;
  • a3=1,a1=2,a2=3a_3 = 1, a_1 = 2, a_2 = 3, this is an invalid permutation because ap2a_{p_2} equals p3p_3;
  • a2=3,a3=1,a1=2a_2 = 3, a_3 = 1, a_1 = 2, this is an invalid permutation because ap2a_{p_2} equals p3p_3;
  • a3=1,a2=3,a1=2a_3 = 1, a_2 = 3, a_1 = 2, this is an invalid permutation because ap1a_{p_1} equals p3p_3.

Therefore, there is no valid permutation.

Constraints

  • For the first 20%20\% of the testdata, 1n101 \le n \le 10.
  • For the first 40%40\% of the testdata, 1n151 \le n \le 15.
  • For the first 60%60\% of the testdata, 1n10001 \le n \le 1000.
  • For the first 80%80\% of the testdata, 1n1051 \le n \le 10^5.
  • For 100%100\% of the testdata, 1n5×1051 \le n \le 5\times 10^5, 0ain0 \le a_i \le n, 1wi1091 \le w_i \le 10^9, and the sum of all wiw_i does not exceed 1.5×10131.5\times 10^{13}.

Translated by ChatGPT 5