A - Integer Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

N 個の整数 A_1,A_2,\dots,A_N が与えられます。

N 個の整数を合計した値を求めてください。

制約

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

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3
2 7 2

出力例 1

11

3 個の整数 2,7,2 が与えられます。

答えは 2 + 7 + 2 = 11 です。


入力例 2

1
3

出力例 2

3

Score : 100 points

Problem Statement

You are given N integers A_1,A_2,\dots, and A_N.

Find the sum of the N integers.

Constraints

  • 1 \le N \le 100
  • 1 \le A_i \le 100
  • All values in the input are integers.

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

3
2 7 2

Sample Output 1

11

You are given three integers: 2, 7, and 2.

The answer is 2 + 7 + 2 = 11.


Sample Input 2

1
3

Sample Output 2

3
B - To Be Saikyo

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

1 から N までの番号が付けられた N 人の人がいます。 それぞれの人にはプログラミング力という整数値が定まっており、人 i のプログラミング力は P_i です。 人 1 が最強になるためには、あといくつプログラミング力を上げる必要がありますか? すなわち、すべての i \neq 1 に対して P_1 + x > P_i を満たすような最小の非負整数 x は何ですか?

制約

  • 1\leq N \leq 100
  • 1\leq P_i \leq 100
  • 入力は全て整数

入力

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

N
P_1 P_2 \dots P_N

出力

答えを整数として出力せよ。


入力例 1

4
5 15 2 10

出力例 1

11

人 1 が最強になるためには、プログラミング力を 16 以上にする必要があります。 よって、答えは 16-5=11 です。


入力例 2

4
15 5 2 10

出力例 2

0

人 1 は既に最強なので、これ以上プログラミング力を上げる必要はありません。


入力例 3

3
100 100 100

出力例 3

1

Score : 100 points

Problem Statement

There are N people numbered 1 through N. Each person has a integer score called programming ability; person i's programming ability is P_i points. How many more points does person 1 need, so that person 1 becomes the strongest? In other words, what is the minimum non-negative integer x such that P_1 + x > P_i for all i \neq 1?

Constraints

  • 1\leq N \leq 100
  • 1\leq P_i \leq 100
  • All input values are integers.

Input

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

N
P_1 P_2 \dots P_N

Output

Print the answer as an integer.


Sample Input 1

4
5 15 2 10

Sample Output 1

11

Person 1 becomes the strongest when their programming skill is 16 points or more, so the answer is 16-5=11.


Sample Input 2

4
15 5 2 10

Sample Output 2

0

Person 1 is already the strongest, so no more programming skill is needed.


Sample Input 3

3
100 100 100

Sample Output 3

1
C - Setsubun

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

年に 1 度の節分には年齢と同じ数の豆を食べます。高橋君はそれ以外に豆を食べることはありません。
高橋君は、今年 ( 0 年後 ) の節分の時点で N 歳です。

高橋君が今年以降 (今年を含む) で累計 K 個以上の豆を食べたことになるのは、最短で何年後の節分ですか?
なお、高橋君は不死身なので、高橋君が今年以降で累計 K 個以上の豆を食べたという状況は必ず訪れます。

制約

  • 入力は全て整数
  • 1 \le N,K \le 10^8

入力

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

N K

出力

答えを出力せよ。


入力例 1

4 43

出力例 1

6
  • 0 年後の節分に高橋君は 4 個の豆を食べ、今年以降で累計で 4 個の豆を食べます。
  • 1 年後の節分に高橋君は 5 個の豆を食べ、今年以降で累計で 9 個の豆を食べます。
  • 2 年後の節分に高橋君は 6 個の豆を食べ、今年以降で累計で 15 個の豆を食べます。
  • 3 年後の節分に高橋君は 7 個の豆を食べ、今年以降で累計で 22 個の豆を食べます。
  • 4 年後の節分に高橋君は 8 個の豆を食べ、今年以降で累計で 30 個の豆を食べます。
  • 5 年後の節分に高橋君は 9 個の豆を食べ、今年以降で累計で 39 個の豆を食べます。
  • 6 年後の節分に高橋君は 10 個の豆を食べ、今年以降で累計で 49 個の豆を食べます。

6 年後の節分の時点で累計 43 個以上の豆を食べたことになるので、 6 と出力してください。


入力例 2

100000000 100000000

出力例 2

0

今年のみで食べた豆の個数が K 個以上に達することもあります。


入力例 3

1234 12345678

出力例 3

3886

Score : 200 points

Problem Statement

At the annual Setsubun festival, one eats the same number of beans as their age. Takahashi-kun does not eat beans at any other time.
He is N years old at this year's Setsubun (0 years later).

At the earliest, how many years later will he have eaten a total of K or more beans from this year onward (including this year)?
He is immortal, so he will eventually have eaten a total of K or more beans from this year onward.

Constraints

  • All input values are integers.
  • 1 \le N,K \le 10^8

Input

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

N K

Output

Print the answer.


Sample Input 1

4 43

Sample Output 1

6
  • At Setsubun 0 years later, Takahashi-kun eats 4 beans, totaling 4 beans from this year onward.
  • At Setsubun 1 year later, he eats 5 beans, totaling 9 beans from this year onward.
  • At Setsubun 2 years later, he eats 6 beans, totaling 15 beans from this year onward.
  • At Setsubun 3 years later, he eats 7 beans, totaling 22 beans from this year onward.
  • At Setsubun 4 years later, he eats 8 beans, totaling 30 beans from this year onward.
  • At Setsubun 5 years later, he eats 9 beans, totaling 39 beans from this year onward.
  • At Setsubun 6 years later, he eats 10 beans, totaling 49 beans from this year onward.

At Setsubun 6 years later, he will have eaten a total of 43 or more beans, so output 6.


Sample Input 2

100000000 100000000

Sample Output 2

0

The number of beans eaten this year alone may reach K or more.


Sample Input 3

1234 12345678

Sample Output 3

3886
D - Locked Rooms

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

N + 1 個の部屋が一列に並んでおり、順に 0, 1, \ldots, N の番号が付けられています。

部屋の間には N 個のドアがあり、1, 2, \ldots, N の番号が付けられています。ドア i は部屋 i - 1 と部屋 i の間にあります。

各ドアについて鍵の状態を表す値 L_i が与えられ、L_i = 0 のときドア i の鍵は開いており、L_i = 1 のときドア i の鍵は閉まっています。

2 人の人がおり、1 人は部屋 0 に、もう 1 人は部屋 N にいます。それぞれの人は、ドア i の鍵が開いているときに限り、部屋 i - 1 と部屋 i の間を移動することができます。

このとき、2 人のいずれも到達できない部屋の個数を求めてください。

制約

  • 2 \leq N \leq 100
  • L_i \in \lbrace 0, 1 \rbrace
  • 入力される値はすべて整数

入力

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

N
L_1 L_2 \ldots L_N

出力

答えを出力せよ。


入力例 1

5
0 1 0 0 1

出力例 1

3

2 人のいずれも到達できない部屋は部屋 2, 3, 4 の 3 つです。


入力例 2

3
1 0 1

出力例 2

2

入力例 3

8
0 0 1 1 0 1 0 0

出力例 3

3

Score : 200 points

Problem Statement

There are N + 1 rooms arranged in a line, numbered 0, 1, \ldots, N in order.

Between the rooms, there are N doors numbered 1, 2, \ldots, N. Door i is between rooms i - 1 and i.

For each door, a value L_i representing the lock state is given. When L_i = 0, door i is unlocked, and when L_i = 1, door i is locked.

There are two people, one in room 0 and the other in room N. Each person can move between rooms i - 1 and i only when door i is unlocked.

Find the number of rooms that neither of the two people can reach.

Constraints

  • 2 \leq N \leq 100
  • L_i \in \lbrace 0, 1 \rbrace
  • All input values are integers.

Input

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

N
L_1 L_2 \ldots L_N

Output

Output the answer.


Sample Input 1

5
0 1 0 0 1

Sample Output 1

3

The rooms that neither of the two people can reach are rooms 2, 3, 4, which is 3 rooms.


Sample Input 2

3
1 0 1

Sample Output 2

2

Sample Input 3

8
0 0 1 1 0 1 0 0

Sample Output 3

3
E - 1 2 1 3 1 2 1

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

列 S_n を次のように定義します。

  • S_1 は 1 つの 1 からなる長さ 1 の列である。
  • S_n (n は 2 以上の整数) は S_{n-1}, n, S_{n-1} をこの順につなげた列である。

たとえば S_2,S_3 は次のような列です。

  • S_2 は S_1, 2, S_1 をこの順につなげた列なので 1,2,1 である。
  • S_3 は S_2, 3, S_2 をこの順につなげた列なので 1,2,1,3,1,2,1 である。

N が与えられるので、列 S_N をすべて出力してください。

制約

  • N は整数
  • 1 \leq N \leq 16

入力

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

N

出力

S_N を空白区切りで出力せよ。


入力例 1

2

出力例 1

1 2 1

問題文の説明にある通り、S_2 は 1,2,1 となります。


入力例 2

1

出力例 2

1

入力例 3

4

出力例 3

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

S_4 は S_3,4,S_3 をこの順につなげた列です。

Score : 300 points

Problem Statement

We define sequences S_n as follows.

  • S_1 is a sequence of length 1 containing a single 1.
  • S_n (n is an integer greater than or equal to 2) is a sequence obtained by concatenating S_{n-1}, n, S_{n-1} in this order.

For example, S_2 and S_3 is defined as follows.

  • S_2 is a concatenation of S_1, 2, and S_1, in this order, so it is 1,2,1.
  • S_3 is a concatenation of S_2, 3, and S_2, in this order, so it is 1,2,1,3,1,2,1.

Given N, print the entire sequence S_N.

Constraints

  • N is an integer.
  • 1 \leq N \leq 16

Input

Input is given from Standard Input in the following format:

N

Output

Print S_N, with spaces in between.


Sample Input 1

2

Sample Output 1

1 2 1

As described in the Problem Statement, S_2 is 1,2,1.


Sample Input 2

1

Sample Output 2

1

Sample Input 3

4

Sample Output 3

1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
  • S_4 is a concatenation of S_3, 4, and S_3, in this order.
F - XX to XXX

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

英小文字からなる 2 つの文字列 S, T が与えられます。 次の操作を好きな回数( 0 回でも良い)行うことで、S を T と一致させることができるかを判定してください。

S において同じ文字が 2 文字連続しているところの間に、その文字と同じ文字を 1 つ挿入する。 すなわち、下記の 3 つの手順からなる操作を行う。

  1. 現在の S の長さを N とし、S = S_1S_2\ldots S_N とする。
  2. 1 以上 N-1 以下の整数 i であって、S_i = S_{i+1} を満たすものを 1 つ選択する。(ただし、そのような i が存在しない場合は、何もせずに手順 3.をスキップして操作を終了する。)
  3. S の i 文字目と i+1 文字目の間に文字 S_i(= S_{i+1}) を 1 つ挿入する。その結果、S は長さ N+1 の文字列 S_1S_2\ldots S_i S_i S_{i+1} \ldots S_N となる。

制約

  • S と T はそれぞれ英小文字からなる長さ 2 以上 2 \times 10^5 以下の文字列

入力

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

S
T

出力

S を T と一致させることができる場合は Yes を、そうでない場合は No を出力せよ。 ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。


入力例 1

abbaac
abbbbaaac

出力例 1

Yes

下記の 3 回の操作によって、S = abbaac を T = abbbbaaac に一致させることができます。

  • まず、S の 2 文字目と 3 文字目の間に b を挿入する。その結果、S = abbbaac となる。
  • 次に、再び S の 2 文字目と 3 文字目の間に b を挿入する。その結果、S = abbbbaac となる。
  • 最後に、S の 6 文字目と 7 文字目の間に a を挿入する。その結果、S = abbbbaaac となる。

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


入力例 2

xyzz
xyyzz

出力例 2

No

どのように操作を行っても、 S = xyzz を T = xyyzz に一致させることはできません。 よって、No を出力します。

Score : 300 points

Problem Statement

You are given two strings S and T. Determine whether it is possible to make S equal T by performing the following operation some number of times (possibly zero).

Between two consecutive equal characters in S, insert a character equal to these characters. That is, take the following three steps.

  1. Let N be the current length of S, and S = S_1S_2\ldots S_N.
  2. Choose an integer i between 1 and N-1 (inclusive) such that S_i = S_{i+1}. (If there is no such i, do nothing and terminate the operation now, skipping step 3.)
  3. Insert a single copy of the character S_i(= S_{i+1}) between the i-th and (i+1)-th characters of S. Now, S is a string of length N+1: S_1S_2\ldots S_i S_i S_{i+1} \ldots S_N.

Constraints

  • Each of S and T is a string of length between 2 and 2 \times 10^5 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If it is possible to make S equal T, print Yes; otherwise, print No. Note that the judge is case-sensitive.


Sample Input 1

abbaac
abbbbaaac

Sample Output 1

Yes

You can make S = abbaac equal T = abbbbaaac by the following three operations.

  • First, insert b between the 2-nd and 3-rd characters of S. Now, S = abbbaac.
  • Next, insert b again between the 2-nd and 3-rd characters of S. Now, S = abbbbaac.
  • Lastly, insert a between the 6-th and 7-th characters of S. Now, S = abbbbaaac.

Thus, Yes should be printed.


Sample Input 2

xyzz
xyyzz

Sample Output 2

No

No sequence of operations makes S = xyzz equal T = xyyzz. Thus, No should be printed.

G - Swap and Range Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられます。

Q 個のクエリが与えられるので、順に処理してください。 各クエリは以下のいずれかの形式です。

  • 1 x : A_x と A_{x+1} の値を入れ替える。
  • 2 l r : \displaystyle \sum_{l\leq i\leq r} A_i の値を求める。

制約

  • 2\leq N \leq 2\times 10^5
  • 1\leq Q \leq 5\times 10^5
  • 1\leq A_i \leq 10^4
  • 1 種類目のクエリについて、1\leq x \leq N-1
  • 2 種類目のクエリについて、1\leq l\leq r \leq N
  • 入力は全て整数

入力

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

N Q
A_1 A_2 \dots A_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

ここで、\text{query}_i は i 番目のクエリを表し、以下のいずれかの形式で与えられる。

1 x
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。 i 行目 (1\leq i \leq q) には、2 種類目のクエリのうち i 個目のものに対する答えを出力せよ。


入力例 1

4 4
2 7 1 8
1 2
2 1 2
1 1
2 2 4

出力例 1

3
17
  • 1 番目のクエリでは、A_2 と A_3 の値を入れ替えます。これにより、A=(2,1,7,8) になります。
  • 2 番目のクエリでは、A_1+A_2 の値を求めます。答えは 2+1=3 です。
  • 3 番目のクエリでは、A_1 と A_2 の値を入れ替えます。これにより、A=(1,2,7,8) になります。
  • 4 番目のクエリでは、A_2+A_3+A_4 の値を求めます。答えは 2+7+8=17 です。

入力例 2

8 10
22 75 26 45 72 81 47 29
2 2 7
2 6 8
2 4 4
1 2
2 1 3
1 1
2 2 4
1 2
1 4
2 1 1

出力例 2

346
157
45
123
142
26

Score : 400 points

Problem Statement

You are given a sequence A=(A_1,A_2,\dots,A_N) of length N.

Process Q queries in order. Each query is in one of the following formats:

  • 1 x : Swap the values of A_x and A_{x+1}.
  • 2 l r : Find the value of \displaystyle \sum_{l\leq i\leq r} A_i.

Constraints

  • 2\leq N \leq 2\times 10^5
  • 1\leq Q \leq 5\times 10^5
  • 1\leq A_i \leq 10^4
  • For queries of the first type, 1\leq x \leq N-1.
  • For queries of the second type, 1\leq l\leq r \leq N.
  • All input values are integers.

Input

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

N Q
A_1 A_2 \dots A_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Here, \text{query}_i represents the i-th query and is given in one of the following formats:

1 x
2 l r

Output

Let q be the number of queries of the second type, and output q lines. The i-th line (1\leq i \leq q) should contain the answer to the i-th query of the second type.


Sample Input 1

4 4
2 7 1 8
1 2
2 1 2
1 1
2 2 4

Sample Output 1

3
17
  • In the 1-st query, swap the values of A_2 and A_3. This makes A=(2,1,7,8).
  • In the 2-nd query, find the value of A_1+A_2. The answer is 2+1=3.
  • In the 3-rd query, swap the values of A_1 and A_2. This makes A=(1,2,7,8).
  • In the 4-th query, find the value of A_2+A_3+A_4. The answer is 2+7+8=17.

Sample Input 2

8 10
22 75 26 45 72 81 47 29
2 2 7
2 6 8
2 4 4
1 2
2 1 3
1 1
2 2 4
1 2
1 4
2 1 1

Sample Output 2

346
157
45
123
142
26
H - Many LCMs

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475 点

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

k=1,2,\dots,N について、A のうち A_k を除いた N-1 個の要素の最小公倍数を 998244353 で割ったあまりを求めてください。

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

制約

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^7
  • 入力はすべて整数
  • 1 つの入力に含まれるテストケースについて、N の総和は 2 \times 10^5 以下

入力

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

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

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

N
A_1 A_2 \cdots A_N

出力

以下の形式で出力せよ。

\mathrm{answer}_1
\mathrm{answer}_2
\vdots
\mathrm{answer}_T

ここで \mathrm{answer}_i は i 番目のテストケースに対する出力を意味する。 各テストケースに対しては、以下の通り出力せよ。

A のうち A_k を除いた N-1 個の要素の最小公倍数を 998244353 で割ったあまりが L_k であるとする。 このとき以下の形式で出力せよ。

L_1 L_2 \cdots L_N

入力例 1

3
5
9 12 25 8 15
8
592 26 167 912 171 321 327 651
10
9566582 2785103 1924635 359502 6912831 4893928 2820155 5071742 1836019 9037883

出力例 1

600 1800 360 900 1800
447582749 506009091 523568328 588652065 196217355 471970745 921220985 738745385
747032704 756838459 344127037 146466685 159487731 555429485 826726159 884617928 322846201 298477407

一個目のテストケースについて、以下のようになります。

  • A のうち A_1 を除いた N-1 個の要素は 12,25,8,15 であり、これらの最小公倍数は 600 です。
  • A のうち A_2 を除いた N-1 個の要素は 9,25,8,15 であり、これらの最小公倍数は 1800 です。
  • A のうち A_3 を除いた N-1 個の要素は 9,12,8,15 であり、これらの最小公倍数は 360 です。
  • A のうち A_4 を除いた N-1 個の要素は 9,12,25,15 であり、これらの最小公倍数は 900 です。
  • A のうち A_5 を除いた N-1 個の要素は 9,12,25,8 であり、これらの最小公倍数は 1800 です。

Score : 475 points

Problem Statement

You are given a sequence of positive integers A=(A_1,A_2,\dots,A_N) of length N.

For each k=1,2,\dots,N, find the remainder when the least common multiple of the N-1 elements of A excluding A_k is divided by 998244353.

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

Constraints

  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^7
  • All input values are integers.
  • The sum of N over all test cases in a single input is at most 2 \times 10^5.

Input

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

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

Here, \mathrm{case}_i denotes the input for the i-th test case. Each test case is given in the following format:

N
A_1 A_2 \cdots A_N

Output

Output in the following format:

\mathrm{answer}_1
\mathrm{answer}_2
\vdots
\mathrm{answer}_T

Here, \mathrm{answer}_i denotes the output for the i-th test case. For each test case, output as follows.

Let L_k be the remainder when the least common multiple of the N-1 elements of A excluding A_k is divided by 998244353. Then, output in the following format:

L_1 L_2 \cdots L_N

Sample Input 1

3
5
9 12 25 8 15
8
592 26 167 912 171 321 327 651
10
9566582 2785103 1924635 359502 6912831 4893928 2820155 5071742 1836019 9037883

Sample Output 1

600 1800 360 900 1800
447582749 506009091 523568328 588652065 196217355 471970745 921220985 738745385
747032704 756838459 344127037 146466685 159487731 555429485 826726159 884617928 322846201 298477407

For the first test case, the following holds.

  • The N-1 elements of A excluding A_1 are 12,25,8,15, and their least common multiple is 600.
  • The N-1 elements of A excluding A_2 are 9,25,8,15, and their least common multiple is 1800.
  • The N-1 elements of A excluding A_3 are 9,12,8,15, and their least common multiple is 360.
  • The N-1 elements of A excluding A_4 are 9,12,25,15, and their least common multiple is 900.
  • The N-1 elements of A excluding A_5 are 9,12,25,8, and their least common multiple is 1800.
I - Farthest Pair Query

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 550 点

問題文

N 頂点の木があります。頂点には 1, 2, \ldots, N の番号が付けられており、i 番目の辺は頂点 U_i と頂点 V_i を結んでいます。

はじめ、すべての頂点は黒く塗られています。

以下のクエリを Q 個順に処理したときの各クエリの答えを求めてください。

  • 整数 x (1 \leq x \leq N) が与えられる。頂点 x が白く塗られているならば黒く、黒く塗られているならば白く塗り替える。その後、黒く塗られている頂点どうしの距離の最大値を求める。ただし、木上の 2 頂点間の距離とは、その 2 頂点を端点とする単純パスに含まれる辺の本数のことを指す。

なお、クエリを順に処理したとき、操作の過程で黒く塗られている頂点が常に 2 個以上存在するような入力のみが与えられます。

制約

  • 3 \leq N \leq 10^5
  • 1 \leq U_i, V_i \leq N
  • 与えられるグラフは木
  • 1 \leq Q \leq 10^5
  • 各クエリについて、1 \leq x \leq N
  • どの時点でも黒く塗られている頂点が 2 個以上存在する
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ここで \text{query}_i は i 番目のクエリを意味する。

N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリは以下の形式で与えられる。

x

出力

Q 行出力せよ。

i 行目には、クエリを順に処理したときの i 番目のクエリの答えを出力せよ。


入力例 1

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

出力例 1

4
3
3
2
2
3
2
3
4
  • 1 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 2,3,4,5,6,7 です。 頂点 4 と頂点 6 の距離は 4 であり、答えは 4 です。
  • 2 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 2,3,5,6,7 です。 頂点 2 と頂点 6 の距離は 3 であり、答えは 3 です。
  • 3 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 3,5,6,7 です。 頂点 5 と頂点 6 の距離は 3 であり、答えは 3 です。
  • 4 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 3,5,7 です。 頂点 5 と頂点 7 の距離は 2 であり、答えは 2 です。
  • 5 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 5,7 です。 頂点 5 と頂点 7 の距離は 2 であり、答えは 2 です。
  • 6 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 1,5,7 です。 頂点 1 と頂点 5 の距離は 3 であり、答えは 3 です。
  • 7 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 5,7 です。 頂点 5 と頂点 7 の距離は 2 であり、答えは 2 です。
  • 8 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 4,5,7 です。 頂点 4 と頂点 5 の距離は 3 であり、答えは 3 です。
  • 9 番目のクエリで色を塗り替えた後、黒く塗られている頂点は 4,5,6,7 です。 頂点 4 と頂点 6 の距離は 4 であり、答えは 4 です。

上に挙げた例は最大値を達成する一例であることに注意してください。

Score : 550 points

Problem Statement

There is a tree with N vertices. The vertices are numbered 1, 2, \ldots, N, and the i-th edge connects vertices U_i and V_i.

Initially, all vertices are painted black.

Process Q queries of the following form in order and find the answer for each query.

  • An integer x (1 \leq x \leq N) is given. If vertex x is white, repaint it black; if vertex x is black, repaint it white. Then, find the maximum distance between two black vertices. Here, the distance between two vertices on a tree is the number of edges in the simple path between them.

In the given inputs, there are always at least two black vertices when processing the queries in order.

Constraints

  • 3 \leq N \leq 10^5
  • 1 \leq U_i, V_i \leq N
  • The given graph is a tree.
  • 1 \leq Q \leq 10^5
  • For each query, 1 \leq x \leq N.
  • There are always at least two black vertices.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \text{query}_i denotes the i-th query:

N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in the following format:

x

Output

Output Q lines.

The i-th line should contain the answer for the i-th query when processed in order.


Sample Input 1

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

Sample Output 1

4
3
3
2
2
3
2
3
4
  • After the repainting in the 1st query, the black vertices are 2, 3, 4, 5, 6, 7. The distance between vertices 4 and 6 is 4, and the answer is 4.
  • After the repainting in the 2nd query, the black vertices are 2, 3, 5, 6, 7. The distance between vertices 2 and 6 is 3, and the answer is 3.
  • After the repainting in the 3rd query, the black vertices are 3, 5, 6, 7. The distance between vertices 5 and 6 is 3, and the answer is 3.
  • After the repainting in the 4th query, the black vertices are 3, 5, 7. The distance between vertices 5 and 7 is 2, and the answer is 2.
  • After the repainting in the 5th query, the black vertices are 5, 7. The distance between vertices 5 and 7 is 2, and the answer is 2.
  • After the repainting in the 6th query, the black vertices are 1, 5, 7. The distance between vertices 1 and 5 is 3, and the answer is 3.
  • After the repainting in the 7th query, the black vertices are 5, 7. The distance between vertices 5 and 7 is 2, and the answer is 2.
  • After the repainting in the 8th query, the black vertices are 4, 5, 7. The distance between vertices 4 and 5 is 3, and the answer is 3.
  • After the repainting in the 9th query, the black vertices are 4, 5, 6, 7. The distance between vertices 4 and 6 is 4, and the answer is 4.

Note that the examples given above are just one instance achieving the maximum value.