A - Poisonous Oyster

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

4 種類の牡蠣 1,2,3,4 があります。 このうちちょうど 1 種類の牡蠣については、食べるとお腹を壊してしまいます。 それ以外の牡蠣については、食べてもお腹を壊しません。

高橋君が牡蠣 1,2 を食べ、青木君が牡蠣 1,3 を食べました。 二人がこれによってお腹を壊したかどうかの情報が二つの文字列 S_1,S_2 によって与えられます。 具体的には、S_1= sick であるとき高橋君がお腹を壊したことを、S_1= fine であるとき高橋君がお腹を壊さなかったことを表します。 同様に、S_2= sick であるとき青木君がお腹を壊したことを、S_2= fine であるとき青木君がお腹を壊さなかったことを表します。

与えられた情報をもとに、どの種類の牡蠣を食べるとお腹を壊すか判定してください。

制約

  • S_1, S_2 はそれぞれ sick または fine

入力

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

S_1 S_2

出力

食べるとお腹を壊す牡蠣の種類の番号を出力せよ。


入力例 1

sick fine

出力例 1

2

牡蠣 1,2 を食べた高橋君はお腹を壊し、牡蠣 1,3 を食べた青木君はお腹を壊さなかったので、牡蠣 2 を食べるとお腹を壊すことがわかります。


入力例 2

fine fine

出力例 2

4

牡蠣 1,2 を食べた高橋君も牡蠣 1,3 を食べた青木君もお腹を壊さなかったので、残る牡蠣 4 を食べるとお腹を壊すことがわかります。

Score : 100 points

Problem Statement

There are four types of oysters, labeled 1, 2, 3, and 4. Exactly one of these types causes stomach trouble if eaten. The other types do not cause stomach trouble when eaten.

Takahashi ate oysters 1 and 2, and Aoki ate oysters 1 and 3. The information on whether each person got sick is given as two strings S_1 and S_2. Specifically, S_1 = sick means Takahashi got sick, and S_1 = fine means Takahashi did not get sick. Likewise, S_2 = sick means Aoki got sick, and S_2 = fine means Aoki did not get sick.

Based on the given information, find which type of oyster causes stomach trouble.

Constraints

  • Each of S_1 and S_2 is sick or fine.

Input

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

S_1 S_2

Output

Print the label of the oyster that causes stomach trouble if eaten.


Sample Input 1

sick fine

Sample Output 1

2

Takahashi (who ate oysters 1 and 2) got sick, and Aoki (who ate oysters 1 and 3) did not get sick, so it can be concluded that oyster 2 causes stomach trouble.


Sample Input 2

fine fine

Sample Output 2

4

Neither Takahashi (who ate oysters 1 and 2) nor Aoki (who ate oysters 1 and 3) got sick, so it can be concluded that oyster 4 causes stomach trouble.

B - First ABC 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

A, B, C からなる長さ N の文字列 S が与えられます。
S の中で ABC が(連続な)部分文字列として初めて現れる位置を答えてください。すなわち、以下の条件を全て満たす整数 n のうち最小のものを答えてください。

  • 1 \leq n \leq N - 2
  • Sn 文字目から n+2 文字目までを取り出して出来る文字列は ABC である。

ただし、ABCS に現れない場合は -1 を出力してください。

制約

  • 3 \leq N \leq 100
  • SA, B, C からなる長さ N の文字列

入力

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

N
S

出力

S の中で ABC が部分文字列として初めて現れる位置を出力せよ。ただし、ABCS に現れない場合は -1 を出力せよ。


入力例 1

8
ABABCABC

出力例 1

3

S の中で ABC が初めて現れるのは 3 文字目から 5 文字目までの位置です。よって 3 が答えになります。


入力例 2

3
ACB

出力例 2

-1

ABCS に現れない場合は -1 を出力してください。


入力例 3

20
BBAAABBACAACABCBABAB

出力例 3

13

Score : 100 points

Problem Statement

You are given a string S of length N consisting of A, B, and C.
Find the position where ABC first appears as a (contiguous) substring in S. In other words, find the smallest integer n that satisfies all of the following conditions.

  • 1 \leq n \leq N - 2.
  • The string obtained by extracting the n-th through (n+2)-th characters of S is ABC.

If ABC does not appear in S, print -1.

Constraints

  • 3 \leq N \leq 100
  • S is a string of length N consisting of A, B, and C.

Input

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

N
S

Output

Print the position where ABC first appears as a substring in S, or -1 if it does not appear in S.


Sample Input 1

8
ABABCABC

Sample Output 1

3

ABC first appears in S at the 3-rd through 5-th characters of S. Therefore, the answer is 3.


Sample Input 2

3
ACB

Sample Output 2

-1

If ABC does not appear in S, print -1.


Sample Input 3

20
BBAAABBACAACABCBABAB

Sample Output 3

13
C - AtCoder Amusement Park

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

AtCoder 遊園地には K 人乗りのアトラクションがあります。 現在、このアトラクションの待機列には N グループが並んでいます。

先頭から i 番目 (1\leq i\leq N) のグループは A _ i 人組です。 すべての i (1\leq i\leq N) について、A _ i\leq K です。

高橋君はこのアトラクションのスタッフとして、並んでいるグループを次の手順に従って誘導します。

はじめ、アトラクションには誰も誘導されておらず、空席は K 個あります。

  1. 待機列に並んでいるグループがない場合、アトラクションをスタートさせ、誘導を終了する。
  2. アトラクションの空席の数と待機列の先頭に並んでいるグループの人数を比較し、次のどちらかを行う。
    • 待機列の先頭に並んでいるグループの人数よりアトラクションの空席の数のほうが少ない場合、アトラクションをスタートさせる。 スタートしたのち、アトラクションの空席が K 個になる。
    • そうでない場合、待機列の先頭に並んでいるグループを全員アトラクションへ誘導する。 先頭のグループが待機列から取り出され、アトラクションの空席がグループの人数ぶんだけ減少する。
  3. 1 に戻る。

ただし、誘導を開始したあとに追加でグループが並ぶことはないとします。 以上の条件のもとで、この手順が有限回で終了することが示せます。

高橋君が誘導を開始してから誘導を終了するまで、何回アトラクションをスタートさせるか求めてください。

制約

  • 1\leq N\leq100
  • 1\leq K\leq100
  • 1\leq A _ i\leq K\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N K
A _ 1 A _ 2 \ldots A _ N

出力

答えを出力せよ。


入力例 1

7 6
2 5 1 4 1 2 3

出力例 1

4

はじめ、7 つのグループは以下のように並んでいます。

高橋君の誘導の様子の一部を以下の図に示します。

  • はじめ、先頭に並んでいるグループは 2 人のグループで、空席は 6 個です。よって、高橋君は先頭のグループをアトラクションに誘導し、空席は 4 個になります。
  • 次に、先頭に並んでいるグループは 5 人のグループで、空席の個数 4 より多いため、アトラクションをスタートさせます。
  • 空席が 6 個になったため、先頭のグループをアトラクションに誘導し、空席は 1 個になります。
  • 次に先頭に並んでいるのは 1 人なので、アトラクションに誘導し、空席は 0 個になります。

すべての誘導が終了するまでに、高橋君は 4 回アトラクションをスタートさせることになります。 よって、4 を出力してください。


入力例 2

7 10
1 10 1 10 1 10 1

出力例 2

7

入力例 3

15 100
73 8 55 26 97 48 37 47 35 55 5 17 62 2 60

出力例 3

8

Score: 200 points

Problem Statement

The AtCoder amusement park has an attraction that can accommodate K people. Now, there are N groups lined up in the queue for this attraction.

The i-th group from the front (1\leq i\leq N) consists of A_i people. For all i (1\leq i\leq N), it holds that A_i \leq K.

Takahashi, as a staff member of this attraction, will guide the groups in the queue according to the following procedure.

Initially, no one has been guided to the attraction, and there are K empty seats.

  1. If there are no groups in the queue, start the attraction and end the guidance.
  2. Compare the number of empty seats in the attraction with the number of people in the group at the front of the queue, and do one of the following:
    • If the number of empty seats is less than the number of people in the group at the front, start the attraction. Then, the number of empty seats becomes K again.
    • Otherwise, guide the entire group at the front of the queue to the attraction. The front group is removed from the queue, and the number of empty seats decreases by the number of people in the group.
  3. Go back to step 1.

Here, no additional groups will line up after the guidance has started. Under these conditions, it can be shown that this procedure will end in a finite number of steps.

Determine how many times the attraction will be started throughout the guidance.

Constraints

  • 1\leq N\leq 100
  • 1\leq K\leq 100
  • 1\leq A_i\leq K\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N K
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

7 6
2 5 1 4 1 2 3

Sample Output 1

4

Initially, the seven groups are lined up as follows:

Part of Takahashi's guidance is shown in the following figure:

  • Initially, the group at the front has 2 people, and there are 6 empty seats. Thus, he guides the front group to the attraction, leaving 4 empty seats.
  • Next, the group at the front has 5 people, which is more than the 4 empty seats, so the attraction is started.
  • After the attraction is started, there are 6 empty seats again, so the front group is guided to the attraction, leaving 1 empty seat.
  • Next, the group at the front has 1 person, so they are guided to the attraction, leaving 0 empty seats.

In total, he starts the attraction four times before the guidance is completed. Therefore, print 4.


Sample Input 2

7 10
1 10 1 10 1 10 1

Sample Output 2

7

Sample Input 3

15 100
73 8 55 26 97 48 37 47 35 55 5 17 62 2 60

Sample Output 3

8
D - Daily Cookie 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

A 問題と似た設定の問題です。A 問題とは、高橋君が食べるクッキーの選び方および求めるものが異なります。

N 個の箱が横一列に並んでおり、そのうちのいくつかの箱にはクッキーが入っています。

各箱の状態は長さ N の文字列 S によって表されます。 具体的には、左から i\ (1\leq i\leq N) 番目の箱は、Si 文字目が @ のときクッキーが 1 枚入っており、. のとき空き箱です。

高橋君は今からの D 日間、一日一回ずつ、その時点でクッキーが入っている箱のうち最も右にある箱のクッキーを選んで食べます。

N 個の箱それぞれについて、D 日間が経過した後にその箱にクッキーが入っているかを求めてください。

なお、S には @D 個以上含まれることが保証されます。

制約

  • 1\leq D \leq N \leq 100
  • N,D は整数
  • S@. からなる長さ N の文字列
  • S には @D 個以上含まれる

入力

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

N D
S

出力

長さ N の文字列を出力せよ。 出力する文字列の i\ (1\leq i\leq N) 文字目は、D 日間が経過した後に左から i 番目の箱にクッキーが入っているならば @、入っていないならば . とせよ。


入力例 1

5 2
.@@.@

出力例 1

.@...

高橋君は以下のように行動します。

  • 1 日目:左から 2,3,5 番目の箱にクッキーが入っている。このうちで最も右にある、左から 5 番目の箱に入っているクッキーを選んで食べる。
  • 2 日目:左から 2,3 番目の箱にクッキーが入っている。このうちで最も右にある、左から 3 番目の箱に入っているクッキーを選んで食べる。
  • 2 日間が経過した後、左から 2 番目の箱にのみクッキーが入っている。

よって、正しい出力は .@... です。


入力例 2

3 3
@@@

出力例 2

...

入力例 3

10 4
@@@.@@.@@.

出力例 3

@@@.......

Score: 200 points

Problem Statement

This problem shares a similar setting with Problem A. The way Takahashi chooses cookies and what you are required to find are different from Problem A.

There are N boxes arranged in a row, and some of these boxes contain cookies.

The state of these boxes is represented by a string S of length N. Specifically, the i-th box (1\leq i \leq N) from the left contains one cookie if the i-th character of S is @, and is empty if it is ..

Over the next D days, Takahashi will choose and eat one cookie per day from among the cookies in these boxes. On each day, he chooses the cookie in the rightmost box that contains a cookie at that point.

Determine, for each of the N boxes, whether it will contain a cookie after D days have passed.

It is guaranteed that S contains at least D occurrences of @.

Constraints

  • 1 \leq D \leq N \leq 100
  • N and D are integers.
  • S is a string of length N consisting of @ and ..
  • S contains at least D occurrences of @.

Input

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

N D
S

Output

Print a string of length N. The i-th character (1 \leq i \leq N) of the string should be @ if the i-th box from the left contains a cookie after D days have passed, and . otherwise.


Sample Input 1

5 2
.@@.@

Sample Output 1

.@...

Takahashi acts as follows:

  • Day 1: There are cookies in the 2nd, 3rd, and 5th boxes from the left. Among these, the rightmost is the 5th box. He eats the cookie in this box.
  • Day 2: There are cookies in the 2nd and 3rd boxes. Among these, the rightmost is the 3rd box. He eats the cookie in this box.
  • After two days have passed, only the 2nd box from the left contains a cookie.

Therefore, the correct output is .@....


Sample Input 2

3 3
@@@

Sample Output 2

...

Sample Input 3

10 4
@@@.@@.@@.

Sample Output 3

@@@.......
E - Minimize Abs 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

正整数 D が与えられます。

非負整数 x,y に対する |x^2+y^2-D| の最小値を求めてください。

制約

  • 1\leq D \leq 2\times 10^{12}
  • 入力は全て整数

入力

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

D

出力

答えを出力せよ。


入力例 1

21

出力例 1

1

x=4,y=2 のとき |x^2+y^2-D| = |16+4-21|=1 となります。

|x^2+y^2-D|=0 を満たすような非負整数 x,y は存在しないので、答えは 1 です。


入力例 2

998244353

出力例 2

0

入力例 3

264428617

出力例 3

32

Score : 300 points

Problem Statement

You are given a positive integer D.

Find the minimum value of |x^2+y^2-D| for non-negative integers x and y.

Constraints

  • 1\leq D \leq 2\times 10^{12}
  • All input values are integers.

Input

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

D

Output

Print the answer.


Sample Input 1

21

Sample Output 1

1

For x=4 and y=2, we have |x^2+y^2-D| = |16+4-21|=1.

There are no non-negative integers x and y such that |x^2+y^2-D|=0, so the answer is 1.


Sample Input 2

998244353

Sample Output 2

0

Sample Input 3

264428617

Sample Output 3

32
F - Changing Jewels

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君はレベル N の赤い宝石を 1 個持っています。(他に宝石は持っていません。)
高橋君は次の操作を好きなだけ行うことができます。

  • レベル n の赤い宝石 (n2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n の青い宝石 X 個」に変換する。
  • レベル n の青い宝石 (n2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n-1 の青い宝石 Y 個」に変換する。

高橋君はレベル 1 の青い宝石ができるだけたくさんほしいです。操作によって高橋君はレベル 1 の青い宝石を最大何個入手できますか?

制約

  • 1 \leq N \leq 10
  • 1 \leq X \leq 5
  • 1 \leq Y \leq 5
  • 入力される値はすべて整数

入力

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

N X Y

出力

答えを出力せよ。


入力例 1

2 3 4

出力例 1

12

次のような変換を行うことで、高橋君はレベル 1 の青い宝石を 12 個手に入れることができます。

  • まず、レベル 2 の赤い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個に変換します。
    • 操作後の高橋君は、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個を持っています。
  • 次に、レベル 2 の青い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 1 の青い宝石 4 個に変換します。この変換を 3 回繰り返します。
    • 操作後の高橋君は、レベル 1 の赤い宝石 4 個とレベル 1 の青い宝石 12 個を持っています。
  • これ以上変換を行うことはできません。

12 個より多くレベル 1 の青い宝石を手に入れることはできないので、答えは 12 になります。


入力例 2

1 5 5

出力例 2

0

高橋君がレベル 1 の青い宝石を 1 個も手に入れられない場合もあります。


入力例 3

10 5 5

出力例 3

3942349900

答えが 32 bit 整数に収まらない場合があることに注意してください。

Score : 300 points

Problem Statement

Takahashi has a red jewel of level N. (He has no other jewels.)
Takahashi can do the following operations any number of times.

  • Convert a red jewel of level n (n is at least 2) into "a red jewel of level (n-1) and X blue jewels of level n".
  • Convert a blue jewel of level n (n is at least 2) into "a red jewel of level (n-1) and Y blue jewels of level (n-1)".

Takahashi wants as many blue jewels of level 1 as possible. At most, how many blue jewels of level 1 can he obtain by the operations?

Constraints

  • 1 \leq N \leq 10
  • 1 \leq X \leq 5
  • 1 \leq Y \leq 5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

Print the answer.


Sample Input 1

2 3 4

Sample Output 1

12

Takahashi can obtain 12 blue jewels of level 1 by the following conversions.

  • First, he converts a red jewel of level 2 into a red jewel of level 1 and 3 blue jewels of level 2.
    • After this operation, Takahashi has 1 red jewel of level 1 and 3 blue jewels of level 2.
  • Next, he repeats the following conversion 3 times: converting a blue jewel of level 2 into a red jewel of level 1 and 4 blue jewels of level 1.
    • After these operations, Takahashi has 4 red jewels of level 1 and 12 blue jewels of level 1.
  • He cannot perform a conversion anymore.

He cannot obtain more than 12 blue jewels of level 1, so the answer is 12.


Sample Input 2

1 5 5

Sample Output 2

0

Takahashi may not be able to obtain a blue jewel of level 1.


Sample Input 3

10 5 5

Sample Output 3

3942349900

Note that the answer may not fit into a 32-bit integer type.

G - Tiling

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

一辺の長さが 1 のマスからなる HW 列のマス目と、N 枚のタイルがあります。
i (1\leq i\leq N) 枚目のタイルは A_i\times B_i の長方形です。
以下の条件をすべてみたすようにタイルをマス目に置くことができるか判定してください。

  • 全てのマスがちょうど 1 枚のタイルで覆われている。
  • 使用されないタイルがあっても良い。
  • 使用するタイルは 回転したり裏返したりして置かれていても良い。ただし、各タイルはマスの線に合わせてマス目からはみ出ることがないように置かれていなければならない。

制約

  • 1\leq N\leq 7
  • 1 \leq H,W \leq 10
  • 1\leq A_i,B_i\leq 10
  • 入力はすべて整数

入力

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

N H W
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

問題文中の条件をみたすようにタイルをマス目に置くことができるならば Yes を、そうでないならば No を出力せよ。


入力例 1

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

出力例 1

Yes

2,4,5 枚目のタイルを使用して次のように置くと、マス目の各マスをちょうど 1 枚のタイルで覆うことができます。

よって、Yes を出力します。


入力例 2

1 1 2
2 3

出力例 2

No

マス目からはみ出さないようにタイルを置くことはできません。
よって、No を出力します。


入力例 3

1 2 2
1 1

出力例 3

No

全てのマスを覆うようにタイルを置くことができません。
よって、No を出力します。


入力例 4

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

出力例 4

No

全てのマスはちょうど 1 枚のタイルで覆われている必要があることに注意してください。

Score: 450 points

Problem Statement

There is a grid of H rows and W columns, each cell having a side length of 1, and we have N tiles.
The i-th tile (1\leq i\leq N) is a rectangle of size A_i\times B_i.
Determine whether it is possible to place the tiles on the grid so that all of the following conditions are satisfied:

  • Every cell is covered by exactly one tile.
  • It is fine to have unused tiles.
  • The tiles may be rotated or flipped when placed. However, each tile must be aligned with the edges of the cells without extending outside the grid.

Constraints

  • 1\leq N\leq 7
  • 1 \leq H,W \leq 10
  • 1\leq A_i,B_i\leq 10
  • All input values are integers.

Input

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

N H W
A_1 B_1
A_2 B_2
\ldots
A_N B_N

Output

If it is possible to place the tiles on the grid so that all of the conditions in the problem statement are satisfied, print Yes; otherwise, print No.


Sample Input 1

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

Sample Output 1

Yes

Placing the 2-nd, 4-th, and 5-th tiles as shown below covers every cell of the grid by exactly one tile.

Hence, print Yes.


Sample Input 2

1 1 2
2 3

Sample Output 2

No

It is impossible to place the tile without letting it extend outside the grid.
Hence, print No.


Sample Input 3

1 2 2
1 1

Sample Output 3

No

It is impossible to cover all cells with the tile.
Hence, print No.


Sample Input 4

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

Sample Output 4

No

Note that each cell must be covered by exactly one tile.

H - Throwing the Die

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

サイコロを使ったゲームをします。ゲームは最大 N 回のターンからなり、各ターンは次のように進行します。

  • 1,\ldots,6 の目が等確率で出る 6 面ダイスを振り、出目を X とする(出目は各ターンで独立とする)。
  • 現在が N ターン目なら、スコアX とし、ゲームを終了する。
  • そうでないとき、ゲームを続行するか終了するか選択する。
    • ゲームを終了する場合、スコアを X とし、残りのターンは行わずにゲームを終了する。

スコアの期待値が最大になるように行動したとき、スコアの期待値を求めてください。

制約

  • 1 \leq N \leq 100

入力

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

N

出力

答えを出力せよ。
なお、真の解との絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。


入力例 1

1

出力例 1

3.5000000000

入力例 2

2

出力例 2

4.2500000000

入力例 3

10

出力例 3

5.6502176688

Score : 500 points

Problem Statement

Let us play a game using a die. The game consists of at most N turns, each of which goes as follows.

  • Throw a 6-sided die that shows 1,\ldots,6 with equal probability, and let X be the number shown (each throw is independent of the others).
  • If it is the N-th turn now, your score is X, and the game ends.
  • Otherwise, choose whether to continue or end the game.
    • If you end the game, your score is X, and there is no more turn.

Find the expected value of your score when you play the game to maximize this expected value.

Constraints

  • 1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.
Your output is considered correct if its absolute or relative error from the true answer is at most 10^{-6}.


Sample Input 1

1

Sample Output 1

3.5000000000

Sample Input 2

2

Sample Output 2

4.2500000000

Sample Input 3

10

Sample Output 3

5.6502176688
I - Add One Edge 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

頂点に 1 から N_1 までの番号が付いた N_1 頂点の木 1 と、頂点に 1 から N_2 までの番号が付いた N_2 頂点の木 2 が与えられます。木 1i 番目の辺は木 1 の頂点 u_{1,i}v_{1,i} を双方向に結び、木 2i 番目の辺は木 2 の頂点 u_{2,i}v_{2,i} を双方向に結んでいます。

1 の頂点 i と、木 2 の頂点 j を双方向に結ぶ辺を追加すると一つの木が得られます。この木の直径を f(i,j) と定めます。

\displaystyle \sum_{i=1}^{N_1}\sum_{j=1}^{N_2} f(i,j) を求めてください。

ただし、木の 2 頂点の間の距離は一方から他方へ移動するときに用いる辺の本数の最小値であり、 木の直径は任意の 2 頂点の間の距離の最大値として定められます。

制約

  • 1\leq N_1,N_2\leq 2\times 10^{5}
  • 1\leq u_{1,i},v_{1,i}\leq N_1
  • 1\leq u_{2,i},v_{2,i}\leq N_2
  • 入力されるグラフは共に木
  • 入力される数値は全て整数

入力

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

N_1
u_{1,1} v_{1,1}
\vdots
u_{1,N_1-1} v_{1,N_1-1}
N_2
u_{2,1} v_{2,1}
\vdots
u_{2,N_2-1} v_{2,N_2-1}

出力

答えを出力せよ。


入力例 1

3
1 3
1 2
3
1 2
3 1

出力例 1

39

例えば木 1 の頂点 2 と木 2 の頂点 3 を結んで得られる木の直径は 5 です。よって f(2,3)5 となります。

f(i,j) の総和は 39 です。


入力例 2

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

出力例 2

267

Score : 500 points

Problem Statement

You are given two trees: tree 1 with N_1 vertices numbered 1 to N_1, and tree 2 with N_2 vertices numbered 1 to N_2. The i-th edge of tree 1 connects vertices u_{1,i} and v_{1,i} bidirectionally, and the i-th edge of tree 2 connects vertices u_{2,i} and v_{2,i} bidirectionally.

One can add a bidirectional edge between vertex i of tree 1 and vertex j of tree 2 to obtain a single tree. Let f(i,j) be the diameter of this tree.

Find \displaystyle \sum_{i=1}^{N_1}\sum_{j=1}^{N_2} f(i,j).

Here, the distance between two vertices of a tree is the minimum number of edges that must be used to move between them, and the diameter of a tree is the maximum distance between two vertices.

Constraints

  • 1 \le N_1, N_2 \le 2 \times 10^{5}
  • 1 \le u_{1,i}, v_{1,i} \le N_1
  • 1 \le u_{2,i}, v_{2,i} \le N_2
  • Both given graphs are trees.
  • All input values are integers.

Input

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

N_1
u_{1,1} v_{1,1}
\vdots
u_{1,N_1-1} v_{1,N_1-1}
N_2
u_{2,1} v_{2,1}
\vdots
u_{2,N_2-1} v_{2,N_2-1}

Output

Print the answer.


Sample Input 1

3
1 3
1 2
3
1 2
3 1

Sample Output 1

39

For example, one can connect vertex 2 of tree 1 and vertex 3 of tree 2 to obtain a tree of diameter 5. Thus, f(2,3) is 5.

The sum of f(i,j) is 39.


Sample Input 2

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

Sample Output 2

267