実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 150 点
問題文
AtCoder社のオフィスには加湿器が 1 つあります。現在は時刻 0 であり、加湿器には水が入っていません。
あなたはこの加湿器にこれから N 回水を追加します。i 回目 (1\leq i \leq N)の水の追加は時刻 T_i に行い、水を V_i リットル追加します。なお、 T_i < T_{i+1} (1 \leq i \leq N-1) を満たすことが保証されます。
ただし、加湿器には穴が空いていて、加湿器に水が入っている間は 1 単位時間につき水が 1 リットル減り続けます。
時刻 T_N に水を追加し終えたとき、加湿器に残っている水の量を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq T_i \leq 100 (1 \leq i \leq N)
- 1 \leq V_i \leq 100 (1 \leq i \leq N)
- T_i < T_{i+1} (1 \leq i \leq N-1)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N T_1 V_1 T_2 V_2 \vdots T_N V_N
出力
答えを出力せよ。
入力例 1
4 1 3 3 1 4 4 7 1
出力例 1
3
時系列で以下のように水が追加されます。
- 時刻 1 : 水を追加する前は 0 リットル入っており、3 リットル追加して加湿器には 3 リットルの水が入っています。
- 時刻 3 : 水を追加する前は 1 リットル入っており、1 リットル追加して加湿器には 2 リットルの水が入っています。
- 時刻 4 : 水を追加する前は 1 リットル入っており、4 リットル追加して加湿器には 5 リットルの水が入っています。
- 時刻 7 : 水を追加する前は 2 リットル入っており、1 リットル追加して加湿器には 3 リットルの水が入っています。
時刻 7 に水を追加し終えたとき、加湿器に残っている水の量は 3 リットルです。よって答えは 3 です。
入力例 2
3 1 8 10 11 21 5
出力例 2
5
入力例 3
10 2 1 22 10 26 17 29 2 45 20 47 32 72 12 75 1 81 31 97 7
出力例 3
57
Score : 150 points
Problem Statement
There is one humidifier in the AtCoder company office. The current time is 0, and the humidifier has no water inside.
You will add water to this humidifier N times. The i-th addition of water (1 \leq i \leq N) takes place at time T_i, and you add V_i liters of water. It is guaranteed that T_i < T_{i+1} for all 1 \leq i \leq N-1.
However, the humidifier has a leak, and as long as there is water inside, the amount of water decreases by 1 liter per unit time.
Find the amount of water remaining in the humidifier immediately after you finish adding water at time T_N.
Constraints
- 1 \leq N \leq 100
- 1 \leq T_i \leq 100 (1 \leq i \leq N)
- 1 \leq V_i \leq 100 (1 \leq i \leq N)
- T_i < T_{i+1} (1 \leq i \leq N-1)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N T_1 V_1 T_2 V_2 \vdots T_N V_N
Output
Print the answer.
Sample Input 1
4 1 3 3 1 4 4 7 1
Sample Output 1
3
At each point in time, water is added as follows:
- Time 1: Before adding, the humidifier has 0 liters. After adding 3 liters, it has 3 liters.
- Time 3: Before adding, it has 1 liter. After adding 1 liter, it has 2 liters total.
- Time 4: Before adding, it has 1 liter. After adding 4 liters, it has 5 liters total.
- Time 7: Before adding, it has 2 liters. After adding 1 liter, it has 3 liters total.
After finishing the addition at time 7, the humidifier contains 3 liters. Thus, the answer is 3.
Sample Input 2
3 1 8 10 11 21 5
Sample Output 2
5
Sample Input 3
10 2 1 22 10 26 17 29 2 45 20 47 32 72 12 75 1 81 31 97 7
Sample Output 3
57
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
N 個の商品があります。高橋君と青木君がどの商品を欲しがっているかを表す長さ N の文字列 T,A が与えられます。T,A の i\ (1\leq i\leq N) 文字目をそれぞれ T_i,A_i とします。
高橋君は T_i が o のとき i 番目の商品を欲しがっており、T_i が x のとき i 番目の商品を欲しがっていません。
同様に、青木君は A_i が o のとき i 番目の商品を欲しがっており、A_i が x のとき i 番目の商品を欲しがっていません。
2 人ともが欲しがっている商品が存在するか判定してください。
制約
- 1\leq N\leq 100
- N は整数
- T,A は
oおよびxからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N T A
出力
2 人とも欲しがっている商品が存在するならば Yes を、存在しないならば No を出力せよ。
入力例 1
4 oxoo xoox
出力例 1
Yes
3 つ目の商品は 2 人ともが欲しがっているため、Yes を出力します。
入力例 2
5 xxxxx ooooo
出力例 2
No
2 人とも欲しがっている商品は存在しないため、No を出力します。
入力例 3
10 xoooxoxxxo ooxooooxoo
出力例 3
Yes
Score : 100 points
Problem Statement
There are N items. You are given strings T and A of length N that represent which items Takahashi and Aoki want, respectively. Let T_i and A_i be the i-th (1\leq i\leq N) characters of T and A, respectively.
Takahashi wants the i-th item when T_i is o, and does not want the i-th item when T_i is x.
Similarly, Aoki wants the i-th item when A_i is o, and does not want the i-th item when A_i is x.
Determine whether there exists an item that both of them want.
Constraints
- 1\leq N\leq 100
- N is an integer.
- T and A are strings of length N consisting of
oandx.
Input
The input is given from Standard Input in the following format:
N T A
Output
If there exists an item that both of them want, output Yes; otherwise, output No.
Sample Input 1
4 oxoo xoox
Sample Output 1
Yes
The third item is wanted by both of them, so output Yes.
Sample Input 2
5 xxxxx ooooo
Sample Output 2
No
There is no item that both of them want, so output No.
Sample Input 3
10 xoooxoxxxo ooxooooxoo
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
H 行 W 列のブロックからなる長方形状のチョコレートがあります。
Q 個のクエリが与えられるので、順に処理したときの各クエリの答えを求めてください。各クエリは、以下のいずれかの形式です。
-
タイプ 1 : 整数 R が与えられる。下 R 行のチョコレートのブロックの個数を求め、それらを食べる。
-
タイプ 2 : 整数 C が与えられる。右 C 列のチョコレートのブロックの個数を求め、それらを食べる。
なお、クエリを順に処理したとき、各クエリを処理した後もチョコレートは長方形状であり、タイプ 1 のクエリを処理する直前の時点でチョコレートは R + 1 行以上存在し、タイプ 2 のクエリを処理する直前の時点でチョコレートは C + 1 列以上存在します。
制約
- 2 \leq H, W \leq 100
- 1 \leq Q \leq 100
- タイプ 1 のクエリについて、1 \leq R
- クエリを順に処理したとき、タイプ 1 のクエリを処理する直前にチョコレートは R + 1 行以上存在する
- タイプ 2 のクエリについて、1 \leq C
- クエリを順に処理したとき、タイプ 2 のクエリを処理する直前にチョコレートは C + 1 列以上存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
ただし、\text{query}_i は i 番目のクエリであり、以下のいずれかの形式で与えられる。
1 R
2 C
出力
Q 行出力せよ。 i (1 \leq i \leq Q) 行目には i 番目のクエリに対する答えを出力せよ。
入力例 1
7 9 5 2 4 1 3 2 1 2 1 1 3
出力例 1
28 15 4 4 9
はじめ、チョコレートは 7 行 9 列の長方形状です。
1 番目のクエリでは、右 4 列のチョコレートの個数は 28 個であるため 28 を出力します。チョコレートは 7 行 5 列となります。
2 番目のクエリでは、下 3 行のチョコレートの個数は 15 個であるため 15 を出力します。チョコレートは 4 行 5 列となります。
3 番目のクエリでは、右 1 列のチョコレートの個数は 4 個であるため 4 を出力します。チョコレートは 4 行 4 列となります。
4 番目のクエリでは、右 1 列のチョコレートの個数は 4 個であるため 4 を出力します。チョコレートは 4 行 3 列となります。
5 番目のクエリでは、下 3 行のチョコレートの個数は 9 個であるため 9 を出力します。チョコレートは 1 行 3 列となります。
Score : 200 points
Problem Statement
There is a rectangular chocolate consisting of H rows and W columns of blocks.
You are given Q queries; process them in order and find the answer to each query. Each query is in one of the following formats:
-
Type 1: An integer R is given. Find the number of chocolate blocks in the bottom R rows, then eat them.
-
Type 2: An integer C is given. Find the number of chocolate blocks in the rightmost C columns, then eat them.
When the queries are processed in order, the chocolate remains rectangular after each query is processed, and it has at least R + 1 rows immediately before processing a type 1 query and has at least C + 1 columns immediately before processing a type 2 query.
Constraints
- 2 \leq H, W \leq 100
- 1 \leq Q \leq 100
- For type 1 queries, 1 \leq R.
- When the queries are processed in order, the chocolate has at least R + 1 rows immediately before processing a type 1 query.
- For type 2 queries, 1 \leq C.
- When the queries are processed in order, the chocolate has at least C + 1 columns immediately before processing a type 2 query.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Here, \text{query}_i is the i-th query, given in one of the following formats:
1 R
2 C
Output
Output Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th query.
Sample Input 1
7 9 5 2 4 1 3 2 1 2 1 1 3
Sample Output 1
28 15 4 4 9
Initially, the chocolate is a rectangle with 7 rows and 9 columns.
For the first query, the number of chocolate blocks in the rightmost 4 columns is 28, so output 28. The chocolate becomes 7 rows and 5 columns.
For the second query, the number of chocolate blocks in the bottom 3 rows is 15, so output 15. The chocolate becomes 4 rows and 5 columns.
For the third query, the number of chocolate blocks in the rightmost 1 column is 4, so output 4. The chocolate becomes 4 rows and 4 columns.
For the fourth query, the number of chocolate blocks in the rightmost 1 column is 4, so output 4. The chocolate becomes 4 rows and 3 columns.
For the fifth query, the number of chocolate blocks in the bottom 3 rows is 9, so output 9. The chocolate becomes 1 row and 3 columns.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
空の数列 A があります。クエリが Q 個与えられるので、与えられた順に処理してください。
クエリは次の 2 種類のいずれかです。
1 x: A の末尾に x を追加する。2 k: A の後ろから k 番目の値を求める。このクエリが与えられるとき、A の長さは k 以上であることが保証される。
制約
- 1 \leq Q \leq 100
- 1 種類目のクエリにおいて x は 1 \leq x \leq 10^9 を満たす整数
- 2 種類目のクエリにおいて k はその時点の数列 A の長さ以下の正の整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
クエリは以下の 2 つのいずれかの形式である。
1 x
2 k
出力
2 種類目のクエリの個数を q として q 行出力せよ。
i 行目にはそのような i 回目のクエリに対する答えを出力せよ。
入力例 1
5 1 20 1 30 2 1 1 40 2 3
出力例 1
30 20
- 最初 A は空である。
- 1 番目のクエリにより A の末尾に 20 が追加され A=(20) となる。
- 2 番目のクエリにより A の末尾に 30 が追加され A=(20,30) となる。
- 3 番目のクエリの答えは A=(20,30) の後ろから 1 番目の値の 30 である。
- 4 番目のクエリにより A の末尾に 40 が追加され A=(20,30,40) となる。
- 5 番目のクエリの答えは A=(20,30,40) の後ろから 3 番目の値の 20 である。
Score: 200 points
Problem Statement
You have an empty sequence A. There are Q queries given, and you need to process them in the order they are given.
The queries are of the following two types:
1 x: Append x to the end of A.2 k: Find the k-th value from the end of A. It is guaranteed that the length of A is at least k when this query is given.
Constraints
- 1 \leq Q \leq 100
- In the first type of query, x is an integer satisfying 1 \leq x \leq 10^9.
- In the second type of query, k is a positive integer not greater than the current length of sequence A.
Input
The input is given from Standard Input in the following format:
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is in one of the following two formats:
1 x
2 k
Output
Print q lines, where q is the number of queries of the second type.
The i-th line should contain the answer to the i-th such query.
Sample Input 1
5 1 20 1 30 2 1 1 40 2 3
Sample Output 1
30 20
- Initially, A is empty.
- The first query appends 20 to the end of A, making A=(20).
- The second query appends 30 to the end of A, making A=(20,30).
- The answer to the third query is 30, which is the 1-st value from the end of A=(20,30).
- The fourth query appends 40 to the end of A, making A=(20,30,40).
- The answer to the fifth query is 20, which is the 3-rd value from the end of A=(20,30,40).
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋くんは、頭パーツを 1 個と体パーツを 1 個組み合わせてロボットを 1 体作ることができます。 ロボットは頭パーツの重さが体パーツの重さより大きいと倒れてしまいます。
現在、高橋くんは頭パーツを N 個と体パーツを M 個持っています。 高橋くんが持っている i 番目 (1\le i\le N) の頭パーツの重さは H _ i グラム、i 番目 (1\le i\le M) の体パーツの重さは B _ i グラムです。
高橋くんは、持っているパーツを適切に組み合わせることで、倒れないロボットを合計 K 体作りたいです。 うまくパーツを組み合わせることで高橋くんが目標を達成することができるか判定してください。
ただし、1 個のパーツを複数のロボットを作るために利用したり、1 体のロボットを作るために頭パーツを 2 個以上(もしくは体パーツを 2 個以上)利用することはできません。
制約
- 1\le N\le2\times10 ^ 5
- 1\le M\le2\times10 ^ 5
- 1\le K\le\min\lbrace N,M\rbrace
- 1\le H _ i\le10 ^ 9\ (1\le i\le N)
- 1\le B _ i\le10 ^ 9\ (1\le i\le M)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M K H _ 1 H _ 2 \ldots H _ N B _ 1 B _ 2 \ldots B _ M
出力
高橋くんがパーツをうまく組み合わせて倒れないロボットを K 体作ることができるなら Yes を、そうでなければ No を出力せよ。
入力例 1
6 6 3 2 7 1 8 2 8 1 8 2 8 4 5
出力例 1
Yes
i 番目の頭パーツと j 番目の体パーツを組み合わせることを (i,j) と書くことにすると、高橋くんは例えば (1,2),(2,4),(3,6) のように組み合わせることで 3 体の倒れないロボットを作ることができます。
よって、Yes を出力してください。
入力例 2
1 1 1 43 1
出力例 2
No
高橋くんが持っている頭パーツが重すぎるため、高橋くんは倒れないロボットを作ることができません。
入力例 3
1 1 1 100 100
出力例 3
Yes
頭の重さと体の重さが等しい場合、ロボットは倒れないことに注意してください。
入力例 4
12 15 12 748 169 586 329 972 529 432 519 408 587 138 249 656 114 632 299 984 755 404 772 155 506 832 854 353 465 387
出力例 4
Yes
Score : 300 points
Problem Statement
Takahashi can combine a head part and a body part to create a robot. A robot falls over if the weight of the head part is greater than the weight of the body part.
Currently, he has N head parts and M body parts. The weight of the i-th (1\le i\le N) head part is H _ i grams, and the weight of the i-th (1\le i\le M) body part is B _ i grams.
He wants to create a total of K robots that do not fall over by appropriately combining the parts he has. Determine whether he can achieve his goal by combining the parts well.
Here, a part cannot be used to create multiple robots, and two or more head parts (or two or more body parts) cannot be used to create one robot.
Constraints
- 1\le N\le2\times10 ^ 5
- 1\le M\le2\times10 ^ 5
- 1\le K\le\min\lbrace N,M\rbrace
- 1\le H _ i\le10 ^ 9\ (1\le i\le N)
- 1\le B _ i\le10 ^ 9\ (1\le i\le M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M K H _ 1 H _ 2 \ldots H _ N B _ 1 B _ 2 \ldots B _ M
Output
Print Yes if Takahashi can combine the parts well to create K robots that do not fall over; otherwise, print No.
Sample Input 1
6 6 3 2 7 1 8 2 8 1 8 2 8 4 5
Sample Output 1
Yes
If we denote combining the i-th head part and the j-th body part as (i,j), then Takahashi can create three robots that do not fall over by combining them as (1,2),(2,4),(3,6), for example.
Thus, print Yes.
Sample Input 2
1 1 1 43 1
Sample Output 2
No
His head part is too heavy, so he cannot create any robot that does not fall over.
Sample Input 3
1 1 1 100 100
Sample Output 3
Yes
Note that a robot does not fall over if the head and body have equal weights.
Sample Input 4
12 15 12 748 169 586 329 972 529 432 519 408 587 138 249 656 114 632 299 984 755 404 772 155 506 832 854 353 465 387
Sample Output 4
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
2 つの文字列 A,B に対して、A の末尾に B を連結した文字列を A+B と表します。
N 個の文字列 S_1,\ldots,S_N が与えられます。i=1,\ldots,N の順に、次の指示に従って加工して出力してください。
- S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が存在しないならば、S_i を出力する。
- S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が X 個 (X>0) 存在するならば、X を文字列として扱って S_i+
(+X+)を出力する。
制約
- 1 \leq N \leq 2\times 10^5
- S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
問題文中の指示にしたがって、N 行出力せよ。
入力例 1
5 newfile newfile newfolder newfile newfolder
出力例 1
newfile newfile(1) newfolder newfile(2) newfolder(1)
入力例 2
11 a a a a a a a a a a a
出力例 2
a a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
Score : 300 points
Problem Statement
For two strings A and B, let A+B denote the concatenation of A and B in this order.
You are given N strings S_1,\ldots,S_N. Modify and print them as follows, in the order i=1, \ldots, N:
- if none of S_1,\ldots,S_{i-1} is equal to S_i, print S_i;
- if X (X>0) of S_1,\ldots,S_{i-1} are equal to S_i, print S_i+
(+X+), treating X as a string.
Constraints
- 1 \leq N \leq 2\times 10^5
- S_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print N lines as specified in the Problem Statement.
Sample Input 1
5 newfile newfile newfolder newfile newfolder
Sample Output 1
newfile newfile(1) newfolder newfile(2) newfolder(1)
Sample Input 2
11 a a a a a a a a a a a
Sample Output 2
a a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
以下の条件を満たす整数 k を「 250 に似た数」と呼びます。
- k が素数 p<q を使って k=p \times q^3 と表される。
N 以下の「 250 に似た数」は全部でいくつありますか?
制約
- N は 1 以上 10^{18} 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを整数として出力せよ。
入力例 1
250
出力例 1
2
- 54 = 2 \times 3^3 なので、「 250 に似た数」です。
- 250 = 2 \times 5^3 なので、「 250 に似た数」です。
250 以下の「 250 に似た数」は、以上の 2 つです。
入力例 2
1
出力例 2
0
入力例 3
123456789012345
出力例 3
226863
Score : 400 points
Problem Statement
Let us regard an integer k as "similar to 250" if the following condition is satisfied:
- k is represented as k=p \times q^3 with primes p<q.
How many integers less than or equal to N are "similar to 250"?
Constraints
- N is an integer between 1 and 10^{18} (inclusive)
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer as an integer.
Sample Input 1
250
Sample Output 1
2
- 54 = 2 \times 3^3 is "similar to 250".
- 250 = 2 \times 5^3 is "similar to 250".
The two integers above are all the integers "similar to 250".
Sample Input 2
1
Sample Output 2
0
Sample Input 3
123456789012345
Sample Output 3
226863
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
長さ N の整数列 (A_1,\dots,A_N) と正整数 D が与えられます。
以下の条件をともに満たす整数の組 (L,R) の個数を求めてください。
- 1 \leq L \leq R \leq N
- (A_L,A_{L+1},\dots,A_R) のどの 2 つの要素も差が D 以上である
- すなわち、 L \leq i < j \leq R を満たす全ての整数の組 (i,j) について、 |A_i-A_j|\geq D である
制約
- 2\leq N \leq 4\times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq D \leq 10^9
入力
入力は以下の形式で標準入力から与えられる。
N D A_1 \dots A_N
出力
答えを出力せよ。
入力例 1
5 3 3 1 4 1 5
出力例 1
8
(1,1),(2,2),(3,3),(4,4),(5,5),(2,3),(3,4),(4,5) の 8 組が条件を満たします。
入力例 2
9 1 1 2 3 4 5 6 7 8 9
出力例 2
45
入力例 3
6 1000000000 123456789 234567891 987654321 321987654 1000000000 1
出力例 3
6
Score : 450 points
Problem Statement
You are given an integer sequence of length N, (A_1,\dots,A_N), and a positive integer D.
Find the number of pairs of integers (L,R) that satisfy both of the following conditions:
- 1 \leq L \leq R \leq N
- Any two elements of (A_L,A_{L+1},\dots,A_R) have a difference of at least D.
- That is, |A_i-A_j|\geq D for all pairs of integers (i,j) satisfying L \leq i < j \leq R.
Constraints
- 2\leq N \leq 4\times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq D \leq 10^9
Input
The input is given from Standard Input in the following format:
N D A_1 \dots A_N
Output
Output the answer.
Sample Input 1
5 3 3 1 4 1 5
Sample Output 1
8
The eight pairs (1,1),(2,2),(3,3),(4,4),(5,5),(2,3),(3,4),(4,5) satisfy the conditions.
Sample Input 2
9 1 1 2 3 4 5 6 7 8 9
Sample Output 2
45
Sample Input 3
6 1000000000 123456789 234567891 987654321 321987654 1000000000 1
Sample Output 3
6
実行時間制限: 2 sec / メモリ制限: 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