#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 of length .
Define the weight of a subarray of sequence 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 into several subarrays without overlap and without omission, such that for every , there are exactly subarrays of length .
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 , representing the length of sequence .
Then input one line with numbers, where the -th number is .
Finally input one line with numbers, where the -th number is .
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."
-
: .
-
: .
-
: .
-
: No special constraints.
For of the testdata: .
Explanation
-
We define that is a perfect square.
-
if and only if is true; otherwise .
Translated by ChatGPT 5