A - Job Interview

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君はある会社の採用面接を受けました。

面接官の人数 N と、各面接官の高橋君への評価を表す長さ N の文字列 S が与えられます。
i=1,2,\ldots,N に対し Si 文字目が i 番目の面接官の評価に対応し、o は「良」、- は「可」、x は 「不可」を表します。

高橋君は以下の 2 つの条件を両方満たすならば合格、そうでなければ不合格です。

  • 「良」と評価した面接官が少なくとも 1 人いる
  • 「不可」と評価した面接官がいない

高橋君が合格かどうかを判定してください。

制約

  • 1 \leq N \leq 100
  • So, -, x のみからなる長さが N の文字列

入力

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

N
S

出力

高橋君が合格ならば Yes と、そうでなければ No と出力せよ。


入力例 1

4
oo--

出力例 1

Yes

1, 2 番目の面接官が「良」と評価していて、さらに「不可」と評価した面接官がいないため合格です。


入力例 2

3
---

出力例 2

No

「良」と評価した面接官が 1 人もいないため不合格です。


入力例 3

1
o

出力例 3

Yes

入力例 4

100
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooox

出力例 4

No

100 番目の面接官が「不可」と評価しているため不合格です。

Score : 100 points

Problem Statement

Takahashi had a job interview.

You are given the number of interviewers, N, and a string S of length N representing the interviewers' evaluations of him.
For each i=1,2,\ldots,N, the i-th character of S corresponds to the i-th interviewer's evaluation; o means Good, - means Fair, and x means Poor.

Takahashi will pass if both of the following conditions are satisfied, and fail otherwise.

  • At least one interviewer's evaluation is Good.
  • No interviewer's evaluation is Poor.

Determine whether Takahashi passes.

Constraints

  • 1 \leq N \leq 100
  • S is a string of length N consisting of o, -, and x.

Input

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

N
S

Output

If Takahashi passes, print Yes; otherwise, print No.


Sample Input 1

4
oo--

Sample Output 1

Yes

The first and second interviewers' evaluations are Good, and no interviewer's evaluation is Poor, so he passes.


Sample Input 2

3
---

Sample Output 2

No

No interviewer's evaluation is Good, so he fails.


Sample Input 3

1
o

Sample Output 3

Yes

Sample Input 4

100
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooox

Sample Output 4

No

The 100-th interviewer's evaluation is Poor, so he fails.

B - Coloring Matrix

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

各要素が 0 あるいは 1 である NN 列の行列 A, B が与えられます。
Ai 行目 j 列目の要素を A_{i,j}Bi 行目 j 列目の要素を B_{i,j} で表します。
A を適切に回転することで、 A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているようにできるか判定してください。
ただし、A を回転するとは、以下の操作を好きな回数(0 回でもよい)繰り返すことをいいます。

  • 1 \leq i, j \leq N を満たすすべての整数の組 (i, j) について同時に A_{i,j}A_{N + 1 - j,i} で置き換える

制約

  • 1 \leq N \leq 100
  • A, B の各要素は 01 のいずれか
  • 入力はすべて整数

入力

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
B_{1,1} B_{1,2} \ldots B_{1,N}
\vdots
B_{N,1} B_{N,2} \ldots B_{N,N}

出力

A を適切に回転することで、A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているようにできる場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

3
0 1 1
1 0 0
0 1 0
1 1 0
0 0 1
1 1 1

出力例 1

Yes

はじめ、A

0 1 1
1 0 0
0 1 0

です。
1 回操作を行うと、A

0 1 0
1 0 1 
0 0 1

となります。
もう 1 度操作を行うと、A

0 1 0
0 0 1
1 1 0

となります。
このとき、A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているので、Yes を出力します。


入力例 2

2
0 0
0 0
1 1
1 1

出力例 2

Yes

入力例 3

5
0 0 1 1 0
1 0 0 1 0
0 0 1 0 1
0 1 0 1 0
0 1 0 0 1
1 1 0 0 1
0 1 1 1 0
0 0 1 1 1
1 0 1 0 1
1 1 0 1 0

出力例 3

No

Score : 200 points

Problem Statement

You are given N-by-N matrices A and B where each element is 0 or 1.
Let A_{i,j} and B_{i,j} denote the element at the i-th row and j-th column of A and B, respectively.
Determine whether it is possible to rotate A so that B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1.
Here, to rotate A is to perform the following operation zero or more times:

  • for every pair of integers (i, j) such that 1 \leq i, j \leq N, simultaneously replace A_{i,j} with A_{N + 1 - j,i}.

Constraints

  • 1 \leq N \leq 100
  • Each element of A and B is 0 or 1.
  • All values in the input are integers.

Input

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
B_{1,1} B_{1,2} \ldots B_{1,N}
\vdots
B_{N,1} B_{N,2} \ldots B_{N,N}

Output

If it is possible to rotate A so that B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1, print Yes; otherwise, print No.


Sample Input 1

3
0 1 1
1 0 0
0 1 0
1 1 0
0 0 1
1 1 1

Sample Output 1

Yes

Initially, A is :

0 1 1
1 0 0
0 1 0

After performing the operation once, A is :

0 1 0
1 0 1 
0 0 1

After performing the operation once again, A is :

0 1 0
0 0 1
1 1 0

Here, B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1, so you should print Yes.


Sample Input 2

2
0 0
0 0
1 1
1 1

Sample Output 2

Yes

Sample Input 3

5
0 0 1 1 0
1 0 0 1 0
0 0 1 0 1
0 1 0 1 0
0 1 0 0 1
1 1 0 0 1
0 1 1 1 0
0 0 1 1 1
1 0 1 0 1
1 1 0 1 0

Sample Output 3

No
C - Cards Query Problem

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

1 から N までの番号がついた N 個の空の箱と、何も書かれていない無数のカードがあります。
Q 個のクエリが与えられるので、順番に処理してください。クエリは次の 3 種類のいずれかです。

  • 1 i j \colon カードを 1 枚選んで数 i を書き込み、そのカードを箱 j に入れる
  • 2 i \coloni に入っているカードに書かれた数を昇順ですべて答える
  • 3 i \coloni が書かれたカードが入っている箱の番号を昇順ですべて答える

ただし、以下の点に注意してください。

  • 2 番目の形式のクエリにおいては、箱 i の中に同じ数が書かれたカードが複数枚あるときは、入っている枚数と同じ回数だけその数を出力する
  • 3 番目の形式のクエリにおいては、数 i が書かれたカードが同じ箱に複数枚入っている場合でも、その箱の番号は 1 度だけ出力する

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • 2 番目の形式のクエリについて、
    • 1 \leq i \leq N
    • このクエリが与えられる時点で箱 i にカードが入っている
  • 3 番目の形式のクエリについて、
    • 1 \leq i \leq 2 \times 10^5
    • このクエリが与えられる時点で数 i が書かれたカードが入っている箱が存在する
  • 出力するべき数は合計で 2 \times 10^5 個以下
  • 入力はすべて整数

入力

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

N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

ただし、\mathrm{query}_qq 個目のクエリを表しており、次のいずれかの形式で与えられる。

1 i j
2 i
3 i

出力

2 番目の形式のクエリと 3 番目の形式のクエリに対し、順番に答えを出力せよ。
各クエリでは出力するべき要素を昇順に空白区切りで出力し、クエリごとに改行せよ。


入力例 1

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

出力例 1

1 2
1 1 2
1 4
4

クエリを順に処理していきます。

  • カードに 1 を書き込み、箱 1 に入れます。
  • カードに 2 を書き込み、箱 4 に入れます。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 4 に入っているカードに書かれた数は 1, 2 です。
    • 1, 2 の順に出力してください。
  • カードに 1 を書き込み、箱 4 に入れます。
  • 4 に入っているカードに書かれた数は 1, 1, 2 です。
    • 12 度出力することに注意してください。
  • 1 が書かれたカードが入っている箱は箱 1, 4 です。
    • 4 には数 1 が書かれたカードが 2 枚入っていますが、41 回しか出力しないことに注意してください。
  • 2 が書かれたカードが入っている箱は箱 4 です。

入力例 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

出力例 2

1 2 200000
1

Score : 300 points

Problem Statement

We have N boxes numbered 1 to N that are initially empty, and an unlimited number of blank cards.
Process Q queries in order. There are three kinds of queries as follows.

  • 1 i j \colon Write the number i on a blank card and put it into box j.
  • 2 i \colon Report all numbers written on the cards in box i, in ascending order.
  • 3 i \colon Report all box numbers of the boxes that contain a card with the number i, in ascending order.

Here, note the following.

  • In a query of the second kind, if box i contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
  • In a query of the third kind, even if a box contains multiple cards with the number i, the box number of that box should be printed only once.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • For a query of the first kind:
    • 1 \leq i \leq 2 \times 10^5
    • 1 \leq j \leq N
  • For a query of the second kind:
    • 1 \leq i \leq N
    • Box i contains some cards when this query is given.
  • For a query of the third kind:
    • 1 \leq i \leq 2 \times 10^5
    • Some boxes contain a card with the number i when this query is given.
  • At most 2 \times 10^5 numbers are to be reported.
  • All values in the input are integers.

Input

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

N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Here, \mathrm{query}_q denotes the q-th query, which is in one of the following formats:

1 i j
2 i
3 i

Output

Respond to the queries of the second and third kinds in order.
For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.


Sample Input 1

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

Sample Output 1

1 2
1 1 2
1 4
4

Let us process the queries in order.

  • Write 1 on a card and put it into box 1.
  • Write 2 on a card and put it into box 4.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1 and 2.
    • Print 1 and 2 in this order.
  • Write 1 on a card and put it into box 4.
  • Box 4 contains cards with the numbers 1, 1, and 2.
    • Note that you should print 1 twice.
  • Boxes 1 and 4 contain a card with the number 1.
    • Note that you should print 4 only once, even though box 4 contains two cards with the number 1.
  • Boxes 4 contains a card with the number 2.

Sample Input 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

Sample Output 2

1 2 200000
1
D - Writing a Numeral

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 400

問題文

文字列 S があり、初め S= 1 です。
以下の形式のクエリが Q 個与えられるので順に処理してください。

  • 1 x : S の末尾に数字 x を追加する
  • 2 : S の先頭の数字を削除する
  • 3 : S を十進数表記の数とみなした値を 998244353 で割った余りを出力する

制約

  • 1 \leq Q \leq 6 \times 10^5
  • 1 番目の形式のクエリについて、x \in \{1,2,3,4,5,6,7,8,9\}
  • 2 番目の形式のクエリは S2 文字以上の時にのみ与えられる
  • 3 番目の形式のクエリが 1 個以上存在する

入力

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

Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

ただし \mathrm{query}_ii 番目のクエリを表し、以下のいずれかの形式である。

1 x
2
3

出力

3 番目の形式のクエリの個数を q として、q 行出力せよ。i (1 \leq i \leq q) 行目には i 番目の 3 番目の形式のクエリに対する出力をせよ。


入力例 1

3
3
1 2
3

出力例 1

1
12

1 番目のクエリにおいて、S1 なので ( 1998244353 で割った余りに等しい) 1 を出力します。
2 番目のクエリにおいて、S12 になります。
3 番目のクエリにおいて、S12 なので ( 12998244353 で割った余りに等しい) 12 を出力します。


入力例 2

3
1 5
2
3

出力例 2

5

入力例 3

11
1 9
1 9
1 8
1 2
1 4
1 4
1 3
1 5
1 3
2
3

出力例 3

0

出力されるべき値は 998244353 で割った余りであることに注意してください。

Score : 400 points

Problem Statement

We have a string S. Initially, S= 1.
Process Q queries in the following formats in order.

  • 1 x : Append a digit x at the end of S.
  • 2 : Delete the digit at the beginning of S.
  • 3 : Print the number represented by S in decimal, modulo 998244353.

Constraints

  • 1 \leq Q \leq 6 \times 10^5
  • For each query in the first format, x \in \{1,2,3,4,5,6,7,8,9\}.
  • A query in the second format is given only if S has a length of 2 or greater.
  • There is at least one query in the third format.

Input

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

Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

Here, \mathrm{query}_i denotes the i-th query, which is in one of the following formats:

1 x
2
3

Output

Print q lines, where q is the number of queries in the third format. The i-th line (1 \leq i \leq q) should correspond to the i-th query in the third format.


Sample Input 1

3
3
1 2
3

Sample Output 1

1
12

In the first query, S is 1, so you should print 1 modulo 998244353, that is, 1.
In the second query, S becomes 12.
In the third query, S is 12, so you should print 12 modulo 998244353, that is, 12.


Sample Input 2

3
1 5
2
3

Sample Output 2

5

Sample Input 3

11
1 9
1 9
1 8
1 2
1 4
1 4
1 3
1 5
1 3
2
3

Sample Output 3

0

Be sure to print numbers modulo 998244353.

E - Unfair Sugoroku

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

高橋君と青木君がすごろくをします。
高橋君ははじめ地点 A、青木君ははじめ地点 B にいて、交互にサイコロを振ります。
高橋君が振るサイコロは 1, 2, \ldots, P の出目が一様ランダムに出るサイコロで、青木君が振るサイコロは 1, 2, \ldots, Q の出目が一様ランダムに出るサイコロです。
地点 x にいるときに自分の振ったサイコロの出目が i であるとき、地点 \min(x + i, N) に進みます。
地点 N に先に着いた人をすごろくの勝者とします。
高橋君が先にサイコロを振るとき、高橋君が勝つ確率を \text{mod }998244353 で求めてください。

確率 \text{mod }998244353 とは この問題で求める確率は必ず有理数になることが証明できます。また、この問題の制約下では、求める確率を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。
このとき xz \equiv y \pmod {998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

  • 2 \leq N \leq 100
  • 1 \leq A, B < N
  • 1 \leq P, Q \leq 10
  • 入力はすべて整数

入力

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

N A B P Q

出力

答えを出力せよ。


入力例 1

4 2 3 3 2

出力例 1

665496236

高橋君が最初の手番で 2 あるいは 3 の出目を出すと、高橋君は地点 4 に進んで高橋君が勝利します。
高橋君が最初の手番で 1 の出目を出すと、高橋君は地点 3 に進み、青木君は次の手番で必ず地点 4 に進んで青木君が勝利します。
よって、高橋君が勝つ確率は \frac{2}{3} です。


入力例 2

6 4 2 1 1

出力例 2

1

サイコロの出目は常に 1 です。
このとき高橋君が地点 5 に進み、次いで青木君が地点 3 に進み、次いで高橋君が地点 6 に進むので、高橋君は必ず勝ちます。


入力例 3

100 1 1 10 10

出力例 3

264077814

Score : 500 points

Problem Statement

Takahashi and Aoki will play a game of sugoroku.
Takahashi starts at point A, and Aoki starts at point B. They will take turns throwing dice.
Takahashi's die shows 1, 2, \ldots, P with equal probability, and Aoki's shows 1, 2, \ldots, Q with equal probability.
When a player at point x throws his die and it shows i, he goes to point \min(x + i, N).
The first player to reach point N wins the game.
Find the probability that Takahashi wins if he goes first, modulo 998244353.

How to find a probability modulo 998244353 It can be proved that the sought probability is always rational. Additionally, the constraints of this problem guarantee that, if that probability is represented as an irreducible fraction \frac{y}{x}, then x is indivisible by 998244353.
Here, there is a unique integer z between 0 and 998244352 such that xz \equiv y \pmod {998244353}. Report this z.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A, B < N
  • 1 \leq P, Q \leq 10
  • All values in the input are integers.

Input

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

N A B P Q

Output

Print the answer.


Sample Input 1

4 2 3 3 2

Sample Output 1

665496236

If Takahashi's die shows 2 or 3 in his first turn, he goes to point 4 and wins.
If Takahashi's die shows 1 in his first turn, he goes to point 3, and Aoki will always go to point 4 in the next turn and win.
Thus, Takahashi wins with the probability \frac{2}{3}.


Sample Input 2

6 4 2 1 1

Sample Output 2

1

The dice always show 1.
Here, Takahashi goes to point 5, Aoki goes to point 3, and Takahashi goes to point 6, so Takahashi always wins.


Sample Input 3

100 1 1 10 10

Sample Output 3

264077814
F - Rook Score

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

10^9 マス、横 10^9 マスのマス目があります。上から i 番目、左から j 番目のマスを (i,j) と表記します。

i=1,2,\ldots,N に対し (r_i,c_i) には正整数 x_i が、他の 10^{18}-N 個のマスには 0 が書かれています。

あなたはあるマス (R,C) を選び、 (R,C) と行または列が同じ 2 \times 10^9 - 1 個のマスに書かれた整数の総和 S を求めました。

S として考えられる最大値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq r_i,c_i,x_i \leq 10^9
  • i \neq j ならば (r_i,c_i) \neq (r_j,c_j)
  • 入力はすべて整数

入力

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

N
r_1 c_1 x_1
\vdots
r_N c_N x_N

出力

答えを出力せよ。


入力例 1

4
1 1 2
1 2 9
2 1 8
3 2 3

出力例 1

20

(R,C) として (2,2) を選ぶと S20 となります。これが最大値です。


入力例 2

1
1 1000000000 1

出力例 2

1

入力例 3

15
158260522 877914575 602436426
24979445 861648772 623690081
433933447 476190629 262703497
211047202 971407775 628894325
731963982 822804784 450968417
430302156 982631932 161735902
880895728 923078537 707723857
189330739 910286918 802329211
404539679 303238506 317063340
492686568 773361868 125660016
650287940 839296263 462224593
492601449 384836991 191890310
576823355 782177068 404011431
818008580 954291757 160449218
155374934 840594328 164163676

出力例 3

1510053068

Score : 500 points

Problem Statement

We have a grid with 10^9 rows and 10^9 columns. Let (i,j) denote the square at the i-th row from the top and j-th column from the left.

For i=1,2,\ldots,N, a positive integer x_i is written on (r_i,c_i). On the other 10^{18}-N squares, 0 is written.

You choose a square (R,C) and compute the sum S of the integers written on the 2 \times 10^9 - 1 squares that share a row or column with (R,C).

Find the maximum possible value of S.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq r_i,c_i,x_i \leq 10^9
  • (r_i,c_i) \neq (r_j,c_j) if i \neq j.
  • All values in the input are integers.

Input

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

N
r_1 c_1 x_1
\vdots
r_N c_N x_N

Output

Print the answer.


Sample Input 1

4
1 1 2
1 2 9
2 1 8
3 2 3

Sample Output 1

20

If you choose (2,2) as (R,C), then S will be 20, which is the maximum possible value.


Sample Input 2

1
1 1000000000 1

Sample Output 2

1

Sample Input 3

15
158260522 877914575 602436426
24979445 861648772 623690081
433933447 476190629 262703497
211047202 971407775 628894325
731963982 822804784 450968417
430302156 982631932 161735902
880895728 923078537 707723857
189330739 910286918 802329211
404539679 303238506 317063340
492686568 773361868 125660016
650287940 839296263 462224593
492601449 384836991 191890310
576823355 782177068 404011431
818008580 954291757 160449218
155374934 840594328 164163676

Sample Output 3

1510053068
G - Strawberry War

Time Limit: 6 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長方形のケーキがあります。このケーキは HW 列に並ぶ区画に分かれていて、上から i 行目、左から j 列目の区画にはイチゴが s_{i,j} 個載っています。

あなたは T 回の切断を行ってケーキを T+1 切れに分割することにしました。各回の切断は、次のいずれかの方法で行います。

  • 現存するケーキであって、区画の行の数が 2 以上であるものを選ぶ。さらに、そのケーキから隣接する 2 行を選び、その境界でケーキを切断してより小さなケーキ 2 切れに分割する。
  • 現存するケーキであって、区画の列の数が 2 以上であるものを選ぶ。さらに、そのケーキから隣接する 2 列を選び、その境界でケーキを切断してより小さなケーキ 2 切れに分割する。

あなたの目標は、分割後のケーキに載ったイチゴの数をできるだけ均等にすることです。
分割後の T+1 切れのケーキに載ったイチゴの個数を x_1,x_2,\ldots,x_{T+1} として、そのうち最大のものを M、最小のものを m とするとき、M-m がとりうる最小値を求めてください。

制約

  • 1 \leq H,W \leq 6
  • 1 \leq T \leq HW-1
  • 0 \leq s_{i,j} \leq 10^{16}
  • 入力はすべて整数

入力

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

H W T
s_{1,1} \ldots s_{1,W}
\vdots
s_{H,1} \ldots s_{H,W}

出力

答えを出力せよ。


入力例 1

2 3 4
2 3 4
4 1 3

出力例 1

2

下のように切り分けると左上のケーキに 2 個、左下のケーキに 4 個、中央のケーキに 4 個、右上のケーキに 4 個、右下のケーキに 3 個のイチゴが載った状態になり、イチゴの個数の最大値と最小値の差は 4-2=2 となります。差をこれよりも小さくすることは出来ないため、2 が答えとなります。


入力例 2

2 2 3
0 0
0 0

出力例 2

0

Score : 600 points

Problem Statement

We have a rectangular cake. It is partitioned into H rows and W columns of sections, and the section at the i-th row from the top and j-th column from the left has s_{i,j} strawberries on it.

You will make T cuts to divide the cake into T+1 pieces. Each cut will be done in one of the following two manners.

  • Choose an existing piece with two or more rows of sections. Additionally, choose two adjacent rows from that piece, and cut the piece along the boundary between them to divide it into two smaller pieces.
  • Choose an existing piece with two or more columns of sections. Additionally, choose two adjacent columns from that piece, and cut the piece along the boundary between them to divide it into two smaller pieces.

You want to distribute the strawberries onto the resulting pieces as evenly as possible.
Let x_1,x_2,\ldots,x_{T+1} be the numbers of strawberries on the resulting T+1 pieces, and M and m be the maximum and minimum among those numbers, respectively. Find the minimum possible value of M-m.

Constraints

  • 1 \leq H,W \leq 6
  • 1 \leq T \leq HW-1
  • 0 \leq s_{i,j} \leq 10^{16}
  • All values in the input are integers.

Input

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

H W T
s_{1,1} \ldots s_{1,W}
\vdots
s_{H,1} \ldots s_{H,W}

Output

Print the answer.


Sample Input 1

2 3 4
2 3 4
4 1 3

Sample Output 1

2

The figure below shows a way to cut the cake so that the top-left, bottom-left, middle, top-right, and bottom-right pieces have 2, 4, 4, 4, and 3 strawberries on them, respectively. Here, the difference between the maximum and minimum number of strawberries is 4-2=2. It is impossible to achieve a smaller difference, so the answer is 2.


Sample Input 2

2 2 3
0 0
0 0

Sample Output 2

0
Ex - Sum of Min of Length

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点の木が与えられます。頂点には 1 から N までの番号がついており、i 番目の辺は頂点 A_i と頂点 B_i を結んでいます。

また、この木における頂点 x と頂点 y の距離を d(x,y) で表します。ただし、頂点 x と頂点 y の距離とは、頂点 x から頂点 y までの最短パス上の辺の本数のことをいいます。

Q 個のクエリが与えられるので、順番に答えてください。i 番目のクエリは以下で説明されます。

  • 整数 L_i, R_i が与えられます。 \displaystyle\sum_{j = 1}^{N} \min(d(j, L_i), d(j, R_i)) の値を求めてください。

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 \leq A_i, B_i, L_i, R_i \leq N
  • 与えられるグラフは木
  • 入力はすべて整数

入力

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

N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
Q
L_1 R_1
\vdots
L_Q R_Q

出力

Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

4
6
3

1 番目のクエリについて説明します。
d(1, 4) = 2d(1, 1) = 0 より \min(d(1, 4), d(1, 1)) = 0 です。
d(2, 4) = 2d(2, 1) = 2 より \min(d(2, 4), d(2, 1)) = 2 です。
d(3, 4) = 1d(3, 1) = 3 より \min(d(3, 4), d(3, 1)) = 1 です。
d(4, 4) = 0d(4, 1) = 2 より \min(d(4, 4), d(4, 1)) = 0 です。
d(5, 4) = 1d(5, 1) = 1 より \min(d(5, 4), d(5, 1)) = 1 です。
0 + 2 + 1 + 0 + 1 = 4 であるため、4 を出力します。


入力例 2

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

出力例 2

14
16
10
16
14
16
8

Score : 600 points

Problem Statement

You are given a tree with N vertices. The vertices are numbered 1 to N, and the i-th edge connects vertex A_i and vertex B_i.

Let d(x,y) denote the distance between vertex x and y in this tree. Here, the distance between vertex x and y is the number of edges on the shortest path from vertex x to y.

Answer Q queries in order. The i-th query is as follows.

  • You are given integers L_i and R_i. Find \displaystyle\sum_{j = 1}^{N} \min(d(j, L_i), d(j, R_i)).

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 \leq A_i, B_i, L_i, R_i \leq N
  • The given graph is a tree.
  • All values in the input are integers.

Input

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

N
A_1 B_1
\vdots
A_{N-1} B_{N-1}
Q
L_1 R_1
\vdots
L_Q R_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

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

Sample Output 1

4
6
3

Let us explain the first query.
Since d(1, 4) = 2 and d(1, 1) = 0, we have \min(d(1, 4), d(1, 1)) = 0.
Since d(2, 4) = 2 and d(2, 1) = 2, we have \min(d(2, 4), d(2, 1)) = 2.
Since d(3, 4) = 1 and d(3, 1) = 3, we have \min(d(3, 4), d(3, 1)) = 1.
Since d(4, 4) = 0 and d(4, 1) = 2, we have \min(d(4, 4), d(4, 1)) = 0.
Since d(5, 4) = 1 and d(5, 1) = 1, we have \min(d(5, 4), d(5, 1)) = 1.
0 + 2 + 1 + 0 + 1 = 4, so you should print 4.


Sample Input 2

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

Sample Output 2

14
16
10
16
14
16
8