A - On and Off

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

配点 : 100

問題文

高橋君は、毎日 S0 分に部屋の電気をつけ、毎日 T0 分に消します。
電気をつけている間に日付が変わることもあります。

X30 分に部屋の電気がついているかどうか判定してください。

制約

  • 0 \leq S, T, X \leq 23
  • S \neq T
  • 入力は全て整数である。

入力

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

S T X

出力

X30 分に部屋の電気がついているならば Yes と、そうでなければ No と出力せよ。


入力例 1

7 20 12

出力例 1

Yes

部屋の電気がついているのは 70 分から 200 分までの間です。1230 分には電気がついているので、Yes と出力します。


入力例 2

20 7 12

出力例 2

No

部屋の電気がついているのは 00 分から 70 分までの間と、200 分から(次の日の)00 分までの間です。 1230 分には電気がついていないので、No と出力します。


入力例 3

23 0 23

出力例 3

Yes

Score : 100 points

Problem Statement

Takahashi turns on the light of his room at S o'clock (on the 24-hour clock) every day and turns it off at T o'clock every day.
The date may change while the light is on.

Determine whether the light is on at 30 minutes past X o'clock.

Constraints

  • 0 \leq S, T, X \leq 23
  • S \neq T
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

S T X

Output

If the light is on at 30 minutes past X o'clock, print Yes; otherwise, print No.


Sample Input 1

7 20 12

Sample Output 1

Yes

The light is on between 7 o'clock and 20 o'clock. At 30 minutes past 12 o'clock, it is on, so we print Yes.


Sample Input 2

20 7 12

Sample Output 2

No

The light is on between 0 o'clock and 7 o'clock, and between 20 o'clock and 0 o'clock (on the next day). At 30 minutes past 12 o'clock, it is off, so we print No.


Sample Input 3

23 0 23

Sample Output 3

Yes
B - Majority

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

配点 : 100

問題文

ある提案に対し、N 人の人が賛成か反対かを表明しています。なお、N は奇数です。

i \, (i = 1, 2, \dots, N) 番目の人の意見は文字列 S_i で表され、S_i = For のとき賛成しており、S_i = Against のとき反対しています。

過半数の人がこの提案に賛成しているかどうかを判定してください。

制約

  • N1 以上 99 以下の奇数
  • 全ての i = 1, 2, \dots, N に対し、S_i = For または S_i = Against

入力

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

N
S_1
S_2
\vdots
S_N

出力

N 人のうち過半数が提案に賛成しているならば Yes、そうでなければ No と出力せよ。


入力例 1

3
For
Against
For

出力例 1

Yes

提案に賛成している人数は 2 人であり、これは半数を超えているので Yes と出力します。


入力例 2

5
Against
Against
For
Against
For

出力例 2

No

提案に賛成している人数は 2 人であり、これは半数以下なので No と出力します。


入力例 3

1
For

出力例 3

Yes

Score : 100 points

Problem Statement

There are N people. Each of them agrees or disagrees with a proposal. Here, N is an odd number.

The i-th (i = 1, 2, \dots, N) person's opinion is represented by a string S_i: the person agrees if S_i = For and disagrees if S_i = Against.

Determine whether the majority agrees with the proposal.

Constraints

  • N is an odd number between 1 and 99, inclusive.
  • S_i = For or S_i = Against, for all i = 1, 2, \dots, N.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print Yes if the majority of the N people agree with the proposal; print No otherwise.


Sample Input 1

3
For
Against
For

Sample Output 1

Yes

The proposal is supported by two people, which is the majority, so Yes should be printed.


Sample Input 2

5
Against
Against
For
Against
For

Sample Output 2

No

The proposal is supported by two people, which is not the majority, so No should be printed.


Sample Input 3

1
For

Sample Output 3

Yes
C - Chessboard

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

配点 : 200

問題文

チェス盤のどこにコマが置かれているか答えてください。

8 マス、横 8 マスのグリッドがあります。グリッドの各マスには、次のルールで定められる長さ 2 の文字列の名前がついています。

  • 左から 1 列目にあるマスの名前の 1 文字目は a である。同様に、左から 2,3,\ldots,8 列目にあるマスの名前の 1 文字目は b, c, d, e, f, g, h である。
  • 下から 1 行目にあるマスの名前の 2 文字目は 1 である。同様に、下から 2,3,\ldots,8 行目にあるマスの名前の 2 文字目は 2, 3, 4, 5, 6, 7, 8 である。

例えば、グリッドの左下のマスの名前は a1、右下のマスの名前は h1、右上のマスの名前は h8 です。

グリッドの状態を表す長さ 88 つの文字列 S_1,\ldots,S_8 が与えられます。
S_ij 文字目は、グリッドの上から i 行目 左から j 列目のマスにコマが置かれているとき *、置かれていないとき . であり、S_1,\ldots,S_8 の中に文字 * はちょうど 1 つ存在します。
コマが置かれているマスの名前を求めてください。

制約

  • S_i. および * のみからなる長さ 8 の文字列である
  • S_1,\ldots,S_8 の中に文字 * はちょうど 1 つ存在する。

入力

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

S_1
S_2
S_3
S_4
S_5
S_6
S_7
S_8

出力

答えを出力せよ。


入力例 1

........
........
........
........
........
........
........
*.......

出力例 1

a1

問題文中で説明したとおり、グリッドの左下のマスの名前は a1 です。


入力例 2

........
........
........
........
........
.*......
........
........

出力例 2

b3

Score : 200 points

Problem Statement

Locate a piece on a chessboard.

We have a grid with 8 rows and 8 columns of squares. Each of the squares has a 2-character name determined as follows.

  • The first character of the name of a square in the 1-st column from the left is a. Similarly, the first character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th column from the left is b, c, d, e, f, g, h, respectively.
  • The second character of the name of a square in the 1-st row from the bottom is 1. Similarly, the second character of the name of a square in the 2-nd, 3-rd, \ldots, 8-th row from the bottom is 2, 3, 4, 5, 6, 7, 8, respectively.

For instance, the bottom-left square is named a1, the bottom-right square is named h1, and the top-right square is named h8.

You are given 8 strings S_1,\ldots,S_8, each of length 8, representing the state of the grid.
The j-th character of S_i is * if the square at the i-th row from the top and j-th column from the left has a piece on it, and . otherwise. The character * occurs exactly once among S_1,\ldots,S_8. Find the name of the square that has a piece on it.

Constraints

  • S_i is a string of length 8 consisting of . and*.
  • The character * occurs exactly once among S_1,\ldots,S_8.

Input

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

S_1
S_2
S_3
S_4
S_5
S_6
S_7
S_8

Output

Print the answer.


Sample Input 1

........
........
........
........
........
........
........
*.......

Sample Output 1

a1

As explained in the problem statement, the bottom-left square is named a1.


Sample Input 2

........
........
........
........
........
.*......
........
........

Sample Output 2

b3
D - Trimmed Mean

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

配点 : 200

問題文

高橋君は体操の大会に参加しています。
大会では、5N 人の審査員それぞれが高橋君の演技に対して評点をつけ、それらを元に次のように高橋君の得点が決定されます。

  • 高い評点をつけた方から順に N 人の審査員による評点を無効にする。
  • 低い評点をつけた方から順に N 人の審査員による評点を無効にする。
  • 残りの 3N 人の審査員による評点の平均点を高橋君の得点とする。

より厳密には、審査員がつけた得点の多重集合 S (|S|=5N) に対して次の操作を行って得られたものが高橋君の得点となります。

  • S の最大の要素(複数ある場合はそのうちの 1 つ)を選び、S から取り除く」という操作を N 回繰り返す。
  • S の最小の要素(複数ある場合はそのうちの 1 つ)を選び、S から取り除く」という操作を N 回繰り返す。
  • S に残った 3N 個の要素の平均を高橋君の得点とする。

高橋君の演技に対する、i 人目 (1\leq i\leq 5N) の審査員の評点は X_i 点でした。 高橋君の得点を計算してください。

制約

  • 1\leq N\leq 100
  • 0\leq X_i\leq 100
  • 入力はすべて整数

入力

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

N
X_1 X_2 \ldots X_{5N}

出力

高橋君の得点を出力せよ。
なお、真の値との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる。


入力例 1

1
10 100 20 50 30

出力例 1

33.333333333333336

N=1 であるので、評点が高い方と低い方からそれぞれ 1 人ずつの審査員による評点を無効にします。
1 番高い評点をつけた審査員は 2 人目 (100 点) であるため、これを無効にします。
また、1 番低い評点をつけた審査員は 1 人目 (10 点) であるため、これも無効にします。
よって、最終的な平均点は \displaystyle\frac{20+50+30}{3}=33.333\cdots となります。

出力は、真の値との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる事に注意してください。


入力例 2

2
3 3 3 4 5 6 7 8 99 100

出力例 2

5.500000000000000

N=2 であるので、評点が高い方と低い方からそれぞれ 2 人ずつの審査員による評点を無効にします。
1,2 番目に高い評点をつけた審査員は順に 10 人目 (100 点), 9 人目 (99 点) であるため、これを無効にします。
1 番低い評点をつけた審査員は 1,2,3 人目 (3 点) の 3 人がいるため、このうち 2 人を無効とします。
よって、答えは \displaystyle\frac{3+4+5+6+7+8}{6}=5.5 となります。

1 番低い評点をつけた 3 人のうちどの 2 人を無効にしたかは、答えに影響しない事に注意してください。

Score : 200 points

Problem Statement

Takahashi is participating in a gymnastic competition.
In the competition, each of 5N judges grades Takahashi's performance, and his score is determined as follows:

  • Invalidate the grades given by the N judges who gave the highest grades.
  • Invalidate the grades given by the N judges who gave the lowest grades.
  • Takahashi's score is defined as the average of the remaining 3N judges' grades.

More formally, Takahashi's score is obtained by performing the following procedure on the multiset of the judges' grades S (|S|=5N):

  • Repeat the following operation N times: choose the maximum element (if there are multiple such elements, choose one of them) and remove it from S.
  • Repeat the following operation N times: choose the minimum element (if there are multiple such elements, choose one of them) and remove it from S.
  • Takahashi's score is defined as the average of the 3N elements remaining in S.

The i-th (1\leq i\leq 5N) judge's grade for Takahashi's performance was X_i points. Find Takahashi's score.

Constraints

  • 1\leq N\leq 100
  • 0\leq X_i\leq 100
  • All values in the input are integers.

Input

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

N
X_1 X_2 \ldots X_{5N}

Output

Print Takahashi's score.
Your answer will be considered correct if the absolute or relative error from the true value is at most 10^{-5}.


Sample Input 1

1
10 100 20 50 30

Sample Output 1

33.333333333333336

Since N=1, the grade given by one judge who gave the highest grade, and one with the lowest, are invalidated.
The 2-nd judge gave the highest grade (100 points), which is invalidated.
Additionally, the 1-st judge gave the lowest grade (10 points), which is also invalidated.
Thus, the average is \displaystyle\frac{20+50+30}{3}=33.333\cdots.

Note that the output will be considered correct if the absolute or relative error from the true value is at most 10^{-5}.


Sample Input 2

2
3 3 3 4 5 6 7 8 99 100

Sample Output 2

5.500000000000000

Since N=2, the grades given by the two judges who gave the highest grades, and two with the lowest, are invalidated.
The 10-th and 9-th judges gave the highest grades (100 and 99 points, respectively), which are invalidated.
Three judges, the 1-st, 2-nd, and 3-rd, gave the lowest grade (3 points), so two of them are invalidated.
Thus, the average is \displaystyle\frac{3+4+5+6+7+8}{6}=5.5.

Note that the choice of the two invalidated judges from the three with the lowest grades does not affect the answer.

E - AtCoder Cards

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

配点 : 300

問題文

AtCoder社ではカードを使った 1 人ゲームが流行っています。
ゲームで使う各カードには、英小文字 1 文字または @ の文字が書かれており、いずれのカードも十分多く存在します。
ゲームは以下の手順で行います。

  1. カードを同じ枚数ずつ 2 列に並べる。
  2. @ のカードを、それぞれ a, t, c, o, d, e, r のいずれかのカードと置き換える。
  3. 2 つの列が一致していれば勝ち。そうでなければ負け。

このゲームに勝ちたいあなたは、次のようなイカサマをすることにしました。

  • 手順 1 以降の好きなタイミングで、列内のカードを自由に並び替えてよい。

手順 1 で並べられた 2 つの列を表す 2 つの文字列 S,T が与えられるので、イカサマをしてもよいときゲームに勝てるか判定してください。

制約

  • S,T は英小文字と @ からなる
  • S,T の長さは等しく 1 以上 2\times 10^5 以下

入力

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

S
T

出力

イカサマをしてもよいとき、ゲームに勝てるなら Yes、勝てないなら No と出力せよ。


入力例 1

ch@ku@ai
choku@@i

出力例 1

Yes

@ をうまく置き換えることによって、両方とも chokudai と一致させることが可能です。


入力例 2

ch@kud@i
akidu@ho

出力例 2

Yes

イカサマをし、@ をうまく置き換えることによって、両方とも chokudai と一致させることが可能です。


入力例 3

aoki
@ok@

出力例 3

No

イカサマをしても勝つことはできません。


入力例 4

aa
bb

出力例 4

No

Score : 300 points

Problem Statement

A single-player card game is popular in AtCoder Inc. Each card in the game has a lowercase English letter or the symbol @ written on it. There is plenty number of cards for each kind. The game goes as follows.

  1. Arrange the same number of cards in two rows.
  2. Replace each card with @ with one of the following cards: a, t, c, o, d, e, r.
  3. If the two rows of cards coincide, you win. Otherwise, you lose.

To win this game, you will do the following cheat.

  • Freely rearrange the cards within a row whenever you want after step 1.

You are given two strings S and T, representing the two rows you have after step 1. Determine whether it is possible to win with cheating allowed.

Constraints

  • S and T consist of lowercase English letters and @.
  • The lengths of S and T are equal and between 1 and 2\times 10^5, inclusive.

Input

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

S
T

Output

If it is possible to win with cheating allowed, print Yes; otherwise, print No.


Sample Input 1

ch@ku@ai
choku@@i

Sample Output 1

Yes

You can replace the @s so that both rows become chokudai.


Sample Input 2

ch@kud@i
akidu@ho

Sample Output 2

Yes

You can cheat and replace the @s so that both rows become chokudai.


Sample Input 3

aoki
@ok@

Sample Output 3

No

You cannot win even with cheating.


Sample Input 4

aa
bb

Sample Output 4

No
F - (K+1)-th Largest Number

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

配点 : 300

問題文

長さ N の数列 A = (A_1, A_2, \ldots, A_N) が与えられます。 K = 0, 1, 2, \ldots, N-1 のそれぞれについて、下記の問題を解いてください。

1 以上 N 以下の整数 i であって、次の条件を満たすものの個数を求めよ。

  • A に含まれる整数のうち A_i より大きいものはちょうど K 種類である。

制約

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

入力

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

N
A_1 A_2 \ldots A_N

出力

N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には K = i-1 の場合の問題の答えを出力せよ。


入力例 1

6
2 7 1 8 2 8

出力例 1

2
1
2
1
0
0

例として、K = 2 の場合の問題の答えを以下で求めます。

  • A_1 = 2 に関して、A に含まれる整数のうち A_1 より大きいものは、7, 82 種類です。
  • A_2 = 7 に関して、A に含まれる整数のうち A_2 より大きいものは、81 種類です。
  • A_3 = 1 に関して、A に含まれる整数のうち A_3 より大きいものは、2, 7, 83 種類です。
  • A_4 = 8 に関して、A に含まれる整数のうち A_4 より大きいものは、0 種類です(存在しません)。
  • A_5 = 2 に関して、A に含まれる整数のうち A_5 より大きいものは、7, 82 種類です。
  • A_6 = 8 に関して、A に含まれる整数のうち A_6 より大きいものは、0 種類です(存在しません)。

よって、A に含まれる整数のうちA_i より大きいものがちょうど K = 2 種類であるような i は、i = 1i = 52 つです。よって、K = 2 の場合の答えは 2 です。


入力例 2

1
1

出力例 2

1

入力例 3

10
979861204 57882493 979861204 447672230 644706927 710511029 763027379 710511029 447672230 136397527

出力例 3

2
1
2
1
2
1
1
0
0
0

Score : 300 points

Problem Statement

You are given a sequence A = (A_1, A_2, \ldots, A_N) of length N. For each K = 0, 1, 2, \ldots, N-1, solve the following problem.

Find the number of integers i between 1 and N (inclusive) such that:

  • A contains exactly K distinct integers greater than A_i.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print N lines. For i = 1, 2, \ldots, N, the i-th line should contain the answer for K = i-1.


Sample Input 1

6
2 7 1 8 2 8

Sample Output 1

2
1
2
1
0
0

For example, we will find the answer for K=2.

  • Regarding A_1 = 2, A contains 2 distinct integers greater than A_1: 7 and 8.
  • Regarding A_2 = 7, A contains 1 distinct integer greater than A_2: 8.
  • Regarding A_3 = 1, A contains 3 distinct integers greater than A_3: 2, 7, and 8.
  • Regarding A_4 = 8, A contains 0 distinct integers greater than A_4 (there is no such integer).
  • Regarding A_5 = 2, A contains 2 distinct integers greater than A_5: 7 and 8.
  • Regarding A_6 = 8, A contains 0 distinct integers greater than A_6 (there is no such integer).

Thus, there are two i's, i = 1 and i = 5, such that A contains exactly K = 2 distinct integers greater than A_i. Therefore, the answer for K = 2 is 2.


Sample Input 2

1
1

Sample Output 2

1

Sample Input 3

10
979861204 57882493 979861204 447672230 644706927 710511029 763027379 710511029 447672230 136397527

Sample Output 3

2
1
2
1
2
1
1
0
0
0
G - LOWER

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

配点 : 400

問題文

英大文字および英小文字からなる長さ N の文字列 S が与えられます。

これから、文字列 S に対して Q 回の操作を行います。 i 番目 (1\leq i\leq Q) の操作は整数 2 つと文字 1 つからなる組 (t _ i,x _ i,c _ i) で表され、それぞれ次のような操作を表します。

  • t _ i=1 のとき、Sx _ i 文字目を c _ i に変更する。
  • t _ i=2 のとき、S に含まれる大文字すべてをそれぞれ小文字に変更する(x _ i,c _ i は操作に使用しない)。
  • t _ i=3 のとき、S に含まれる小文字すべてをそれぞれ大文字に変更する(x _ i,c _ i は操作に使用しない)。

Q 回の操作がすべて終わったあとの S を出力してください。

制約

  • 1\leq N\leq5\times10^5
  • S は英大文字および英小文字からなる長さ N の文字列
  • 1\leq Q\leq5\times10^5
  • 1\leq t _ i\leq3\ (1\leq i\leq Q)
  • t _ i=1 ならば 1\leq x _ i\leq N\ (1\leq i\leq Q)
  • c _ i は英大文字もしくは英小文字
  • t _ i\neq 1 ならば x _ i=0 かつ c _ i= 'a'
  • N,Q,t _ i,x _ i はすべて整数

入力

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

N
S
Q
t _ 1 x _ 1 c _ 1
t _ 2 x _ 2 c _ 2
\vdots
t _ Q x _ Q c _ Q

出力

答えを 1 行で出力せよ。


入力例 1

7
AtCoder
5
1 4 i
3 0 a
1 5 b
2 0 a
1 4 Y

出力例 1

atcYber

はじめ、文字列 SAtCoder です。

  • 1 番目の操作では、4 文字目を i に変更します。変更後の SAtCider です。
  • 2 番目の操作では、すべての小文字を大文字に変更します。変更後の SATCIDER です。
  • 3 番目の操作では、5 文字目を b に変更します。変更後の SATCIbER です。
  • 4 番目の操作では、すべての大文字を小文字に変更します。変更後の Satciber です。
  • 5 番目の操作では、4 文字目を Y に変更します。変更後の SatcYber です。

すべての操作が終わったあとの SatcYber なので、atcYber と出力してください。


入力例 2

35
TheQuickBrownFoxJumpsOverTheLazyDog
10
2 0 a
1 19 G
1 13 m
1 2 E
1 21 F
2 0 a
1 27 b
3 0 a
3 0 a
1 15 i

出力例 2

TEEQUICKBROWMFiXJUGPFOVERTBELAZYDOG

Score : 400 points

Problem Statement

You are given a string S of length N consisting of uppercase and lowercase English letters.

Let us perform Q operations on the string S. The i-th operation (1\leq i\leq Q) is represented by a tuple (t _ i,x _ i,c _ i) of two integers and one character, as follows.

  • If t _ i=1, change the x _ i-th character of S to c _ i.
  • If t _ i=2, convert all uppercase letters in S to lowercase (do not use x _ i,c _ i for this operation).
  • If t _ i=3, convert all lowercase letters in S to uppercase (do not use x _ i,c _ i for this operation).

Print the S after the Q operations.

Constraints

  • 1\leq N\leq5\times10^5
  • S is a string of length N consisting of uppercase and lowercase English letters.
  • 1\leq Q\leq5\times10^5
  • 1\leq t _ i\leq3\ (1\leq i\leq Q)
  • If t _ i=1, then 1\leq x _ i\leq N\ (1\leq i\leq Q).
  • c _ i is an uppercase or lowercase English letter.
  • If t _ i\neq 1, then x _ i=0 and c _ i= 'a'.
  • N,Q,t _ i,x _ i are all integers.

Input

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

N
S
Q
t _ 1 x _ 1 c _ 1
t _ 2 x _ 2 c _ 2
\vdots
t _ Q x _ Q c _ Q

Output

Print the answer in a single line.


Sample Input 1

7
AtCoder
5
1 4 i
3 0 a
1 5 b
2 0 a
1 4 Y

Sample Output 1

atcYber

Initially, the string S is AtCoder.

  • The first operation changes the 4-th character to i, changing S to AtCider.
  • The second operation converts all lowercase letters to uppercase, changing S to ATCIDER.
  • The third operation changes the 5-th character to b, changing S to ATCIbER.
  • The fourth operation converts all uppercase letters to lowercase, changing S to atciber.
  • The fifth operation changes the 4-th character to Y, changing S to atcYber.

After the operations, the string S is atcYber, so print atcYber.


Sample Input 2

35
TheQuickBrownFoxJumpsOverTheLazyDog
10
2 0 a
1 19 G
1 13 m
1 2 E
1 21 F
2 0 a
1 27 b
3 0 a
3 0 a
1 15 i

Sample Output 2

TEEQUICKBROWMFiXJUGPFOVERTBELAZYDOG
H - NAND repeatedly

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

配点 : 450

問題文

01 からなる長さ N の文字列 S が与えられます。 S は長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) の情報を表しており、Si 文字目 (1\leq i\leq N)0 のとき A _ i=01 のとき A _ i=1です。

\[\sum _ {1\leq i\leq j\leq N}(\cdots((A _ i\barwedge A _ {i+1})\barwedge A _ {i+2})\barwedge\cdots\barwedge A _ j)\]

を求めてください。

より厳密には、次のように定められる f(i,j)\ (1\leq i\leq j\leq N) に対して \displaystyle\sum _ {i=1} ^ {N}\sum _ {j=i} ^ Nf(i,j) を求めてください。

\[f(i,j)=\left\{\begin{matrix} A _ i&(i=j)\\ f(i,j-1)\barwedge A _ j\quad&(i\lt j) \end{matrix}\right.\]

ただし、否定論理積 \barwedge は次を満たす二項演算子です。

\[0\barwedge0=1,0\barwedge1=1,1\barwedge0=1,1\barwedge1=0\]

制約

  • 1\leq N\leq10^6
  • S01 からなる長さ N の文字列
  • 入力はすべて整数

入力

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

N
S

出力

答えを 1 行で出力せよ。


入力例 1

5
00110

出力例 1

9

1\leq i\leq j\leq N を満たすすべての組 (i,j) に対して、f(i,j) の値は以下のようになります。

  • f(1,1)=0=0
  • f(1,2)=0\barwedge0=1
  • f(1,3)=(0\barwedge0)\barwedge1=0
  • f(1,4)=((0\barwedge0)\barwedge1)\barwedge1=1
  • f(1,5)=(((0\barwedge0)\barwedge1)\barwedge1)\barwedge0=1
  • f(2,2)=0=0
  • f(2,3)=0\barwedge1=1
  • f(2,4)=(0\barwedge1)\barwedge1=0
  • f(2,5)=((0\barwedge1)\barwedge1)\barwedge0=1
  • f(3,3)=1=1
  • f(3,4)=1\barwedge1=0
  • f(3,5)=(1\barwedge1)\barwedge0=1
  • f(4,4)=1=1
  • f(4,5)=1\barwedge0=1
  • f(5,5)=0=0

これらの総和は 0+1+0+1+1+0+1+0+1+1+0+1+1+1+0=9 なので、9 を出力してください。

\barwedge は結合法則を満たさないことに注意してください。 例えば、(1\barwedge1)\barwedge0=0\barwedge0=1\neq0=1\barwedge1=1\barwedge(1\barwedge0) です。


入力例 2

30
101010000100101011010011000010

出力例 2

326

Score : 450 points

Problem Statement

You are given a string S of length N consisting of 0 and 1. It describes a length-N sequence A=(A _ 1,A _ 2,\ldots,A _ N). If the i-th character of S (1\leq i\leq N) is 0, then A _ i=0; if it is 1, then A _ i=1.

Find the following:

\[\sum _ {1\leq i\leq j\leq N}(\cdots((A _ i\barwedge A _ {i+1})\barwedge A _ {i+2})\barwedge\cdots\barwedge A _ j)\]

More formally, find \displaystyle\sum _ {i=1} ^ {N}\sum _ {j=i} ^ Nf(i,j) for f(i,j)\ (1\leq i\leq j\leq N) defined as follows:

\[f(i,j)=\left\{\begin{matrix} A _ i&(i=j)\\ f(i,j-1)\barwedge A _ j\quad&(i\lt j) \end{matrix}\right.\]

Here, \barwedge, NAND, is a binary operator satisfying the following:

\[0\barwedge0=1,0\barwedge1=1,1\barwedge0=1,1\barwedge1=0.\]

Constraints

  • 1\leq N\leq10^6
  • S is a string of length N consisting of 0 and 1.
  • All input values are integers.

Input

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

N
S

Output

Print the answer in a single line.


Sample Input 1

5
00110

Sample Output 1

9

Here are the values of f(i,j) for the pairs (i,j) such that 1\leq i\leq j\leq N:

  • f(1,1)=0=0
  • f(1,2)=0\barwedge0=1
  • f(1,3)=(0\barwedge0)\barwedge1=0
  • f(1,4)=((0\barwedge0)\barwedge1)\barwedge1=1
  • f(1,5)=(((0\barwedge0)\barwedge1)\barwedge1)\barwedge0=1
  • f(2,2)=0=0
  • f(2,3)=0\barwedge1=1
  • f(2,4)=(0\barwedge1)\barwedge1=0
  • f(2,5)=((0\barwedge1)\barwedge1)\barwedge0=1
  • f(3,3)=1=1
  • f(3,4)=1\barwedge1=0
  • f(3,5)=(1\barwedge1)\barwedge0=1
  • f(4,4)=1=1
  • f(4,5)=1\barwedge0=1
  • f(5,5)=0=0

Their sum is 0+1+0+1+1+0+1+0+1+1+0+1+1+1+0=9, so print 9.

Note that \barwedge does not satisfy the associative property. For instance, (1\barwedge1)\barwedge0=0\barwedge0=1\neq0=1\barwedge1=1\barwedge(1\barwedge0).


Sample Input 2

30
101010000100101011010011000010

Sample Output 2

326
I - Dist Max 2

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

配点 : 500

問題文

2 次元平面上の N 個の相異なる点が与えられます。点 i\, (1 \leq i \leq N) の座標は (x_i,y_i) です。

2 つの点 i,j\, (1 \leq i,j \leq N) の距離を \mathrm{min} (|x_i-x_j|,|y_i-y_j|) 、すなわち x 座標の差と y 座標の差の小さい方と定義します。

異なる 2 つの点の距離の最大値を求めてください。

制約

  • 2 \leq N \leq 200000
  • 0 \leq x_i,y_i \leq 10^9
  • (x_i,y_i) \neq (x_j,y_j) (i \neq j)
  • 入力は全て整数である。

入力

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

N
x_1 y_1
x_2 y_2
\vdots
x_N y_N

出力

異なる 2 つの点の距離の最大値を出力せよ。


入力例 1

3
0 3
3 1
4 10

出力例 1

4

1 と点 2 の距離は 2 、点 1 と点 3 の距離は 4 、点 2 と点 3 の距離は 1 です。よって 4 を出力してください。


入力例 2

4
0 1
0 4
0 10
0 6

出力例 2

0

入力例 3

8
897 729
802 969
765 184
992 887
1 104
521 641
220 909
380 378

出力例 3

801

Score : 500 points

Problem Statement

Given are N distinct points in a two-dimensional plane. Point i (1 \leq i \leq N) has the coordinates (x_i,y_i).

Let us define the distance between two points i and j be \mathrm{min} (|x_i-x_j|,|y_i-y_j|): the smaller of the difference in the x-coordinates and the difference in the y-coordinates.

Find the maximum distance between two different points.

Constraints

  • 2 \leq N \leq 200000
  • 0 \leq x_i,y_i \leq 10^9
  • (x_i,y_i) \neq (x_j,y_j) (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
\vdots
x_N y_N

Output

Print the maximum distance between two different points.


Sample Input 1

3
0 3
3 1
4 10

Sample Output 1

4

The distances between Points 1 and 2, between Points 1 and 3, and between Points 2 and 3 are 2, 4, and 1, respectively, so your output should be 4.


Sample Input 2

4
0 1
0 4
0 10
0 6

Sample Output 2

0

Sample Input 3

8
897 729
802 969
765 184
992 887
1 104
521 641
220 909
380 378

Sample Output 3

801