A - Leftrightarrow

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

<, =, > のみからなる文字列 S が与えられます。
S双方向矢印型 の文字列であるか判定してください。
ただし、文字列 S が双方向矢印型の文字列であるとは、 ある正整数 k が存在して、
S1 個の <k 個の =1 個の > をこの順に連結した長さ (k+2) の文字列であることをいいます。

制約

  • S<, =, > のみからなる長さ 3 以上 100 以下の文字列

入力

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

S

出力

S双方向矢印型 の文字列ならば Yes を、そうでないならば No を出力せよ。


入力例 1

<====>

出力例 1

Yes

<====> は、1 個の <4 個の =1 個の > をこの順に連結した文字列であり、双方向矢印型の文字列です。
よって、Yes を出力します。


入力例 2

==>

出力例 2

No

==> は双方向矢印型の文字列の条件をみたしていません。 よって、No を出力します。


入力例 3

<>>

出力例 3

No

Scoring: 100 points

Problem Statement

You are given a string S consisting of <, =, and >.
Determine whether S is a bidirectional arrow string.
A string S is a bidirectional arrow string if and only if there is a positive integer k such that S is a concatenation of one <, k =s, and one >, in this order, with a length of (k+2).

Constraints

  • S is a string of length between 3 and 100, inclusive, consisting of <, =, and >.

Input

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

S

Output

If S is a bidirectional arrow string, print Yes; otherwise, print No.


Sample Input 1

<====>

Sample Output 1

Yes

<====> is a concatenation of one <, four =s, and one >, in this order, so it is a bidirectional arrow string.
Hence, print Yes.


Sample Input 2

==>

Sample Output 2

No

==> does not meet the condition for a bidirectional arrow string.
Hence, print No.


Sample Input 3

<>>

Sample Output 3

No
B - ?UPC

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

文字列 S が与えられます。ここで、S1 文字目は英大文字、2 文字目以降は英小文字です。

S1 文字目と UPC をこの順に結合した文字列を出力してください。

制約

  • S は長さ 1 以上 100 以下の文字列
  • S1 文字目は英大文字
  • S2 文字目以降は英小文字

入力

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

S

出力

S1 文字目と UPC をこの順に結合した文字列を出力せよ。


入力例 1

Kyoto

出力例 1

KUPC

Kyoto1 文字目は K であるため、KUPC を結合した KUPC を出力します。


入力例 2

Tohoku

出力例 2

TUPC

Score : 100 points

Problem Statement

You are given a string S. Here, the first character of S is an uppercase English letter, and the second and subsequent characters are lowercase English letters.

Print the string formed by concatenating the first character of S and UPC in this order.

Constraints

  • S is a string of length between 1 and 100, inclusive.
  • The first character of S is an uppercase English letter.
  • The second and subsequent characters of S are lowercase English letters.

Input

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

S

Output

Print the string formed by concatenating the first character of S and UPC in this order.


Sample Input 1

Kyoto

Sample Output 1

KUPC

The first character of Kyoto is K, so concatenate K and UPC, and print KUPC.


Sample Input 2

Tohoku

Sample Output 2

TUPC
C - Default Price

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋くんは回転寿司で N 皿の料理を食べました。 i 皿目の色は文字列 C_i で表されます。

また、料理の価格は皿の色と対応し、 i=1,\ldots,M のそれぞれについて、色が文字列 D_i の皿の料理は一皿 P_i 円です。また、D_1,\ldots,D_M のいずれとも異なる色の皿の料理は一皿 P_0 円です。

高橋くんが食べた料理の価格の合計を求めてください。

制約

  • 1\leq N,M\leq 100
  • C_i,D_i は英小文字からなる長さ 1 以上 20 以下の文字列
  • D_1,\ldots,D_M はすべて異なる
  • 1\leq P_i\leq 10000
  • N,M,P_i は整数

入力

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

N M
C_1 \ldots C_N
D_1 \ldots D_M
P_0 P_1 \ldots P_M

出力

答えを整数として出力せよ。


入力例 1

3 2
red green blue
blue red
800 1600 2800

出力例 1

5200

blue の皿は P_1 = 1600 円、red の皿は P_2 = 2800 円、green の皿は P_0 = 800 円です。

高橋くんが食べた料理の価格の合計は、2800+800+1600=5200 円です。


入力例 2

3 2
code queen atcoder
king queen
10 1 1

出力例 2

21

Score : 200 points

Problem Statement

Takahashi ate N plates of sushi at a sushi restaurant. The color of the i-th plate is represented by a string C_i.

The price of a sushi corresponds to the color of the plate. For each i=1,\ldots,M, the sushi on a plate whose color is represented by a string D_i is worth P_i yen a plate (yen is the currency of Japan). If the color does not coincide with any of D_1,\ldots, and D_M, it is worth P_0 yen a plate.

Find the total amount of the prices of sushi that Takahashi ate.

Constraints

  • 1\leq N,M\leq 100
  • C_i and D_i are strings of length between 1 and 20, inclusive, consisting of lowercase English letters.
  • D_1,\ldots, and D_M are distinct.
  • 1\leq P_i\leq 10000
  • N, M, and P_i are integers.

Input

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

N M
C_1 \ldots C_N
D_1 \ldots D_M
P_0 P_1 \ldots P_M

Output

Print the answer as an integer.


Sample Input 1

3 2
red green blue
blue red
800 1600 2800

Sample Output 1

5200

A blue plate, red plate, and green plate are worth P_1 = 1600, P_2 = 2800, and P_0 = 800 yen, respectively.

The total amount of the prices of the sushi that he ate is 2800+800+1600=5200 yen.


Sample Input 2

3 2
code queen atcoder
king queen
10 1 1

Sample Output 2

21
D - 9x9 Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150

問題文

九九の表に現れる 81 個の整数のうち、X でないものの総和を求めてください。

9 マス、横 9 マスのグリッドがあります。
グリッドの各マスには整数が書きこまれていて、グリッドの上から i 行目、左から j 列目のマスには i \times j が書きこまれています。
整数 X が与えられます。グリッドに書きこまれた 81 個の整数のうち X でないものの総和を求めてください。値の等しい整数が異なるマスに書きこまれている場合は別々に加算する点に注意してください。

制約

  • X1 以上 81 以下の整数

入力

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

X

出力

グリッドに書きこまれた 81 個の整数のうち X でないものの総和を出力せよ。


入力例 1

1

出力例 1

2024

グリッドに 1 が書きこまれたマスは上から 1 行目、左から 1 列目のマスのみです。1 でない整数を全て足し合わせると総和は 2024 になります。


入力例 2

11

出力例 2

2025

グリッドに 11 が書きこまれたマスは存在しません。よって 81 個の整数全てを足し合わせた総和である 2025 が答えとなります。


入力例 3

24

出力例 3

1929

Score : 150 points

Problem Statement

Among the 81 integers that appear in the 9-by-9 multiplication table, find the sum of those that are not X.

There is a grid of size 9 by 9.
Each cell of the grid contains an integer: the cell at the i-th row from the top and the j-th column from the left contains i \times j.
You are given an integer X. Among the 81 integers written in this grid, find the sum of those that are not X. If the same value appears in multiple cells, add it for each cell.

Constraints

  • X is an integer between 1 and 81, inclusive.

Input

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

X

Output

Print the sum of the integers that are not X among the 81 integers written in the grid.


Sample Input 1

1

Sample Output 1

2024

The only cell with 1 in the grid is the cell at the 1st row from the top and 1st column from the left. Summing all integers that are not 1 yields 2024.


Sample Input 2

11

Sample Output 2

2025

There is no cell containing 11 in the grid. Thus, the answer is 2025, the sum of all 81 integers.


Sample Input 3

24

Sample Output 3

1929
E - Dash

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

二次元平面の点 (0,0) に高橋君がいます。初め、高橋君の体力は H です。また、二次元平面には M 個の体力を回復するアイテムがあり、i 個目のアイテムは点 (x_i,y_i) に置いてあります。

高橋君は、これから N 回の移動をします。i 回目の移動は以下の方法で行われます。

  1. 今高橋君がいる点を (x,y) とする。体力を 1 消費し、Si 番目の文字 S_i に応じて以下の点に移動する。

    • S_iR のとき: (x+1,y)
    • S_iL のとき: (x-1,y)
    • S_iU のとき: (x,y+1)
    • S_iD のとき: (x,y-1)
  2. 高橋君の体力が負になった場合、高橋君は倒れてしまい、移動をやめる。そうでない場合、移動した点にアイテムがあり、かつ高橋君の体力が K 未満ならば、移動した点に置かれたアイテムを消費し、高橋君の体力が K になる。

高橋君が一度も倒れることなく N 回の移動を行えるか判定してください。

制約

  • 1\leq N,M,H,K\leq 2\times 10^5
  • SR, L, U, D からなる長さ N の文字列
  • |x_i|,|y_i| \leq 2\times 10^5
  • (x_i,y_i) は互いに異なる
  • S 以外の入力は全て整数

入力

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

N M H K
S
x_1 y_1
\vdots
x_M y_M

出力

高橋君が一度も倒れることなく N 回の移動を行える場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

4 2 3 1
RUDL
-1 -1
1 0

出力例 1

Yes

初め高橋君の体力は 3 です。以下で移動を説明します。

  • 1 回目の移動: S_iR なので点 (1,0) に移動する。高橋君の体力は 2 に減る。点 (1,0) にはアイテムが置いてあるが、高橋君の体力は K=1 以上なのでアイテムは消費されない。

  • 2 回目の移動: S_iU なので点 (1,1) に移動する。高橋君の体力は 1 に減る。

  • 3 回目の移動: S_iD なので点 (1,0) に移動する。高橋君の体力は 0 に減る。点 (1,0) にはアイテムが置いてあり、体力は K=1 未満なのでアイテムを消費し、体力が 1 になる。

  • 4 回目の移動: S_iL なので点 (0,0) に移動する。高橋君の体力は 0 に減る。

以上より、高橋君は倒れずに 4 回の移動を行えるので、Yes を出力してください。体力は 0 になってもいいことに注意してください。


入力例 2

5 2 1 5
LDRLD
0 0
-1 -1

出力例 2

No

初め高橋君の体力は 1 です。以下で移動を説明します。

  • 1 回目の移動: S_iL なので点 (-1,0) に移動する。高橋君の体力は 0 に減る。

  • 2 回目の移動: S_iD なので点 (-1,-1) に移動する。高橋君の体力は -1 に減る。体力が -1 になってしまったので、高橋君は倒れてしまい、移動をやめる。

以上より、高橋君は倒れてしまうので、No を出力してください。

高橋君がはじめいる点 (0,0) にはアイテムが置いてありますが、移動後にアイテムは消費されるので、1 回目の移動前にアイテムを消費しないことに注意してください。

Score : 300 points

Problem Statement

On a two-dimensional plane, Takahashi is initially at point (0, 0), and his initial health is H. M items to recover health are placed on the plane; the i-th of them is placed at (x_i,y_i).

Takahashi will make N moves. The i-th move is as follows.

  1. Let (x,y) be his current coordinates. He consumes a health of 1 to move to the following point, depending on S_i, the i-th character of S:

    • (x+1,y) if S_i is R;
    • (x-1,y) if S_i is L;
    • (x,y+1) if S_i is U;
    • (x,y-1) if S_i is D.
  2. If Takahashi's health has become negative, he collapses and stops moving. Otherwise, if an item is placed at the point he has moved to, and his health is strictly less than K, then he consumes the item there to make his health K.

Determine if Takahashi can complete the N moves without being stunned.

Constraints

  • 1\leq N,M,H,K\leq 2\times 10^5
  • S is a string of length N consisting of R, L, U, and D.
  • |x_i|,|y_i| \leq 2\times 10^5
  • (x_i, y_i) are pairwise distinct.
  • All values in the input are integers, except for S.

Input

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

N M H K
S
x_1 y_1
\vdots
x_M y_M

Output

Print Yes if he can complete the N moves without being stunned; print No otherwise.


Sample Input 1

4 2 3 1
RUDL
-1 -1
1 0

Sample Output 1

Yes

Initially, Takahashi's health is 3. We describe the moves below.

  • 1-st move: S_i is R, so he moves to point (1,0). His health reduces to 2. Although an item is placed at point (1,0), he do not consume it because his health is no less than K=1.

  • 2-nd move: S_i is U, so he moves to point (1,1). His health reduces to 1.

  • 3-rd move: S_i is D, so he moves to point (1,0). His health reduces to 0. An item is placed at point (1,0), and his health is less than K=1, so he consumes the item to make his health 1.

  • 4-th move: S_i is L, so he moves to point (0,0). His health reduces to 0.

Thus, he can make the 4 moves without collapsing, so Yes should be printed. Note that the health may reach 0.


Sample Input 2

5 2 1 5
LDRLD
0 0
-1 -1

Sample Output 2

No

Initially, Takahashi's health is 1. We describe the moves below.

  • 1-st move: S_i is L, so he moves to point (-1,0). His health reduces to 0.

  • 2-nd move: S_i is D, so he moves to point (-1,-1). His health reduces to -1. Now that the health is -1, he collapses and stops moving.

Thus, he will be stunned, so No should be printed.

Note that although there is an item at his initial point (0,0), he does not consume it before the 1-st move, because items are only consumed after a move.

F - Not All Covered

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

AtCoder 王国には 1 から N まで番号がつけられた N 個の城壁があります。また、 M 個の砲台があります。

砲台 iL_i 以上 R_i 以下の番号の城壁を守っています。

ある砲台を破壊すると、その砲台が守っていた城壁はその砲台により守られなくなります。

どの砲台にも守られていない城壁が存在する状態にするためには、最小で何個の砲台を破壊する必要がありますか?

制約

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
  • 入力はすべて整数

入力

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

N M
L_1 R_1
L_2 R_2
 \vdots 
L_M R_M

出力

砲台に守られていない城壁が存在する状態にするために破壊する必要がある砲台の個数の最小値を出力せよ。


入力例 1

10 4
1 6
4 5
5 10
7 10

出力例 1

1

砲台 1 を破壊するとどの砲台も城壁 3 を守っていない状態になります。 また、砲台を 1 個も破壊しなければ全ての城壁がどれかの砲台に守られている状態になります。 そのため、1 を出力します。


入力例 2

5 2
1 2
3 4

出力例 2

0

どの砲台も城壁 5 を守っていないため、砲台を 1 個も破壊しないでも砲台に守られていない城壁が存在する状態になります。そのため、0 を出力します。


入力例 3

5 10
2 5
1 5
1 2
2 4
2 2
5 5
2 4
1 2
2 2
2 3

出力例 3

3

Score : 300 points

Problem Statement

In the AtCoder Kingdom, there are N castle walls numbered from 1 through N. There are also M turrets.

Turret i guards castle walls numbered from L_i through R_i.

When a turret is destroyed, the castle walls that were guarded by that turret are no longer guarded by that turret.

What is the minimum number of turrets that need to be destroyed so that at least one castle wall is not guarded by any turret?

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
  • All input values are integers.

Input

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

N M
L_1 R_1
L_2 R_2
 \vdots 
L_M R_M

Output

Output the minimum number of turrets that need to be destroyed so that at least one castle wall is not guarded by any turret.


Sample Input 1

10 4
1 6
4 5
5 10
7 10

Sample Output 1

1

If turret 1 is destroyed, no turret guards castle wall 3. Also, if no turrets are destroyed, all castle walls are guarded by some turret. Therefore, output 1.


Sample Input 2

5 2
1 2
3 4

Sample Output 2

0

Since no turret guards castle wall 5, there already exists a castle wall not guarded by any turret without destroying any turrets. Therefore, output 0.


Sample Input 3

5 10
2 5
1 5
1 2
2 4
2 2
5 5
2 4
1 2
2 2
2 3

Sample Output 3

3
G - Gomamayo Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

0, 1 からなる長さ N の文字列 S が与えられます。

0, 1 からなる長さ N の文字列 T は以下の条件を満たすとき、またそのときに限り 良い文字列 であると定義します。

  • 1 \leq i \leq N - 1 を満たす整数 i であって、Ti 文字目と i + 1 文字目が一致するようなものがちょうど 1 つ存在する。

i = 1,2,\ldots, N について以下の操作を 1 度行うか行わないか選ぶことができます。

  • Si 文字目が 0 であるとき Si 文字目を 1 に、そうでないとき Si 文字目を 0 に置き換える。操作を行った場合、C_i のコストがかかる。

S を良い文字列にするために必要なコストの総和の最小値を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • S は長さ N0,1 からなる文字列
  • 1 \leq C_i \leq 10^9
  • N, C_i は整数

入力

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

N
S
C_1 C_2 \ldots C_N

出力

答えを出力せよ。


入力例 1

5
00011
3 9 2 6 4

出力例 1

7

i = 1, 5 に対して操作を行い、i = 2, 3, 4 に対して操作を行わないことで S = 10010 となり、S は良い文字列となります。このときかかるコストは 7 であり、コスト 7 未満で S を良い文字列にすることはできないため、7 を出力します。


入力例 2

4
1001
1 2 3 4

出力例 2

0

入力例 3

11
11111100111
512298012 821282085 543342199 868532399 690830957 973970164 928915367 954764623 923012648 540375785 925723427

出力例 3

2286846953

Score: 400 points

Problem Statement

You are given a string S of length N consisting of 0 and 1.

A string T of length N consisting of 0 and 1 is a good string if and only if it satisfies the following condition:

  • There is exactly one integer i such that 1 \leq i \leq N - 1 and the i-th and (i + 1)-th characters of T are the same.

For each i = 1,2,\ldots, N, you can choose whether or not to perform the following operation once:

  • If the i-th character of S is 0, replace it with 1, and vice versa. The cost of this operation, if performed, is C_i.

Find the minimum total cost required to make S a good string.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • S is a string of length N consisting of 0 and 1.
  • 1 \leq C_i \leq 10^9
  • N and C_i are integers.

Input

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

N
S
C_1 C_2 \ldots C_N

Output

Print the answer.


Sample Input 1

5
00011
3 9 2 6 4

Sample Output 1

7

Performing the operation for i = 1, 5 and not performing it for i = 2, 3, 4 makes S = 10010, which is a good string. The cost incurred in this case is 7, and it is impossible to make S a good string for less than 7, so print 7.


Sample Input 2

4
1001
1 2 3 4

Sample Output 2

0

Sample Input 3

11
11111100111
512298012 821282085 543342199 868532399 690830957 973970164 928915367 954764623 923012648 540375785 925723427

Sample Output 3

2286846953
H - Sugoroku 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

マス 1 からマス NN 個のマスがあります。はじめ、あなたはマス 1 にいます。

また、マス 1 からマス N-1 にはそれぞれサイコロが置いてあります。マス i のサイコロは 0 以上 A_i 以下の整数を等確率にランダムで出します。(サイコロを振る操作は毎回独立です。)

あなたは、マス N に到達するまで、現在いるマスに置かれているサイコロを振り、出た目の数だけ進むことを繰り返します。厳密に言うと、マス X にいるときにサイコロで Y が出た場合はマス X+Y に移動します。

サイコロを振る回数の期待値 \bmod\ 998244353 を求めてください。

注記

求める期待値は必ず有理数となることが証明できます。またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N-i(1 \le i \le N-1)
  • 入力は全て整数。

入力

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

N
A_1 A_2 \dots A_{N-1}

出力

答えを出力せよ。


入力例 1

3
1 1

出力例 1

4

求める期待値は 4 であるため、4 を出力します。

マス N に到達するまでの流れとしては、以下のようなものが考えられます。

  • マス 11 を出し、マス 2 に移動する。
  • マス 20 を出し、移動しない。
  • マス 21 を出し、マス 3 に移動する。

このようになる確率は \frac{1}{8} です。


入力例 2

5
3 1 2 1

出力例 2

332748122

Score : 500 points

Problem Statement

There are N squares called Square 1 though Square N. You start on Square 1.

Each of the squares from Square 1 through Square N-1 has a die on it. The die on Square i is labeled with the integers from 0 through A_i, each occurring with equal probability. (Die rolls are independent of each other.)

Until you reach Square N, you will repeat rolling a die on the square you are on. Here, if the die on Square x rolls the integer y, you go to Square x+y.

Find the expected value, modulo 998244353, of the number of times you roll a die.

Notes

It can be proved that the sought expected value is always a rational number. Additionally, if that value is represented \frac{P}{Q} using two coprime integers P and Q, there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Find this R.

Constraints

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N-i(1 \le i \le N-1)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_{N-1}

Output

Print the answer.


Sample Input 1

3
1 1

Sample Output 1

4

The sought expected value is 4, so 4 should be printed.

Here is one possible scenario until reaching Square N:

  • Roll 1 on Square 1, and go to Square 2.
  • Roll 0 on Square 2, and stay there.
  • Roll 1 on Square 2, and go to Square 3.

This scenario occurs with probability \frac{1}{8}.


Sample Input 2

5
3 1 2 1

Sample Output 2

332748122
I - Tree Degree Optimization

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

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

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

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

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

制約

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

入力

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

N 
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4
3 2 5 2

出力例 1

24

頂点 1 と頂点 2 を結ぶ辺、頂点 2 と頂点 4 を結ぶ辺、頂点 4 と頂点 3 を結ぶ辺からなるような木 T を考えます。

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


入力例 2

3
4 3 2

出力例 2

15

入力例 3

7
10 5 10 2 10 13 15

出力例 3

128

Score : 550 points

Problem Statement

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

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

Find the minimum possible value of f(T).

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

Constraints

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

Input

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

N 
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

4
3 2 5 2

Sample Output 1

24

Consider a tree T with an edge connecting vertices 1 and 2, an edge connecting vertices 2 and 4, and an edge connecting vertices 4 and 3.

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).


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