#abc359f. F - Tree Degree Optimization

F - Tree Degree Optimization

Score : 550550 points

问题陈述

给定一个整数序列 A=(A1,,AN)A=(A_1,\ldots,A_N)。对于一个有 NN 个顶点的树 TT,定义 f(T)f(T) 如下:

  • did_i 为树 TT 中顶点 ii 的度数。那么,f(T)=i=1Ndi2Aif(T)=\sum_{i=1}^N {d_i}^2 A_i

找出 f(T)f(T) 的最小可能值。

约束条件保证答案小于 2632^{63}

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a sequence of integers A=(A1,,AN)A=(A_1,\ldots,A_N). For a tree TT with NN vertices, define f(T)f(T) as follows:

  • Let did_i be the degree of vertex ii in TT. Then, f(T)=i=1Ndi2Aif(T)=\sum_{i=1}^N {d_i}^2 A_i.

Find the minimum possible value of f(T)f(T).

The constraints guarantee the answer to be less than 2632^{63}.

Constraints

  • 2N2×1052\leq N\leq 2\times 10^5
  • 1Ai1091\leq A_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

Output

Print the answer.

Sample Input 1

4
3 2 5 2

Sample Output 1

24

Consider a tree TT with an edge connecting vertices 11 and 22, an edge connecting vertices 22 and 44, and an edge connecting vertices 44 and 33.

Then, $f(T) = 1^2\times 3 + 2^2\times 2 + 1^2\times 5 + 2^2\times 2 = 24$. It can be proven that this is the minimum value of f(T)f(T).

Sample Input 2

3
4 3 2

Sample Output 2

15

Sample Input 3

7
10 5 10 2 10 13 15

Sample Output 3

128