A - Octave

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

配点 : 100

問題文

音の高さが 1 オクターヴ上がるごとに、音の周波数は 2 倍になります。

周波数 X ヘルツの音の高さを Y オクターヴ上げると、その周波数は何ヘルツになりますか?

制約

  • 1 \leq X \leq 444
  • 1 \leq Y \leq 3
  • 入力される値はすべて整数

入力

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

X Y

出力

答えを整数として 1 行に出力せよ。単位 (ヘルツ) は省いて出力すること。


入力例 1

110 2

出力例 1

440

周波数 110 ヘルツの音の 2 オクターヴ上の音について、その周波数は 110 \times 2 \times 2 = 440 ヘルツです。


入力例 2

233 3

出力例 2

1864

周波数 233 ヘルツの音の 3 オクターヴ上の音について、その周波数は 233 \times 2 \times 2 \times 2 = 1864 ヘルツです。


入力例 3

432 1

出力例 3

864

周波数 432 ヘルツの音の 1 オクターヴ上の音について、その周波数は 432 \times 2 = 864 ヘルツです。

Score : 100 points

Problem Statement

The frequency of a sound doubles for every increase of 1 octave in pitch.

If the pitch of a sound with frequency X hertz is raised by Y octaves, what will its frequency be in hertz?

Constraints

  • 1 \leq X \leq 444
  • 1 \leq Y \leq 3
  • All input values are integers.

Input

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

X Y

Output

Output the answer as an integer in one line. Omit the unit (hertz).


Sample Input 1

110 2

Sample Output 1

440

For a sound 2 octaves above a sound with frequency 110 hertz, its frequency is 110 \times 2 \times 2 = 440 hertz.


Sample Input 2

233 3

Sample Output 2

1864

For a sound 3 octaves above a sound with frequency 233 hertz, its frequency is 233 \times 2 \times 2 \times 2 = 1864 hertz.


Sample Input 3

432 1

Sample Output 3

864

For a sound 1 octave above a sound with frequency 432 hertz, its frequency is 432 \times 2 = 864 hertz.

B - Trifecta

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

配点 : 200

問題文

1 から N の番号がついた N 頭の馬が競争をしました。

全ての馬は同時にスタートし、 i 番の馬はスタートからゴールまで T_i 秒かかりました。

1,2,3 着の馬の番号を求めてください。なお、 T_i は相異なることが保証されます。

制約

  • 3\leq N \leq 32
  • 1\leq T_i \leq 200
  • T_i は相異なる
  • 入力は全て整数

入力

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

N
T_1 \dots T_N

出力

1,2,3 着の馬の番号をそれぞれ空白区切りでこの順に出力せよ。


入力例 1

4
100 110 105 95

出力例 1

4 1 3

4,1,3,2 番の順にゴールしました。1,2,3 着の番号である 4,1,3 をこの順に空白区切りで出力してください。


入力例 2

8
72 74 69 70 73 75 71 77

出力例 2

3 4 7

Score : 200 points

Problem Statement

N horses numbered 1 to N had a race.

All horses started simultaneously, and horse i took T_i seconds from the start to the goal.

Find the numbers of the horses that finished in 1st, 2nd, and 3rd places. It is guaranteed that all T_i are distinct.

Constraints

  • 3\leq N \leq 32
  • 1\leq T_i \leq 200
  • All T_i are distinct.
  • All input values are integers.

Input

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

N
T_1 \dots T_N

Output

Output the numbers of the horses that finished in 1st, 2nd, and 3rd places, in this order, separated by spaces.


Sample Input 1

4
100 110 105 95

Sample Output 1

4 1 3

The horses finished in the order 4, 1, 3, 2. Output the numbers for 1st, 2nd, and 3rd places, which are 4, 1, 3, in this order, separated by spaces.


Sample Input 2

8
72 74 69 70 73 75 71 77

Sample Output 2

3 4 7
C - Striped Horse

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

配点 : 300

問題文

りんごさんは馬を縞模様に塗ってシマウマにしようとしています。

1 から N の番号がついた N 個のマスが一列に並んでいます。
最初、全てのマスは白色であり、マス i を黒く塗るためにはコストが C_i かかります。

以下の手順を一度だけ行い、いくつかのマスを黒く塗ることを考えます。

  • 正整数 x を自由に選ぶ。
  • 1 \leq i \leq N を満たす整数 i のうち、 (i+x)2W で割った余りが W より小さくなるもの全てに対し、マス i を黒く塗る。

この手順を行うためのコストの合計の最小値を求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\leq T \leq 2\times 10^5
  • 1\leq N \leq 2\times 10^5
  • 1\leq W \leq 2\times 10^5
  • 1\leq C_i \leq 10^9
  • T 個のテストケースにおける N の総和は 2\times 10^5 以下
  • T 個のテストケースにおける W の総和は 2\times 10^5 以下
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_ii 番目のテストケースを意味する。

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

N W
C_1 \dots C_N

出力

T 行出力せよ。i 行目には i 番目のテストケースに対する答えを出力せよ。


入力例 1

4
8 2
1 10 10 1 1 10 10 1
8 3
1 10 10 1 1 10 10 1
8 4
1 10 10 1 1 10 10 1
4 100
100000 100000 100000 100000

出力例 1

4
12
22
0

1 番目のテストケースにおいて、x=4 として手順を実行すると、マス 1,4,5,8 が黒く塗られ、コストの合計は 4 となります。 コストの合計を 4 未満にすることはできないため、これが最小値となります。

Score : 300 points

Problem Statement

Ringo-san is trying to paint a horse with stripes to make it a zebra.

There are N squares numbered 1 to N arranged in a line.
Initially, all squares are white, and the cost of painting square i black is C_i.

Consider performing the following procedure once to paint some squares black:

  • Choose a positive integer x freely.
  • Paint square i black for all integers i satisfying 1 \leq i \leq N such that the remainder when (i+x) is divided by 2W is less than W.

Find the minimum total cost to perform this procedure.

You are given T test cases; solve each of them.

Constraints

  • 1\leq T \leq 2\times 10^5
  • 1\leq N \leq 2\times 10^5
  • 1\leq W \leq 2\times 10^5
  • 1\leq C_i \leq 10^9
  • The sum of N over the T test cases is at most 2\times 10^5.
  • The sum of W over the T test cases is at most 2\times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case.

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N W
C_1 \dots C_N

Output

Output T lines. The i-th line should contain the answer for the i-th test case.


Sample Input 1

4
8 2
1 10 10 1 1 10 10 1
8 3
1 10 10 1 1 10 10 1
8 4
1 10 10 1 1 10 10 1
4 100
100000 100000 100000 100000

Sample Output 1

4
12
22
0

In the first test case, if the procedure is executed with x=4, squares 1,4,5,8 are painted black, and the total cost is 4. The total cost cannot be less than 4, so this is the minimum.

D - Forbidden List 2

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

配点 : 400

問題文

相異なる N 個の整数からなるリストがあります。リストの i 個目 (1 \leq i \leq N) の整数は A_i です。

Q 個の質問が与えられるので、それぞれの答えを求めてください。j 個目の質問 (1 \leq j \leq Q) は以下の通りです。

  • X_j 以上の整数であってリストに含まれないもののうち、小さいほうから Y_j 番目の値を求めよ。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq Q \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • A_1, \dots, A_N は相異なる
  • 1 \leq X_j \leq 10^9 (1 \leq j \leq Q)
  • 1 \leq Y_j \leq 10^9 (1 \leq j \leq Q)
  • 入力される値はすべて整数

入力

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

N Q
A_1 \cdots A_N
X_1 Y_1
\vdots
X_Q Y_Q

出力

Q 行出力せよ。j 行目 (1 \leq j \leq Q) には j 個目の質問の答えを出力せよ。


入力例 1

5 4
16 9 2 3 1
6 10
12 4
1 1
1000000000 1000000000

出力例 1

17
15
4
1999999999

1 個目の質問について、6 以上の整数であってリストに含まれないもののうち、小さいほうから 10 個を挙げると 6,\ 7,\ 8,\ 10,\ 11,\ 12,\ 13,\ 14,\ 15,\ 17 となります。したがって、質問の答えは 17 です。

2 個目の質問について、12 以上の整数であってリストに含まれないもののうち、小さいほうから 4 個を挙げると 12,\ 13,\ 14,\ 15 となります。したがって、質問の答えは 15 です。

3 個目の質問について、1 以上の整数であってリストに含まれないもののうち、小さいほうから 1 番目の値は 4 です。

4 個目の質問について、1\,000\,000\,000 以上の整数であってリストに含まれないもののうち、小さいほうから 1\,000\,000\,000 番目の値は 1\,999\,999\,999 です。


入力例 2

10 10
284008711 658403910 982178205 50598815 694147827 230009803 763277509 509451676 821970166 284008710
740250292 159734720
255870361 8400028
23659634 718117163
697334729 301140741
698853172 270344164
713418715 285312509
50065000 52368934
46642556 591869945
607623561 273664826
482426028 265015448

出力例 2

899985013
264270388
741776803
998475472
969197337
998731226
102433934
638512505
881288390
747441478

Score : 400 points

Problem Statement

There is a list consisting of N distinct integers. The i-th (1 \leq i \leq N) integer in the list is A_i.

You are given Q questions; answer each of them. The j-th question (1 \leq j \leq Q) is as follows:

  • Find the Y_j-th smallest value among the integers greater than or equal to X_j that are not in the list.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq Q \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • A_1, \dots, A_N are distinct.
  • 1 \leq X_j \leq 10^9 (1 \leq j \leq Q)
  • 1 \leq Y_j \leq 10^9 (1 \leq j \leq Q)
  • All input values are integers.

Input

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

N Q
A_1 \cdots A_N
X_1 Y_1
\vdots
X_Q Y_Q

Output

Output Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to the j-th question.


Sample Input 1

5 4
16 9 2 3 1
6 10
12 4
1 1
1000000000 1000000000

Sample Output 1

17
15
4
1999999999

For the first question, the smallest 10 integers greater than or equal to 6 that are not in the list are 6,\ 7,\ 8,\ 10,\ 11,\ 12,\ 13,\ 14,\ 15,\ 17. Thus, the answer to the question is 17.

For the second question, the smallest 4 integers greater than or equal to 12 that are not in the list are 12,\ 13,\ 14,\ 15. Thus, the answer to the question is 15.

For the third question, the 1st smallest value among the integers greater than or equal to 1 that are not in the list is 4.

For the fourth question, the 1\,000\,000\,000-th smallest value among the integers greater than or equal to 1\,000\,000\,000 that are not in the list is 1\,999\,999\,999.


Sample Input 2

10 10
284008711 658403910 982178205 50598815 694147827 230009803 763277509 509451676 821970166 284008710
740250292 159734720
255870361 8400028
23659634 718117163
697334729 301140741
698853172 270344164
713418715 285312509
50065000 52368934
46642556 591869945
607623561 273664826
482426028 265015448

Sample Output 2

899985013
264270388
741776803
998475472
969197337
998731226
102433934
638512505
881288390
747441478
E - Cookies

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

配点 : 450

問題文

N 種類のクッキーがそれぞれ 10^{100} 枚あります。 i 種類目のクッキーの 1 枚あたりの美味しさは A_i です。

これらのクッキーから合計で K 枚を選びます。クッキーの選び方は、選んだクッキーの種類の多重集合が一致するときかつその時に限り同じとみなします。

全ての選び方 \binom{N+K-1}{K} 通りそれぞれについて、選んだクッキーの美味しさの和を考えます。これらを大きな値から順に重複を込めて S_1,S_2,\dots とするとき、S_1,\dots,S_X を求めてください。

制約

  • 1\leq N \leq 50
  • 1 \leq K \leq 10^5
  • 1 \leq X \leq \min\left(10^5, \binom{N+K-1}{K}\right)
  • -10^9 \leq A_i \leq 10^9
  • 入力は全て整数

入力

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

N K X
A_1 \dots A_N

出力

K 枚選んだクッキーの美味しさの和として考えられる値を、大きなものから順に重複を込めて S_1,S_2,\dots とするとき、S_1,\dots,S_X をこの順に改行区切りで出力せよ。


入力例 1

2 4 3
20 10

出力例 1

80
70
60

クッキーを 4 枚選ぶ方法は、「 1 種類目を k 枚、 2 種類目を 4-k 枚」(0\leq k \leq 4)5 通りあり、選んだクッキーの美味しさの和はそれぞれ 80,70,60,50,40 となります。


入力例 2

3 100000 5
-1 -1 -1

出力例 2

-100000
-100000
-100000
-100000
-100000

異なるクッキーの選び方で美味しさの和が同じになることもあります。


入力例 3

9 14142 13
31 41 59 26 53 58 97 93 23

出力例 3

1371774
1371770
1371766
1371762
1371758
1371754
1371750
1371746
1371742
1371738
1371736
1371735
1371734

Score : 450 points

Problem Statement

There are 10^{100} cookies of each of N types. The deliciousness per cookie of the i-th type is A_i.

You will choose a total of K cookies from these. Two ways of choosing cookies are considered the same if and only if the multisets of types of chosen cookies match.

For each of the \binom{N+K-1}{K} ways of choosing, consider the sum of deliciousness of the chosen cookies. Let S_1,S_2,\dots be these sums in descending order, with duplicates included. Find S_1,\dots,S_X.

Constraints

  • 1\leq N \leq 50
  • 1 \leq K \leq 10^5
  • 1 \leq X \leq \min\left(10^5, \binom{N+K-1}{K}\right)
  • -10^9 \leq A_i \leq 10^9
  • All input values are integers.

Input

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

N K X
A_1 \dots A_N

Output

Let S_1,S_2,\dots be the possible sums of deliciousness of the chosen K cookies in descending order, with duplicates included. Output S_1,\dots,S_X in this order, separated by newlines.


Sample Input 1

2 4 3
20 10

Sample Output 1

80
70
60

There are 5 ways to choose 4 cookies: "k cookies of type 1 and 4-k cookies of type 2" (0\leq k \leq 4), where the sums of deliciousness of the chosen cookies are 80,70,60,50,40.


Sample Input 2

3 100000 5
-1 -1 -1

Sample Output 2

-100000
-100000
-100000
-100000
-100000

Different ways of choosing cookies may result in the same sum of deliciousness.


Sample Input 3

9 14142 13
31 41 59 26 53 58 97 93 23

Sample Output 3

1371774
1371770
1371766
1371762
1371758
1371754
1371750
1371746
1371742
1371738
1371736
1371735
1371734
F - Egoism

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

配点 : 550

問題文

AtCoder 牧場では馬がみずから水浴びをします。後片づけをする馬もいれば、散らかしたままにする馬もいます。

N 頭の馬がおり、1 から N までの番号が付けられています。馬 i (1 \leq i \leq N) は機嫌 A_i丁寧さ B_i をもちます(ここで、B_i1, 2 のいずれか)。

夜になると、N 頭の馬が 1 回ずつ水浴びをします。j 回目 (1 \leq j \leq N) に水浴びをする馬の番号を p_j とおくと、馬 p_j満足度が以下で与えられます。

  • j \geq 2 の場合、馬 p_j の機嫌と馬 p_{j-1} の丁寧さの積。
  • j = 1 の場合、馬 p_j の機嫌。

これから Q 日にわたって毎日 1 つずつクエリが与えられるので、順に処理してください。k 日目 (1 \leq k \leq Q) のクエリは以下の通りです。

  • W_k の機嫌を X_k に、丁寧さを Y_k に変更する(ここで、Y_k1, 2 のいずれか)。その後、その夜に N 頭の馬が水浴びをする順番を任意に決められるとき、その夜の N 頭の水浴びの満足度の総和が最大でいくらになるかを求める。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^6 (1 \leq i \leq N)
  • B_i1,2 のいずれか (1 \leq i \leq N)
  • 1 \leq W_k \leq N (1 \leq k \leq Q)
  • 1 \leq X_k \leq 10^6 (1 \leq k \leq Q)
  • Y_k1,2 のいずれか (1 \leq k \leq Q)
  • 入力される値はすべて整数

入力

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

N Q
A_1 B_1
\vdots
A_N B_N
W_1 X_1 Y_1
\vdots
W_Q X_Q Y_Q

出力

Q 行出力せよ。k 行目 (1 \leq k \leq Q) には k 日目のクエリの答えを出力せよ。


入力例 1

4 4
1 2
10 1
3 1
7 2
2 7 1
1 3 1
2 2 1
3 1000000 2

出力例 1

32
27
18
2000015

1 日目のクエリにおいて、馬 3,1,4,2 の順に水浴びすることで、各馬の満足度は以下のようになります。

  • 3 の満足度は 3
  • 1 の満足度は 1 \times 1 = 1
  • 4 の満足度は 7 \times 2 = 14
  • 2 の満足度は 7 \times 2 = 14

このときの満足度の総和は 32 です。

2 日目のクエリにおいて、馬 4,2,1,3 の順に水浴びすることで、各馬の満足度は以下のようになります。

  • 4 の満足度は 7
  • 2 の満足度は 7 \times 2 = 14
  • 1 の満足度は 3 \times 1 = 3
  • 3 の満足度は 3 \times 1 = 3

このときの満足度の総和は 27 です。

3 日目のクエリにおいて、馬 4,3,2,1 の順に水浴びすることで、満足度の総和は 18 になります。

4 日目のクエリにおいて、馬 4,3,1,2 の順に水浴びすることで、満足度の総和は 2000015 になります。


入力例 2

10 10
340019 2
598908 1
177330 2
575439 2
916653 2
451275 2
762769 2
36625 2
273231 2
367619 2
8 334230 2
8 835378 1
4 777076 2
6 61501 1
4 516395 1
4 35678 2
5 751493 1
3 815798 1
2 369777 2
5 941470 2

出力例 2

9417616
10146681
10549955
9708906
8847525
8463663
7860112
8606740
8516097
9236070

Score : 550 points

Problem Statement

At AtCoder Ranch, horses bathe themselves. Some horses clean up afterward, while others leave a mess.

There are N horses, numbered 1 to N. Horse i (1 \leq i \leq N) has a mood A_i and a tidiness B_i (where B_i is 1 or 2).

At night, the N horses bathe once each. Let p_j be the number of the horse that bathes in the j-th turn (1 \leq j \leq N). The satisfaction of horse p_j is given as follows:

  • If j \geq 2, the product of the mood of horse p_j and the tidiness of horse p_{j-1}.
  • If j = 1, the mood of horse p_j.

You will be given Q queries, one for each day over Q days. Process them in order. The query for the k-th day (1 \leq k \leq Q) is as follows:

  • Change the mood of horse W_k to X_k and its tidiness to Y_k (where Y_k is 1 or 2). Then, find the maximum possible total satisfaction of the N horses' baths that night if you can decide the order in which the N horses bathe arbitrarily.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^6 (1 \leq i \leq N)
  • B_i is 1 or 2. (1 \leq i \leq N)
  • 1 \leq W_k \leq N (1 \leq k \leq Q)
  • 1 \leq X_k \leq 10^6 (1 \leq k \leq Q)
  • Y_k is 1 or 2. (1 \leq k \leq Q)
  • All input values are integers.

Input

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

N Q
A_1 B_1
\vdots
A_N B_N
W_1 X_1 Y_1
\vdots
W_Q X_Q Y_Q

Output

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


Sample Input 1

4 4
1 2
10 1
3 1
7 2
2 7 1
1 3 1
2 2 1
3 1000000 2

Sample Output 1

32
27
18
2000015

For the query on the first day, if the horses bathe in the order 3,1,4,2, the satisfaction of each horse is as follows:

  • Horse 3's satisfaction is 3
  • Horse 1's satisfaction is 1 \times 1 = 1
  • Horse 4's satisfaction is 7 \times 2 = 14
  • Horse 2's satisfaction is 7 \times 2 = 14

The total satisfaction in this case is 32.

For the query on the second day, if the horses bathe in the order 4,2,1,3, the satisfaction of each horse is as follows:

  • Horse 4's satisfaction is 7
  • Horse 2's satisfaction is 7 \times 2 = 14
  • Horse 1's satisfaction is 3 \times 1 = 3
  • Horse 3's satisfaction is 3 \times 1 = 3

The total satisfaction in this case is 27.

For the query on the third day, if the horses bathe in the order 4,3,2,1, the total satisfaction is 18.

For the query on the fourth day, if the horses bathe in the order 4,3,1,2, the total satisfaction is 2000015.


Sample Input 2

10 10
340019 2
598908 1
177330 2
575439 2
916653 2
451275 2
762769 2
36625 2
273231 2
367619 2
8 334230 2
8 835378 1
4 777076 2
6 61501 1
4 516395 1
4 35678 2
5 751493 1
3 815798 1
2 369777 2
5 941470 2

Sample Output 2

9417616
10146681
10549955
9708906
8847525
8463663
7860112
8606740
8516097
9236070
G - Haunted House

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

配点 : 600

問題文

ハロウィンの季節がやってきました。あなたは肝試しとして、お化け屋敷に挑戦しようとしています。

F 階だてのお化け屋敷があり、各階は HW 列のグリッドで表されます。k 階を表すグリッドの上から i 行目・左から j 列目のマスをマス (k,i,j) とします。

マス (k,i,j) の内容は文字 S_{k,i,j} によって、以下のように表されます。

  • S_{k,i,j} が数字 (0 - 9) の場合、マス (k,i,j) は空きマスであり、数字が表す 1 桁の整数と同じ枚数のコインがある。
  • S_{k,i,j}# の場合、マス (k,i,j) は壁マスである。

空きマス (k,i,j) からほかのマス (k', i', j') に直接移動できる条件は、マス (k', i', j') が空きマスであり、かつ以下のいずれかを満たすことです。

  • k' = k および |i' - i| + |j' - j| = 1 を満たす。
  • (k', i', j') = (k+1, i, j) を満たす。
  • (k', i', j') = (k-1, i, j) を満たし、かつマス (k,i,j)ハシゴが置かれている。

グリッドの外部に移動することはできません。

Q 個の質問が与えられるので、それぞれの答えを求めてください。i 個目の質問 (1 \leq i \leq Q) は以下の通りです。

  • 好きな空きマスを 1 つだけ選んでハシゴを置いてから、マス (G_i, A_i, B_i) を開始地点として移動を繰り返したとき、訪れたマスにあるコインの合計枚数の最大値を求めよ。

なお、各質問は独立です。つまり、ある質問で置いたハシゴは、ほかの質問に影響を与えません。

制約

  • F,H,W は整数
  • 1 \leq F \leq 10
  • 1 \leq H,W \leq 500
  • S_{k,i,j} は数字 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) または # のいずれか (1 \leq k \leq F, 1 \leq i \leq H, 1 \leq j \leq W)
  • Q は整数
  • 1 \leq Q \leq 10^5
  • G_i, A_i, B_i は整数 (1 \leq i \leq Q)
  • 1 \leq G_i \leq F (1 \leq i \leq Q)
  • 1 \leq A_i \leq H (1 \leq i \leq Q)
  • 1 \leq B_i \leq W (1 \leq i \leq Q)
  • S_{G_i, A_i, B_i}# でない (1 \leq i \leq Q)

入力

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

F H W
S_{1,1,1} S_{1,1,2} \cdots S_{1,1,W}
S_{1,2,1} S_{1,2,2} \cdots S_{1,2,W}
\vdots
S_{1,H,1} S_{1,H,2} \cdots S_{1,H,W}
S_{2,1,1} S_{2,1,2} \cdots S_{2,1,W}
S_{2,2,1} S_{2,2,2} \cdots S_{2,2,W}
\vdots
S_{2,H,1} S_{2,H,2} \cdots S_{2,H,W}
\vdots
S_{F,1,1} S_{F,1,2} \cdots S_{F,1,W}
S_{F,2,1} S_{F,2,2} \cdots S_{F,2,W}
\vdots
S_{F,H,1} S_{F,H,2} \cdots S_{F,H,W}
Q
G_1 A_1 B_1
G_2 A_2 B_2
\vdots
G_Q A_Q B_Q

出力

Q 行出力せよ。i 行目 (1 \leq i \leq Q) には i 個目の質問の答えを出力せよ。


入力例 1

3 5 6
######
##5###
#1#11#
#1#11#
######
######
##313#
#4##1#
#242##
######
######
#0####
##4###
###99#
######
3
1 2 3
2 3 2
3 2 2

出力例 1

47
34
0

1 番目の質問において、マス (2,3,5) にハシゴを置いた状態で以下のように移動することで、訪れたマスにあるコインの合計枚数を 47 にすることができます。

  1. お化け屋敷の 1 階において、マス (1,2,3) から開始する。
  2. お化け屋敷の 2 階に上がり、マス (2,2,3) \to (2,2,4) \to (2,2,5) \to (2,3,5) の順に移動する。
  3. お化け屋敷の 1 階に降りて、マス (1,3,5) \to (1,4,5) \to (1,4,4) \to (1,3,4) \to (1,4,4) の順に移動する。
  4. お化け屋敷の 2 階に上がり、マス (2,4,4) \to (2,4,3) \to (2,4,2) \to (2,3,2) \to (2,4,2) \to (2,4,3) \to (2,4,4) の順に移動する。
  5. お化け屋敷の 3 階に上がり、マス (3,4,4) \to (3,4,5) の順に移動する。

2 番目の質問において、マス (2,4,4) にハシゴを置いた状態で以下のように移動することで、訪れたマスにあるコインの合計枚数を 34 にすることができます。

  1. お化け屋敷の 2 階において、マス (2,3,2) \to (2,4,2) \to (2,4,3) \to (2,4,4) の順に移動する。
  2. お化け屋敷の 1 階に降りて、マス (1,4,4) \to (1,4,5) \to (1,3,5) \to (1,3,4) \to (1,4,4) の順に移動する。
  3. お化け屋敷の 2 階に上がり、マス (2,4,4) にいる。
  4. お化け屋敷の 3 階に上がり、マス (3,4,4) \to (3,4,5) の順に移動する。

3 番目の質問において、どのようにハシゴを置いても開始地点のマス (3,2,2) から移動することはできません。


入力例 2

3 6 30
31#990#7###71298#6#9####3##04#
###66###6######7#4#3#1079239#6
891###8#73#7#5838#589#225#1282
#####5#306#9#38#50##6#71##8658
6#68#4#5###30###3#5716987#####
06#8####3#####134#5##2##28#169
#5#11####0#75#8######28#6#904#
85#71####0##9#7#0##8#2##00####
#1#09#3772#838#67#6##261###0#4
#4##78###7#########9#9#488##08
43#1##4632##46714436##849#77#6
8#83#61##4#14476#9#1##72#3####
5#99####52##6#9####9#4#4132#3#
##910#27529#69##491###0#06#9##
0#60845#02##28##610#2####25#6#
#229156374##47#30907#60#####28
#0#810#54###21##1#3###8#5917##
###6#56#60##04#73###6255###2##
10
3 4 23
3 2 4
1 4 14
2 3 12
1 1 16
1 2 9
3 5 6
1 3 19
2 2 4
3 4 9

出力例 2

136
217
474
255
474
285
217
451
251
217

Score : 600 points

Problem Statement

Halloween season has arrived. As a test of courage, you are about to challenge a haunted house.

There is a haunted house with F floors, and each floor is represented by an H-row by W-column grid. Let cell (k,i,j) be the cell at the i-th row from the top and j-th column from the left of the grid representing floor k.

The content of cell (k,i,j) is represented by the character S_{k,i,j} as follows:

  • If S_{k,i,j} is a digit (0 - 9), cell (k,i,j) is an empty cell with a number of coins equal to the single-digit integer represented by the digit.
  • If S_{k,i,j} is #, cell (k,i,j) is a wall cell.

You can move directly from an empty cell (k,i,j) to another cell (k', i', j') if cell (k', i', j') is an empty cell and one of the following is satisfied:

  • k' = k and |i' - i| + |j' - j| = 1 are satisfied.
  • (k', i', j') = (k+1, i, j) is satisfied.
  • (k', i', j') = (k-1, i, j) is satisfied, and a ladder is placed at cell (k,i,j).

You cannot move outside the grid.

You are given Q questions; answer each of them. The i-th question (1 \leq i \leq Q) is as follows:

  • Find the maximum total number of coins in the cells you visit if you choose exactly one empty cell and place a ladder there, start at cell (G_i, A_i, B_i), and repeatedly move.

Each question is independent. That is, a ladder placed for one question does not affect other questions.

Constraints

  • F,H,W are integers.
  • 1 \leq F \leq 10
  • 1 \leq H,W \leq 500
  • S_{k,i,j} is either a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) or #. (1 \leq k \leq F, 1 \leq i \leq H, 1 \leq j \leq W)
  • Q is an integer.
  • 1 \leq Q \leq 10^5
  • G_i, A_i, B_i are integers. (1 \leq i \leq Q)
  • 1 \leq G_i \leq F (1 \leq i \leq Q)
  • 1 \leq A_i \leq H (1 \leq i \leq Q)
  • 1 \leq B_i \leq W (1 \leq i \leq Q)
  • S_{G_i, A_i, B_i} is not #. (1 \leq i \leq Q)

Input

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

F H W
S_{1,1,1} S_{1,1,2} \cdots S_{1,1,W}
S_{1,2,1} S_{1,2,2} \cdots S_{1,2,W}
\vdots
S_{1,H,1} S_{1,H,2} \cdots S_{1,H,W}
S_{2,1,1} S_{2,1,2} \cdots S_{2,1,W}
S_{2,2,1} S_{2,2,2} \cdots S_{2,2,W}
\vdots
S_{2,H,1} S_{2,H,2} \cdots S_{2,H,W}
\vdots
S_{F,1,1} S_{F,1,2} \cdots S_{F,1,W}
S_{F,2,1} S_{F,2,2} \cdots S_{F,2,W}
\vdots
S_{F,H,1} S_{F,H,2} \cdots S_{F,H,W}
Q
G_1 A_1 B_1
G_2 A_2 B_2
\vdots
G_Q A_Q B_Q

Output

Output Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th question.


Sample Input 1

3 5 6
######
##5###
#1#11#
#1#11#
######
######
##313#
#4##1#
#242##
######
######
#0####
##4###
###99#
######
3
1 2 3
2 3 2
3 2 2

Sample Output 1

47
34
0

For the first question, by placing a ladder at cell (2,3,5) and moving as follows, you can make the total number of coins in the visited cells 47:

  1. Start at cell (1,2,3) on the first floor.
  2. Go up to the second floor and move in the order (2,2,3) \to (2,2,4) \to (2,2,5) \to (2,3,5).
  3. Go down to the first floor and move in the order (1,3,5) \to (1,4,5) \to (1,4,4) \to (1,3,4) \to (1,4,4).
  4. Go up to the second floor and move in the order (2,4,4) \to (2,4,3) \to (2,4,2) \to (2,3,2) \to (2,4,2) \to (2,4,3) \to (2,4,4).
  5. Go up to the third floor and move in the order (3,4,4) \to (3,4,5).

For the second question, by placing a ladder at cell (2,4,4) and moving as follows, you can make the total number of coins in the visited cells 34:

  1. Move in the order (2,3,2) \to (2,4,2) \to (2,4,3) \to (2,4,4) on the second floor.
  2. Go down to the first floor and move in the order (1,4,4) \to (1,4,5) \to (1,3,5) \to (1,3,4) \to (1,4,4).
  3. Go up to the second floor and be at cell (2,4,4).
  4. Go up to the third floor and move in the order (3,4,4) \to (3,4,5).

For the third question, no matter how you place the ladder, you cannot move from the starting cell (3,2,2).


Sample Input 2

3 6 30
31#990#7###71298#6#9####3##04#
###66###6######7#4#3#1079239#6
891###8#73#7#5838#589#225#1282
#####5#306#9#38#50##6#71##8658
6#68#4#5###30###3#5716987#####
06#8####3#####134#5##2##28#169
#5#11####0#75#8######28#6#904#
85#71####0##9#7#0##8#2##00####
#1#09#3772#838#67#6##261###0#4
#4##78###7#########9#9#488##08
43#1##4632##46714436##849#77#6
8#83#61##4#14476#9#1##72#3####
5#99####52##6#9####9#4#4132#3#
##910#27529#69##491###0#06#9##
0#60845#02##28##610#2####25#6#
#229156374##47#30907#60#####28
#0#810#54###21##1#3###8#5917##
###6#56#60##04#73###6255###2##
10
3 4 23
3 2 4
1 4 14
2 3 12
1 1 16
1 2 9
3 5 6
1 3 19
2 2 4
3 4 9

Sample Output 2

136
217
474
255
474
285
217
451
251
217