#P9375. 「DROI」Round 2 划分

「DROI」Round 2 划分

Background

Instead of writing a weak background story, it is better to provide a higher-quality problem.

Problem Description

Given a sequence AA of length nn.

Define the weight of a subarray [L,R][L, R] of sequence AA as:

$$\sum_{i=L}^{R}[\vert A_i - A_L \vert是完全平方数] \times \sum_{i=L}^{R}[\vert A_R - A_i \vert是完全平方数]$$

Now you need to partition sequence AA into several subarrays without overlap and without omission, such that for every i[1,n]i \in [1, n], there are exactly cic_i subarrays of length ii.

Based on this, find a partition plan that maximizes the sum of weights of all subarrays, and output this maximum value. In particular, if no valid partition plan exists, output -1.

If the statement is unclear, please see the explanation in the Hint section below.

Input Format

First, input an integer nn, representing the length of sequence AA.

Then input one line with nn numbers, where the ii-th number is AiA_i.

Finally input one line with nn numbers, where the ii-th number is cic_i.

Output Format

Output one line containing one integer, representing the answer.

6
2 1 4899 4 1 4
1 1 1 0 0 0
9
10
1 1 1 2 4 3 3 3 8 8
2 1 2 0 0 0 0 0 0 0
24

Hint

Sample Explanation

For Sample 1, one optimal partition is to cut the sequence after the 2nd and 3rd numbers.

For Sample 2, one optimal partition is to cut the sequence after the 3rd, 4th, 5th, and 8th numbers.


Constraints

"This problem uses bundled tests."

  • Subtask1(10%)\operatorname{Subtask} 1(10\%): n20n \leq 20.

  • Subtask2(20%)\operatorname{Subtask} 2(20\%): n50,i=1nci20n \leq 50, \sum_{i=1}^{n} c_i \leq 20.

  • Subtask3(20%)\operatorname{Subtask} 3(20\%): n50,i>5,ci=0n \leq 50, \forall i>5, c_i=0.

  • Subtask4(50%)\operatorname{Subtask} 4(50\%): No special constraints.

For 100%100\% of the testdata: 0cin120,1ai1040 \leq c_i \leq n \leq 120, 1 \leq a_i \leq 10^4.


Explanation

  • We define that 00 is a perfect square.

  • [P]=1[P]=1 if and only if PP is true; otherwise [P]=0[P]=0.

Translated by ChatGPT 5