#P8920. 『MdOI R5』Variance

『MdOI R5』Variance

Background

Subtasks 1 to 5 use the original testdata, and Subtask 6 uses hack testdata.

Problem Description

Given two integer sequences a,ba, b of length nn, satisfying:

  • $\forall i\in [1,n), a_i \le a_{i+1}, b_i \le b_{i+1}$.
  • i[1,n],aibi\forall i\in [1,n], a_i \le b_i.

There is a real-valued sequence cc of length nn such that ci[ai,bi]c_i \in [a_i, b_i]. Find the maximum possible variance of cc.

You only need to output the result after multiplying the answer by n2n^2. It is easy to prove that this is an integer.

Input Format

The first line contains one integer nn.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n.

The third line contains nn integers b1,b2,,bnb_1, b_2, \dots, b_n.

Output Format

One line containing one integer, which is the answer.

2
1 10
1 10
81
3
1 2 3
3 4 5
26

Hint

The variance of a sequence aa of length nn is $\dfrac{1}{n}\sum\limits_{i=1}^n (a_i-\overline{a})^2$, where a=1ni=1nai\overline{a}=\dfrac{1}{n}\sum\limits_{i=1}^n a_i.

During the computation for this problem, numbers exceeding the range of long long may appear. In that case, you may need to use __int128.

We provide the following code, which can be used to output a value of type __int128:

void print(__int128 x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x<10)
	{
		putchar(x+48);
		return;
	}
	print(x/10);
	putchar(x%10+48);
}

Constraints: for 100%100\% of the data, 1n1061 \le n \le 10^6, 1ai,bi1091 \le a_i, b_i \le 10^9.

Subtask1(10%)\operatorname{Subtask} 1(10\%): n2×103n \le 2\times 10^3, ai=bi105a_i=b_i \le 10^5.

Subtask2(20%)\operatorname{Subtask} 2(20\%): n10n \le 10, ai,bi5a_i, b_i \le 5.

Subtask3(20%)\operatorname{Subtask} 3(20\%): n2×103n \le 2\times 10^3, ai,bi105a_i, b_i \le 10^5.

Subtask4(20%)\operatorname{Subtask} 4(20\%): n105n \le 10^5, ai,bi2×103a_i, b_i \le 2\times 10^3.

Subtask5(30%)\operatorname{Subtask} 5(30\%): no special constraints.

Sample Explanation 1

cc can only be (1,10)(1,10).

Sample Explanation 2

One optimal cc is (1,2,5)(1,2,5).

Translated by ChatGPT 5