F - Tree Degree Optimization Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 550550

問題文

整数列 A=(A1,,AN)A=(A_1,\ldots,A_N) が与えられます。 NN 頂点の木 TT に対して、 f(T)f(T) を以下で定めます。

  • TT の頂点 ii の次数を did_i とする。このとき、f(T)=i=1Ndi2Aif(T)=\sum_{i=1}^N {d_i}^2 A_i とする。

f(T)f(T) として考えられる最小値を求めてください。

なお、制約下において答えが 2632^{63} 未満となることは保証されています。

制約

  • 2N2×1052\leq N\leq 2\times 10^5
  • 1Ai1091\leq A_i \leq 10^9
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

NN 
A1A_1 A2A_2 \ldots ANA_N

出力

答えを出力せよ。


入力例 1Copy

Copy
4
3 2 5 2

出力例 1Copy

Copy
24

頂点 11 と頂点 22 を結ぶ辺、頂点 22 と頂点 44 を結ぶ辺、頂点 44 と頂点 33 を結ぶ辺からなるような木 TT を考えます。

このとき f(T)=12×3+22×2+12×5+22×2=24f(T) = 1^2\times 3 + 2^2\times 2+1^2\times 5 +2^2\times 2 = 24 です。これが f(T)f(T) の最小値であることが証明できます。


入力例 2Copy

Copy
3
4 3 2

出力例 2Copy

Copy
15

入力例 3Copy

Copy
7
10 5 10 2 10 13 15

出力例 3Copy

Copy
128

Score : 550550 points

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 1Copy

Copy
4
3 2 5 2

Sample Output 1Copy

Copy
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)=12×3+22×2+12×5+22×2=24f(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 2Copy

Copy
3
4 3 2

Sample Output 2Copy

Copy
15

Sample Input 3Copy

Copy
7
10 5 10 2 10 13 15

Sample Output 3Copy

Copy
128


2025-04-24 (Thu)
17:41:24 +00:00