E - Long Sequence

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

配点 : 300 点

問題文

長さ N の正整数のみからなる数列 A=(A_1,\dots,A_N) があります。
A を 10^{100} 回連結した数列を数列 B とします。

B の項を前から順に足したとき、和が初めて X を超えるのは何項目まで足したときですか?
すなわち、以下の式を満たす最小の整数 k を求めてください。

\displaystyle{\sum_{i=1}^{k} B_i \gt X}

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq 10^{18}
  • 入力は全て整数

入力

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

N
A_1 \ldots A_N
X

出力

答えを出力せよ。


入力例 1

3
3 5 2
26

出力例 1

8

B=(3,5,2,3,5,2,3,5,2,\dots) です。
\displaystyle{\sum_{i=1}^{8} B_i = 28 \gt 26} であり、k が 7 以下のとき条件を満たさないので、8 が答えです。


入力例 2

4
12 34 56 78
1000

出力例 2

23

Score : 300 points

Problem Statement

We have a sequence of N positive integers: A=(A_1,\dots,A_N).
Let B be the concatenation of 10^{100} copies of A.

Consider summing up the terms of B from left to right. When does the sum exceed X for the first time?
In other words, find the minimum integer k such that:

\displaystyle{\sum_{i=1}^{k} B_i \gt X}.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N
X

Output

Print the answer.


Sample Input 1

3
3 5 2
26

Sample Output 1

8

We have B=(3,5,2,3,5,2,3,5,2,\dots).
\displaystyle{\sum_{i=1}^{8} B_i = 28 \gt 26} holds, but the condition is not satisfied when k is 7 or less, so the answer is 8.


Sample Input 2

4
12 34 56 78
1000

Sample Output 2

23
F - Jumping Takahashi

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

配点 : 300 点

問題文

高橋君は数直線上の座標 0 の位置にいます。

これから高橋君は N 回のジャンプを行います。i \, (1 \leq i \leq N) 回目のジャンプでは、正の方向に a_i または b_i 移動します。

N 回のジャンプの後に座標 X の位置にいるようにすることはできますか?

制約

  • 1 \leq N \leq 100
  • 1 \leq a_i \lt b_i \leq 100 \, (1 \leq i \leq N)
  • 1 \leq X \leq 10000
  • 入力は全て整数

入力

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

N X
a_1 b_1
\vdots
a_N b_N

出力

N 回のジャンプの後に座標 X の位置にいるようにすることができるならば Yes と、そうでないなら No と出力せよ。


入力例 1

2 10
3 6
4 5

出力例 1

Yes

1 回目のジャンプでは b_1 (= 6) 移動し、2 回目のジャンプでは a_2 (= 4) 移動することで、座標 X (= 10) の位置にいるようにすることができます。


入力例 2

2 10
10 100
10 100

出力例 2

No

1 回目のジャンプの後に座標 X (= 10) の位置にいるようにすることはできますが、全てのジャンプの後に座標 X (= 10) の位置にいるようにすることはできません。


入力例 3

4 12
1 8
5 7
3 4
2 6

出力例 3

Yes

Score : 300 points

Problem Statement

Takahashi is standing at the coordinate 0 on a number line.

He will now perform N jumps. In the i-th jump (1 \leq i \leq N), he moves a_i or b_i in the positive direction.

Is it possible for him to be at the coordinate X after N jumps?

Constraints

  • 1 \leq N \leq 100
  • 1 \leq a_i \lt b_i \leq 100 \, (1 \leq i \leq N)
  • 1 \leq X \leq 10000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
a_1 b_1
\vdots
a_N b_N

Output

If it is possible for Takahashi to be at the coordinate X after N jumps, print Yes; otherwise, print No.


Sample Input 1

2 10
3 6
4 5

Sample Output 1

Yes

By moving b_1 (= 6) in the first jump and a_2 (= 4) in the second jump, he can be at the coordinate X (= 10).


Sample Input 2

2 10
10 100
10 100

Sample Output 2

No

He can be at the coordinate X (= 10) after the first jump, but not after all jumps.


Sample Input 3

4 12
1 8
5 7
3 4
2 6

Sample Output 3

Yes
G - Take ABC

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

配点 : 425 点

問題文

A , B , C の 3 種類の文字のみからなる文字列 S が与えられます。

S が連続な部分文字列として文字列 ABC を含む限り、下記の操作を繰り返します。

S に連続な部分文字列として含まれる文字列 ABC のうち、S の中で最も左にあるものを、S から削除する。

上記の手順を行った後の、最終的な S を出力してください。

制約

  • S は A , B , C のみからなる長さ 1 以上 2\times 10^5 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

BAABCBCCABCAC

出力例 1

BCAC

与えられた文字列 S = BAABCBCCABCAC に対して、下記の通りに操作が行われます。

  • 1 回目の操作で、S = BAABCBCCABCAC の 3 文字目から 5 文字目の ABC が削除され、その結果 S = BABCCABCAC となります。
  • 2 回目の操作で、S = BABCCABCAC の 2 文字目から 4 文字目の ABC が削除され、その結果 S = BCABCAC となります。
  • 3 回目の操作で、S = BCABCAC の 3 文字目から 5 文字目の ABC が削除され、その結果 S = BCAC となります。

よって、最終的な S は BCAC です。


入力例 2

ABCABC

出力例 2



この入力例では、最終的な S は空文字列です。


入力例 3

AAABCABCABCAABCABCBBBAABCBCCCAAABCBCBCC

出力例 3

AAABBBCCC

Score : 425 points

Problem Statement

You are given a string S consisting of three different characters: A, B, and C.

As long as S contains the string ABC as a consecutive substring, repeat the following operation:

Remove the leftmost occurrence of the substring ABC from S.

Print the final string S after performing the above procedure.

Constraints

  • S is a string of length between 1 and 2 \times 10^5, inclusive, consisting of the characters A, B, and C.

Input

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

S

Output

Print the answer.


Sample Input 1

BAABCBCCABCAC

Sample Output 1

BCAC

For the given string S = BAABCBCCABCAC, the operations are performed as follows.

  • In the first operation, the ABC from the 3-rd to the 5-th character in S = BAABCBCCABCAC is removed, resulting in S = BABCCABCAC.
  • In the second operation, the ABC from the 2-nd to the 4-th character in S = BABCCABCAC is removed, resulting in S = BCABCAC.
  • In the third operation, the ABC from the 3-rd to the 5-th character in S = BCABCAC is removed, resulting in S = BCAC.

Therefore, the final S is BCAC.


Sample Input 2

ABCABC

Sample Output 2



In this example, the final S is an empty string.


Sample Input 3

AAABCABCABCAABCABCBBBAABCBCCCAAABCBCBCC

Sample Output 3

AAABBBCCC
H - Family and Insurance

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

配点 : 425 点

問題文

人 1、人 2、\ldots、人 N からなる家系があります。i\geq 2 に対し、人 i の親は人 p_i です。

この家系の人たちは M 回保険に加入しました。i=1,2,\ldots,M に対し、i 番目の保険の加入者は人 x_i で、本人及びその y_i 代先までの子たちが補償対象者です。

1 個以上の保険の補償対象者になっている人が何人いるかを求めてください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq M \leq 3 \times 10^5
  • 1 \leq p_i \leq i-1
  • 1 \leq x_i \leq N
  • 1 \leq y_i \leq 3 \times 10^5
  • 入力はすべて整数

入力

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

N M
p_2 \ldots p_N
x_1 y_1
\vdots
x_M y_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

4

1 番目の保険について、人 1 の 1 代先の子たちは人 2 と人 4 なので人 1,2,4 が補償対象者です。
2 番目の保険について、人 1 の 1 代先の子たちは人 2 と人 4、2 代先の子は人 3 なので人 1,2,3,4 が補償対象者です。
3 番目の保険について、人 4 の 1,2,3 代先の子は存在しないので人 4 が補償対象者です。

以上より、1 個以上の保険の補償対象者になっている人は人 1,2,3,4 の 4 人です。


入力例 2

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

出力例 2

10

Score : 425 points

Problem Statement

There is a family consisting of person 1, person 2, \ldots, and person N. For i\geq 2, person i's parent is person p_i.

They bought insurance M times. For i=1,2,\ldots,M, person x_i bought the i-th insurance, which covers that person and their descendants in the next y_i generations.

How many people are covered by at least one insurance?

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq M \leq 3 \times 10^5
  • 1 \leq p_i \leq i-1
  • 1 \leq x_i \leq N
  • 1 \leq y_i \leq 3 \times 10^5
  • All input values are integers.

Input

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

N M
p_2 \ldots p_N
x_1 y_1
\vdots
x_M y_M

Output

Print the answer.


Sample Input 1

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

Sample Output 1

4

The 1-st insurance covers people 1, 2, and 4, because person 1's 1-st generation descendants are people 2 and 4.
The 2-nd insurance covers people 1, 2, 3, and 4, because person 1's 1-st generation descendants are people 2 and 4, and person 1's 2-nd generation descendant is person 3.
The 3-rd insurance covers person 4, because person 4 has no 1-st, 2-nd, or 3-rd descendants.

Therefore, four people, people 1, 2, 3, and 4, are covered by at least one insurance.


Sample Input 2

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

Sample Output 2

10
I - Xor Minimization

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

配点 : 500 点

問題文

非負整数列 A=(a_1,\ldots,a_N) が与えられます。

A に対して次の操作をちょうど 1 回行います。

  • 非負整数 x を選ぶ。そして、i=1,\ldots,N すべてに対し、a_i の値を「a_i と x のビット単位 xor」に置き換える。

操作後の A に含まれる値の最大値を M とします。M の最小値を求めてください。

ビット単位 xor とは 非負整数 A, B のビット単位 xor 、A \oplus B は、以下のように定義されます。
  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。

制約

  • 1 \leq N \leq 1.5 \times 10^5
  • 0 \leq a_i \lt 2^{30}
  • 入力はすべて整数

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

3
12 18 11

出力例 1

16

x=2 として操作をすると、操作後の数列は (12 \oplus 2,18 \oplus 2, 11 \oplus 2) = (14,16,9) となり、最大値 M は 16 となります。
M を 16 より小さくすることは出来ないため、この値が答えです。


入力例 2

10
0 0 0 0 0 0 0 0 0 0

出力例 2

0

入力例 3

5
324097321 555675086 304655177 991244276 9980291

出力例 3

805306368

Score : 500 points

Problem Statement

You are given a sequence of non-negative integers A=(a_1,\ldots,a_N).

Let us perform the following operation on A just once.

  • Choose a non-negative integer x. Then, for every i=1, \ldots, N, replace the value of a_i with the bitwise XOR of a_i and x.

Let M be the maximum value in A after the operation. Find the minimum possible value of M.

What is bitwise XOR? The bitwise XOR of non-negative integers A and B, A \oplus B, is defined as follows.
  • When A \oplus B is written in binary, the k-th lowest bit (k \geq 0) is 1 if exactly one of the k-th lowest bits of A and B is 1, and 0 otherwise.
For instance, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110).

Constraints

  • 1 \leq N \leq 1.5 \times 10^5
  • 0 \leq a_i \lt 2^{30}
  • All values in the input are integers.

Input

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

N
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

3
12 18 11

Sample Output 1

16

If we do the operation with x=2, the sequence becomes (12 \oplus 2,18 \oplus 2, 11 \oplus 2) = (14,16,9), where the maximum value M is 16.
We cannot make M smaller than 16, so this value is the answer.


Sample Input 2

10
0 0 0 0 0 0 0 0 0 0

Sample Output 2

0

Sample Input 3

5
324097321 555675086 304655177 991244276 9980291

Sample Output 3

805306368