A - 穴あきカード

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

配点 : 266 点

問題文

高橋君は、情報の授業で使う教材として、穴あきカードを作成しています。

穴あきカードは N \times M のグリッド状になっており、各マスには穴が開いているか、開いていないかのどちらかです。穴が開いているマスを .、開いていないマスを # として図を描くことにしました。

高橋君は最初、どのマスにも穴が開いていないカード(すべてのマスが #)を用意しました。その後、青木君の指示に従って、K 箇所に穴を開けることにしました。

青木君が指定した穴の位置は座標 (R_i, C_i) で表されます(1 \leq i \leq K)。ここで、(R_i, C_i) は上から R_i 行目、左から C_i 列目のマスを指します。

青木君の指示通りに穴を開けた後のカードの状態を出力してください。

制約

  • 1 \leq N \leq 1000
  • 1 \leq M \leq 1000
  • 1 \leq K \leq N \times M
  • 1 \leq R_i \leq N (1 \leq i \leq K)
  • 1 \leq C_i \leq M (1 \leq i \leq K)
  • (R_i, C_i) \neq (R_j, C_j) (i \neq j)
  • 入力はすべて整数

入力

N M K
R_1 C_1
R_2 C_2
:
R_K C_K
  • 1 行目には、カードの行数を表す N 、列数を表す M 、穴を開ける箇所の個数を表す K が、スペース区切りで与えられる。
  • 2 行目から K + 1 行目では、穴を開ける位置が与えられる。
  • 1 + i 行目では、 i 番目に穴を開ける位置の行番号 R_i と列番号 C_i がスペース区切りで与えられる。

出力

N 行にわたって、カードの状態を出力せよ。 i 行目には M 文字からなる文字列を出力し、 j 文字目は座標 (i, j) に穴が開いていない場合は #、穴が開いている場合は . とせよ。


入力例 1

3 4 2
1 2
2 3

出力例 1

#.##
##.#
####

入力例 2

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

出力例 2

.###.
#.#.#
##.##
#.#.#
.####

入力例 3

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

出力例 3

##.###.###
.###.###.#
#.###.###.
###.###.##
.###.####.
##.###.###

Score : 266 pts

Problem Statement

Takahashi is creating punched cards as teaching materials for his information class.

A punched card is arranged in an N \times M grid, where each cell either has a hole punched in it or not. We will draw a diagram using . for cells with holes and # for cells without holes.

Takahashi initially prepared a card with no holes in any cell (all cells are #). After that, following Aoki's instructions, he decided to punch holes in K locations.

The positions of the holes specified by Aoki are represented by coordinates (R_i, C_i) (1 \leq i \leq K). Here, (R_i, C_i) refers to the cell in the R_i-th row from the top and the C_i-th column from the left.

Output the state of the card after punching holes according to Aoki's instructions.

Constraints

  • 1 \leq N \leq 1000
  • 1 \leq M \leq 1000
  • 1 \leq K \leq N \times M
  • 1 \leq R_i \leq N (1 \leq i \leq K)
  • 1 \leq C_i \leq M (1 \leq i \leq K)
  • (R_i, C_i) \neq (R_j, C_j) (i \neq j)
  • All inputs are integers

Input

N M K
R_1 C_1
R_2 C_2
:
R_K C_K
  • The first line contains N representing the number of rows of the card, M representing the number of columns, and K representing the number of locations to punch holes, separated by spaces.
  • From the 2nd line to the (K + 1)-th line, the positions to punch holes are given.
  • The (1 + i)-th line contains the row number R_i and column number C_i of the i-th hole position, separated by a space.

Output

Output the state of the card over N lines. The i-th line should contain a string of M characters, where the j-th character is # if there is no hole at coordinate (i, j), and . if there is a hole.


Sample Input 1

3 4 2
1 2
2 3

Sample Output 1

#.##
##.#
####

Sample Input 2

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

Sample Output 2

.###.
#.#.#
##.##
#.#.#
.####

Sample Input 3

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

Sample Output 3

##.###.###
.###.###.#
#.###.###.
###.###.##
.###.####.
##.###.###
B - 数の差がKのペア

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

配点 : 333 点

問題文

高橋君は N 個の整数からなる数列を持っています。数列の i 番目の要素は A_i です。

高橋君は、この数列から異なる 2 つの要素を選び、その差がちょうど K になるようなペアの数を数えたいと考えています。

N 個の要素の中から 2 つを選ぶ方法のうち、選んだ 2 つの要素の差の絶対値がちょうど K となるペアの数を求めてください。ただし、i 番目の要素と j 番目の要素のペアと、j 番目の要素と i 番目の要素のペアは同じペアとして数えます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、数列の要素数を表す N と、目標とする差を表す K が、スペース区切りで与えられる。
  • 2 行目には、数列の各要素 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

差の絶対値がちょうど K となるペアの数を 1 行で出力してください。


入力例 1

5 2
1 3 5 7 9

出力例 1

4

入力例 2

7 3
1 4 7 10 2 5 8

出力例 2

5

入力例 3

10 100
50 150 250 350 450 200 300 400 500 600

出力例 3

8

Score : 333 pts

Problem Statement

Takahashi has a sequence of N integers. The i-th element of the sequence is A_i.

Takahashi wants to count the number of pairs where he selects two different elements from this sequence such that their difference is exactly K.

Among all ways to select 2 elements from the N elements, find the number of pairs where the absolute difference of the two selected elements is exactly K. Note that a pair of the i-th element and the j-th element is counted as the same pair as the j-th element and the i-th element.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^9
  • All inputs are integers

Input

N K
A_1 A_2 \ldots A_N
  • The first line contains N, the number of elements in the sequence, and K, the target difference, separated by a space.
  • The second line contains each element of the sequence A_1, A_2, \ldots, A_N, separated by spaces.

Output

Print the number of pairs whose absolute difference is exactly K in one line.


Sample Input 1

5 2
1 3 5 7 9

Sample Output 1

4

Sample Input 2

7 3
1 4 7 10 2 5 8

Sample Output 2

5

Sample Input 3

10 100
50 150 250 350 450 200 300 400 500 600

Sample Output 3

8
C - スイッチパネル

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

配点 : 366 点

問題文

高橋君の家には N 個の電球があり、それぞれ 1 から N までの番号が付けられています。最初、すべての電球は消えています。

高橋君はスイッチパネルを持っています。このパネルには M 個のスイッチがあり、それぞれ 1 から M までの番号が付けられています。j 番目のスイッチは c_j 個の電球に影響します。具体的には、j 番目のスイッチを押すと、電球 s_{j,1}, s_{j,2}, \ldots, s_{j,c_j} の状態がすべて反転します(点いている電球は消え、消えている電球は点きます)。

高橋君は M 個のスイッチそれぞれについて、押すか押さないかを独立に選びます。各スイッチは高々 1 回しか押せません。

高橋君は、すべてのスイッチ操作が終わった後に点灯している電球の数をちょうど K 個にしたいと考えています。これが達成可能かどうかを判定し、達成可能な場合は押すスイッチの個数の最小値を求めてください。

制約

  • 1 \leq N \leq 23
  • 1 \leq M \leq 23
  • 0 \leq K \leq N
  • 1 \leq c_j \leq N (1 \leq j \leq M)
  • 1 \leq s_{j,k} \leq N (1 \leq j \leq M,\ 1 \leq k \leq c_j)
  • s_{j,1}, s_{j,2}, \ldots, s_{j,c_j} はすべて異なる (1 \leq j \leq M)
  • 入力はすべて整数である

入力

N M K
c_1 s_{1,1} s_{1,2} \ldots s_{1,c_1}
c_2 s_{2,1} s_{2,2} \ldots s_{2,c_2}
\vdots
c_M s_{M,1} s_{M,2} \ldots s_{M,c_M}
  • 1 行目には、電球の数を表す N 、スイッチの数を表す M 、点灯させたい電球の数を表す K が、スペース区切りで与えられる。
  • 続く M 行にわたって、各スイッチの情報が与えられる。
  • j + 1 行目 (1 \leq j \leq M) では、j 番目のスイッチが影響する電球の個数 c_j と、影響する電球の番号 s_{j,1}, s_{j,2}, \ldots, s_{j,c_j} がスペース区切りで与えられる。

出力

すべてのスイッチ操作が終わった後にちょうど K 個の電球を点灯させることが可能な場合、押すスイッチの個数の最小値を 1 行で出力せよ。不可能な場合は -1 を出力せよ。


入力例 1

3 3 2
2 1 2
2 2 3
1 1

出力例 1

1

入力例 2

2 2 1
2 1 2
2 1 2

出力例 2

-1

入力例 3

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

出力例 3

1

入力例 4

18 20 9
5 1 2 3 4 5
6 4 5 6 7 8 9
4 10 11 12 13
7 1 3 5 7 9 11 13
3 14 15 16
8 2 4 6 8 10 12 14 16
5 9 10 15 17 18
6 1 6 11 16 17 18
2 3 18
9 1 2 8 9 10 11 12 17 18
4 5 6 7 8
7 2 3 4 12 13 14 15
3 1 9 17
6 4 8 12 13 16 18
5 6 7 11 14 18
8 1 5 9 13 14 15 16 17
4 2 6 10 14
6 3 7 11 15 17 18
5 4 10 12 16 18
7 1 2 5 8 11 14 17

出力例 4

1

入力例 5

1 1 0
1 1

出力例 5

0

Score : 366 pts

Problem Statement

Takahashi's house has N light bulbs, numbered from 1 to N. Initially, all light bulbs are off.

Takahashi has a switch panel. This panel has M switches, numbered from 1 to M. The j-th switch affects c_j light bulbs. Specifically, pressing the j-th switch toggles the states of light bulbs s_{j,1}, s_{j,2}, \ldots, s_{j,c_j} (lit bulbs turn off, and unlit bulbs turn on).

For each of the M switches, Takahashi independently chooses whether to press it or not. Each switch can be pressed at most once.

Takahashi wants the number of lit light bulbs after all switch operations are completed to be exactly K. Determine whether this is achievable, and if so, find the minimum number of switches that need to be pressed.

Constraints

  • 1 \leq N \leq 23
  • 1 \leq M \leq 23
  • 0 \leq K \leq N
  • 1 \leq c_j \leq N (1 \leq j \leq M)
  • 1 \leq s_{j,k} \leq N (1 \leq j \leq M,\ 1 \leq k \leq c_j)
  • s_{j,1}, s_{j,2}, \ldots, s_{j,c_j} are all distinct (1 \leq j \leq M)
  • All input values are integers

Input

N M K
c_1 s_{1,1} s_{1,2} \ldots s_{1,c_1}
c_2 s_{2,1} s_{2,2} \ldots s_{2,c_2}
\vdots
c_M s_{M,1} s_{M,2} \ldots s_{M,c_M}
  • The first line contains N representing the number of light bulbs, M representing the number of switches, and K representing the desired number of lit bulbs, separated by spaces.
  • The following M lines provide information about each switch.
  • The (j + 1)-th line (1 \leq j \leq M) contains the number of light bulbs c_j affected by the j-th switch, followed by the numbers of the affected light bulbs s_{j,1}, s_{j,2}, \ldots, s_{j,c_j}, separated by spaces.

Output

If it is possible to have exactly K light bulbs lit after all switch operations are completed, output the minimum number of switches to press in one line. If it is impossible, output -1.


Sample Input 1

3 3 2
2 1 2
2 2 3
1 1

Sample Output 1

1

Sample Input 2

2 2 1
2 1 2
2 1 2

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

1

Sample Input 4

18 20 9
5 1 2 3 4 5
6 4 5 6 7 8 9
4 10 11 12 13
7 1 3 5 7 9 11 13
3 14 15 16
8 2 4 6 8 10 12 14 16
5 9 10 15 17 18
6 1 6 11 16 17 18
2 3 18
9 1 2 8 9 10 11 12 17 18
4 5 6 7 8
7 2 3 4 12 13 14 15
3 1 9 17
6 4 8 12 13 16 18
5 6 7 11 14 18
8 1 5 9 13 14 15 16 17
4 2 6 10 14
6 3 7 11 15 17 18
5 4 10 12 16 18
7 1 2 5 8 11 14 17

Sample Output 4

1

Sample Input 5

1 1 0
1 1

Sample Output 5

0
D - 領地の争奪戦

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

配点 : 400 点

問題文

高橋君と青木君は、王国の領地をめぐる争奪戦を行っています。

王国には N 個の都市があり、都市には 1 から N までの番号が付いています。都市同士は道路で結ばれており、全体として根付き木の構造をしています。都市 1 は王都であり、木の根にあたります。各都市 i(2 \leq i \leq N)の親都市は都市 P_i です。各都市 i(1 \leq i \leq N)には、その都市の資源価値を表す整数 V_i が定められています。資源価値は負であることもあり、その場合は占領すると損失が生じることを意味します。

争奪戦は「道路の封鎖」と「占領ゲーム」の 2 つのフェーズからなります。

フェーズ 1:道路の封鎖

占領ゲームの前に、青木君には特別な権限が与えられています。青木君は木の構造と各都市の資源価値をすべて見た上で、木に含まれる辺(道路)を ちょうど 1 本選んで封鎖するか、または 1 本も封鎖しないかを選べます。ただし、木に辺が存在しない場合(N = 1 の場合)は封鎖しない一択です。

道路を 1 本封鎖すると、木は 2 つの連結成分に分かれます。このとき、王都(都市 1)を含む連結成分のみが占領ゲームの対象となり、もう一方の連結成分に属する都市と道路はゲームから取り除かれます。ゲームの対象となる木の根は引き続き都市 1 であり、残った都市間の親子関係は元の木における親子関係がそのまま引き継がれます。道路を封鎖しない場合は、元の木全体がゲームの対象です。

フェーズ 2:占領ゲーム

道路の封鎖が行われた後、ゲームの対象となるすべての都市は未占領の状態から始まります。2 人は交互に手番を行い、高橋君が先手です。

ある都市が 占領可能 であるとは、以下の条件をともに満たすことをいいます。

  • その都市が現在未占領である。
  • その都市が王都(都市 1)であるか、またはその都市の親都市がすでに占領済みである。

ここで「占領済み」とは、高橋君・青木君のいずれかによって過去の手番で占領されたことを意味します。占領可能かどうかの判定において、その都市の親を占領したのが高橋君か青木君かは区別しません。王都は親を持たないため、未占領である限り常に占領可能です。

手番が回ってきたとき、占領可能な都市が 1 つ以上存在するならば、手番のプレイヤーはその中からちょうど 1 つを選んで占領しなければなりません(パスは認められません)。占領可能な都市が 1 つも存在しなくなった時点で、ゲームは直ちに終了します。

目的

ゲーム終了時、占領された都市全体の集合を S とします。

高橋君は資源価値の合計 \displaystyle\sum_{i \in S} V_i を最大化したく、青木君はこの同じ値を最小化したいです。

青木君はフェーズ 1 の道路の封鎖を最適に行い、フェーズ 2 の占領ゲームでも最適にプレイします。高橋君もフェーズ 2 で最適にプレイします。

両者がすべての選択を最適に行ったとき、ゲーム終了時の \displaystyle\sum_{i \in S} V_i の値を求めてください。

制約

  • 1 \leq N \leq 5 \times 10^5
  • -10^9 \leq V_i \leq 10^9
  • 1 \leq P_i < i (2 \leq i \leq N)
  • 入力はすべて整数である。

入力

N
V_1 V_2 \ldots V_N
P_2 P_3 \ldots P_N
  • 1 行目には、都市の数を表す整数 N が与えられる。
  • 2 行目には、各都市の資源価値を表す N 個の整数 V_1, V_2, \ldots, V_N がスペース区切りで与えられる。
  • 3 行目には、都市 2, 3, \ldots, N の親都市を表す N - 1 個の整数 P_2, P_3, \ldots, P_N がスペース区切りで与えられる。P_i は都市 i の親の都市番号である。N = 1 の場合、3 行目は存在しない(入力は 2 行のみである)。

出力

両者が最適にプレイしたときの、占領された都市の資源価値の合計を 1 行で出力せよ。


入力例 1

3
5 -2 4
1 1

出力例 1

3

入力例 2

5
3 8 -5 2 -10
1 2 2 3

出力例 2

-4

入力例 3

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

出力例 3

5

入力例 4

30
12 -7 25 -30 8 14 -5 19 -11 6 -3 21 -17 4 10 -9 13 -2 7 -15 18 -6 5 -12 9 -4 16 -20 11 -8
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

出力例 4

12

入力例 5

1
-1000000000

出力例 5

-1000000000

Score : 400 pts

Problem Statement

Takahashi and Aoki are engaged in a battle over the territory of a kingdom.

The kingdom consists of N cities, numbered 1 to N. The cities are connected by roads, forming a rooted tree structure as a whole. City 1 is the capital and serves as the root of the tree. For each city i (2 \leq i \leq N), its parent city is City P_i. Each city i (1 \leq i \leq N) has an integer V_i representing its resource value. The resource value can be negative, which means occupying it will result in a loss.

The battle consists of two phases: "Road Blockade" and "Occupation Game".

Phase 1: Road Blockade

Before the occupation game begins, Aoki is given a special authority. After looking at the tree structure and all resource values of the cities, Aoki can choose to block exactly one edge (road) in the tree, or block no edges at all. However, if there are no edges in the tree (i.e., when N = 1), he must choose not to block any.

If a road is blocked, the tree is split into two connected components. At this point, only the connected component containing the capital (City 1) will be used for the occupation game, and the cities and roads in the other connected component are removed from the game. The root of the tree used for the game remains City 1, and the parent-child relationships among the remaining cities are inherited directly from the original tree. If no road is blocked, the entire original tree is used for the game.

Phase 2: Occupation Game

After the road blockade phase, all cities in the game start in an unoccupied state. The two players take turns, with Takahashi going first.

A city is said to be occupiable if it satisfies both of the following conditions:

  • The city is currently unoccupied.
  • The city is the capital (City 1), or its parent city is already occupied.

Here, "occupied" means the city has been occupied by either Takahashi or Aoki in a previous turn. In determining whether a city is occupiable, it does not matter whether Takahashi or Aoki occupied its parent. Since the capital has no parent, it is always occupiable as long as it is unoccupied.

On a player's turn, if there is at least one occupiable city, the player must choose and occupy exactly one of them (passing is not allowed). The game ends immediately when there are no more occupiable cities.

Objective

Let S be the set of all occupied cities at the end of the game.

Takahashi wants to maximize the total resource value \displaystyle\sum_{i \in S} V_i, and Aoki wants to minimize this same value.

Aoki will optimally perform the road blockade in Phase 1 and play optimally in the Phase 2 occupation game. Takahashi will also play optimally in Phase 2.

Find the value of \displaystyle\sum_{i \in S} V_i at the end of the game when both players make all their choices optimally.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • -10^9 \leq V_i \leq 10^9
  • 1 \leq P_i < i (2 \leq i \leq N)
  • All input values are integers.

Input

N
V_1 V_2 \ldots V_N
P_2 P_3 \ldots P_N
  • The first line contains an integer N, representing the number of cities.
  • The second line contains N space-separated integers V_1, V_2, \ldots, V_N, representing the resource values of each city.
  • The third line contains N - 1 space-separated integers P_2, P_3, \ldots, P_N, representing the parent cities of cities 2, 3, \ldots, N. P_i is the parent city number of city i. If N = 1, the third line does not exist (the input has only two lines).

Output

Print the total resource value of the occupied cities when both players play optimally in a single line.


Sample Input 1

3
5 -2 4
1 1

Sample Output 1

3

Sample Input 2

5
3 8 -5 2 -10
1 2 2 3

Sample Output 2

-4

Sample Input 3

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

Sample Output 3

5

Sample Input 4

30
12 -7 25 -30 8 14 -5 19 -11 6 -3 21 -17 4 10 -9 13 -2 7 -15 18 -6 5 -12 9 -4 16 -20 11 -8
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

Sample Output 4

12

Sample Input 5

1
-1000000000

Sample Output 5

-1000000000
E - お菓子の選抜

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

配点 : 433 点

問題文

高橋君はお菓子屋さんで N 個のお菓子を見つけました。これらのお菓子には 1 から N までの番号がつけられており、i 番目のお菓子の満足度は A_i です。

高橋君は N 個のお菓子の中から重複なく K 個を選んで買おうと考えています。選んだ K 個のお菓子の満足度の合計が X 以上になるような選び方は何通りあるかを求めてください。

ただし、選ぶお菓子の組み合わせ(番号の集合)が異なる場合に「選び方が異なる」とみなします。

制約

  • 1 \leq N \leq 36
  • 1 \leq K \leq N
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq K \times 10^9
  • 入力はすべて整数

入力

入力は以下の形式で与えられます。

N K X
A_1 A_2 \ldots A_N

出力

満足度の合計が X 以上となる K 個のお菓子の選び方の総数を 1 行で出力してください。


入力例 1

4 2 7
2 4 5 7

出力例 1

5

入力例 2

5 3 20
1 2 3 4 5

出力例 2

0

入力例 3

16 7 100
3 8 12 17 5 21 9 14 6 25 11 19 4 16 23 7

出力例 3

2291

入力例 4

36 18 333
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

出力例 4

4594114161

入力例 5

1 1 1
1

出力例 5

1

Score : 433 pts

Problem Statement

Takahashi found N sweets at a candy store. These sweets are numbered 1 to N, and the satisfaction level of the i-th sweet is A_i.

Takahashi is considering choosing and buying K distinct sweets out of the N sweets. Find the number of ways to choose K sweets such that the total satisfaction level of the chosen sweets is at least X.

Two ways of choosing sweets are considered different if the set of chosen sweets' numbers (indices) is different.

Constraints

  • 1 \leq N \leq 36
  • 1 \leq K \leq N
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq K \times 10^9
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N K X
A_1 A_2 \ldots A_N

Output

Print the total number of ways to choose K sweets such that the total satisfaction level is at least X in a single line.


Sample Input 1

4 2 7
2 4 5 7

Sample Output 1

5

Sample Input 2

5 3 20
1 2 3 4 5

Sample Output 2

0

Sample Input 3

16 7 100
3 8 12 17 5 21 9 14 6 25 11 19 4 16 23 7

Sample Output 3

2291

Sample Input 4

36 18 333
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

Sample Output 4

4594114161

Sample Input 5

1 1 1
1

Sample Output 5

1