実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
頂点 1, 頂点 2,\ldots, 頂点 N の N 個の頂点からなる単純無向グラフ G,H が与えられます。 G には M _ G 本の辺があり、i 本目 (1\leq i\leq M _ G) の辺は頂点 u _ i と頂点 v _ i を結んでいます。 H には M _ H 本の辺があり、i 本目 (1\leq i\leq M _ H) の辺は頂点 a _ i と頂点 b _ i を結んでいます。
あなたは、グラフ H に対して次の操作を 0 回以上の好きな回数繰り返すことができます。
- 1\leq i\lt j\leq N を満たす整数の組 (i,j) をひとつ選ぶ。A _ {i,j} 円を支払って、頂点 i と頂点 j を結ぶ辺がなければ追加し、あれば取り除く。
G と H を同型にするために少なくとも何円支払う必要があるか求めてください。
単純無向グラフとは?
単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。
グラフの同型とは?
N 頂点のグラフ G と H が同型であるとは、ある (1,2,\ldots,N) の並べ替え (P _ 1,P _ 2,\ldots,P _ N) が存在して、どの 1\leq i\lt j\leq N に対しても
- G に頂点 i, 頂点 j を結ぶ辺が存在するとき、かつそのときに限り H に頂点 P _ i, 頂点 P _ j を結ぶ辺が存在する
制約
- 1\leq N\leq8
- 0\leq M _ G\leq\dfrac{N(N-1)}2
- 0\leq M _ H\leq\dfrac{N(N-1)}2
- 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M _ G)
- (u _ i,v _ i)\neq(u _ j,v _ j)\ (1\leq i\lt j\leq M _ G)
- 1\leq a _ i\lt b _ i\leq N\ (1\leq i\leq M _ H)
- (a _ i,b _ i)\neq(a _ j,b _ j)\ (1\leq i\lt j\leq M _ H)
- 1\leq A _ {i,j}\leq 10 ^ 6\ (1\leq i\lt j\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
M _ G
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {M _ G} v _ {M _ G}
M _ H
a _ 1 b _ 1
a _ 2 b _ 2
\vdots
a _ {M _ H} b _ {M _ H}
A _ {1,2} A _ {1,3} \ldots A _ {1,N}
A _ {2,3} \ldots A _ {2,N}
\vdots
A _ {N-1,N}
出力
答えを出力せよ。
入力例 1
5 4 1 2 2 3 3 4 4 5 4 1 2 1 3 1 4 1 5 3 1 4 1 5 9 2 6 5 3
出力例 1
9
与えられたグラフはそれぞれ以下のようになります。

たとえば、H に対して次のような 4 つの操作を順に行うことで、9 円を支払ってG と H を同型にすることができます。
- (i,j)=(1,3) として操作を行う。H には頂点 1 と頂点 3 を結ぶ辺があるので、1 円を支払ってこれを取り除く。
- (i,j)=(2,5) として操作を行う。H には頂点 2 と頂点 5 を結ぶ辺はないので、2 円を支払ってこれを追加する。
- (i,j)=(1,5) として操作を行う。H には頂点 1 と頂点 5 を結ぶ辺があるので、1 円を支払ってこれを取り除く。
- (i,j)=(3,5) として操作を行う。H には頂点 3 と頂点 5 を結ぶ辺はないので、5 円を支払ってこれを追加する。
操作の結果、H は以下のようになります。

支払う金額を 8 円以下にして G と H を同型にすることはできないため、9 を出力してください。
入力例 2
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 9 1 1 1 1 1 1 1 1 9
出力例 2
3
たとえば、(i,j)=(2,3),(2,4),(3,4) とした 3 回の操作を行うことで G と H を同型にすることができます。
入力例 3
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 5 4 4 4 4 4 4 4 4 5
出力例 3
5
たとえば、(i,j)=(4,5) とした 1 回の操作を行うことで G と H を同型にすることができます。
入力例 4
2 0 0 371
出力例 4
0
G や H には辺が含まれていないこともあります。 また、一度も操作をする必要がないこともあります。
入力例 5
8 13 1 8 5 7 4 6 1 5 7 8 1 6 1 2 5 8 2 6 5 6 6 7 3 7 4 8 15 3 5 1 7 4 6 3 8 7 8 1 2 5 6 1 6 1 5 1 4 2 8 2 6 2 4 4 7 1 3 7483 1694 5868 3296 9723 5299 4326 5195 4088 5871 1384 2491 6562 1149 6326 2996 9845 7557 4041 7720 1554 5060 8329 8541 3530 4652 3874 3748
出力例 5
21214
Score : 300 points
Problem Statement
You are given simple undirected graphs G and H, each with N vertices: vertices 1, 2, \ldots, N. Graph G has M_G edges, and its i-th edge (1\leq i\leq M_G) connects vertices u_i and v_i. Graph H has M_H edges, and its i-th edge (1\leq i\leq M_H) connects vertices a_i and b_i.
You can perform the following operation on graph H any number of times, possibly zero.
- Choose a pair of integers (i,j) satisfying 1\leq i<j\leq N. Pay A_{i,j} yen, and if there is no edge between vertices i and j in H, add one; if there is, remove it.
Find the minimum total cost required to make G and H isomorphic.
What is a simple undirected graph?
A simple undirected graph is a graph without self-loops or multi-edges, where edges have no direction.
What does it mean for graphs to be isomorphic?
Two graphs G and H with N vertices are isomorphic if and only if there exists a permutation (P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) such that for all 1\leq i\lt j\leq N:
- an edge exists between vertices i and j in G if and only if an edge exists between vertices P_i and P_j in H.
Constraints
- 1\leq N\leq8
- 0\leq M _ G\leq\dfrac{N(N-1)}2
- 0\leq M _ H\leq\dfrac{N(N-1)}2
- 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M _ G)
- (u _ i,v _ i)\neq(u _ j,v _ j)\ (1\leq i\lt j\leq M _ G)
- 1\leq a _ i\lt b _ i\leq N\ (1\leq i\leq M _ H)
- (a _ i,b _ i)\neq(a _ j,b _ j)\ (1\leq i\lt j\leq M _ H)
- 1\leq A _ {i,j}\leq 10 ^ 6\ (1\leq i\lt j\leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
M _ G
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {M _ G} v _ {M _ G}
M _ H
a _ 1 b _ 1
a _ 2 b _ 2
\vdots
a _ {M _ H} b _ {M _ H}
A _ {1,2} A _ {1,3} \ldots A _ {1,N}
A _ {2,3} \ldots A _ {2,N}
\vdots
A _ {N-1,N}
Output
Print the answer.
Sample Input 1
5 4 1 2 2 3 3 4 4 5 4 1 2 1 3 1 4 1 5 3 1 4 1 5 9 2 6 5 3
Sample Output 1
9
The given graphs are as follows:

For example, you can perform the following four operations on H to make it isomorphic to G at a cost of 9 yen.
- Choose (i,j)=(1,3). There is an edge between vertices 1 and 3 in H, so pay 1 yen to remove it.
- Choose (i,j)=(2,5). There is no edge between vertices 2 and 5 in H, so pay 2 yen to add it.
- Choose (i,j)=(1,5). There is an edge between vertices 1 and 5 in H, so pay 1 yen to remove it.
- Choose (i,j)=(3,5). There is no edge between vertices 3 and 5 in H, so pay 5 yen to add it.
After these operations, H becomes:

You cannot make G and H isomorphic at a cost less than 9 yen, so print 9.
Sample Input 2
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 9 1 1 1 1 1 1 1 1 9
Sample Output 2
3
For example, performing the operations (i,j)=(2,3),(2,4),(3,4) on H will make it isomorphic to G.
Sample Input 3
5 3 1 2 2 3 3 4 4 1 2 2 3 3 4 4 5 5 4 4 4 4 4 4 4 4 5
Sample Output 3
5
For example, performing the operation (i,j)=(4,5) once will make G and H isomorphic.
Sample Input 4
2 0 0 371
Sample Output 4
0
Note that G and H may have no edges.
Also, it is possible that no operations are needed.
Sample Input 5
8 13 1 8 5 7 4 6 1 5 7 8 1 6 1 2 5 8 2 6 5 6 6 7 3 7 4 8 15 3 5 1 7 4 6 3 8 7 8 1 2 5 6 1 6 1 5 1 4 2 8 2 6 2 4 4 7 1 3 7483 1694 5868 3296 9723 5299 4326 5195 4088 5871 1384 2491 6562 1149 6326 2996 9845 7557 4041 7720 1554 5060 8329 8541 3530 4652 3874 3748
Sample Output 5
21214
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
AtCoder社のオフィスは H 行 W 列のマス目で表されます。上から i 行目、左から j 列目のマスを (i, j) と表します。
各マスの状態は文字 S_{i,j} で表され、 S_{i,j} が # のときそのマスは壁、. のときそのマスは床、H のときそのマスは加湿器が置かれた床です。
あるマスは、ある加湿器から壁を通らず上下左右に D 回以下の移動で辿り着ける場合加湿されます。ここで、加湿器が置かれた床のマスは必ず加湿されていることに注意してください。
加湿されている床のマスの個数を求めてください。
制約
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- 0 \leq D \leq H\times W
- S_{i,j} は
#または.またはHである (1 \leq i \leq H, 1 \leq j \leq W) - 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
H W D
S_{1,1}S_{1,2}\cdotsS_{1,W}
S_{2,1}S_{2,2}\cdotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\cdotsS_{H,W}
出力
答えを出力せよ。
入力例 1
3 4 1 H... #..H .#.#
出力例 1
5
マス (1,1),(1,2),(1,4),(2,3),(2,4) の 5 つのマスが加湿されています。
入力例 2
5 6 2 ##...H H..... ..H.#. .HH... .###..
出力例 2
21
入力例 3
1 6 3 ...#..
出力例 3
0
加湿されるマスが存在しない場合もあります。
Score : 350 points
Problem Statement
The AtCoder company office is represented as a grid of H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.
The state of each cell is represented by a character S_{i,j}. If S_{i,j} is #, that cell has a wall; if S_{i,j} is ., that cell is a floor; if S_{i,j} is H, that cell has a humidifier placed on a floor cell.
A certain cell is considered humidified if it can be reached from at least one humidifier cell by at most D moves up, down, left, or right without passing through a wall. Note that any cell with a humidifier is always humidified.
Find the number of humidified floor cells.
Constraints
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- 0 \leq D \leq H\times W
- S_{i,j} is
#,., orH. (1 \leq i \leq H, 1 \leq j \leq W) - All input numbers are integers.
Input
The input is given from Standard Input in the following format:
H W D
S_{1,1}S_{1,2}\cdotsS_{1,W}
S_{2,1}S_{2,2}\cdotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\cdotsS_{H,W}
Output
Print the answer.
Sample Input 1
3 4 1 H... #..H .#.#
Sample Output 1
5
Five cells (1,1), (1,2), (1,4), (2,3), (2,4) are humidified.
Sample Input 2
5 6 2 ##...H H..... ..H.#. .HH... .###..
Sample Output 2
21
Sample Input 3
1 6 3 ...#..
Sample Output 3
0
It is possible that no cells are humidified.
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
数直線があり、最初は座標 0 に人 0 がひとりで立っています。
これから、人 1,2,\dots,N がこの順に到着し、数直線上に立ちます。
人 i は座標 X_i に立ちます。なお、 X_i \ge 1 であり、全ての人について X_i は相異なります。
人が到着するたびに、以下の問いに答えてください。
- 現在数直線に人 0,1,\dots,r の r+1 人が立っているとする。
- このとき、 d_i を「人 i に最も近い別の人までの距離」と定義する。
- より厳密には、 \displaystyle d_i = \min_{0 \le j \le r, j \neq i} \vert X_i - X_j \vert とする。
- この d の総和、すなわち \displaystyle \sum_{i=0}^r d_i を求めよ。
制約
- 入力は全て整数
- 1 \le N \le 5 \times 10^5
- 1 \le X_i \le 10^9
- i \neq j ならば X_i \neq X_j
入力
入力は以下の形式で標準入力から与えられる。
N X_1 X_2 \dots X_N
出力
N 行に出力せよ。
そのうち i ( 1 \le i \le N ) 行目には、人 i が到着した時点での問いの答えを出力せよ。
入力例 1
10 5 2 7 4 108728325 390529120 597713292 322456626 845148281 812604915
出力例 1
10 7 8 8 108728326 390529121 523096670 452057486 699492475 517144218
この入力では、人が 10 人到着します。
このうち最初の 4 人について説明します。
- 人 1 が到着した時点で、座標 0,5 に人がいます。
- 求める値は 5+5=10 です。
- 人 2 が到着した時点で、座標 0,2,5 に人がいます。
- 求める値は 2+2+3=7 です。
- 人 3 が到着した時点で、座標 0,2,5,7 に人がいます。
- 求める値は 2+2+2+2=8 です。
- 人 4 が到着した時点で、座標 0,2,4,5,7 に人がいます。
- 求める値は 2+2+1+1+2=8 です。
Score : 400 points
Problem Statement
There is a number line, and initially person 0 is standing alone at coordinate 0.
From now on, persons 1,2,\dots,N will arrive in this order and stand on the number line.
Person i stands at coordinate X_i. Here, X_i \ge 1, and X_i is distinct for all persons.
Each time a person arrives, answer the following question.
- Suppose that currently r+1 persons 0,1,\dots,r are standing on the number line.
- Here, define d_i as the distance to the nearest other person from person i.
- More formally, \displaystyle d_i = \min_{0 \le j \le r, j \neq i} \vert X_i - X_j \vert.
- Find the sum of this d, that is, \displaystyle \sum_{i=0}^r d_i.
Constraints
- All input values are integers.
- 1 \le N \le 5 \times 10^5
- 1 \le X_i \le 10^9
- X_i \neq X_j if i \neq j.
Input
The input is given from Standard Input in the following format:
N X_1 X_2 \dots X_N
Output
Print N lines.
The i-th line ( 1 \le i \le N ) should contain the answer to the question when person i arrives.
Sample Input 1
10 5 2 7 4 108728325 390529120 597713292 322456626 845148281 812604915
Sample Output 1
10 7 8 8 108728326 390529121 523096670 452057486 699492475 517144218
In this input, 10 persons arrive.
The first four persons are explained below.
- When person 1 arrives, there are persons at coordinates 0,5.
- The required value is 5+5=10.
- When person 2 arrives, there are persons at coordinates 0,2,5.
- The required value is 2+2+3=7.
- When person 3 arrives, there are persons at coordinates 0,2,5,7.
- The required value is 2+2+2+2=8.
- When person 4 arrives, there are persons at coordinates 0,2,4,5,7.
- The required value is 2+2+1+1+2=8.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
Atcoder 国には 1 から N の番号がついた N 個の街と、1 から M の番号がついた M 本の電車が走っています。
電車 i は街 A_i を時刻 S_i に発車し、街 B_i に時刻 T_i に到着します。
正の整数 X_1 が与えられるので、0 以上の整数 X_2,\ldots,X_M を以下の条件を満たすように定める方法のうち、X_2+\ldots+X_M が最小になるものを求めてください。
- 条件:1 \leq i,j \leq M を満たす全ての組 (i,j) について、「B_i=A_j かつ T_i \leq S_j」ならば「T_i+X_i \leq S_j+X_j」
- すなわち、もともと乗り換え可能だった電車の組は、各電車 i の発車時刻・到着時刻を X_i 遅らせても乗り換え可能である
なお、X_2+\ldots+X_M が最小になるような X_2,\ldots,X_M の定め方は一意であることが証明できます。
制約
- 2 \leq N \leq 2\times 10^5
- 2 \leq M \leq 2\times 10^5
- 1 \leq A_i,B_i \leq N
- A_i \neq B_i
- 0 \leq S_i < T_i \leq 10^9
- 1 \leq X_1 \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 A_1 B_1 S_1 T_1 \vdots A_M B_M S_M T_M
出力
条件を満たし、和を最小化するような X_2,\ldots,X_M を、この順に空白区切りで出力せよ。
入力例 1
3 6 15 1 2 10 20 1 2 20 30 2 3 25 40 2 3 35 50 3 1 15 30 3 1 45 60
出力例 1
0 10 0 0 5
街 1 から 2 へ行く電車 1 の到着が 15 遅れ、時刻 35 になりました。
街 2 での電車 1 から 3 への乗り換えのため、電車 3 の発車時刻を 10 遅らせて、時刻 35 発 時刻 50 着とします。
さらに街 3 での電車 3 から 6 への乗り換えのため、電車 6 の発車時刻を 5 遅らせて、時刻 50 発とします。
他の電車は発車を遅らせることなく、元々乗り換え可能だった電車の間を乗り換えることができるため、(X_2,X_3,X_4,X_5,X_6)=(0,10,0,0,5) は条件を満たします。
また、条件を満たすもののうち和がこれより小さいものは存在しないため、これが答えとなります。
入力例 2
10 9 100 1 10 0 1 10 2 1 100 10 3 1 100 10 4 1 100 10 5 1 100 10 6 1 100 10 7 1 100 10 8 1 100 10 9 1 100
出力例 2
100 100 100 100 100 100 100 100
入力例 3
4 4 10 1 2 0 1 1 2 0 10 2 3 100 200 2 4 100 200
出力例 3
0 0 0
Score : 475 points
Problem Statement
In the nation of Atcoder, there are N cities numbered 1 to N, and M trains numbered 1 to M. Train i departs from city A_i at time S_i and arrives at city B_i at time T_i.
Given a positive integer X_1, find a way to set non-negative integers X_2,\ldots,X_M that satisfies the following condition with the minimum possible value of X_2+\ldots+X_M.
- Condition: For all pairs (i,j) satisfying 1 \leq i,j \leq M, if B_i=A_j and T_i \leq S_j, then T_i+X_i \leq S_j+X_j.
- In other words, for any pair of trains that are originally possible to transfer between, it is still possible to transfer even after delaying the departure and arrival times of each train i by X_i.
It can be proved that such a way to set X_2,\ldots,X_M with the minimum possible value of X_2+\ldots+X_M is unique.
Constraints
- 2 \leq N \leq 2\times 10^5
- 2 \leq M \leq 2\times 10^5
- 1 \leq A_i,B_i \leq N
- A_i \neq B_i
- 0 \leq S_i < T_i \leq 10^9
- 1 \leq X_1 \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M X_1 A_1 B_1 S_1 T_1 \vdots A_M B_M S_M T_M
Output
Print X_2,\ldots,X_M that satisfy the condition with the minimum possible sum, in that order, separated by spaces.
Sample Input 1
3 6 15 1 2 10 20 1 2 20 30 2 3 25 40 2 3 35 50 3 1 15 30 3 1 45 60
Sample Output 1
0 10 0 0 5
The arrival of train 1 from city 1 to 2 is delayed by 15 and becomes time 35.
To allow transfer from train 1 to 3 in city 2, the departure of train 3 is delayed by 10, making it depart at time 35 and arrive at time 50.
Further, to allow transfer from train 3 to 6 in city 3, the departure of train 6 is delayed by 5, making it depart at time 50.
Other trains can operate without delay while still allowing transfers between originally transferable trains, so (X_2,X_3,X_4,X_5,X_6)=(0,10,0,0,5) satisfies the condition.
Moreover, there is no solution with a smaller sum that satisfies the condition, so this is the answer.
Sample Input 2
10 9 100 1 10 0 1 10 2 1 100 10 3 1 100 10 4 1 100 10 5 1 100 10 6 1 100 10 7 1 100 10 8 1 100 10 9 1 100
Sample Output 2
100 100 100 100 100 100 100 100
Sample Input 3
4 4 10 1 2 0 1 1 2 0 10 2 3 100 200 2 4 100 200
Sample Output 3
0 0 0
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
半径で切って N 等分された円形のケーキがあります。
各ピースには時計回りに 1, 2, \ldots, N の番号が付けられています。また、1 \leq i \leq N なる整数 i についてピース i をピース N + i とも呼ぶこととします。
はじめ、すべてのピースの色は色 0 です。
あなたは、以下の操作を好きな回数行うことができます。
- 1 \leq a, b, c \leq N なる整数 a, b, c を選ぶ。0 \leq i < b なる各整数 i に対してピース a + i の色を色 c に変更する。この操作にはコストが b + X_c かかる。
1 \leq i \leq N なるすべての整数 i に対してピース i の色を色 C_i にするために必要なコストの合計の最小値を求めてください。
制約
- 1 \leq N \leq 400
- 1 \leq C_i \leq N
- 1 \leq X_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N C_1 C_2 \ldots C_N X_1 X_2 \ldots X_N
出力
答えを出力せよ。
入力例 1
6 1 4 2 1 2 5 1 2 3 4 5 6
出力例 1
20
ピース i の色が色 A_i であるとします。はじめ、(A_1, A_2, A_3, A_4, A_5, A_6) = (0, 0, 0, 0, 0, 0) です。
(a, b, c) = (2, 1, 4) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (0, 4, 0, 0, 0, 0) となります。
(a, b, c) = (3, 3, 2) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (0, 4, 2, 2, 2, 0) となります。
(a, b, c) = (1, 1, 1) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 2, 2, 0) となります。
(a, b, c) = (4, 1, 1) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 1, 2, 0) となります。
(a, b, c) = (6, 1, 5) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 1, 2, 5) となります。
このとき、コストの合計は 5 + 5 + 2 + 2 + 6 = 20 となります。
入力例 2
5 1 2 3 4 5 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 2
5000000005
入力例 3
8 2 3 3 1 2 1 3 1 3 4 1 2 5 3 1 2
出力例 3
23
Score : 550 points
Problem Statement
There is a circular cake that has been cut into N equal slices by its radii.
Each piece is labeled with an integer from 1 to N in clockwise order, and for each integer i with 1 \leq i \leq N, the piece i is also referred to as piece N + i.
Initially, every piece’s color is color 0.
You can perform the following operation any number of times:
- Choose integers a, b, and c such that 1 \leq a, b, c \leq N. For each integer i with 0 \leq i < b, change the color of piece a + i to color c. The cost of this operation is b + X_c.
You want each piece i (for 1 \leq i \leq N) to have color C_i. Find the minimum total cost of operations needed to achieve this.
Constraints
- 1 \leq N \leq 400
- 1 \leq C_i \leq N
- 1 \leq X_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N C_1 C_2 \ldots C_N X_1 X_2 \ldots X_N
Output
Print the answer.
Sample Input 1
6 1 4 2 1 2 5 1 2 3 4 5 6
Sample Output 1
20
Let A_i denote the color of piece i. Initially, (A_1, A_2, A_3, A_4, A_5, A_6) = (0, 0, 0, 0, 0, 0).
Performing an operation with (a, b, c) = (2, 1, 4) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (0, 4, 0, 0, 0, 0).
Performing an operation with (a, b, c) = (3, 3, 2) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (0, 4, 2, 2, 2, 0).
Performing an operation with (a, b, c) = (1, 1, 1) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (1, 4, 2, 2, 2, 0).
Performing an operation with (a, b, c) = (4, 1, 1) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (1, 4, 2, 1, 2, 0).
Performing an operation with (a, b, c) = (6, 1, 5) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (1, 4, 2, 1, 2, 5).
In this case, the total cost is 5 + 5 + 2 + 2 + 6 = 20.
Sample Input 2
5 1 2 3 4 5 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 2
5000000005
Sample Input 3
8 2 3 3 1 2 1 3 1 3 4 1 2 5 3 1 2
Sample Output 3
23