C - Prefix?

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

配点 : 200

問題文

英小文字のみからなる 2 つの文字列 S, T が与えられます。 ST の接頭辞かどうかを判定してください。

接頭辞とは 長さ N の文字列 T_1T_2\ldots T_N の接頭辞とは、 0 \leq i \leq N を満たすある整数 i によって、T の先頭 i 文字目までの文字列 T_1T_2\ldots T_i として表される文字列です。例えば、T = abc のとき、T の接頭辞は、空文字列、a 、ab 、abc の 4 つです。

制約

  • ST はそれぞれ英小文字のみからなる長さが 1 以上 100 以下の文字列

入力

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

S
T

出力

ST の接頭辞である場合は Yes を、そうでない場合は No を出力せよ。 ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。


入力例 1

atco
atcoder

出力例 1

Yes

atcoatcoder の接頭辞です。よって、Yes を出力します。


入力例 2

code
atcoder

出力例 2

No

codeatcoder の接頭辞ではありません。よって、No を出力します。


入力例 3

abc
abc

出力例 3

Yes

文字列全体もその文字列の接頭辞であることに注意してください。


入力例 4

aaaa
aa

出力例 4

No

Score : 200 points

Problem Statement

You are given two strings S and T consisting of lowercase English letters. Determine if S is a prefix of T.

What is a prefix? A prefix of a string T_1T_2\ldots T_N of length N is a string expressed as the first i characters of T, T_1T_2\ldots T_i, where i is an integer such that 0 \leq i \leq N. For example, when T = abc, there are four prefixes of T: an empty string, a, ab, and abc.

Constraints

  • S and T are strings of lengths between 1 and 100 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print Yes if S is a prefix of T; print No otherwise. Note that the judge is case-sensitive.


Sample Input 1

atco
atcoder

Sample Output 1

Yes

atco is a prefix of atcoder. Thus, Yes should be printed.


Sample Input 2

code
atcoder

Sample Output 2

No

code is not a prefix of atcoder. Thus, No should be printed.


Sample Input 3

abc
abc

Sample Output 3

Yes

Note that a string is also a prefix of itself.


Sample Input 4

aaaa
aa

Sample Output 4

No
D - Uppercase and Lowercase

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

配点 : 200

問題文

英小文字と英大文字からなる文字列 S が与えられます。S の長さは奇数です。
S に含まれる大文字の個数が小文字の個数よりも多ければ、S に含まれる全ての小文字を大文字に変換してください。
そうでない場合は、S に含まれる全ての大文字を小文字に変換してください。

制約

  • S は英小文字および英大文字からなる文字列
  • S の長さは 1 以上 99 以下の奇数

入力

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

S

出力

問題文の指示に従って文字を変換した後の S を出力せよ。


入力例 1

AtCoder

出力例 1

atcoder

AtCoder に含まれる小文字の個数は 5 個、大文字の個数は 2 個です。よって AtCoder に含まれる全ての大文字を小文字に変換した atcoder が答えとなります。


入力例 2

SunTORY

出力例 2

SUNTORY

SunTORY に含まれる小文字の個数は 2 個、大文字の個数は 5 個です。よって SunTORY に含まれる全ての小文字を大文字に変換した SUNTORY が答えとなります。


入力例 3

a

出力例 3

a

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase and uppercase English letters. The length of S is odd.
If the number of uppercase letters in S is greater than the number of lowercase letters, convert all lowercase letters in S to uppercase.
Otherwise, convert all uppercase letters in S to lowercase.

Constraints

  • S is a string consisting of lowercase and uppercase English letters.
  • The length of S is an odd number between 1 and 99, inclusive.

Input

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

S

Output

Print the string S after converting the letters according to the problem statement.


Sample Input 1

AtCoder

Sample Output 1

atcoder

The string AtCoder contains five lowercase letters and two uppercase letters. Thus, convert all uppercase letters in AtCoder to lowercase, which results in atcoder.


Sample Input 2

SunTORY

Sample Output 2

SUNTORY

The string SunTORY contains two lowercase letters and five uppercase letters. Thus, convert all lowercase letters in SunTORY to uppercase, which results in SUNTORY.


Sample Input 3

a

Sample Output 3

a
E - Merge the balls

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

配点 : 250

問題文

空の列と、N 個のボールがあります。i 個目 (1\leq i\leq N) のボールの大きさは 2^{A_i} です。

これから N 回の操作を行います。
i 回目の操作では、i 個目のボールを列の一番右に付け加えた後、次の手順を繰り返します。

  1. 列にあるボールが 1 つ以下ならば操作を終了する。
  2. 列にあるボールのうち右から 1 番目のものと 2 番目のものの大きさが 異なる ならば操作を終了する。
  3. 列にあるボールのうち右から 1 番目のものと 2 番目のものの大きさが 等しい ならば、2 つのボールを取り除き、「取り除かれた 2 つのボールの大きさの和」の大きさのボール 1 つを列の一番右に付け加える。その後、1. に戻り、手順を繰り返す。

N 回の操作の後で、列にあるボールの数を求めてください。

制約

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

入力

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

N
A_1 A_2 \ldots A_N

出力

N 回の操作の後で、列にあるボールの数を出力せよ。


入力例 1

7
2 1 1 3 5 3 3

出力例 1

3

操作は次のように行われます。

  • 1 回目の操作の後、列にあるボールは 1 つで、大きさは 2^2 です。
  • 2 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^2, 2^1 です。
  • 3 回目の操作の後、列にあるボールは 1 つで、大きさは 2^3 です。これは次のようにして得ることができます。
    • 3 回目の操作において 3 個目のボールを付け加えたとき、列にあるボールの大きさは順に 2^2,2^1,2^1 となります。
    • 右から 1 番目のボールと 2 番目のボールの大きさが等しいため、これらのボールが取り除かれ、大きさが 2^1+2^1=2^2 のボールが追加されます。このとき、列にあるボールの大きさは 2^2, 2^2 となります。
    • さらに、再び右から 1 番目のボールと 2 番目のボールの大きさが等しいため、これらのボールが取り除かれ、大きさが 2^2+2^2=2^3 のボールが追加され、列にあるボールの大きさは 2^3 となります。
  • 4 回目の操作の後、列にあるボールは 1 つで、大きさは 2^4 です。
  • 5 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^4, 2^5 です。
  • 6 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^4, 2^5, 2^3 です。
  • 7 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^4, 2^5, 2^4 です。

よって、最後に列にあるボールの数である 3 を出力します。


入力例 2

5
0 0 0 1 2

出力例 2

4

操作は次のように行われます。

  • 1 回目の操作の後、列にあるボールは 1 つで、大きさは 2^0 です。
  • 2 回目の操作の後、列にあるボールは 1 つで、大きさは 2^1 です。
  • 3 回目の操作の後、列にあるボールは 2 つで、大きさは順に 2^1, 2^0 です。
  • 4 回目の操作の後、列にあるボールは 3 つで、大きさは順に 2^1, 2^0, 2^1 です。
  • 5 回目の操作の後、列にあるボールは 4 つで、大きさは順に 2^1, 2^0, 2^1, 2^2 です。

よって、最後に列にあるボールの数である 4 を出力します。

Score: 250 points

Problem Statement

You have an empty sequence and N balls. The size of the i-th ball (1 \leq i \leq N) is 2^{A_i}.

You will perform N operations.
In the i-th operation, you add the i-th ball to the right end of the sequence, and repeat the following steps:

  1. If the sequence has one or fewer balls, end the operation.
  2. If the rightmost ball and the second rightmost ball in the sequence have different sizes, end the operation.
  3. If the rightmost ball and the second rightmost ball in the sequence have the same size, remove these two balls and add a new ball to the right end of the sequence with a size equal to the sum of the sizes of the two removed balls. Then, go back to step 1 and repeat the process.

Determine the number of balls remaining in the sequence after the N operations.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \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 \ldots A_N

Output

Print the number of balls in the sequence after the N operations.


Sample Input 1

7
2 1 1 3 5 3 3

Sample Output 1

3

The operations proceed as follows:

  • After the first operation, the sequence has one ball, of size 2^2.
  • After the second operation, the sequence has two balls, of sizes 2^2 and 2^1 in order.
  • After the third operation, the sequence has one ball, of size 2^3. This is obtained as follows:
    • When the third ball is added during the third operation, the sequence has balls of sizes 2^2, 2^1, 2^1 in order.
    • The first and second balls from the right have the same size, so these balls are removed, and a ball of size 2^1 + 2^1 = 2^2 is added. Now, the sequence has balls of sizes 2^2, 2^2.
    • Again, the first and second balls from the right have the same size, so these balls are removed, and a ball of size 2^2 + 2^2 = 2^3 is added, leaving the sequence with a ball of size 2^3.
  • After the fourth operation, the sequence has one ball, of size 2^4.
  • After the fifth operation, the sequence has two balls, of sizes 2^4 and 2^5 in order.
  • After the sixth operation, the sequence has three balls, of sizes 2^4, 2^5, 2^3 in order.
  • After the seventh operation, the sequence has three balls, of sizes 2^4, 2^5, 2^4 in order.

Therefore, you should print 3, the final number of balls in the sequence.


Sample Input 2

5
0 0 0 1 2

Sample Output 2

4

The operations proceed as follows:

  • After the first operation, the sequence has one ball, of size 2^0.
  • After the second operation, the sequence has one ball, of size 2^1.
  • After the third operation, the sequence has two balls, of sizes 2^1 and 2^0 in order.
  • After the fourth operation, the sequence has three balls, of sizes 2^1, 2^0, 2^1 in order.
  • After the fifth operation, the sequence has four balls, of sizes 2^1, 2^0, 2^1, 2^2 in order.

Therefore, you should print 4, the final number of balls in the sequence.

F - Robot Takahashi

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

配点 : 300

問題文

子供と大人があわせて N 人います。i 番目の人の体重は W_i です。
それぞれの人が子供か大人かは、01 からなる長さ N の文字列 S によって表され、
Si 文字目が 0 であるとき i 番目の人が子供であることを、1 であるとき i 番目の人が大人であることをさします。

ロボットである高橋君に対して実数 X を設定すると、 高橋君はそれぞれの人に対して、体重が X 未満なら子供、X 以上なら大人と判定します。
実数 X に対してf(X) を、高橋君に X を設定したときに N 人のうち子供か大人かを正しく判定できる人数で定めます。

X が実数全体を動くとき、f(X) の最大値を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • S01 からなる長さ N の文字列
  • 1\leq W_i\leq 10^9
  • N,W_i は整数

入力

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

N
S
W_1 W_2 \ldots W_N

出力

f(X) の最大値を整数で一行に出力せよ。


入力例 1

5
10101
60 45 30 40 80

出力例 1

4

X=50 と設定すると、高橋君は 2,3,4 番目の人を子供、 1,5 番目の人を大人と判定します。
実際には 2,4 番目の人が子供、 1,3,5 番目の人が大人であるので、このとき、1,2,4,5 番目の合計 4 人に対して正しく判定できています。 よって、f(50)=4 です。

5 人全員に対して正しく判定できるような X は存在しないのでこのときが最大です。よって、4 を出力します。


入力例 2

3
000
1 2 3

出力例 2

3

例えば、X=10 とすると最大値 f(10)=3 を達成します。
全員が大人、または全員が子供である可能性もあることに注意してください。


入力例 3

5
10101
60 50 50 50 60

出力例 3

4

例えば、X=55 とすると最大値 f(55)=4 を達成します。
同じ体重の人が複数人存在する可能性もあることに注意してください。

Score : 300 points

Problem Statement

There are N people, each of whom is either a child or an adult. The i-th person has a weight of W_i.
Whether each person is a child or an adult is specified by a string S of length N consisting of 0 and 1.
If the i-th character of S is 0, then the i-th person is a child; if it is 1, then the i-th person is an adult.

When Takahashi the robot is given a real number X, Takahashi judges a person with a weight less than X to be a child and a person with a weight more than or equal to X to be an adult.
For a real value X, let f(X) be the number of people whom Takahashi correctly judges whether they are children or adults.

Find the maximum value of f(X) for all real values of X.

Constraints

  • 1\leq N\leq 2\times 10^5
  • S is a string of length N consisting of 0 and 1.
  • 1\leq W_i\leq 10^9
  • N and W_i are integers.

Input

Input is given from Standard Input in the following format:

N
S
W_1 W_2 \ldots W_N

Output

Print the maximum value of f(X) as an integer in a single line.


Sample Input 1

5
10101
60 45 30 40 80

Sample Output 1

4

When Takahashi is given X=50, it judges the 2-nd, 3-rd, and 4-th people to be children and the 1-st and 5-th to be adults.
In reality, the 2-nd and 4-th are children, and the 1-st, 3-rd, and 5-th are adults, so the 1-st, 2-nd, 4-th, and 5-th people are correctly judged. Thus, f(50)=4.

This is the maximum since there is no X that judges correctly for all 5 people. Thus, 4 should be printed.


Sample Input 2

3
000
1 2 3

Sample Output 2

3

For example, X=10 achieves the maximum value f(10)=3.
Note that the people may be all children or all adults.


Sample Input 3

5
10101
60 50 50 50 60

Sample Output 3

4

For example, X=55 achieves the maximum value f(55)=4.
Note that there may be multiple people with the same weight.

G - ±1 Operation 2

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

配点 : 400

問題文

長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられます。この A に以下を施すことを「操作」と呼びます。

  • まず、 1 \le i \le N を満たす整数 i を選択する。
  • 次に、以下の 2 つのうちどちらかを選択し、実行する。
    • A_i1 を加算する。
    • A_i から 1 を減算する。

Q 個の質問に答えてください。
i 個目の質問は以下です。

  • 「操作」を 0 回以上何度でも使って A の要素を全て X_i にする時、必要な「操作」の最小回数を求めてください。

制約

  • 入力は全て整数
  • 1 \le N,Q \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • 0 \le X_i \le 10^9

入力

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

N Q
A_1 A_2 \dots A_N
X_1
X_2
\vdots
X_Q

出力

Q 行にわたって出力せよ。
出力のうち i 行目には、 i 個目の質問に対する答えを整数として出力せよ。


入力例 1

5 3
6 11 2 5 5
5
20
0

出力例 1

10
71
29

A=(6,11,2,5,5) であり、この入力には 3 つの質問が含まれます。

1 つ目の質問について、 A に以下のように 10 回の「操作」を施すことで、 A の要素を全て 5 にすることができます。

  • A_1 から 1 減算する。
  • A_2 から 1 減算することを 6 度繰り返す。
  • A_31 加算することを 3 度繰り返す。

9 回以下の「操作」で A の要素を全て 5 にすることはできません。

2 つ目の質問について、 A71 回の「操作」を施すことで、 A の要素を全て 20 にすることができます。

3 つ目の質問について、 A29 回の「操作」を施すことで、 A の要素を全て 0 にすることができます。


入力例 2

10 5
1000000000 314159265 271828182 141421356 161803398 0 777777777 255255255 536870912 998244353
555555555
321654987
1000000000
789456123
0

出力例 2

3316905982
2811735560
5542639502
4275864946
4457360498

出力が 32bit 整数に収まらない場合もあります。

Score : 400 points

Problem Statement

You are given a sequence of length N: A=(A_1,A_2,\dots,A_N). The following action on this sequence is called an operation.

  • First, choose an integer i such that 1 \le i \le N.
  • Next, choose and do one of the following.
    • Add 1 to A_i.
    • Subtract 1 from A_i.

Answer Q questions.
The i-th question is the following.

  • Consider performing zero or more operations to change every element of A to X_i. Find the minimum number of operations required to do so.

Constraints

  • All values in input are integers.
  • 1 \le N,Q \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • 0 \le X_i \le 10^9

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 \dots A_N
X_1
X_2
\vdots
X_Q

Output

Print Q lines.
The i-th line should contain the answer to the i-th question as an integer.


Sample Input 1

5 3
6 11 2 5 5
5
20
0

Sample Output 1

10
71
29

We have A=(6,11,2,5,5) and three questions in this input.

For the 1-st question, you can change every element of A to 5 in 10 operations as follows.

  • Subtract 1 from A_1.
  • Subtract 1 from A_2 six times.
  • Add 1 to A_3 three times.

It is impossible to change every element of A to 5 in 9 or fewer operations.

For the 2-nd question, you can change every element of A to 20 in 71 operations.

For the 3-rd question, you can change every element of A to 0 in 29 operations.


Sample Input 2

10 5
1000000000 314159265 271828182 141421356 161803398 0 777777777 255255255 536870912 998244353
555555555
321654987
1000000000
789456123
0

Sample Output 2

3316905982
2811735560
5542639502
4275864946
4457360498

The output may not fit into 32-bit integers.