#P5154. 数列游戏

数列游戏

Description

{{The rules are as follows:

LJC writes down two sequences of positive integers AA and BB, both of length NN. The two sequences are paired one-to-one, that is, for each ii there is a pair (Ai,Bi)(A_i, B_i).

Each time, HKE may choose an adjacent pair A[i]A[i] and A[i+1]A[i+1]. If they are not coprime (i.e., gcd(A[i],A[i+1])>1\gcd(A[i], A[i+1]) > 1), he may remove this pair and gain B[i]+B[i+1]B[i] + B[i+1] points.

The removed pair is simultaneously deleted from both sequences, and the sequences are compacted (the remaining elements shift left).

When all adjacent pairs in the sequence are coprime, the game ends. HKE wants to know the maximum total score he can obtain.}}

Input Format

{{- Line 1: an integer NN (the length of the sequences).

  • Line 2: NN integers, representing A1,A_1, A2,,ANA_2, \ldots, A_N.
  • Line 3: NN integers, representing B1,B_1, B2,,BNB_2, \ldots, B_N.}}

Output Format

{{- Output one integer, the maximum total score HKE can obtain.}}

6
9 8 6 5 6 3
11 19 12 17 18 15
64
//解释:擦去A[2],A[3]与A[5],A[6],得分为64

Hint

{{Sample Explanation:

  • First remove A[2]=8A[2]=8 and A[3]=6A[3]=6, gaining B[2]+B[3]=19+12=31B[2]+B[3]=19+12=31 points.
  • After the update, they become 9,5,6,39, 5, 6, 3 and 11,17,18,1511, 17, 18, 15.
  • Then remove A[3]=6A[3]=6 and A[4]=3A[4]=3, gaining B[3]+B[4]=18+15=33B[3]+B[4]=18+15=33 points.
  • The total score is 31+33=6431+33=64.

Constraints:

  • For 30% of the testdata, N20N \leq 20.
  • For 60% of the testdata, N100N \leq 100.
  • For 80% of the testdata, N500N \leq 500.
  • For 100% of the testdata, N800N \leq 800.
  • It is guaranteed that 1Ai,Bi1091 \leq A_i, B_i \leq 10^9.}}

Translated by ChatGPT 5