A - Nine

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

以下のような、1 から 9 までの数字が書かれた 3 \times 3 の盤面があります。

1 以上 9 以下の整数 A,B が与えられます。ただし、A < B です。

A が書かれたマスと B が書かれたマスが左右に隣接しているか判定してください。

制約

  • 1 \le A < B \le 9
  • A, B は整数である。

入力

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

A B

出力

A が書かれたマスと B が書かれたマスが左右に隣接しているならば Yes、そうでないならば No を出力せよ。


入力例 1

7 8

出力例 1

Yes

7 が書かれたマスと 8 が書かれたマスは左右に隣り合っているので、Yes を出力します。


入力例 2

1 9

出力例 2

No

入力例 3

3 4

出力例 3

No

Score : 100 points

Problem Statement

We have the following 3 \times 3 board with integers from 1 through 9 written on it.

You are given two integers A and B between 1 and 9, where A < B.

Determine if the two squares with A and B written on them are adjacent horizontally.

Constraints

  • 1 \le A < B \le 9
  • A and B are integers.

Input

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

A B

Output

Print Yes if the two squares with A and B written on them are adjacent horizontally, and No otherwise.


Sample Input 1

7 8

Sample Output 1

Yes

The two squares with 7 and 8 written on them are adjacent horizontally, so print Yes.


Sample Input 2

1 9

Sample Output 2

No

Sample Input 3

3 4

Sample Output 3

No
B - Rotate

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

NN 列のマス目が与えられます。上から i 行目、左から j 列目のマスには整数 A_{i,j} が書かれています。ここで、A_{i,j}01 であることが保証されます。

マス目の外側のマスに書かれた整数を時計回りに 1 個ずつずらしたときのマス目を出力してください。

ただし外側のマスとは、1 行目、N 行目、1 列目、N 列目のいずれか 1 つ以上に属するマスの集合のことを指します。

制約

  • 2 \le N \le 100
  • 0 \le A_{i,j} \le 1(1 \le i,j \le N)
  • 入力は全て整数

入力

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

出力

マス目の外側のマスに書かれた整数を時計回りに 1 個ずつずらしたときのマス目において、上から i 行目、左から j 列目のマスに書かれている整数を B_{i,j} と置く。このとき、以下の形式で出力せよ。

B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}

入力例 1

4
0101
1101
1111
0000

出力例 1

1010
1101
0111
0001

上から i 行目、左から j 列目のマスを (i,j) と呼ぶこととします。

外側のマスは (1,1) から時計回りに列挙すると (1,1),(1,2),(1,3),(1,4),(2,4),(3,4),(4,4),(4,3),(4,2),(4,1),(3,1),(2,1)12 個です。

これらのマスに書かれている整数を、時計回りに 1 個ずつ動かすと出力欄のようになります。


入力例 2

2
11
11

出力例 2

11
11

入力例 3

5
01010
01001
10110
00110
01010

出力例 3

00101
11000
00111
00110
10100

Score : 200 points

Problem Statement

You are given a grid with N rows and N columns. An integer A_{i, j} is written on the square at the i-th row from the top and j-th column from the left. Here, it is guaranteed that A_{i,j} is either 0 or 1.

Shift the integers written on the outer squares clockwise by one square each, and print the resulting grid.

Here, the outer squares are those in at least one of the 1-st row, N-th row, 1-st column, and N-th column.

Constraints

  • 2 \le N \le 100
  • 0 \le A_{i,j} \le 1(1 \le i,j \le N)
  • All input values are integers.

Input

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

N
A_{1,1}A_{1,2}\dots A_{1,N}
A_{2,1}A_{2,2}\dots A_{2,N}
\vdots
A_{N,1}A_{N,2}\dots A_{N,N}

Output

Let B_{i,j} be the integer written on the square at the i-th row from the top and j-th column from the left in the grid resulting from shifting the outer squares clockwise by one square each. Print them in the following format:

B_{1,1}B_{1,2}\dots B_{1,N}
B_{2,1}B_{2,2}\dots B_{2,N}
\vdots
B_{N,1}B_{N,2}\dots B_{N,N}

Sample Input 1

4
0101
1101
1111
0000

Sample Output 1

1010
1101
0111
0001

We denote by (i,j) the square at the i-th row from the top and j-th column from the left.

The outer squares, in clockwise order starting from (1,1), are the following 12 squares: (1,1),(1,2),(1,3),(1,4),(2,4),(3,4),(4,4),(4,3),(4,2),(4,1),(3,1), and (2,1).

The sample output shows the resulting grid after shifting the integers written on those squares clockwise by one square.


Sample Input 2

2
11
11

Sample Output 2

11
11

Sample Input 3

5
01010
01001
10110
00110
01010

Sample Output 3

00101
11000
00111
00110
10100
C - Medicine

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 350

問題文

高橋君は医者のすぬけ君から N 種類の薬を処方されました。i 種類目の薬は(処方された日を含めて) a_i 日間、毎日 b_i 錠ずつ飲む必要があります。また、高橋君はこれ以外の薬を飲む必要がありません。

薬を処方された日を 1 日目とします。1 日目以降で、初めて高橋君がその日に飲む必要がある薬が K 錠以下になるのは何日目かを求めてください。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq K \leq 10^9
  • 1 \leq a_i,b_i \leq 10^9
  • 入力はすべて整数

入力

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

N K
a_1 b_1
\vdots
a_N b_N

出力

1 日目以降で、初めて高橋君がその日に飲む必要がある薬が K 錠以下になるのが X 日目の時、 X を出力せよ。


入力例 1

4 8
6 3
2 5
1 9
4 2

出力例 1

3

1 日目には、高橋君は 1,2,3,4 種類目の薬をそれぞれ 3,5,9,2 錠飲む必要があります。よってこの日は 19 錠飲む必要があり、K(=8) 錠以下ではありません。
2 日目には、高橋君は 1,2,4 種類目の薬をそれぞれ 3,5,2 錠飲む必要があります。よってこの日は 10 錠飲む必要があり、K(=8) 錠以下ではありません。
3 日目には、高橋君は 1,4 種類目の薬をそれぞれ 3,2 錠飲む必要があります。よってこの日は 5 錠飲む必要があり、初めて K(=8) 錠以下になります。

以上より、3 が答えです。


入力例 2

4 100
6 3
2 5
1 9
4 2

出力例 2

1

入力例 3

15 158260522
877914575 2436426
24979445 61648772
623690081 33933447
476190629 62703497
211047202 71407775
628894325 31963982
822804784 50968417
430302156 82631932
161735902 80895728
923078537 7723857
189330739 10286918
802329211 4539679
303238506 17063340
492686568 73361868
125660016 50287940

出力例 3

492686569

Score : 350 points

Problem Statement

Snuke the doctor prescribed N kinds of medicine for Takahashi. For the next a_i days (including the day of the prescription), he has to take b_i pills of the i-th medicine. He does not have to take any other medicine.

Let the day of the prescription be day 1. On or after day 1, when is the first day on which he has to take K pills or less?

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq K \leq 10^9
  • 1 \leq a_i,b_i \leq 10^9
  • All input values are integers.

Input

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

N K
a_1 b_1
\vdots
a_N b_N

Output

If Takahashi has to take K pills or less on day X for the first time on or after day 1, print X.


Sample Input 1

4 8
6 3
2 5
1 9
4 2

Sample Output 1

3

On day 1, he has to take 3,5,9, and 2 pills of the 1-st, 2-nd, 3-rd, and 4-th medicine, respectively. In total, he has to take 19 pills on this day, which is not K(=8) pills or less.
On day 2, he has to take 3,5, and 2 pills of the 1-st, 2-nd, and 4-th medicine, respectively. In total, he has to take 10 pills on this day, which is not K(=8) pills or less.
On day 3, he has to take 3 and 2 pills of the 1-st and 4-th medicine, respectively. In total, he has to take 5 pills on this day, which is K(=8) pills or less for the first time.

Thus, the answer is 3.


Sample Input 2

4 100
6 3
2 5
1 9
4 2

Sample Output 2

1

Sample Input 3

15 158260522
877914575 2436426
24979445 61648772
623690081 33933447
476190629 62703497
211047202 71407775
628894325 31963982
822804784 50968417
430302156 82631932
161735902 80895728
923078537 7723857
189330739 10286918
802329211 4539679
303238506 17063340
492686568 73361868
125660016 50287940

Sample Output 3

492686569
D - Add One Edge

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

N_1+N_2 頂点 M 辺の無向グラフがあります。i=1,2,\ldots,M に対し、i 番目の辺は頂点 a_i と頂点 b_i を結びます。
また、以下を満たすことが保障されます。

  • 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • 頂点 1 と頂点 N_1+N_2 は非連結

次の操作をちょうど 1 回行います。

  • 1 \leq u \leq N_1 を満たす整数 uN_1+1 \leq v \leq N_1+N_2 を満たす整数 v を選び、頂点 u と頂点 v を結ぶ辺を追加する

操作後のグラフにおいて、頂点 1 と頂点 N_1+N_2 は必ず連結であることが示せます。そこで、頂点 1 と頂点 N_1+N_2 を結ぶ経路の長さ(辺の本数)の最小値を d とします。

操作で追加する辺を適切に選んだ時にありえる d の最大値を求めてください。

連結とは? 無向グラフの頂点 u,v が連結であるとは、頂点 u と頂点 v を結ぶ経路が存在することをいいます。

制約

  • 1 \leq N_1,N_2 \leq 1.5 \times 10^5
  • 0 \leq M \leq 3 \times 10^5
  • 1 \leq a_i \leq b_i \leq N_1+N_2
  • i \neq j ならば (a_i,b_i) \neq (a_j,b_j)
  • 1 \leq u,v \leq N_1 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • N_1+1 \leq u,v \leq N_1+N_2 を満たす整数 u,v に対し、頂点 u と頂点 v は連結
  • 頂点 1 と頂点 N_1+N_2 は非連結
  • 入力はすべて整数

入力

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

N_1 N_2 M
a_1 b_1
\vdots
a_M b_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

5

u=2,v=5 として操作することで d=5 と出来ます。これが最大値です。


入力例 2

7 5 20
10 11
4 5
10 12
1 2
1 5
5 6
2 4
3 5
9 10
2 5
1 4
11 12
9 12
8 9
5 7
3 7
3 6
3 4
8 12
9 11

出力例 2

4

Score : 400 points

Problem Statement

We have an undirected graph with (N_1+N_2) vertices and M edges. For i=1,2,\ldots,M, the i-th edge connects vertex a_i and vertex b_i.
The following properties are guaranteed:

  • Vertex u and vertex v are connected, for all integers u and v with 1 \leq u,v \leq N_1.
  • Vertex u and vertex v are connected, for all integers u and v with N_1+1 \leq u,v \leq N_1+N_2.
  • Vertex 1 and vertex (N_1+N_2) are disconnected.

Consider performing the following operation exactly once:

  • choose an integer u with 1 \leq u \leq N_1 and an integer v with N_1+1 \leq v \leq N_1+N_2, and add an edge connecting vertex u and vertex v.

We can show that vertex 1 and vertex (N_1+N_2) are always connected in the resulting graph; so let d be the minimum length (number of edges) of a path between vertex 1 and vertex (N_1+N_2).

Find the maximum possible d resulting from adding an appropriate edge to add.

Definition of "connected" Two vertices u and v of an undirected graph are said to be connected if and only if there is a path between vertex u and vertex v.

Constraints

  • 1 \leq N_1,N_2 \leq 1.5 \times 10^5
  • 0 \leq M \leq 3 \times 10^5
  • 1 \leq a_i \leq b_i \leq N_1+N_2
  • (a_i,b_i) \neq (a_j,b_j) if i \neq j.
  • Vertex u and vertex v are connected for all integers u and v such that 1 \leq u,v \leq N_1.
  • Vertex u and vertex v are connected for all integers u and v such that N_1+1 \leq u,v \leq N_1+N_2.
  • Vertex 1 and vertex (N_1+N_2) are disconnected.
  • All input values are integers.

Input

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

N_1 N_2 M
a_1 b_1
\vdots
a_M b_M

Output

Print the answer.


Sample Input 1

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

Sample Output 1

5

If we set u=2 and v=5, the operation yields d=5, which is the maximum possible.


Sample Input 2

7 5 20
10 11
4 5
10 12
1 2
1 5
5 6
2 4
3 5
9 10
2 5
1 4
11 12
9 12
8 9
5 7
3 7
3 6
3 4
8 12
9 11

Sample Output 2

4
E - Family and Insurance

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 425

問題文

1、人 2\ldots、人 N からなる家系があります。i\geq 2 に対し、人 i の親は人 p_i です。

この家系の人たちは M 回保険に加入しました。i=1,2,\ldots,M に対し、i 番目の保険の加入者は人 x_i で、本人及びその y_i 代先までの子たちが補償対象者です。

1 個以上の保険の補償対象者になっている人が何人いるかを求めてください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq M \leq 3 \times 10^5
  • 1 \leq p_i \leq i-1
  • 1 \leq x_i \leq N
  • 1 \leq y_i \leq 3 \times 10^5
  • 入力はすべて整数

入力

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

N M
p_2 \ldots p_N
x_1 y_1
\vdots
x_M y_M

出力

答えを出力せよ。


入力例 1

7 3
1 2 1 3 3 3
1 1
1 2
4 3

出力例 1

4

1 番目の保険について、人 11 代先の子たちは人 2 と人 4 なので人 1,2,4 が補償対象者です。
2 番目の保険について、人 11 代先の子たちは人 2 と人 42 代先の子は人 3 なので人 1,2,3,4 が補償対象者です。
3 番目の保険について、人 41,2,3 代先の子は存在しないので人 4 が補償対象者です。

以上より、1 個以上の保険の補償対象者になっている人は人 1,2,3,44 人です。


入力例 2

10 10
1 1 3 1 2 3 3 5 7
2 1
5 1
4 3
6 3
2 1
7 3
9 2
1 2
6 2
8 1

出力例 2

10

Score : 425 points

Problem Statement

There is a family consisting of person 1, person 2, \ldots, and person N. For i\geq 2, person i's parent is person p_i.

They bought insurance M times. For i=1,2,\ldots,M, person x_i bought the i-th insurance, which covers that person and their descendants in the next y_i generations.

How many people are covered by at least one insurance?

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq M \leq 3 \times 10^5
  • 1 \leq p_i \leq i-1
  • 1 \leq x_i \leq N
  • 1 \leq y_i \leq 3 \times 10^5
  • All input values are integers.

Input

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

N M
p_2 \ldots p_N
x_1 y_1
\vdots
x_M y_M

Output

Print the answer.


Sample Input 1

7 3
1 2 1 3 3 3
1 1
1 2
4 3

Sample Output 1

4

The 1-st insurance covers people 1, 2, and 4, because person 1's 1-st generation descendants are people 2 and 4.
The 2-nd insurance covers people 1, 2, 3, and 4, because person 1's 1-st generation descendants are people 2 and 4, and person 1's 2-nd generation descendant is person 3.
The 3-rd insurance covers person 4, because person 4 has no 1-st, 2-nd, or 3-rd descendants.

Therefore, four people, people 1, 2, 3, and 4, are covered by at least one insurance.


Sample Input 2

10 10
1 1 3 1 2 3 3 5 7
2 1
5 1
4 3
6 3
2 1
7 3
9 2
1 2
6 2
8 1

Sample Output 2

10
F - Box in Box

実行時間制限: 2.5 sec / メモリ制限: 1024 MiB

配点 : 525

問題文

N 個の箱があります。 i 番目の箱は高さ・幅・奥行きがそれぞれ h_i,w_i,d_i の直方体の形をしています。

二つの箱であって、必要ならば回転させることで片方の高さ・幅・奥行きがもう片方の高さ・幅・奥行きをそれぞれ上回るようなものが存在するかを判定してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq h_i,w_i,d_i \leq 10^9
  • 入力はすべて整数

入力

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

N
h_1 w_1 d_1
\vdots
h_N w_N d_N

出力

二つの箱であって、必要ならば回転させることで片方の高さ・幅・奥行きがもう片方の高さ・幅・奥行きをそれぞれ上回るようなものが存在するならば Yes と、そうでなければ No と出力せよ。


入力例 1

3
19 8 22
10 24 12
15 25 11

出力例 1

Yes

2 番目の箱を回転させて高さと奥行きを入れ替えると、3 番目の箱が高さ・幅・奥行きをそれぞれ上回ります。


入力例 2

3
19 8 22
10 25 12
15 24 11

出力例 2

No

入力例 3

2
1 1 2
1 2 2

出力例 3

No

Score : 525 points

Problem Statement

There are N boxes. The i-th box has a shape of a rectangular cuboid whose height, width, and depth are h_i,w_i, and d_i, respectively.

Determine if there are two boxes such that one's height, width, and depth are strictly greater than those of the other after rotating them if necessary.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq h_i,w_i,d_i \leq 10^9
  • All input values are integers.

Input

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

N
h_1 w_1 d_1
\vdots
h_N w_N d_N

Output

Print Yes if there are two boxes such that one's height, width, and depth are strictly greater than those of the other after rotating them if necessary; print No otherwise.


Sample Input 1

3
19 8 22
10 24 12
15 25 11

Sample Output 1

Yes

If you rotate the 2-nd box to swap its height and depth, the 3-rd box will have greater height, depth, and width.


Sample Input 2

3
19 8 22
10 25 12
15 24 11

Sample Output 2

No

Sample Input 3

2
1 1 2
1 2 2

Sample Output 3

No
G - Ban Permutation

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 575

問題文

(1,2,\dots,N) の順列 P=(P_1,P_2,\dots,P_N) のうち、以下の条件を満たすものの個数を 998244353 で割ったあまりを求めてください。

  • 1 \le i \le N を満たす全ての整数 i に対して、|P_i - i| \ge X である。

制約

  • 1 \le N \le 100
  • 1 \le X \le 5
  • 入力はすべて整数

入力

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

N X

出力

答えを出力せよ。


入力例 1

3 1

出力例 1

2

条件を満たす順列 P=(P_1,P_2,P_3) は、(2,3,1),(3,1,2)2 個です。よって答えは 2 です。


入力例 2

5 2

出力例 2

4

入力例 3

98 5

出力例 3

809422418

Score : 575 points

Problem Statement

Find the number, modulo 998244353, of permutations P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N) such that:

  • |P_i - i| \ge X for all integers i with 1 \le i \le N.

Constraints

  • 1 \le N \le 100
  • 1 \le X \le 5
  • All input values are integers.

Input

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

N X

Output

Print the answer.


Sample Input 1

3 1

Sample Output 1

2

The conforming permutations P=(P_1,P_2,P_3) are the following two, (2,3,1) and (3,1,2), so the answer is 2.


Sample Input 2

5 2

Sample Output 2

4

Sample Input 3

98 5

Sample Output 3

809422418
Ex - Simple Path Counting Problem

実行時間制限: 10 sec / メモリ制限: 1024 MiB

配点 : 650

問題文

NM 列のマス目があります。上から i 行目、左から j 列目のマスを (i,j) と呼ぶこととします。

長さ K の整数列 A=(A_1,A_2,\dots,A_K) と長さ L の整数列 B=(B_1,B_2,\dots,B_L) が与えられます。

1 \le i \le K,1 \le j \le L を満たす全ての整数の組 (i,j) に対して以下の問題を解き、その答えの総和を 998244353 で割ったあまりを求めてください。

  • はじめ (1,A_i) に駒が置かれている。以下の操作を N-1 回繰り返して駒を (N,B_j) に移動する方法の個数を求めよ。
    • 今駒が置かれているマスを (p,q) とする。(p+1,q-1),(p+1,q),(p+1,q+1) のいずれかに駒を移動する。ただし、マス目の外に出てはならない。

制約

  • 1 \le N \le 10^9
  • 1 \le M,K,L \le 10^5
  • 1 \le A_i,B_j \le M

入力

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

N M K L
A_1 A_2 \dots A_K
B_1 B_2 \dots B_L

出力

答えを出力せよ。


入力例 1

3 4 1 2
1
1 2

出力例 1

4

(i,j)=(1,1) のとき、駒の移動方法は以下の 2 通りです。

  • (1,1) \rightarrow (2,1) \rightarrow (3,1)
  • (1,1) \rightarrow (2,2) \rightarrow (3,1)

(i,j)=(1,2) のとき、駒の移動方法は以下の 2 通りです。

  • (1,1) \rightarrow (2,1) \rightarrow (3,2)
  • (1,1) \rightarrow (2,2) \rightarrow (3,2)

よって、答えは 2 + 2 =4 です。


入力例 2

5 8 4 5
3 1 4 1
2 7 1 8 2

出力例 2

137

入力例 3

883671387 87719 10 12
86879 64174 47274 41688 17713 50897 53989 7210 30894 5714
60358 28835 48036 48450 67149 36558 35929 69025 77539 19195 60762 60721

出力例 3

941873621

Score : 650 points

Problem Statement

We have a grid with N rows and M columns. We denote by (i,j) the cell in the i-th row from the top and j-th column from the left.

You are given integer sequences A=(A_1,A_2,\dots,A_K) and B=(B_1,B_2,\dots,B_L) of lengths K and L, respectively.

Find the sum, modulo 998244353, of the answers to the following question over all integer pairs (i,j) such that 1 \le i \le K and 1 \le j \le L.

  • A piece is initially placed at (1,A_i). How many paths are there to take it to (N,B_j) by repeating the following move (N-1) times?
    • Let (p,q) be the piece's current cell. Move it to (p+1,q-1),(p+1,q), or (p+1,q+1), without moving it outside the grid.

Constraints

  • 1 \le N \le 10^9
  • 1 \le M,K,L \le 10^5
  • 1 \le A_i,B_j \le M

Input

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

N M K L
A_1 A_2 \dots A_K
B_1 B_2 \dots B_L

Output

Print the answer.


Sample Input 1

3 4 1 2
1
1 2

Sample Output 1

4

For (i,j)=(1,1), the following two paths are possible:

  • (1,1) \rightarrow (2,1) \rightarrow (3,1)
  • (1,1) \rightarrow (2,2) \rightarrow (3,1)

For (i,j)=(1,2), the following two paths are possible:

  • (1,1) \rightarrow (2,1) \rightarrow (3,2)
  • (1,1) \rightarrow (2,2) \rightarrow (3,2)

Thus, the answer is 2 + 2 =4.


Sample Input 2

5 8 4 5
3 1 4 1
2 7 1 8 2

Sample Output 2

137

Sample Input 3

883671387 87719 10 12
86879 64174 47274 41688 17713 50897 53989 7210 30894 5714
60358 28835 48036 48450 67149 36558 35929 69025 77539 19195 60762 60721

Sample Output 3

941873621