#P13950. [EC Final 2019] Value

[EC Final 2019] Value

Problem Description

Pang\textit{Pang} believes that one cannot make an omelet without breaking eggs.

For a subset AA of {1,2,…,n}\{1,2,\ldots,n\}, we calculate the score of AA as follows:

  • Initialize the score as 00.
  • For any i∈Ai\in A, add aia_i to the score.
  • For any pair of integers (i,j)(i, j) satisfying i≥2i\ge 2, j≥2j\ge 2, i∈Ai\in A and j∈Aj\in A, if there exists positive integer k>1k > 1 such that ik=ji^k=j, subtract bjb_j from the score.

Find the maximum possible score over the choice of AA.

Input Format

The first line contains a single integer nn (1≤n≤100000)(1\le n\le 100000).

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤1000000000)(1\le a_i\le 1000000000).

The third line contains nn integers b1,b2,…,bnb_1,b_2,\ldots,b_n (1≤bi≤1000000000)(1\le b_i\le 1000000000).

Output Format

Print a single integer xx --- the maximum possible score.

4
1 1 1 2
1 1 1 1
4
4
1 1 1 1
1 1 1 2
3