A - Sanitize Hands

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

消毒液の入ったボトルがあり、その消毒液によってちょうど M 本の手を消毒することができます。

N 人の宇宙人が順に手の消毒を行いに来ます。
i 人目 (1\leq i\leq N) の宇宙人は H_i 本の手を持っており、それぞれ自身のすべての手を 1 回ずつ消毒したいと考えています。

何人目の宇宙人までがすべての手を消毒できるか求めてください。
ただし、ある宇宙人が消毒を始める時点で、自身のすべての手を消毒する分の消毒液が残っていなかったとしても、その宇宙人はその消毒液を使い切ってしまうものとします。

制約

  • 1\leq N,M\leq 100
  • 1\leq H_i\leq 100
  • 入力はすべて整数

入力

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

N M
H_1 H_2 \ldots H_N

出力

何人目の宇宙人までが自身のすべての手を消毒できるか出力せよ。


入力例 1

5 10
2 3 2 5 3

出力例 1

3

次の手順で宇宙人は自身の手を消毒します。

  • 1 人目の宇宙人は自身の 2 本の手を消毒します。残りの消毒液によって、10-2=8 本の手を消毒できます。
  • 2 人目の宇宙人は自身の 3 本の手を消毒します。残りの消毒液によって、8-3=5 本の手を消毒できます。
  • 3 人目の宇宙人は自身の 2 本の手を消毒します。残りの消毒液によって、5-2=3 本の手を消毒できます。
  • 4 人目の宇宙人は 5 本の手を持っていますが、消毒液は 3 本分しかないため消毒液を使い切り、かつ自身のすべての手を消毒できません。

よって、3 人目の宇宙人までが自身のすべての手を消毒できるため、3 を出力します。


入力例 2

5 10
2 3 2 3 5

出力例 2

4

入力例 3

1 5
1

出力例 3

1

すべての宇宙人が自身の手を消毒することができます。

Score : 100 points

Problem Statement

There is a bottle of disinfectant that can disinfect exactly M hands.

N aliens come one by one to disinfect their hands.
The i-th alien (1 \leq i \leq N) has H_i hands and wants to disinfect all of their hands once.

Determine how many aliens can disinfect all of their hands.
Here, even if there is not enough disinfectant left for an alien to disinfect all of their hands when they start, they will use up the remaining disinfectant.

Constraints

  • 1 \leq N, M \leq 100
  • 1 \leq H_i \leq 100
  • All input values are integers.

Input

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

N M
H_1 H_2 \ldots H_N

Output

Print the number of aliens who can disinfect all of their hands.


Sample Input 1

5 10
2 3 2 5 3

Sample Output 1

3

The aliens disinfect their hands in the following steps:

  • The first alien disinfects their two hands. The remaining disinfectant can disinfect 10-2=8 hands.
  • The second alien disinfects their three hands. The remaining disinfectant can disinfect 8-3=5 hands.
  • The third alien disinfects their two hands. The remaining disinfectant can disinfect 5-2=3 hands.
  • The fourth alien has five hands, but there is only enough disinfectant for three hands, so they use up the disinfectant without disinfecting all of their hands.

Thus, the first three aliens can disinfect all of their hands, so print 3.


Sample Input 2

5 10
2 3 2 3 5

Sample Output 2

4

Sample Input 3

1 5
1

Sample Output 3

1

All aliens can disinfect their hands.

B - Jogging

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君と青木君はジョギングをすることにしました。
高橋君は「A 秒間秒速 B メートルで歩き、C 秒間休む」ことを繰り返します。
青木君は「D 秒間秒速 E メートルで歩き、F 秒間休む」ことを繰り返します。
二人が同時にジョギングを始めてから X 秒後、高橋君と青木君のうちどちらが長い距離を進んでいますか?

制約

  • 1 \leq A, B, C, D, E, F, X \leq 100
  • 入力は全て整数

入力

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

A B C D E F X

出力

二人が同時にジョギングを始めてから X 秒後時点で、高橋君の方が青木君よりも長い距離を進んでいるならば Takahashi、青木君の方が高橋君よりも長い距離を進んでいるならば Aoki、二人が同じ距離を進んでいるならば Draw と出力せよ。


入力例 1

4 3 3 6 2 5 10

出力例 1

Takahashi

二人はジョギングを始めてから 10 秒間の間、以下のように行動します。

  • 高橋君は 4 秒間歩き、3 秒間休んだ後、再び 3 秒間歩く。合計 (4 + 3) \times 3 = 21 メートル歩く。
  • 青木君は 6 秒間歩き、4 秒間休む。合計 6 \times 2 = 12 メートル歩く。

高橋君の方が長い距離を進んでいるので、Takahashi と出力します。


入力例 2

3 1 4 1 5 9 2

出力例 2

Aoki

入力例 3

1 1 1 1 1 1 1

出力例 3

Draw

Score : 100 points

Problem Statement

Takahashi and Aoki decided to jog.
Takahashi repeats the following: "walk at B meters a second for A seconds and take a rest for C seconds."
Aoki repeats the following: "walk at E meters a second for D seconds and take a rest for F seconds."
When X seconds have passed since they simultaneously started to jog, which of Takahashi and Aoki goes ahead?

Constraints

  • 1 \leq A, B, C, D, E, F, X \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D E F X

Output

When X seconds have passed since they simultaneously started to jog, if Takahashi goes ahead of Aoki, print Takahashi; if Aoki goes ahead of Takahashi, print Aoki; if they have advanced the same distance, print Draw.


Sample Input 1

4 3 3 6 2 5 10

Sample Output 1

Takahashi

During the first 10 seconds after they started to jog, they move as follows.

  • Takahashi walks for 4 seconds, takes a rest for 3 seconds, and walks again for 3 seconds. As a result, he advances a total of (4 + 3) \times 3 = 21 meters.
  • Aoki walks for 6 seconds and takes a rest for 4 seconds. As a result, he advances a total of 6 \times 2 = 12 meters.

Since Takahashi goes ahead, Takahashi should be printed.


Sample Input 2

3 1 4 1 5 9 2

Sample Output 2

Aoki

Sample Input 3

1 1 1 1 1 1 1

Sample Output 3

Draw
C - The Middle Day

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

AtCoder 国の暦では、一年は 1,2,\dots,M 番目の月の M か月からなり、そのうち i 番目の月は 1,2,\dots,D_i 番目の日の D_i 日からなります。
さらに、 AtCoder 国の一年の日数は奇数、即ち D_1+D_2+\dots+D_M は奇数です。
一年の真ん中の日は何番目の月の何番目の日か求めてください。
言い換えると、 1 番目の月の 1 番目の日を 1 日目としたときの (D_1+D_2+\dots+D_M+1)/2 日目が何番目の月の何番目の日かを求めてください。

制約

  • 入力は全て整数
  • 1 \le M \le 100
  • 1 \le D_i \le 100
  • D_1 + D_2 + \dots + D_M は奇数

入力

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

M
D_1 D_2 \dots D_M

出力

答えが a 番目の月の b 番目の日であるとき、以下の形式で出力せよ。

a b

入力例 1

12
31 28 31 30 31 30 31 31 30 31 30 31

出力例 1

7 2

この入力では、 1 年は 31+28+31+30+31+30+31+31+30+31+30+31=365 日からなります。
真ん中の日は (365+1)/2 = 183 日目であり、これを求めることを考えます。

  • 1,2,3,4,5,6 番目の月に含まれる日数の合計は 181 日です。
  • 7 番目の月の 1 番目の日は 182 日目です。
  • 7 番目の月の 2 番目の日は 183 日目です。

以上から、答えが 7 番目の月の 2 番目の日であることが分かります。


入力例 2

1
1

出力例 2

1 1

入力例 3

6
3 1 4 1 5 9

出力例 3

5 3

Score : 200 points

Problem Statement

In the calendar of AtCoderLand, a year consists of M months: month 1, month 2, \dots, month M. The i-th month consists of D_i days: day 1, day 2, \dots, day D_i.
Furthermore, the number of days in a year is odd, that is, D_1+D_2+\dots+D_M is odd.
Find what day of what month is the middle day of the year.
In other words, let day 1 of month 1 be the first day, and find a and b such that the ((D_1+D_2+\dots+D_M+1)/2)-th day is day b of month a.

Constraints

  • All input values are integers.
  • 1 \le M \le 100
  • 1 \le D_i \le 100
  • D_1 + D_2 + \dots + D_M is odd.

Input

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

M
D_1 D_2 \dots D_M

Output

Let the answer be day b of month a, and print it in the following format:

a b

Sample Input 1

12
31 28 31 30 31 30 31 31 30 31 30 31

Sample Output 1

7 2

In this input, a year consists of 31+28+31+30+31+30+31+31+30+31+30+31=365 days.
Let us find the middle day, which is the ((365+1)/2 = 183)-th day.

  • Months 1,2,3,4,5,6 contain a total of 181 days.
  • Day 1 of month 7 is the 182-th day.
  • Day 2 of month 7 is the 183-th day.

Thus, the answer is day 2 of month 7.


Sample Input 2

1
1

Sample Output 2

1 1

Sample Input 3

6
3 1 4 1 5 9

Sample Output 3

5 3
D - Nutrients

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150

問題文

健康に気を使っている高橋君は、M 種類の栄養素について、食事によって十分な量を摂取できているか気になりました。

i 番目の栄養素は 1 日あたり A_i 以上摂取することが目標です。

高橋君は今日 N 品の食品を食べ、i 品目の食品からは栄養素 jX_{i,j} 摂取しました。

M 種類全ての栄養素で目標を達成しているかどうかを判定してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 0 \leq A_i,X_{i,j} \leq 10^7
  • 入力は全て整数である

入力

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

N M
A_1 \ldots A_M
X_{1,1} \ldots X_{1,M}
\vdots
X_{N,1} \ldots X_{N,M}

出力

M 種類全ての栄養素で目標を達成しているなら Yes、そうでないならば No を出力せよ。


入力例 1

2 3
10 20 30
20 0 10
0 100 100

出力例 1

Yes

栄養素 11 品目から 202 品目から 0 摂取したため、合わせて 20 摂取しており、10 以上摂取するという目標を達成しています。
栄養素 2,3 についても同様に目標を達成しています。


入力例 2

2 4
10 20 30 40
20 0 10 30
0 100 100 0

出力例 2

No

栄養素 4 について目標を達成していません。

Score : 150 points

Problem Statement

Takahashi is health-conscious and concerned about whether he is getting enough of M types of nutrients from his diet.

For the i-th nutrient, his goal is to take at least A_i units per day.

Today, he ate N foods, and from the i-th food, he took X_{i,j} units of nutrient j.

Determine whether he has met the goal for all M types of nutrients.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 0 \leq A_i, X_{i,j} \leq 10^7
  • All input values are integers.

Input

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

N M
A_1 \ldots A_M
X_{1,1} \ldots X_{1,M}
\vdots
X_{N,1} \ldots X_{N,M}

Output

Print Yes if the goal is met for all M types of nutrients, and No otherwise.


Sample Input 1

2 3
10 20 30
20 0 10
0 100 100

Sample Output 1

Yes

For nutrient 1, Takahashi took 20 units from the 1-st food and 0 units from the 2-nd food, totaling 20 units, thus meeting the goal of taking at least 10 units.
Similarly, he meets the goal for nutrients 2 and 3.


Sample Input 2

2 4
10 20 30 40
20 0 10 30
0 100 100 0

Sample Output 2

No

The goal is not met for nutrient 4.

E - False Hope

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

3\times3 のマス目に 1 から 9 までの数字が書き込まれており、上から i 行目、左から j 列目 (1\leq i\leq3,1\leq j\leq3) に書き込まれている数字は c _ {i,j} です。

異なるマスに同じ数字が書き込まれている場合もありますが、同じ数字が縦・横・斜めに 3 つ連続して書き込まれていることはありません。 より厳密には、c _ {i,j} について次の条件のすべてが成り立っていることが保証されます。

  • どの 1\leq i\leq3 についても、c _ {i,1}=c _ {i,2}=c _ {i,3} ではない
  • どの 1\leq j\leq3 についても、c _ {1,j}=c _ {2,j}=c _ {3,j} ではない
  • c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
  • c _ {3,1}=c _ {2,2}=c _ {1,3} ではない

高橋くんは、それぞれのマスに書かれている数字をランダムな順番で知ります。 高橋くんは、縦・横・斜めの列のうちの 1 つでも次の条件を満たしたときがっかりします。

  • はじめに知ったほうの 2 マスに書かれた数字が同じであり、最後に知ったマスに書かれた数字がそれと異なる。

高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を求めてください。

制約

  • c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
  • c _ {i,1}=c _ {i,2}=c _ {i,3} ではない (1\leq i\leq3)
  • c _ {1,j}=c _ {2,j}=c _ {3,j} ではない (1\leq j\leq3)
  • c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
  • c _ {1,3}=c _ {2,2}=c _ {3,1} ではない

入力

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

c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}

出力

高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を 1 行で出力せよ。 真の値からの絶対誤差が 10 ^ {-8} 以下であるとき、正答と判定される。


入力例 1

3 1 9
2 5 6
2 7 1

出力例 1

0.666666666666666666666666666667

例えば、高橋くんが c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 の順に知った場合、高橋くんはがっかりしてしまいます。

対して、高橋くんが c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} の順に数字を知った場合、がっかりすることなくすべての数字を知ることができます。

高橋くんががっかりすることなくすべての数字を知ることができる確率は \dfrac 23 です。 絶対誤差が 10 ^ {-8} 以下であれば正答と判定されるため、0.6666666570.666666676 のように出力しても正解になります。


入力例 2

7 7 6
8 6 8
7 7 6

出力例 2

0.004982363315696649029982363316

入力例 3

3 6 7
1 9 7
5 7 5

出力例 3

0.4

Score : 300 points

Problem Statement

There is a 3\times3 grid with numbers between 1 and 9, inclusive, written in each square. The square at the i-th row from the top and j-th column from the left (1\leq i\leq3,1\leq j\leq3) contains the number c _ {i,j}.

The same number may be written in different squares, but not in three consecutive cells vertically, horizontally, or diagonally. More precisely, it is guaranteed that c _ {i,j} satisfies all of the following conditions.

  • c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
  • c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
  • c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
  • c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.

Takahashi will see the numbers written in each cell in random order. He will get disappointed when there is a line (vertical, horizontal, or diagonal) that satisfies the following condition.

  • The first two squares he sees contain the same number, but the last square contains a different number.

Find the probability that Takahashi sees the numbers in all the squares without getting disappointed.

Constraints

  • c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
  • c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
  • c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
  • c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
  • c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.

Input

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

c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}

Output

Print one line containing the probability that Takahashi sees the numbers in all the squares without getting disappointed. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}.


Sample Input 1

3 1 9
2 5 6
2 7 1

Sample Output 1

0.666666666666666666666666666667

For example, if Takahashi sees c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 in this order, he will get disappointed.

On the other hand, if Takahashi sees c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} in this order, he will see all numbers without getting disappointed.

The probability that Takahashi sees all the numbers without getting disappointed is \dfrac 23. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}, so outputs such as 0.666666657 and 0.666666676 would also be accepted.


Sample Input 2

7 7 6
8 6 8
7 7 6

Sample Output 2

0.004982363315696649029982363316

Sample Input 3

3 6 7
1 9 7
5 7 5

Sample Output 3

0.4
F - Count Arithmetic Subarrays

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

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

1\leq l\leq r\leq N を満たす整数の組 (l,r) であって、数列 (A_l,A_{l+1},\dots,A_r) が等差数列であるようなものが何通りあるか求めてください。

なお、数列 (x_1,x_2,\dots,x_{|x|}) が等差数列であるとは、ある d が存在して x_{i+1}-x_i=d\ (1\leq i < |x|) であることをいいます。 特に、長さ 1 の数列は常に等差数列です。

制約

  • 1\leq N \leq 2\times 10^5
  • 1\leq A_i \leq 10^9
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4
3 6 9 3

出力例 1

8

条件を満たす整数の組 (l,r)(1,1),(2,2),(3,3),(4,4),(1,2),(2,3),(3,4),(1,3)8 通りです。

実際、(l,r)=(1,3) のとき (A_l,\dots,A_r)=(3,6,9) は等差数列なので条件を満たしますが、 (l,r)=(2,4) のとき (A_l,\dots,A_r)=(6,9,3) は等差数列ではないので条件を満たしません。


入力例 2

5
1 1 1 1 1

出力例 2

15

すべての整数の組 (l,r)\ (1\leq l\leq r\leq 5) が条件を満たします。


入力例 3

8
87 42 64 86 72 58 44 30

出力例 3

22

Score : 300 points

Problem Statement

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

Find the number of pairs of integers (l,r) satisfying 1\leq l\leq r\leq N such that the subsequence (A_l,A_{l+1},\dots,A_r) forms an arithmetic progression.

A sequence (x_1,x_2,\dots,x_{|x|}) is an arithmetic progression if and only if there exists a d such that x_{i+1}-x_i=d\ (1\leq i < |x|). In particular, a sequence of length 1 is always an arithmetic progression.

Constraints

  • 1\leq N \leq 2\times 10^5
  • 1\leq A_i \leq 10^9
  • All input values 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

4
3 6 9 3

Sample Output 1

8

There are eight pairs of integers (l,r) satisfying the condition: (1,1),(2,2),(3,3),(4,4),(1,2),(2,3),(3,4),(1,3).

Indeed, when (l,r)=(1,3), (A_l,\dots,A_r)=(3,6,9) is an arithmetic progression, so it satisfies the condition. However, when (l,r)=(2,4), (A_l,\dots,A_r)=(6,9,3) is not an arithmetic progression, so it does not satisfy the condition.


Sample Input 2

5
1 1 1 1 1

Sample Output 2

15

All pairs of integers (l,r)\ (1\leq l\leq r\leq 5) satisfy the condition.


Sample Input 3

8
87 42 64 86 72 58 44 30

Sample Output 3

22
G - LR insertion

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

1 個の 0 のみからなる数列 A=(0) があります。
また、LR のみからなる長さ N の文字列 S=s_1s_2\ldots s_N が与えられます。

i=1,2,\ldots ,N の順番で、次の操作を行います。

  • s_iL のとき、A 内にある i-1 のすぐ左に i を挿入する
  • s_iR のとき、A 内にある i-1 のすぐ右に i を挿入する

最終的な A を求めてください。

制約

  • 1\leq N \leq 5\times 10^5
  • N は整数である
  • |S| = N
  • s_iLR のいずれかである

入力

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

N
S

出力

最終的な A を空白区切りで出力せよ。


入力例 1

5
LRRLR

出力例 1

1 2 4 5 3 0

はじめ、A=(0) です。
s_1L なので、A=(1,0) となります。
s_2R なので、A=(1,2,0) となります。
s_3R なので、A=(1,2,3,0) となります。
s_4L なので、A=(1,2,4,3,0) となります。
s_5R なので、A=(1,2,4,5,3,0) となります。


入力例 2

7
LLLLLLL

出力例 2

7 6 5 4 3 2 1 0

Score : 400 points

Problem Statement

There is a sequence that contains one 0, A=(0).
Additionally, you are given a string of length N, S=s_1s_2\ldots s_N, consisting of L and R.

For each i=1, 2, \ldots, N in this order, the following will be done.

  • If s_i is L, insert i to the immediate left of i-1 in A.
  • If s_i is R, insert i to the immediate right of i-1 in A.

Find the final contents of A.

Constraints

  • 1\leq N \leq 5\times 10^5
  • N is an integer.
  • |S| = N
  • s_i is L or R.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the final contents of A, separated by spaces.


Sample Input 1

5
LRRLR

Sample Output 1

1 2 4 5 3 0

Initially, A=(0).
S_1 is L, which makes it A=(1,0).
S_2 is R, which makes it A=(1,2,0).
S_3 is R, which makes it A=(1,2,3,0).
S_4 is L, which makes it A=(1,2,4,3,0).
S_5 is R, which makes it A=(1,2,4,5,3,0).


Sample Input 2

7
LLLLLLL

Sample Output 2

7 6 5 4 3 2 1 0
H - Many Operations

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

変数 X と、X の値を変更する N 種類の操作があります。操作 i は整数の組 (T_i,A_i) で表され、意味は次の通りです。

  • T_i=1 のとき、X の値を X\ {\rm and}\ A_i に置き換える。
  • T_i=2 のとき、X の値を X\ {\rm or}\ A_i に置き換える。
  • T_i=3 のとき、X の値を X\ {\rm xor}\ A_i に置き換える。

変数 X を値 C で初期化した状態から、以下の処理を順に実行してください。

  • 操作 1 を行い、操作後の X の値を出力する。
  • 続けて、操作 1,2 を順に行い、操作後の X の値を出力する。
  • 続けて、操作 1,2,3 を順に行い、操作後の X の値を出力する。
  • \vdots
  • 続けて、操作 1,2,\ldots,N を順に行い、操作後の X の値を出力する。
{\rm and}, {\rm or}, {\rm xor} とは 非負整数 A, B{\rm and}, {\rm or}, {\rm xor} は、以下のように定義されます。
  • A\ {\rm and}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち両方が 1 であれば 1、そうでなければ 0 である。
  • A\ {\rm or}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも一方が 1 であれば 1、そうでなければ 0 である。
  • A\ {\rm xor}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうちちょうど一方が 1 であれば 1、そうでなければ 0 である。
例えば、3\ {\rm and}\ 5 = 13\ {\rm or}\ 5 = 73\ {\rm xor}\ 5 = 6 となります。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1\leq T_i \leq 3
  • 0\leq A_i \lt 2^{30}
  • 0\leq C \lt 2^{30}
  • 入力に含まれる値は全て整数である

入力

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

N C
T_1 A_1
T_2 A_2
\vdots
T_N A_N

出力

問題文中の指示に従って N 行出力せよ。


入力例 1

3 10
3 3
2 5
1 12

出力例 1

9
15
12

最初、X の値は 10 です。

  • 操作 1 を行うと X の値は 9 になります。
  • 続けて操作 1 を行うと X の値は 10 になり、さらに操作 2 を行うと 15 になります。
  • 続けて操作 1 を行うと X の値は 12 になり、さらに操作 2 を行うと 13 に、さらに続けて操作 3 を行うと 12 になります。

入力例 2

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

出力例 2

0
2
1
0
5
3
3
11
2

Score : 500 points

Problem Statement

We have a variable X and N kinds of operations that change the value of X. Operation i is represented as a pair of integers (T_i,A_i), and is the following operation:

  • if T_i=1, it replaces the value of X with X\ {\rm and}\ A_i;
  • if T_i=2, it replaces the value of X with X\ {\rm or}\ A_i;
  • if T_i=3, it replaces the value of X with X\ {\rm xor}\ A_i.

Initialize X with the value of C and execute the following procedures in order:

  • Perform Operation 1, and then print the resulting value of X.
  • Next, perform Operation 1, 2 in this order, and then print the value of X.
  • Next, perform Operation 1, 2, 3 in this order, and then print the value of X.
  • \vdots
  • Next, perform Operation 1, 2, \ldots, N in this order, and then print the value of X.
What are {\rm and}, {\rm or}, {\rm xor}?

The {\rm and}, {\rm or}, {\rm xor} of non-negative integers A and B are defined as follows:

  • When A\ {\rm and}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if both of the digits in that place of A and B are 1, and 0 otherwise.
  • When A\ {\rm or}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if at least one of the digits in that place of A and B is 1, and 0 otherwise.
  • When A\ {\rm xor}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of the digits in that place of A and B is 1, and 0 otherwise.
For example, 3\ {\rm and}\ 5 = 1, 3\ {\rm or}\ 5 = 7, and 3\ {\rm xor}\ 5 = 6.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1\leq T_i \leq 3
  • 0\leq A_i \lt 2^{30}
  • 0\leq C \lt 2^{30}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N C
T_1 A_1
T_2 A_2
\vdots
T_N A_N

Output

Print N lines, as specified in the Problem Statement.


Sample Input 1

3 10
3 3
2 5
1 12

Sample Output 1

9
15
12

The initial value of X is 10.

  • Operation 1 changes X to 9.
  • Next, Operation 1 changes X to 10, and then Operation 2 changes it to 15.
  • Next, Operation 1 changes X to 12, and then Operation 2 changes it to 13, and then Operation 3 changes it to 12.

Sample Input 2

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

Sample Output 2

0
2
1
0
5
3
3
11
2
I - Double Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

整数列 A = (A_1, A_2, \dots, A_N) が与えられます。
次の式を計算してください。

\displaystyle \sum_{i=1}^N \sum_{j=i+1}^N \max(A_j - A_i, 0)


なお、制約下において答えが 2^{63} 未満となることは保証されています。

制約

  • 2 \leq N \leq 4 \times 10^5
  • 0 \leq A_i \leq 10^8
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

式の値を出力せよ。


入力例 1

3
2 5 3

出力例 1

4

(i, j) = (1, 2) のとき \max(A_j - A_i, 0) = \max(3, 0) = 3 です。
(i, j) = (1, 3) のとき \max(A_j - A_i, 0) = \max(1, 0) = 1 です。
(i, j) = (2, 3) のとき \max(A_j - A_i, 0) = \max(-2, 0) = 0 です。
これらを足し合わせた 3 + 1 + 0 = 4 が答えとなります。


入力例 2

10
5 9 3 0 4 8 7 5 4 0

出力例 2

58

Score: 500 points

Problem Statement

You are given an integer sequence A = (A_1, A_2, \dots, A_N).
Calculate the following expression:

\displaystyle \sum_{i=1}^N \sum_{j=i+1}^N \max(A_j - A_i, 0)


The constraints guarantee that the answer is less than 2^{63}.

Constraints

  • 2 \leq N \leq 4 \times 10^5
  • 0 \leq A_i \leq 10^8
  • All input values 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 value of the expression.


Sample Input 1

3
2 5 3

Sample Output 1

4

For (i, j) = (1, 2), we have \max(A_j - A_i, 0) = \max(3, 0) = 3.
For (i, j) = (1, 3), we have \max(A_j - A_i, 0) = \max(1, 0) = 1.
For (i, j) = (2, 3), we have \max(A_j - A_i, 0) = \max(-2, 0) = 0.
Adding these together gives 3 + 1 + 0 = 4, which is the answer.


Sample Input 2

10
5 9 3 0 4 8 7 5 4 0

Sample Output 2

58