#P72. [CSP-S 2019 Day 2] Brute Force Optimization

[CSP-S 2019 Day 2] Brute Force Optimization

Person in Charge

Attention

The data range of this problem is not exactly the same as the original problem.

Problem Description

In 2048, in the examination room of the 30th CSP Certification, Xiao Ming, a contestant, opened the first problem. This problem has nn groups of sample data, numbered from 11 to nn, and the scale of the ii-th group of data is aia_i.

Xiao Ming designed a brute-force program for this problem. For a group of data with scale uu, the running time of the program is u2u^2. However, after the program runs a group of data with scale uu, it will run incorrectly on any group of data with a scale smaller than uu. The aia_i in the sample are not necessarily increasing, but Xiao Ming wants to run the sample correctly without modifying the program, so he decides to use a very primitive solution: divide all data into several consecutive data segments (the numbers of data in a segment are consecutive), then merge the data in the same segment into new data whose scale is equal to the sum of the scales of the original data in the segment, and Xiao Ming will make the scales of the new data non-decreasing.

That is to say, Xiao Ming needs to find some dividing points 1k1<k2<<kp<n1 \le k_1 \lt k_2 \lt \cdots \lt k_p \lt n such that:

$$\sum_{i=1}^{k_1} a_i \le \sum_{i=k_1+1}^{k_2} a_i \le \cdots \le \sum_{i=k_p+1}^{n} a_i$$

Note that pp can be 00 (in this case k0=0k_0 = 0), which means Xiao Ming can merge all data and run them together.

Xiao Ming hopes that under the condition that the program runs the sample correctly, the running time is as small as possible, that is, to minimize:

$$(\sum_{i=1}^{k_1} a_i)^2 + (\sum_{i=k_1+1}^{k_2} a_i)^2 + \cdots + (\sum_{i=k_p+1}^{n} a_i)^2$$

Xiao Ming thinks this problem is very interesting and asks you for help: given nn and aia_i, please find the minimum running time of Xiao Ming's program under the optimal partitioning scheme.

Input Format

Due to the large data range of this problem, the aia_i of some test points will be generated within the program.

The first line contains two integers n,typen, type. nn has the meaning described in the problem, and typetype indicates the input method:

  1. If type=0type = 0, the aia_i of this test point are given directly. Next in the input file: the second line contains nn integers aia_i separated by spaces, representing the scale of each group of data.
  2. If type=1type = 1, the aia_i of this test point will be generated specially, and the generation method is described later. Next in the input file: the second line contains six integers x,y,z,b1,b2,mx, y, z, b_1, b_2, m separated by spaces. The next mm lines each contain three positive integers pi,li,rip_i, l_i, r_i separated by spaces (the ii-th line, 1im1 \le i \le m).

For test points 232523 \sim 25 with type=1type = 1, the generation method of aia_i is as follows:

Given integers x,y,z,b1,b2,mx, y, z, b_1, b_2, m, and mm triples (pi,li,ri)(p_i, l_i, r_i).

It is guaranteed that n2n \ge 2. If n>2n \gt 2, then $\forall 3 \le i \le n, b_i = (x \times b_{i−1} + y \times b_{i−2} + z) \mod 2^{30}$.

It is guaranteed that 1pin1 \le p_i \le n and pm=np_m = n. Let p0=0p_0 = 0, then pip_i also satisfies 0i<m\forall 0 \le i \lt m, pi<pi+1p_i \lt p_{i+1}.

For all 1jm1 \le j \le m, if the subscript i(1in)i (1 \le i \le n) satisfies pj1<ipjp_{j−1} \lt i \le p_j, then:

$$a_i = \left(b_i \mod \left( r_j − l_j + 1 \right) \right) + l_j$$

The above data generation method is only to reduce the size of the input file, and the standard algorithm does not depend on this generation method.

Output Format

Output a single integer on one line, representing the answer.

Samples

5 0
5 1 7 9 9
247
10 0
5 6 7 7 4 6 2 13 19 9
1256

Sample 1 Explanation

The optimal partitioning scheme is {5,1},{7},{9},{9}\{5,1\}, \{7\}, \{9\}, \{9\}. It is a valid scheme because 5+17995 + 1 \le 7 \le 9 \le 9.

The answer is (5+1)2+72+92+92=247(5 + 1)^2 + 7^2 + 9^2 + 9^2 = 247.

Although the partitioning scheme {5},{1},{7},{9},{9}\{5\}, \{1\}, \{7\}, \{9\}, \{9\} has a smaller running time than 247, it is not a valid scheme because 5>15 \gt 1.

Although the partitioning scheme {5},{1,7},{9},{9}\{5\}, \{1,7\}, \{9\}, \{9\} is valid, its corresponding running time is 251, which is larger than 247.

Sample 2 Explanation

The optimal partitioning scheme is $\{5\}, \{6\}, \{7\}, \{7\}, \{4,6,2\}, \{13\}, \{19,9\}$.

Sample 3

See partition/partition3.in and partition/partition3.ans in the contestant's directory.

Data Range

Test Case ID nn \le aia_i \le type=type =
131 \sim 3 1010 00
464 \sim 6 5050 10310^3
797 \sim 9 400400 10410^4
101610 \sim 16 50005000 10510^5
172217 \sim 22 5×1055 \times 10^5 10610^6
232523 \sim 25 1×1071 \times 10^7 10910^9 11

For all test points with type=0type=0, it is guaranteed that the final output answer 4×1018\le 4 \times 10^{18}.

For 100%100\% of the data: type{0,1}type \in \{0,1\}, 2n1×1072 \le n \le 1 \times 10^7, 1ai1091 \le a_i \le 10^9, 1m1051 \le m \le 10^5, 1liri1091 \le l_i \le r_i \le 10^9, 0x,y,z,b1,b2<2300 \le x,y,z,b_1,b_2 \lt 2^{30}.