A - Christmas Present

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

野球少年の高橋君は今年とても良い子にしていたので、サンタさんはバットかグローブのうち値段が高い方を高橋君にプレゼントすることにしました。

バットの値段が B 円、グローブの値段が G 円 (B\neq G) のとき、サンタさんが高橋君にプレゼントするのはどちらですか?

制約

  • B,G1 以上 1000 以下の相異なる整数

入力

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

B G

出力

サンタさんが高橋君にプレゼントするのがバットであるとき Bat を、グローブであるとき Glove を出力せよ。


入力例 1

300 100

出力例 1

Bat

バットの方がグローブより値段が高いので、サンタさんは高橋君にバットをプレゼントします。


入力例 2

334 343

出力例 2

Glove

グローブの方がバットより値段が高いので、サンタさんは高橋君にグローブをプレゼントします。

Score : 100 points

Problem Statement

Takahashi, a young baseball enthusiast, has been a very good boy this year, so Santa has decided to give him a bat or a glove, whichever is more expensive.

If a bat costs B yen and a glove costs G yen (B\neq G), which one will Santa give to Takahashi?

Constraints

  • B and G are different integers between 1 and 1000, inclusive.

Input

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

B G

Output

If Santa gives Takahashi a bat, print Bat; if Santa gives him a glove, print Glove.


Sample Input 1

300 100

Sample Output 1

Bat

The bat is more expensive than the glove, so Santa will give Takahashi the bat.


Sample Input 2

334 343

Sample Output 2

Glove

The glove is more expensive than the bat, so Santa will give Takahashi the glove.

B - Full Moon

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋くんは満月が好きです。

今日を 1 日目とすると、今日以降で満月を見られる最初の日は M 日目です。以後は P 日ごと、つまり M+P 日目、M+2P 日目、\ldots に満月を見られます。

1 日目から N 日目まで(両端を含む)の中で、高橋くんが満月を見られる日の数を求めてください。

制約

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

入力

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

N M P

出力

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


入力例 1

13 3 5

出力例 1

3

満月を見られる日は、3 日目、8 日目、13 日目、18 日目、\ldots です。

1 日目から 13 日目までの中で高橋くんが満月を見られる日は、3 日目、8 日目、13 日目の 3 個です。


入力例 2

5 6 6

出力例 2

0

高橋くんが満月を見られる日が存在しない場合もあります。


入力例 3

200000 314 318

出力例 3

628

Score : 100 points

Problem Statement

Takahashi likes full moons.

Let today be day 1. The first day on or after today on which he can see a full moon is day M. After that, he can see a full moon every P days, that is, on day M+P, day M+2P, and so on.

Find the number of days between day 1 and day N, inclusive, on which he can see a full moon.

Constraints

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

Input

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

N M P

Output

Print the answer as an integer.


Sample Input 1

13 3 5

Sample Output 1

3

He can see a full moon on day 3, 8, 13, 18, and so on.

From day 1 to 13, he can see a full moon on three days: day 3, 8, and 13.


Sample Input 2

5 6 6

Sample Output 2

0

There may be no days he can see a full moon.


Sample Input 3

200000 314 318

Sample Output 3

628
C - Uppercase and Lowercase

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

英小文字と英大文字からなる文字列 S が与えられます。S の長さは奇数です。
S に含まれる大文字の個数が小文字の個数よりも多ければ、S に含まれる全ての小文字を大文字に変換してください。
そうでない場合は、S に含まれる全ての大文字を小文字に変換してください。

制約

  • S は英小文字および英大文字からなる文字列
  • S の長さは 1 以上 99 以下の奇数

入力

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

S

出力

問題文の指示に従って文字を変換した後の S を出力せよ。


入力例 1

AtCoder

出力例 1

atcoder

AtCoder に含まれる小文字の個数は 5 個、大文字の個数は 2 個です。よって AtCoder に含まれる全ての大文字を小文字に変換した atcoder が答えとなります。


入力例 2

SunTORY

出力例 2

SUNTORY

SunTORY に含まれる小文字の個数は 2 個、大文字の個数は 5 個です。よって SunTORY に含まれる全ての小文字を大文字に変換した SUNTORY が答えとなります。


入力例 3

a

出力例 3

a

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase and uppercase English letters. The length of S is odd.
If the number of uppercase letters in S is greater than the number of lowercase letters, convert all lowercase letters in S to uppercase.
Otherwise, convert all uppercase letters in S to lowercase.

Constraints

  • S is a string consisting of lowercase and uppercase English letters.
  • The length of S is an odd number between 1 and 99, inclusive.

Input

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

S

Output

Print the string S after converting the letters according to the problem statement.


Sample Input 1

AtCoder

Sample Output 1

atcoder

The string AtCoder contains five lowercase letters and two uppercase letters. Thus, convert all uppercase letters in AtCoder to lowercase, which results in atcoder.


Sample Input 2

SunTORY

Sample Output 2

SUNTORY

The string SunTORY contains two lowercase letters and five uppercase letters. Thus, convert all lowercase letters in SunTORY to uppercase, which results in SUNTORY.


Sample Input 3

a

Sample Output 3

a
D - Maintain Multiple Sequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

整数からなる数列が N 個あります。
i \, (1 \leq i \leq N) 番目の数列は L_i 項からなり、i 番目の数列の第 j \, (1 \leq j \leq L_i) 項 は a_{i, j} です。

Q 個のクエリが与えられます。k \, (1 \leq k \leq Q) 番目のクエリでは、整数 s_k, t_k が与えられるので、s_k 番目の数列の第 t_k 項を求めてください。

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • L_i \geq 1 \, (1 \leq i \leq N)
  • \sum_{i=1}^N L_i \leq 2 \times 10^5
  • 1 \leq a_{i, j} \leq 10^9 \, (1 \leq i \leq N, 1 \leq j \leq L_i)
  • 1 \leq s_k \leq N, 1 \leq t_k \leq L_{s_k} \, (1 \leq k \leq Q)
  • 入力は全て整数

入力

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

N Q
L_1 a_{1, 1} \ldots a_{1, L_1}
\vdots
L_N a_{N, 1} \ldots a_{N, L_N}
s_1 t_1
\vdots 
s_Q t_Q

出力

Q 行出力せよ。k \, (1 \leq k \leq Q) 行目には、k 番目のクエリに対する答えを出力せよ。


入力例 1

2 2
3 1 4 7
2 5 9
1 3
2 1

出力例 1

7
5

1 番目の数列は (1, 4, 7)2 番目の数列は (5, 9) です。
それぞれのクエリに対する答えは次のようになります。

  • 1 番目の数列の第 3 項は 7 です。
  • 2 番目の数列の第 1 項は 5 です。

入力例 2

3 4
4 128 741 239 901
2 1 1
3 314 159 26535
1 1
2 2
3 3
1 4

出力例 2

128
1
26535
901

Score : 200 points

Problem Statement

There are N sequences of integers.
The i-th (1 \leq i \leq N) sequence has L_i terms; the j-th (1 \leq j \leq L_i) term of the i-th sequence is a_{i, j}.

You are given Q queries. For the k-th (1 \leq k \leq Q) query, given integers s_k and t_k, find the t_k-th term of the s_k-th sequence.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • L_i \geq 1 \, (1 \leq i \leq N)
  • \sum_{i=1}^N L_i \leq 2 \times 10^5
  • 1 \leq a_{i, j} \leq 10^9 \, (1 \leq i \leq N, 1 \leq j \leq L_i)
  • 1 \leq s_k \leq N, 1 \leq t_k \leq L_{s_k} \, (1 \leq k \leq Q)
  • All values in the input are integers.

Input

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

N Q
L_1 a_{1, 1} \ldots a_{1, L_1}
\vdots
L_N a_{N, 1} \ldots a_{N, L_N}
s_1 t_1
\vdots 
s_Q t_Q

Output

Print Q lines. The k-th (1 \leq k \leq Q) line should contain the answer to the k-th query.


Sample Input 1

2 2
3 1 4 7
2 5 9
1 3
2 1

Sample Output 1

7
5

The 1-st sequence is (1, 4, 7) and the 2-nd is (5, 9).
The answer to each query is as follows:

  • The 3-rd term of the 1-st sequence is 7.
  • The 1-st term of the 2-nd sequence is 5.

Sample Input 2

3 4
4 128 741 239 901
2 1 1
3 314 159 26535
1 1
2 2
3 3
1 4

Sample Output 2

128
1
26535
901
E - Simple path

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 頂点の木 T があり、 i (1\leq i\leq N-1) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。

T 上の相異なる 2 頂点 X,Y が与えられるので、 頂点 X から頂点 Y への単純パス上の頂点(端点含む)を順に列挙してください。

ただし、木上の任意の相異なる 2 頂点 a,b について、a から b への単純パスがただ一つ存在することが証明できます。

単純パスとは? グラフ G 上の頂点 X,Y に対して、頂点列 v_1,v_2, \ldots, v_k であって、 v_1=X, v_k=Y かつ、1\leq i\leq k-1 に対して v_iv_{i+1} が辺で結ばれているようなものを頂点 X から頂点 Y への パス と呼びます。 さらに、v_1,v_2, \ldots, v_k がすべて異なるようなものを頂点 X から頂点 Y への 単純パス と呼びます。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq X,Y\leq N
  • X\neq Y
  • 1\leq U_i,V_i\leq N
  • 入力はすべて整数
  • 与えられるグラフは木

入力

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

N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

出力

頂点 X から頂点 Y への単純パス上の頂点番号を順に空白区切りで出力せよ。


入力例 1

5 2 5
1 2
1 3
3 4
3 5

出力例 1

2 1 3 5

T は以下のような形であり、頂点 2 から頂点 5への単純パスは 頂点 2 \to 頂点 1 \to 頂点 3 \to 頂点 5 となります。
よって、2,1,3,5 をこの順に空白区切りで出力します。


入力例 2

6 1 2
3 1
2 5
1 2
4 1
2 6

出力例 2

1 2

T は以下のような形です。

Score : 300 points

Problem Statement

There is a tree T with N vertices. The i-th edge (1\leq i\leq N-1) connects vertex U_i and vertex V_i.

You are given two different vertices X and Y in T. List all vertices along the simple path from vertex X to vertex Y in order, including endpoints.

It can be proved that, for any two different vertices a and b in a tree, there is a unique simple path from a to b.

What is a simple path? For vertices X and Y in a graph G, a path from vertex X to vertex Y is a sequence of vertices v_1,v_2, \ldots, v_k such that v_1=X, v_k=Y, and v_i and v_{i+1} are connected by an edge for each 1\leq i\leq k-1. Additionally, if all of v_1,v_2, \ldots, v_k are distinct, the path is said to be a simple path from vertex X to vertex Y.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq X,Y\leq N
  • X\neq Y
  • 1\leq U_i,V_i\leq N
  • All values in the input are integers.
  • The given graph is a tree.

Input

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

N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

Output

Print the indices of all vertices along the simple path from vertex X to vertex Y in order, with spaces in between.


Sample Input 1

5 2 5
1 2
1 3
3 4
3 5

Sample Output 1

2 1 3 5

The tree T is shown below. The simple path from vertex 2 to vertex 5 is 2 \to 1 \to 3 \to 5.
Thus, 2,1,3,5 should be printed in this order, with spaces in between.


Sample Input 2

6 1 2
3 1
2 5
1 2
4 1
2 6

Sample Output 2

1 2

The tree T is shown below.

F - Chinese Restaurant

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

回転テーブルの周りに人 0、人 1\ldots、人 N-1 がこの順番で反時計回りに等間隔で並んでいます。また、人 i の目の前には料理 p_i が置かれています。
あなたは次の操作を 0 回以上何度でも行うことが出来ます。

  • 回転テーブルを反時計回りに 1 周の \frac{1}{N} だけ回す。これによって、(この操作の直前に)人 i の目の前にあった料理は人 (i+1) \bmod N の目の前に移動する。

操作を完了させた後において、人 i は料理 i が人 (i-1) \bmod N、人 i、人 (i+1) \bmod N のいずれかの目の前に置かれていると喜びます。
喜ぶ人数の最大値を求めてください。

a \bmod m とは 整数 a と正整数 m に対し、a \bmod ma-xm の倍数となるような 0 以上 m 未満の整数 x を表します。(このような x は一意に定まることが証明できます)

制約

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • i \neq j ならば p_i \neq p_j
  • 入力はすべて整数

入力

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

N
p_0 \ldots p_{N-1}

出力

答えを出力せよ。


入力例 1

4
1 2 0 3

出力例 1

4

操作を 1 回行うと下の画像のようになります。

この時、4 人が喜ぶことを以下のように確かめられます。

  • 0 は料理 0 が人 3\ (=(0-1) \bmod 4) の目の前に置かれているので喜びます。
  • 1 は料理 1 が人 1\ (=1) の目の前に置かれているので喜びます。
  • 2 は料理 2 が人 2\ (=2) の目の前に置かれているので喜びます。
  • 3 は料理 3 が人 0\ (=(3+1) \bmod 4) の目の前に置かれているので喜びます。

5 人以上が喜ぶことは無いため、答えは 4 です。


入力例 2

3
0 1 2

出力例 2

3

入力例 3

10
3 9 6 1 7 2 8 0 5 4

出力例 3

5

Score : 300 points

Problem Statement

Person 0, Person 1, \ldots, and Person (N-1) are sitting around a turntable in their counterclockwise order, evenly spaced. Dish p_i is in front of Person i on the table.
You may perform the following operation 0 or more times:

  • Rotate the turntable by one N-th of a counterclockwise turn. As a result, the dish that was in front of Person i right before the rotation is now in front of Person (i+1) \bmod N.

When you are finished, Person i is happy if Dish i is in front of Person (i-1) \bmod N, Person i, or Person (i+1) \bmod N.
Find the maximum possible number of happy people.

What is a \bmod m? For an integer a and a positive integer m, a \bmod m denotes the integer x between 0 and (m-1) (inclusive) such that (a-x) is a multiple of m. (It can be proved that such x is unique.)

Constraints

  • 3 \leq N \leq 2 \times 10^5
  • 0 \leq p_i \leq N-1
  • p_i \neq p_j if i \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
p_0 \ldots p_{N-1}

Output

Print the answer.


Sample Input 1

4
1 2 0 3

Sample Output 1

4

The figure below shows the table after one operation.

Here, there are four happy people:

  • Person 0 is happy because Dish 0 is in front of Person 3\ (=(0-1) \bmod 4);
  • Person 1 is happy because Dish 1 is in front of Person 1\ (=1);
  • Person 2 is happy because Dish 2 is in front of Person 2\ (=2);
  • Person 3 is happy because Dish 3 is in front of Person 0\ (=(3+1) \bmod 4).

There cannot be five or more happy people, so the answer is 4.


Sample Input 2

3
0 1 2

Sample Output 2

3

Sample Input 3

10
3 9 6 1 7 2 8 0 5 4

Sample Output 3

5
G - Strange Lunchbox

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

N 種類の弁当が、それぞれ 1 個ずつ売られています。
i = 1, 2, \ldots, N について、i 種類目の弁当には A_i 個のたこ焼きと B_i 個のたい焼きが入っています。

高橋君は、 X 個以上のたこ焼きと Y 個以上のたい焼きを食べたいです。
高橋君がいくつかの弁当を選んで買うことで、 X 個以上のたこ焼きと Y 個以上のたい焼きを手に入れることが可能かどうか判定して下さい。また、可能な場合はそのために高橋君が購入しなければならない弁当の個数の最小値を求めて下さい。

各種類の弁当は 1 個しか売られていないため、同じ種類の弁当を 2 個以上購入することは出来ないことに注意して下さい。

制約

  • 1 \leq N \leq 300
  • 1 \leq X, Y \leq 300
  • 1 \leq A_i, B_i \leq 300
  • 入力はすべて整数

入力

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

N
X Y
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

高橋君が X 個以上のたこ焼きと Y 個以上のたい焼きを手に入れることが不可能な場合は -1 を出力し、 可能な場合はそのために高橋君が購入しなければならない弁当の個数の最小値を出力せよ。


入力例 1

3
5 6
2 1
3 4
2 3

出力例 1

2

高橋君は、5 個以上のたこ焼きと 6 個以上のたい焼きを食べたいです。
高橋君は 2 種類目の弁当と 3 種類目の弁当を買うことで、 たこ焼きを 3 + 2 = 5 個、たい焼きを 4 + 3 = 7 個手に入れることができます。


入力例 2

3
8 8
3 4
2 3
2 1

出力例 2

-1

高橋君がたとえすべての弁当を買ったとしても、高橋君は 8 個以上のたこ焼きと 8 個以上のたい焼きを手に入れることが出来ません。
よって、-1 を出力します。

Score : 400 points

Problem Statement

A shop sells N kinds of lunchboxes, one for each kind.
For each i = 1, 2, \ldots, N, the i-th kind of lunchbox contains A_i takoyaki (octopus balls) and B_i taiyaki (fish-shaped cakes).

Takahashi wants to eat X or more takoyaki and Y or more taiyaki.
Determine whether it is possible to buy some number of lunchboxes to get at least X takoyaki and at least Y taiyaki. If it is possible, find the minimum number of lunchboxes that Takahashi must buy to get them.

Note that just one lunchbox is in stock for each kind; you cannot buy two or more lunchboxes of the same kind.

Constraints

  • 1 \leq N \leq 300
  • 1 \leq X, Y \leq 300
  • 1 \leq A_i, B_i \leq 300
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X Y
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

If Takahashi cannot get at least X takoyaki and at least Y taiyaki, print -1; otherwise, print the minimum number of lunchboxes that he must buy to get them.


Sample Input 1

3
5 6
2 1
3 4
2 3

Sample Output 1

2

He wants to eat 5 or more takoyaki and 6 or more taiyaki.
Buying the second and third lunchboxes will get him 3 + 2 = 5 taiyaki and 4 + 3 = 7 taiyaki.


Sample Input 2

3
8 8
3 4
2 3
2 1

Sample Output 2

-1

Even if he is to buy every lunchbox, it is impossible to get at least 8 takoyaki and at least 8 taiyaki.
Thus, print -1.

H - Geometric Progression

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

整数 A, X, M が与えられます。\displaystyle \sum_{i = 0}^{X-1} A^iM で割った余りを求めてください。

制約

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • 入力はすべて整数

入力

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

A X M

出力

答えを出力せよ。


入力例 1

3 4 7

出力例 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40 です。407 で割った余りは 5 であるため、5 を出力します。


入力例 2

8 10 9

出力例 2

0

入力例 3

1000000000 1000000000000 998244353

出力例 3

919667211

Score : 500 points

Problem Statement

Given integers A, X, and M, find \displaystyle \sum_{i = 0}^{X-1} A^i, modulo M.

Constraints

  • 1 \leq A, M \leq 10^9
  • 1 \leq X \leq 10^{12}
  • All values in the input are integers.

Input

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

A X M

Output

Print the answer.


Sample Input 1

3 4 7

Sample Output 1

5

3^0 + 3^1 + 3^2 + 3^3 = 40, which equals 5 modulo 7, so 5 should be printed.


Sample Input 2

8 10 9

Sample Output 2

0

Sample Input 3

1000000000 1000000000000 998244353

Sample Output 3

919667211
I - Double Sum 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

1\le L\le R\le N を満たす整数の組 (L,R) に対し f(L,R) を以下のように定義します。

  • 何も書かれていない黒板に R-L+1 個の整数 A_L,A_{L+1},\ldots,A_R を順に書き込む。
  • 以下の操作を黒板に書かれた整数が全て消えるまで繰り返す。
    • 整数 l,r を選ぶ。ただし、 l\le r かつ黒板に l 以上 r 以下の整数が全て 1 つ以上書かれているように l,r を選ぶ必要がある。その後、黒板に書かれた l 以上 r 以下の整数を全て消す。
  • 黒板に書かれた整数が全て消えるまでに必要な操作回数の最小値を f(L,R) とする。

\displaystyle \sum_{L=1}^N\sum_{R=L}^N f(L,R) を求めてください。

制約

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

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4
1 3 1 4

出力例 1

16

例えば (L,R)=(1,4) の場合、以下の手順で f(L,R) を計算することができます。

  • 黒板に 1,3,1,4 が書かれている。
  • (l,r)=(1,1) を選び、黒板に書かれた 1 を全て消す。黒板には 3,4 が書かれた状態になる。
  • (l,r)=(3,4) を選び、黒板に書かれた 3,4 を全て消す。黒板は何も書かれていない状態になる。
  • 2 回未満の操作で黒板の整数を全て消すことはできないので、f(1,4)=2 である。

同様の計算で、例えば f(2,4)=2, f(1,1)=1 なども分かります。

\displaystyle \sum_{L=1}^N\sum_{R=L}^N f(L,R)=16 なので、 16 を出力してください。


入力例 2

5
3 1 4 2 4

出力例 2

23

入力例 3

10
5 1 10 9 2 5 6 9 1 6

出力例 3

129

Score : 525 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.

For each integer pair (L,R) with 1 \le L \le R \le N, define f(L,R) as follows:

  • Start with an empty blackboard. Write the R-L+1 integers A_L, A_{L+1}, \ldots, A_R on the blackboard in order.
  • Repeat the following operation until all integers on the blackboard are erased:
    • Choose integers l, r with l \le r such that every integer from l through r appears at least once on the blackboard. Then, erase all integers from l through r that are on the blackboard.
  • Let f(L,R) be the minimum number of such operations needed to erase all the integers from the blackboard.

Find \displaystyle \sum_{L=1}^N \sum_{R=L}^N f(L,R).

Constraints

  • 1 \le N \le 3 \times 10^5
  • 1 \le A_i \le N
  • 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
1 3 1 4

Sample Output 1

16

For example, in the case of (L,R)=(1,4):

  • The blackboard has 1,3,1,4.
  • Choose (l,r)=(1,1) and erase all occurrences of 1. The blackboard now has 3,4.
  • Choose (l,r)=(3,4) and erase all occurrences of 3 and 4. The blackboard becomes empty.
  • It cannot be done in fewer than two operations, so f(1,4) = 2.

Similarly, you can find f(2,4)=2, f(1,1)=1, etc.

\displaystyle \sum_{L=1}^N \sum_{R=L}^N f(L,R) = 16, so print 16.


Sample Input 2

5
3 1 4 2 4

Sample Output 2

23

Sample Input 3

10
5 1 10 9 2 5 6 9 1 6

Sample Output 3

129