NOIP '21 P3 - Variance

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem type

You are given a sequence of nondecreasing positive integers 1a1a2an.

At each step, you can perform the following operation: Choose any positive integer 1<i<n, change ai to ai1+ai+1ai.

Determine the minimum variance of the sequence a after performing any number of operations.

The variance is the average of the squared differences between numbers and their mean value. Formally, variance is D=1ni=1n(aia)2 where a=1ni=1nai.

Input Specification

The first line contains a positive integer n. It is guaranteed that n104.

The second line contains n positive integers where the i-th number represents the value of ai. The data guarantee that 1a1a2an.

Output Specification

Output a single nonnegative integer representing n2 times the minimum variance you determine.

Sample Input

Copy
4
1 2 4 6

Sample Output

Copy
52

Sample Explanation

For (a1,a2,a3,a4)=(1,2,4,6), after the first operation, we can get (1,3,4,6). After the second operation, we can get (1,3,5,6). After that we cannot get any new sequence.

For (a1,a2,a3,a4)=(1,2,4,6), the mean is 134, the variance is 14((1134)2+(2134)2+(4134)2+(6134)2)=5916.

For (a1,a2,a3,a4)=(1,3,4,6), the mean is 72, the variance is 14((172)2+(372)2+(472)2+(672)2)=134.

For (a1,a2,a3,a4)=(1,3,5,6), the mean is 154, the variance is 14((1154)2+(3154)2+(5154)2+(6154)2)=5916.

Additional Samples

Additional samples can be found here.

Constraints

Test cases n ai
13 4 10
45 10 40
68 15 20
912 20 300
1315 50 70
1618 100 40
1922 400 600
2325 10000 50

For all test cases, it is guaranteed that n10000, ai600.


Comments

There are no comments at the moment.