A - A Healthy Breakfast

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

配点 : 100

問題文

高橋くんは、朝食にご飯、味噌汁、サラダを 1 皿ずつ食べます。

高橋くんの家のテーブルは細長いので、 3 皿を横一列に並べました。並べ方は文字列 S によって与えられ、左から i 番目の皿は S_iR ならご飯、 S_iM なら味噌汁、 S_iS ならサラダです。

ご飯の皿が味噌汁の皿より左にあるかどうかを判定してください。

制約

  • |S| = 3
  • S には R M S1 文字ずつ含まれる

入力

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

S

出力

ご飯の皿が味噌汁の皿より左にあれば Yes 、そうでなければ No を出力してください。


入力例 1

RSM

出力例 1

Yes

ご飯の皿は左から 1 番目にあり、味噌汁の皿は左から 3 番目にあります。ご飯の皿の方が左にあるので、 Yes を出力します。


入力例 2

SMR

出力例 2

No

左から順に、サラダの皿、味噌汁の皿、ご飯の皿と並んでいます。

Score : 100 points

Problem Statement

Takahashi eats three plates for breakfast: rice, miso soup, and salad.

His table is long and narrow, so he arranged the three plates in a row. The arrangement is given by a string S, where the i-th plate from the left is rice if S_i is R, miso soup if S_i is M, and salad if S_i is S.

Determine whether the plate of rice is to the left of the plate of miso soup.

Constraints

  • |S| = 3
  • S contains one R, one M, and one S.

Input

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

S

Output

Print Yes if the plate of rice is to the left of the plate of miso soup, and No otherwise.


Sample Input 1

RSM

Sample Output 1

Yes

The plate of rice is at the 1st position from the left, and the plate of miso soup is at the 3rd position from the left. Since the plate of rice is to the left, print Yes.


Sample Input 2

SMR

Sample Output 2

No

The plates are arranged as salad, miso soup, and rice from left to right.

B - Vertical Reading

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

配点 : 200

問題文

英小文字からなる文字列 ST が与えられます。

以下の条件を満たす 1 \leq c \leq w < |S| となる整数の組 cw が存在するか判定してください。ただし、 |S| は文字列 S の長さを表します。ここで、w|S| 未満である必要があることに注意してください。

  • S を先頭から順に w 文字毎に区切ったとき、長さが c 以上の文字列の c 文字目を順番に連結した文字列が T と一致する

制約

  • ST は英小文字からなる文字列
  • 1 \leq |T| \leq |S| \leq 100

入力

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

S T

出力

条件を満たすような 1 \leq c \leq w < |S| となる整数の組 cw が存在する場合は Yes を、存在しない場合は No を出力せよ。


入力例 1

atcoder toe

出力例 1

Yes

S2 文字毎に区切ると以下のようになります。

at
co
de
r

区切った後、 2 文字以上の文字列の 2 文字目を取り出し連結させたときの文字列は、 toe となり T と一致します。よって、 Yes を出力します。


入力例 2

beginner r

出力例 2

No

w=|S| であることはないため、条件を満たすような 1 \leq c \leq w < |S| となる整数の組 cw は存在しません。よって、 No を出力します。


入力例 3

verticalreading agh

出力例 3

No

Score : 200 points

Problem Statement

You are given two strings S and T consisting of lowercase English letters.

Determine if there exists a pair of integers c and w such that 1 \leq c \leq w < |S| and the following condition is satisfied. Here, |S| denotes the length of the string S. Note that w must be less than |S|.

  • If S is split at every w characters from the beginning, the concatenation of the c-th characters of the substrings of length at least c in order equals T.

Constraints

  • S and T are strings consisting of lowercase English letters.
  • 1 \leq |T| \leq |S| \leq 100

Input

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

S T

Output

Print Yes if there exists a pair of integers c and w such that 1 \leq c \leq w < |S| and the condition is satisfied, and No otherwise.


Sample Input 1

atcoder toe

Sample Output 1

Yes

If S is split at every two characters, it looks like this:

at
co
de
r

Then, the concatenation of the 2nd characters of the substrings of length at least 2 is toe, which equals T. Thus, print Yes.


Sample Input 2

beginner r

Sample Output 2

No

w=|S| is not allowed, and no pair of integers 1 \leq c \leq w < |S| satisfies the condition. Thus, print No.


Sample Input 3

verticalreading agh

Sample Output 3

No
C - Move It

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

配点 : 250

問題文

1 から N の番号がついた N 個の箱と 1 から N の番号がついた N 個の荷物があります。荷物 i (1 \leq i \leq N) は箱 A_i の中にあり、重さは W_i です。

あなたは荷物を一つ選び、他の箱の中に移動させる操作を 0 回以上繰り返し行うことができます。1 回の操作で移動させる荷物の重さが w であるとき、w のコストがかかります。

全ての箱に荷物が 1 つずつ入っている状態にするためにかかるコストの総和の最小値を求めてください。

制約

  • 1 \leq N \leq 10^{5}
  • 1 \leq A_i \leq N (1 \leq i \leq N)
  • 1 \leq W_i \leq 10^{4} (1 \leq i \leq N)
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N
W_1 W_2 \ldots W_N

出力

全ての箱に荷物が 1 つずつ入っている状態にするためにかかるコストの総和の最小値を出力せよ。


入力例 1

5
2 2 3 3 5
33 40 2 12 16

出力例 1

35

以下の 2 回の荷物の移動で、すべての箱に荷物が 1 つずつ入っている状態にすることができます。

  • 荷物 1 を箱 2 から箱 1 に移す。このとき、コストは 33 である。
  • 荷物 3 を箱 3 から箱 4 に移す。このとき、コストは 2 である。

この 2 回の荷物の移動は合計 35 のコストかかります。 35 未満のコストですべての箱に荷物が 1 つずつ入っている状態にすることはできないため、 35 を出力します。


入力例 2

12
3 6 7 4 12 4 8 11 11 1 8 11
3925 9785 9752 3587 4013 1117 3937 7045 6437 6208 3391 6309

出力例 2

17254

Score : 250 points

Problem Statement

There are N boxes numbered 1 to N and N items numbered 1 to N. Item i (1 \leq i \leq N) is in box A_i and has a weight of W_i.

You can repeatedly perform the operation of choosing an item and moving it to another box zero or more times. If the weight of the item being moved is w, the cost of the operation is w.

Find the minimum total cost required to make each box contain exactly one item.

Constraints

  • 1 \leq N \leq 10^{5}
  • 1 \leq A_i \leq N (1 \leq i \leq N)
  • 1 \leq W_i \leq 10^{4} (1 \leq i \leq N)
  • 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
W_1 W_2 \ldots W_N

Output

Print the minimum total cost required to make each box contain exactly one item.


Sample Input 1

5
2 2 3 3 5
33 40 2 12 16

Sample Output 1

35

With the following two moves, you can make each box contain exactly one item:

  • Move item 1 from box 2 to box 1. The cost is 33.
  • Move item 3 from box 3 to box 4. The cost is 2.

The total cost of these two moves is 35. It is impossible to make each box contain exactly one item with a cost less than 35, so print 35.


Sample Input 2

12
3 6 7 4 12 4 8 11 11 1 8 11
3925 9785 9752 3587 4013 1117 3937 7045 6437 6208 3391 6309

Sample Output 2

17254
D - Ghost Ants

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

配点 : 350

問題文

数直線上に 1 から N の番号がつけられた N 匹の蟻がいます。 蟻 i (1 \leq i \leq N) ははじめ座標 X_i にいて、正負どちらかの方向を向いています。はじめに全ての蟻は相異なる座標にいます。各蟻が向いている方向は長さ N01 文字列 S で表され、S_i0 のとき蟻 i は負の方向を向いており、 1 のとき蟻 i は正の方向を向いています。

現在を時刻 0 とし、時刻 (T+0.1) までの (T+0.1) 単位時間にわたって、N 匹の蟻がそれぞれの向いている方向に向かって単位時間あたり 1 の速さで移動します。 複数の蟻が同じ座標に到達すると、それらの蟻はすれ違い、方向や速度を変えずに通り過ぎます。 (T+0.1) 単位時間が経過したとき、すべての蟻は停止します。

1 \leq i < j \leq N を満たし、今から時刻 (T+0.1) までに蟻 i と蟻 j がすれ違う整数の組 (i,j) の個数を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^{5}
  • 1 \leq T \leq 10^{9}
  • S01 からなる長さ N の文字列
  • -10^{9} \leq X_i \leq 10^{9} (1 \leq i \leq N)
  • X_i \neq X_j (1 \leq i < j \leq N)
  • N,T,X_i (1 \leq i \leq N) は整数

入力

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

N T
S
X_1 X_2 ... X_N

出力

答えを出力せよ。


入力例 1

6 3
101010
-5 -1 0 1 2 4

出力例 1

5

以下の 5 つの蟻の組み合わせがすれ違います。

  • 3 と蟻 4 が時刻 0.5 にすれ違う。
  • 5 と蟻 6 が時刻 1 にすれ違う。
  • 1 と蟻 2 が時刻 2 にすれ違う。
  • 3 と蟻 6 が時刻 2 にすれ違う。
  • 1 と蟻 4 が時刻 3 にすれ違う。

これ以外の蟻の組み合わせはすれ違うことはないため、5 を出力します。


入力例 2

13 656320850
0100110011101
-900549713 -713494784 -713078652 -687818593 -517374932 -498415009 -472742091 -390030458 -379340552 -237481538 -44636942 352721061 695864366

出力例 2

14

Score : 350 points

Problem Statement

There are N ants on a number line, labeled 1 to N. Ant i (1 \leq i \leq N) starts at coordinate X_i and faces either a positive or negative direction. Initially, all ants are at distinct coordinates. The direction each ant is facing is represented by a binary string S of length N, where ant i is facing the negative direction if S_i is 0 and the positive direction if S_i is 1.

Let the current time be 0, and the ants move in their respective directions at a speed of 1 unit per unit time for (T+0.1) units of time until time (T+0.1). If multiple ants reach the same coordinate, they pass through each other without changing direction or speed. After (T+0.1) units of time, all ants stop.

Find the number of pairs (i, j) such that 1 \leq i < j \leq N and ants i and j pass each other from now before time (T+0.1).

Constraints

  • 2 \leq N \leq 2 \times 10^{5}
  • 1 \leq T \leq 10^{9}
  • S is a string of length N consisting of 0 and 1.
  • -10^{9} \leq X_i \leq 10^{9} (1 \leq i \leq N)
  • X_i \neq X_j (1 \leq i < j \leq N)
  • N, T, and X_i (1 \leq i \leq N) are integers.

Input

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

N T
S
X_1 X_2 ... X_N

Output

Print the answer.


Sample Input 1

6 3
101010
-5 -1 0 1 2 4

Sample Output 1

5

The following five pairs of ants pass each other:

  • Ant 3 and ant 4 pass each other at time 0.5.
  • Ant 5 and ant 6 pass each other at time 1.
  • Ant 1 and ant 2 pass each other at time 2.
  • Ant 3 and ant 6 pass each other at time 2.
  • Ant 1 and ant 4 pass each other at time 3.

No other pairs of ants pass each other, so print 5.


Sample Input 2

13 656320850
0100110011101
-900549713 -713494784 -713078652 -687818593 -517374932 -498415009 -472742091 -390030458 -379340552 -237481538 -44636942 352721061 695864366

Sample Output 2

14
E - Random Swaps of Balls

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

配点 : 450

問題文

N - 1 個の白いボールと 1 個の黒いボールがあります。これらの N 個のボールが横一列に並んでおり、はじめ黒いボールが最も左にあります。

高橋くんは、これから以下の操作をちょうど K 回行います。

  • 1 以上 N 以下の整数を一様ランダムに選ぶ試行を 2 回行う。選んだ整数をそれぞれ a, b とする。さらに、 a \neq b であれば左から a 番目のボールと b 番目のボールを交換する。

K 回の操作のあと黒いボールがある位置を左から x 番目とします。x の期待値を \text{mod} \ 998244353 で求めてください。

期待値 \text{mod} \ 998244353 とは 求める期待値は必ず有理数になることが証明できます。 また、この問題の制約のもとでは、その値を既約分数 \frac{P}{Q} で表した時、Q \not \equiv 0 \pmod{998244353} となることも証明できます。 よって、R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353 を満たす整数 R が一意に定まります。 この R を答えてください。

制約

  • 1 \leq N \leq 998244352
  • 1 \leq K \leq 10^5

入力

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

N K

出力

答えを 1 行に出力せよ。


入力例 1

2 1

出力例 1

499122178

1 回の操作が終わった後、黒いボールが左から 1 番目にある確率、 2 番目にある確率はそれぞれ \displaystyle \frac{1}{2} です。よって期待値は \displaystyle \frac{3}{2} です。


入力例 2

3 2

出力例 2

554580198

入力例 3

4 4

出力例 3

592707587

Score : 450 points

Problem Statement

There are N - 1 white balls and one black ball. These N balls are arranged in a row, with the black ball initially at the leftmost position.

Takahashi will perform the following operation exactly K times.

  • Choose an integer uniformly at random between 1 and N, inclusive, twice. Let a and b the chosen integers. If a \neq b, swap the a-th and b-th balls from the left.

After K operations, let the black ball be at the x-th position from the left. Find the expected value of x, modulo 998244353.

What is expected value modulo 998244353? It can be proved that the sought expected value will always be rational. Additionally, under the constraints of this problem, it can be proved that if this value is expressed as an irreducible fraction \frac{P}{Q}, then Q \not \equiv 0 \pmod{998244353}. Therefore, there exists a unique integer R such that R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353. Report this R.

Constraints

  • 1 \leq N \leq 998244352
  • 1 \leq K \leq 10^5

Input

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

N K

Output

Print the answer in one line.


Sample Input 1

2 1

Sample Output 1

499122178

After one operation, the probabilities that the black ball is at the 1st position and the 2nd position from the left are both \displaystyle \frac{1}{2}. Thus, the expected value is \displaystyle \frac{3}{2}.


Sample Input 2

3 2

Sample Output 2

554580198

Sample Input 3

4 4

Sample Output 3

592707587
F - InterSections

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

配点 : 550

問題文

1 から N までの番号のついた N 個の区間が与えられます。 区間 i[L_i,R_i] です。

区間 [l_a,r_a] と区間 [l_b,r_b](l_a < l_b < r_a < r_b) または (l_b < l_a < r_b < r_a) を満たすとき、交差するといいます。

f(l,r)1 \leq i \leq N を満たし、区間 [l,r] と区間 i が交差する i の個数と定義します。

0 \leq l < r \leq 10^{9} を満たす整数の組 (l,r) において、 f(l,r) の最大値を達成する (l,r) の組のうち l が最小のものを答えてください。そのような組が複数存在する場合はさらにそのうちで r が最小のものを答えてください (0 \leq l < r より、 答えるべき (l,r) の組は一意に定まります)。

制約

  • 1 \leq N \leq 10^{5}
  • 0 \leq L_i < R_i \leq 10^{9} (1 \leq i \leq N)
  • 入力は全て整数である

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

出力

答えとなる組 (l,r) を次の形式で出力せよ。

l r

入力例 1

5
1 7
3 9
7 18
10 14
15 20

出力例 1

4 11

f(l,r) の最大値は 4 であり、f(l,r)=4 となる (l,r) のうち l の最小値は 4 です。 f(l,r)=4 かつ l=4 を満たす (l,r) は以下の 5 通りです。

  • (l,r)=(4,11)
  • (l,r)=(4,12)
  • (l,r)=(4,13)
  • (l,r)=(4,16)
  • (l,r)=(4,17)

このうち、r の最小値は 11 であるため、411 を出力します。


入力例 2

11
856977192 996441446
298251737 935869360
396653206 658841528
710569907 929136831
325371222 425309117
379628374 697340458
835681913 939343451
140179224 887672320
375607390 611397526
93530028 581033295
249611310 775998537

出力例 2

396653207 887672321

Score : 550 points

Problem Statement

You are given N intervals numbered 1 to N. Interval i is [L_i, R_i].

Two intervals [l_a, r_a] and [l_b, r_b] are said to intersect if and only if they satisfy either (l_a < l_b < r_a < r_b) or (l_b < l_a < r_b < r_a).

Define f(l, r) as the number of intervals i (1 \leq i \leq N) that intersect with the interval [l, r].

Among all pairs of integers (l, r) satisfying 0 \leq l < r \leq 10^{9}, find the pair (l, r) that maximizes f(l, r). If there are multiple such pairs, choose the one with the smallest l. If there are still multiple pairs, choose the one with the smallest r among them. (Since 0 \leq l < r, the pair (l, r) to be answered is uniquely determined.)

Constraints

  • 1 \leq N \leq 10^{5}
  • 0 \leq L_i < R_i \leq 10^{9} (1 \leq i \leq N)
  • All input values are integers.

Input

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N

Output

Print the sought pair (l, r) in the following format:

l r

Sample Input 1

5
1 7
3 9
7 18
10 14
15 20

Sample Output 1

4 11

The maximum value of f(l, r) is 4, and among the pairs (l, r) that achieve f(l, r) = 4, the smallest l is 4. The pairs (l, r) that satisfy f(l, r) = 4 and l = 4 are the following five:

  • (l, r) = (4, 11)
  • (l, r) = (4, 12)
  • (l, r) = (4, 13)
  • (l, r) = (4, 16)
  • (l, r) = (4, 17)

Among these, the smallest r is 11, so print 4 and 11.


Sample Input 2

11
856977192 996441446
298251737 935869360
396653206 658841528
710569907 929136831
325371222 425309117
379628374 697340458
835681913 939343451
140179224 887672320
375607390 611397526
93530028 581033295
249611310 775998537

Sample Output 2

396653207 887672321
G - Suitable Edit for LIS

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

配点 : 625

問題文

長さ N の整数列 A が与えられます。高橋くんは、 1 回だけ次の操作をします。

  • 1 以上 N 以下の整数 x と、任意の整数 y を選ぶ。A_xy に置き換える。

操作をした後の A の最長増加部分列の長さとしてあり得る最大の値を求めてください。

最長増加部分列とは?

A の部分列とは A の要素をいくつか抜き出して元の順に並べてできる列を指します。

A の最長増加部分列とは、 A の狭義単調増加な部分列のうち列の長さが最大のものを指します。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9

入力

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

N
A_1 A_2 \cdots A_N

出力

答えを 1 行に出力せよ。


入力例 1

4
3 2 2 4

出力例 1

3

与えられた数列 A の LIS の長さは 2 です。例えば A_11 に置き換えると、操作後の A の LIS の長さが 3 になり、これが最大です。


入力例 2

5
4 5 3 6 7

出力例 2

4

Score : 625 points

Problem Statement

You are given an integer sequence A of length N. Takahashi will perform the following operation exactly once:

  • Choose an integer x between 1 and N, inclusive, and an arbitrary integer y. Replace A_x with y.

Find the maximum possible length of a longest increasing subsequence (LIS) of A after performing the operation.

What is longest increasing subsequence?

A subsequence of a sequence A is a sequence that can be derived from A by extracting some elements without changing the order.

A longest increasing subsequence of a sequence A is a longest subsequence of A that is strictly increasing.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9

Input

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

N
A_1 A_2 \cdots A_N

Output

Print the answer on a single line.


Sample Input 1

4
3 2 2 4

Sample Output 1

3

The length of a LIS of the given sequence A is 2. For example, if you replace A_1 with 1, the length of a LIS of A becomes 3, which is the maximum.


Sample Input 2

5
4 5 3 6 7

Sample Output 2

4