A - Thoughtful Shopping

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

高橋君はスーパーマーケットに買い物に来ました。スーパーマーケットでは、玉ねぎが A 個、じゃがいもが B 個売られています。

高橋君は非常に思慮深いので、各食材について、自分がその食材を全て買い占めてしまうことになる場合、購入を諦めます。

高橋君は、玉ねぎを C 個、じゃがいもを D 個買うことができますか?

制約

  • 1 \leq A,B,C,D \leq 100
  • 入力は全て整数である

入力

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

A B C D

出力

玉ねぎを C 個、じゃがいもを D 個買うことができるならば Yes、できないならば No と出力せよ。


入力例 1

3 5 4 2

出力例 1

No

玉ねぎが 3 個しか売られていないため、4 個買うことはできません。


入力例 2

30 10 6 10

出力例 2

No

じゃがいもが 10 個売られていますが、10 個買うと全て買い占めてしまうことになるため、高橋君は購入を諦めます。


入力例 3

100 100 1 1

出力例 3

Yes

Problem Statement

Takahashi went shopping at a supermarket. The supermarket has A onions and B potatoes in stock.

Takahashi is so thoughtful that, for each vegetable, if buying the desired quantity would empty the stock, he decides not buying it.

Can Takahashi buy C onions and D potatoes?

Constraints

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

Input

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

A B C D

Output

Print Yes if he can buy C onions and D potatoes, and No otherwise.


Sample Input 1

3 5 4 2

Sample Output 1

No

They sells only three onions, so he cannot buy four.


Sample Input 2

30 10 6 10

Sample Output 2

No

They sells ten potatoes, but buying ten will empty the stock, so he decides not buying them.


Sample Input 3

100 100 1 1

Sample Output 3

Yes
B - Before After

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

A+C=B または B+C=A を満たす 3 つの整数が与えられます。
2 つの等式のうち、成立する方を x+C=y とします。
この等式の x,y,Cx -> y (C) の形で出力してください。
ただし、 C については符号付きで出力してください。

具体例を示します。

  • A=1000, B=1050, C=50 のとき 1000+50=1050 が成立するので、 1000 -> 1050 (+50) と出力してください。
  • A=1000, B=1050, C=-50 のとき 1050+(-50)=1000 が成立するので、 1050 -> 1000 (-50) と出力してください。

制約

  • 入力は全て整数
  • 0 \le A,B \le 5000
  • -5000 \le C \le 5000
  • C \neq 0
  • A+C=B または B+C=A が成り立つ

入力

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

A B C

出力

答えを出力せよ。


入力例 1

1000 1050 50

出力例 1

1000 -> 1050 (+50)

入力例 2

1000 1050 -50

出力例 2

1050 -> 1000 (-50)

入力例 3

1234 1111 -123

出力例 3

1234 -> 1111 (-123)

入力例 4

3000 2500 500

出力例 4

2500 -> 3000 (+500)

Problem Statement

You are given three integers satisfying A+C=B or B+C=A.
Among these two equations, let x+C=y be the one that is satisfied.
Print x,y,C in this equation in the format x -> y (C).
Here, C must be printed as a signed integer.

For example:

  • If A=1000, B=1050, C=50, we have 1000+50=1050, so print 1000 -> 1050 (+50).
  • If A=1000, B=1050, C=-50, we have 1050+(-50)=1000, so print 1050 -> 1000 (-50).

Constraints

  • All input values are integers.
  • 0 \le A,B \le 5000
  • -5000 \le C \le 5000
  • C \neq 0
  • A+C=B or B+C=A.

Input

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

A B C

Output

Print the answer.


Sample Input 1

1000 1050 50

Sample Output 1

1000 -> 1050 (+50)

Sample Input 2

1000 1050 -50

Sample Output 2

1050 -> 1000 (-50)

Sample Input 3

1234 1111 -123

Sample Output 3

1234 -> 1111 (-123)

Sample Input 4

3000 2500 500

Sample Output 4

2500 -> 3000 (+500)
C - Postponement of meetings

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

整数 N,K と長さ N の文字列 S が与えられます。

あなたは会議の日程を決めています。

ある日に会議ができるかどうかは文字列 S で与えられ、 Si 文字目が o なら i 日目に会議ができることを、 x なら会議ができないことを表しています。

あなたは会議を K 日目に行う予定でしたが、その日の会議は延期になりました。

あなたは K+1 日目以降で会議ができる日のうち K 日目に最も近い日に会議を振り替えようとしています。

振り替えた後の会議がいつになるか求めてください。ただし、会議が N 日目までに必ず振り替えられることが保証されます。

制約

  • 2\le N\le 10^3
  • 1\le K\le N
  • N,K は整数
  • So および x からなる長さ N の文字列
  • SK 文字目は o
  • SK+1 文字目以降に o が必ず存在する

入力

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

N K
S

出力

答えを出力せよ。


入力例 1

6 2
ooxoxo

出力例 1

4

会議が可能なのは 1,2,4,6 日目です。 2+1=3 日目以降で会議可能な日のうち 2 日目に最も近いのは 4 日目なので、 4 を出力してください。


入力例 2

5 4
ooooo

出力例 2

5

入力例 3

11 6
ooxxxoxooxx

出力例 3

8

Problem Statement

You are given integers N and K, and a length-N string S.

You are scheduling a meeting.

Whether you can set a meeting on each day is described by a string S. If the i-th character of S is o, you can set a meeting on day i; if it is x, you cannot.

The meeting was scheduled on day K, but it was postponed.

You now need to reschedule it to the closest available day on or after day (K + 1).

Find the new day for the meeting. It is guaranteed that you can always set the meeting by day N.

Constraints

  • 2\le N\le 10^3
  • 1\le K\le N
  • N and K are integers.
  • S is a string of length N, consisting of o and x.
  • The K-th character of S is o.
  • There is an o at the (K+1)-th or later position of S.

Input

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

N K
S

Output

Print the answer.


Sample Input 1

6 2
ooxoxo

Sample Output 1

4

You can set the meeting on days 1,2,4,6. On or after day 2+1=3, the closest to day 2 is day 4, so print 4.


Sample Input 2

5 4
ooooo

Sample Output 2

5

Sample Input 3

11 6
ooxxxoxooxx

Sample Output 3

8
D - Character Adjacent

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

HW 列のグリッドがあります。グリッドの各マスには英小文字が書かれており、上から i 行目、左から j 列目に書かれた文字は与えられる文字列 S_ij 文字目と一致します。

英小文字 c_1,c_2 が与えられます。グリッドで文字 c_1 が書かれたマスと文字 c_2 が書かれたマスが辺で接する場所が存在するか判定してください。

より厳密には、以下の条件を満たす正整数の組 (h_1,w_1,h_2,w_2) が存在するか判定してください。

  • 1\le h_1,h_2\le H
  • 1\le w_1,w_2\le W
  • グリッドの上から h_1 行目、左から w_1 列目に書かれた文字は c_1 である。
  • グリッドの上から h_2 行目、左から w_2 列目に書かれた文字は c_2 である。
  • |h_1-h_2|+|w_1-w_2|=1

制約

  • 1\le H,W\le 10
  • H,W は整数
  • c_1,c_2 は英小文字
  • S_1,S_2,\ldots,S_H は英小文字からなる長さ W の文字列

入力

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

H W c_1 c_2
S_1
S_2
\vdots
S_H

出力

グリッドで文字 c_1c_2 が辺で接する場所が存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

3 3 a t
abc
xtc
qad

出力例 1

Yes

上から 3 行目、左から 2 列目に a が書かれており、上から 2 行目、左から 2 列目に t が書かれています。これらのマスは隣接しているので、 Yes を出力してください。


入力例 2

4 3 x r
xxx
xxb
kkr
rrr

出力例 2

No

xr が隣接している場所は存在しないので、 No を出力してください。


入力例 3

5 5 n x
iaamg
krwwu
nzyxc
sgrnt
mmjip

出力例 3

Yes

Problem Statement

You are given a grid with H rows and W columns. Each cell contains a lowercase English letter; the letter in the cell at row i (counted from the top) and column j (from the left) is the j-th character of the given string S_i.

Given lowercase English letters c_1 and c_2, determine if the grid has a pair of cells that share a side, containing letters c_1 and c_2.

More precisely, determine if there is a tuple of positive integers (h_1,w_1,h_2,w_2) such that:

  • 1\le h_1,h_2\le H
  • 1\le w_1,w_2\le W
  • The cell in row h_1 and column w_1 contains the letter c_1.
  • The cell in row h_2 and column w_2 contains the letter c_2.
  • |h_1-h_2|+|w_1-w_2|=1

Constraints

  • 1\le H,W\le 10
  • H and W are integers.
  • c_1 and c_2 are lowercase English letters.
  • Each of S_1,S_2,\ldots,S_H is a string of length W, consisting of lowercase English letters.

Input

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

H W c_1 c_2
S_1
S_2
\vdots
S_H

Output

Print Yes if the grid has adjacent cells with the letters c_1 and c_2, and No otherwise.


Sample Input 1

3 3 a t
abc
xtc
qad

Sample Output 1

Yes

The cell in row 3 and column 2 from the left contains a; the cell in row 2 and column 2 contains t. These cells share a side, so print Yes.


Sample Input 2

4 3 x r
xxx
xxb
kkr
rrr

Sample Output 2

No

There are no adjacent cells with x and r, so print No.


Sample Input 3

5 5 n x
iaamg
krwwu
nzyxc
sgrnt
mmjip

Sample Output 3

Yes
E - Sort Fractions

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N 個の相異なる既約分数からなる列 \displaystyle \left(\frac{A_1}{B_1}, \frac{A_2}{B_2}, \dots, \frac{A_N}{B_N}\right) が与えられるので、小さい順にソートしてください。

制約

  • 1 \leq N \leq 10^3
  • 1 \leq A_i \leq 10^{18}
  • 1 \leq B_i \leq 10^{18}
  • \gcd(A_i, B_i) = 1
  • i \neq j ならば (A_i,B_i) \neq (A_j,B_j)
  • 入力される値は全て整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

ソートした後の列を \displaystyle \left(\frac{C_1}{D_1}, \frac{C_2}{D_2}, \dots, \frac{C_N}{D_N} \right) として以下の形式で答えを出力せよ。

C_1 D_1
C_2 D_2
\vdots
C_N D_N

入力例 1

3
2 3
1 2
3 5

出力例 1

1 2
3 5
2 3

\displaystyle \frac{1}{2} \lt \frac{3}{5} \lt \frac{2}{3} が成り立つので、ソートした後の列は \displaystyle \left(\frac{1}{2}, \frac{3}{5}, \frac{2}{3} \right) になります。


入力例 2

5
1 5
1 7
6 5
9 8
9 4

出力例 2

1 7
1 5
9 8
6 5
9 4

入力例 3

10
39 83
93 70
5 92
81 67
30 53
57 5
91 58
21 85
81 34
37 38

出力例 3

5 92
21 85
39 83
30 53
37 38
81 67
93 70
91 58
81 34
57 5

Problem Statement

Given a sequence of N pairwise-distinct irreducible fractions \displaystyle \left(\frac{A_1}{B_1}, \frac{A_2}{B_2}, \dots, \frac{A_N}{B_N}\right), sort it in ascending order.

Constraints

  • 1 \leq N \leq 10^3
  • 1 \leq A_i \leq 10^{18}
  • 1 \leq B_i \leq 10^{18}
  • \gcd(A_i, B_i) = 1
  • (A_i,B_i) \neq (A_j,B_j) if i \neq j.
  • All input values are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

If the resulting sequence is \displaystyle \left(\frac{C_1}{D_1}, \frac{C_2}{D_2}, \dots, \frac{C_N}{D_N} \right), print it in the following format.

C_1 D_1
C_2 D_2
\vdots
C_N D_N

Sample Input 1

3
2 3
1 2
3 5

Sample Output 1

1 2
3 5
2 3

Since \displaystyle \frac{1}{2} \lt \frac{3}{5} \lt \frac{2}{3}, the sorted sequence is \displaystyle \left(\frac{1}{2}, \frac{3}{5}, \frac{2}{3} \right).


Sample Input 2

5
1 5
1 7
6 5
9 8
9 4

Sample Output 2

1 7
1 5
9 8
6 5
9 4

Sample Input 3

10
39 83
93 70
5 92
81 67
30 53
57 5
91 58
21 85
81 34
37 38

Sample Output 3

5 92
21 85
39 83
30 53
37 38
81 67
93 70
91 58
81 34
57 5
F - Love Triangle

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N 人の人がいます。

i の親友は人 A_i です。人 i が人 j の親友ならば、人 j は人 i の親友であることが保証されます。

i が好きな人は人 B_i です。

次の条件を全て満たす整数の組 (i,j,k) の個数を求めてください。

  • 1 \leq i < j \leq N
  • 1 \leq k \leq N
  • i と人 j は親友である
  • i と人 j はともに人 k が好きな人である

制約

  • 2 \leq N \leq 2\times 10^5
  • N は偶数
  • 1 \leq A_i,B_i \leq N
  • A_i \neq i
  • A_i=j ならば A_j=i
  • B_i \neq i
  • 入力は全て整数である

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

答えを出力せよ。


入力例 1

6
6 3
3 1
2 1
5 5
4 4
1 2

出力例 1

1

(2,3,1) のみが条件を満たします。


入力例 2

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

出力例 2

2

(1,9,7),(4,5,8) が条件を満たします。

Problem Statement

There are N people.

Person i's best friend is person A_i. If person i is person j's best friend, then it is also guaranteed that person j is person i's best friend.

Person i loves person B_i.

Find the number of triples of integers (i,j,k) such that:

  • 1 \leq i < j \leq N.
  • 1 \leq k \leq N.
  • Persons i and j are best friends of each other.
  • Persons i and j both love person k.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • N is even.
  • 1 \leq A_i,B_i \leq N
  • A_i \neq i
  • If A_i=j, then A_j=i.
  • B_i \neq i
  • All input values are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the answer.


Sample Input 1

6
6 3
3 1
2 1
5 5
4 4
1 2

Sample Output 1

1

Only (2,3,1) is eligible.


Sample Input 2

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

Sample Output 2

2

Tuples (1,9,7),(4,5,8) are eligible.

G - Long Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

長さ N の数列 A があります。
数列 BA を無限に繰り返したものです。
厳密には、全ての整数 i について B_i = A_{((i-1)\ {\rm mod}\ N) + 1} と定義します。但し、 p\ {\rm mod}\ qpq で割った余りとします。

以下の Q 個の質問に答えてください。そのうち i 個目は次の通りです。

  • BL_i 項目から R_i 項目までの総和を求めよ。

制約

  • 入力は全て整数
  • 1 \le N \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • 0 \le A_i \le 10^6
  • 1 \le L_i \le R_i \le 10^{12}

入力

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

N Q
A_1 A_2 \dots A_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

出力

全体で Q 行出力せよ。
そのうち i 行目には、 i 個目の質問に対する答えを出力せよ。


入力例 1

8 5
3 1 4 1 5 9 2 6
3 7
6 10
1 16
4 21
1 1000000000000

出力例 1

21
21
62
68
3875000000000

B=(3,1,4,1,5,9,2,6,3,1,4,1,5,9,2,6,\dots) です。

この入力には 5 個の質問が含まれ、最初の 2 個は次の通りです。

  • 1 個目の質問では、 3 項目から 7 項目までの総和を求めます。この値は 4+1+5+9+2 = 21 です。
  • 2 個目の質問では、 6 項目から 10 項目までの総和を求めます。この値は 9+2+6+3+1=21 です。

Problem Statement

You are given a sequence A of length N.
Sequence B is an infinite-time repetition of A.
More precisely, define B_i = A_{((i-1)\ {\rm mod}\ N) + 1} for all integers i, where p\ {\rm mod}\ q denotes the remainder when p is divided by q.

Answer the following Q queries. The i-th query is:

  • Find the sum of the L_i-th through R_i-th terms of B.

Constraints

  • All input values are integers.
  • 1 \le N \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • 0 \le A_i \le 10^6
  • 1 \le L_i \le R_i \le 10^{12}

Input

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

N Q
A_1 A_2 \dots A_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

Output

Print Q lines.
The i-th line should contain the answer to the i-th query.


Sample Input 1

8 5
3 1 4 1 5 9 2 6
3 7
6 10
1 16
4 21
1 1000000000000

Sample Output 1

21
21
62
68
3875000000000

We have B=(3,1,4,1,5,9,2,6,3,1,4,1,5,9,2,6,\dots).

This input contains five queries. The first two are as follows.

  • For the 1-st query, find the sum of the 3-rd through 7-th terms: 4+1+5+9+2 = 21.
  • For the 2-nd query, find the sum of the 6-th through 10-th terms: 9+2+6+3+1=21.
H - Sandwich triplet

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

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

以下の条件を全て満たす整数の組 (i,j,k)サンドイッチな組 と呼びます。

  • 1\le i < j < k \le N
  • A_i=A_k\neq A_j

サンドイッチな組がいくつあるか求めてください。

制約

  • 3\le N\le 2\times 10^5
  • 1\le A_i\le 10^9
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

5
2 5 2 2 5

出力例 1

4

(i,j,k)=(1,2,3),(1,2,4),(2,3,5),(2,4,5) がサンドイッチな組です。


入力例 2

5
4 4 4 4 4

出力例 2

0

どのように (i,j,k) を選んでも A_i=A_k\neq A_j を満たしません。


入力例 3

10
2 2 6 4 4 6 6 2 3 2

出力例 3

27

Problem Statement

You are given an integer N, and an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.

A sandwich triple is an integer triple (i,j,k) such that:

  • 1\le i < j < k \le N; and
  • A_i=A_k\neq A_j.

How many sandwich triples are there?

Constraints

  • 3\le N\le 2\times 10^5
  • 1\le A_i\le 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 answer.


Sample Input 1

5
2 5 2 2 5

Sample Output 1

4

The sandwich triples are (i,j,k)=(1,2,3),(1,2,4),(2,3,5),(2,4,5).


Sample Input 2

5
4 4 4 4 4

Sample Output 2

0

No choice of (i,j,k) satisfies A_i=A_k\neq A_j.


Sample Input 3

10
2 2 6 4 4 6 6 2 3 2

Sample Output 3

27
I - Num of Unique Characters

Time Limit: 3 sec / Memory Limit: 1024 MiB

問題文

文字列屋さんに N 個の文字列が売られています。i 番目の文字列は S_i であり、値段は X_i 円です。

Y 円以内でいくつか( 0 個でもよい)の文字列を購入するとき、購入した文字列のいずれかに含まれる文字の種類数の最大値を求めてください。

制約

  • 1\leq N \leq 18
  • 1 \leq Y \leq 10^9
  • S_i は英小文字からなる長さ 1 以上 100 以下の文字列
  • 1 \leq X_i \leq 10^9
  • 入力の数値は全て整数

入力

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

N Y
S_1 X_1
\vdots
S_N X_N

出力

答えを整数として出力せよ。


入力例 1

4 900
apple 300
orange 200
lemon 500
pomegranate 1000

出力例 1

8

2,3 番目の文字列を 700 円で購入すると a, e, g, l, m, n, o, r8 種類の文字が含まれます。


入力例 2

2 200
melon 100
lemon 100

出力例 2

5

1,2 番目の文字列を合計 200 円で購入すると e, l, m, n, o5 種類の文字が含まれます。


入力例 3

1 1
abcdefghijklmnopqrstuvwxyz 10000

出力例 3

0

文字列を 1 つも購入できないこともあります。

Problem Statement

A string shop sells N strings. The i-th string is S_i and sold for X_i yen (the currency in Japan).

You will buy zero or more strings for a total of at most Y yen. Find the maximum possible number of distinct letters in the bought strings.

Constraints

  • 1\leq N \leq 18
  • 1 \leq Y \leq 10^9
  • S_i is a string of length between 1 and 100, inclusive, consisting of lowercase English letters.
  • 1 \leq X_i \leq 10^9
  • All input numbers are integers.

Input

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

N Y
S_1 X_1
\vdots
S_N X_N

Output

Print the answer as an integer.


Sample Input 1

4 900
apple 300
orange 200
lemon 500
pomegranate 1000

Sample Output 1

8

If you buy the 2-nd and 3-rd strings for a total of 700 yen, they will contain eight distinct letters: a, e, g, l, m, n, o, r.


Sample Input 2

2 200
melon 100
lemon 100

Sample Output 2

5

If you buy the 1-st and 2-nd strings for a total of 200 yen, they will contain five distinct letters: e, l, m, n, o.


Sample Input 3

1 1
abcdefghijklmnopqrstuvwxyz 10000

Sample Output 3

0

You may not be able to buy any strings.

J - Office

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

A 社には N 人の社員が所属しています。

ある日、社員 i は時刻 S_i にオフィスに出社し、時刻 T_i に退社しました。

i について、この日 社員 i がオフィスで会った自分以外の社員の数を求めてください。

ただし、社員 i と社員 j が同時にオフィスにいる時間が存在するとき、かつそのときに限り社員 i と社員 j は会ったとみなします。
また、ある社員の退社と同時に別の社員が出社するときも、これらの社員は同時にオフィスに存在したとみなします。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq S_i \leq T_i \leq 2\times 10^5
  • 入力は全て整数である

入力

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

N
S_1 T_1
S_2 T_2
\vdots
S_N T_N

出力

社員 i がオフィスで会った自分以外の社員の数を X_i とする。X_1,X_2,\ldots,X_N をこの順に空白区切りで出力せよ。


入力例 1

4
1 10
5 30
10 15
20 40

出力例 1

2 3 2 1

社員 1 は社員 2,3 と、社員 2 は社員 1,3,4 と、社員 3 は社員 1,2 と、社員 4 は社員 2 と会いました。


入力例 2

10
33553 130683
93150 198918
44559 198032
87304 140783
58113 161838
858 74380
39597 150815
2425 149780
8298 10367
37788 184869

出力例 2

8 7 8 7 8 7 8 9 2 8

Problem Statement

Company A has N employees.

One day, employee i arrived at work at time S_i and left there at time T_i.

For each i, find how many other employees met employee i at the office on that day.

Here, employees i and j are considered to have met each other if and only if they were at the office at any moment, even if one arrived at work exactly when the other left there.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq S_i \leq T_i \leq 2\times 10^5
  • All input values are integers.

Input

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

N
S_1 T_1
S_2 T_2
\vdots
S_N T_N

Output

If employee i met X_i other employees at the office, print X_1,X_2,\ldots,X_N, separated by spaces.


Sample Input 1

4
1 10
5 30
10 15
20 40

Sample Output 1

2 3 2 1

Employee 1 met employees 2,3; employee 2 met employees 1,3,4; employee 3 met employees 1,2; employee 4 met employee 2.


Sample Input 2

10
33553 130683
93150 198918
44559 198032
87304 140783
58113 161838
858 74380
39597 150815
2425 149780
8298 10367
37788 184869

Sample Output 2

8 7 8 7 8 7 8 9 2 8
K - Most Frequent Character Query

Time Limit: 4 sec / Memory Limit: 1024 MiB

問題文

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

Q 個のクエリが与えられるので処理してください。クエリは 2 種類あり、以下のいずれかの形式で与えられます。

  • 1 p c : Sp 文字目を文字 c に置き換える。
  • 2 l r : Sl 文字目から r 文字目までに最も多く含まれる文字が何個含まれるかを出力する。

制約

  • 1\le N\le 2\times 10^5
  • 1\le Q\le 2\times 10^5
  • S は英小文字からなる長さ N の文字列
  • 1\le p\le N
  • c は英小文字
  • 1\le l\le r\le N

入力

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

N Q
S
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリ \text{query}_Q は以下の 2 種類のいずれかの形式で与えられる。

1 p c
2 l r

出力

問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。


入力例 1

6 4
abbabc
2 1 3
2 1 6
1 5 d
2 1 6

出力例 1

2
3
2
  • S1 文字目から 3 文字目までは abb です。この中では b が最も多く含まれており、その出現回数は 2 回です。したがって、 1 つ目のクエリでは 2 を出力します。
  • S1 文字目から 6 文字目までは abbabc です。この中では b が最も多く含まれており、その出現回数は 3 回です。したがって、 3 つ目のクエリでは 3 を出力します。
  • 3 つ目のクエリでは S5 文字目を d に変更します。その結果、 S= abbadc となります。
  • S1 文字目から 6 文字目までは abbadc です。この中では ab が最も多く含まれており、その出現回数は 2 回です。したがって、 4 つ目のクエリでは 2 を出力します。

入力例 2

8 4
nooutput
1 5 a
1 4 y
1 6 f
1 1 q

出力例 2



入力例 3

7 6
atcoder
2 1 7
1 1 r
2 1 7
1 2 r
2 1 7
1 4 r

出力例 3

1
2
3

Problem Statement

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

Process the given Q queries. There are two kinds of queries given as follows:

  • 1 p c: Set the p-th character of S to the character c.
  • 2 l r: Print how many times the most frequent letter occur, among the l-th through r-th characters of S.

Constraints

  • 1\le N\le 2\times 10^5
  • 1\le Q\le 2\times 10^5
  • S is a string of length N, consisting of lowercase English letters.
  • 1\le p\le N
  • c is a lowercase English letter.
  • 1\le l\le r\le N

Input

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

N Q
S
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query \text{query}_Q is given in one of the following formats:

1 p c
2 l r

Output

Print the response to the queries asked in the problem statement, separated by newlines.


Sample Input 1

6 4
abbabc
2 1 3
2 1 6
1 5 d
2 1 6

Sample Output 1

2
3
2
  • The 1-st through 3-rd characters of S are abb. Among them, b occurs most frequently, namely 2 times. Thus, print 2 for the 1-st query.
  • The 1-st through 6-th characters of S are abbabc. Among them, b occurs most frequently, namely 3 times. Thus, print 3 for the 2-nd query.
  • For the 3-rd query, set the 5-th character of S to d, making S= abbadc.
  • The 1-st through 6-th characters of S are abbadc. Among them, a and b occur most frequently, namely 2 times each. Thus, print 2 for the 4-th query.

Sample Input 2

8 4
nooutput
1 5 a
1 4 y
1 6 f
1 1 q

Sample Output 2



Sample Input 3

7 6
atcoder
2 1 7
1 1 r
2 1 7
1 2 r
2 1 7
1 4 r

Sample Output 3

1
2
3
L - Sushi-Go-Round

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

高橋君と青木君が回転寿司屋に来ました。最初、高橋君と青木君の満足度はともに 0 です。

これから N 皿の寿司が順に流れ、各皿を高橋君と青木君のどちらかが必ず取ります。
i 番目の皿を高橋君が取ると高橋君の満足度は A_i 増え、青木君が取ると青木君の満足度は B_i 増えます。皿を取らなかった方の満足度は変化しません。

どの瞬間にも高橋君と青木君の満足度の差の絶対値が M 以下であるように N 皿を順に分けるとき、最終的な高橋君の満足度の最大値を求めてください。

どの瞬間にも高橋君と青木君の満足度の差の絶対値が M 以下であるように N 皿を順に分けることができないときはそのことを報告してください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 100
  • 1 \leq A_i,B_i \leq 100
  • 入力は全て整数である

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

どの瞬間にも高橋君と青木君の満足度の差が M 以下であるように N 皿を順に分けるとき、最終的な高橋君の満足度の最大値を出力せよ。

どの瞬間にも高橋君と青木君の満足度の差が M 以下であるように N 皿を順に分けることができないとき、かわりに -1 を出力せよ。


入力例 1

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

出力例 1

14

次のように分けることができます。

  • 1 番目の皿を高橋君が取る。高橋君の満足度は 3、青木君の満足度は 0 となる。
  • 2 番目の皿を高橋君が取る。高橋君の満足度は 7、青木君の満足度は 0 となる。
  • 3 番目の皿を青木君が取る。高橋君の満足度は 7、青木君の満足度は 9 となる。
  • 4 番目の皿を高橋君が取る。高橋君の満足度は 9、青木君の満足度は 9 となる。
  • 5 番目の皿を高橋君が取る。高橋君の満足度は 14、青木君の満足度は 9 となる。

どの瞬間にも満足度の差の絶対値が 7 以下になるようにして高橋君の満足度を 15 以上にすることはできないため、これが最大です。


入力例 2

5 3
3 1
4 1
5 9
2 6
5 3

出力例 2

10

次のように分けることができます。

  • 1 番目の皿を青木君が取る。高橋君の満足度は 0、青木君の満足度は 1 となる。
  • 2 番目の皿を青木君が取る。高橋君の満足度は 0、青木君の満足度は 2 となる。
  • 3 番目の皿を高橋君が取る。高橋君の満足度は 5、青木君の満足度は 2 となる。
  • 4 番目の皿を青木君が取る。高橋君の満足度は 5、青木君の満足度は 8 となる。
  • 5 番目の皿を高橋君が取る。高橋君の満足度は 10、青木君の満足度は 8 となる。

入力例 3

5 2
3 1
4 1
5 9
2 6
5 3

出力例 3

-1

どのように分けても、満足度の差の絶対値が 2 より大きくなる瞬間が発生してしまいます。


入力例 4

20 70
22 75
26 45
72 81
47 29
97 2
75 25
82 84
17 56
32 2
28 37
57 39
18 11
79 6
40 68
68 16
40 63
93 49
91 10
55 68
31 80

出力例 4

496

Problem Statement

Takahashi and Aoki came to a conveyor belt sushi restaurant. Initially, each person's happiness is 0.

N dishes of sushi will approach them in order. Each dish is always taken by Takahashi or Aoki.
If Takahahsi takes the i-th dish, Takahashi's happiness increases by A_i; if Aoki takes it, Aoki's happiness increases by B_i. Here, the other person's happiness does not change.

They will distribute the N dishes in order so that the absolute difference of their happinesses does not exceed M at any moment. Find the maximum possible final happiness of Takahashi.

If they cannot distribute the N dishes in order so that the absolute difference of their happinesses does not exceed M at any moment, report that fact.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 100
  • 1 \leq A_i,B_i \leq 100
  • All input values are integers.

Input

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

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum final happiness of Takahashi when they distribute the N dishes in order so that the absolute difference of their happinesses does not exceed M at any moment.

Print -1 instead if they cannot distribute the N dishes in order so that the absolute difference of their happinesses does not exceed M at any moment.


Sample Input 1

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

Sample Output 1

14

They can distribute the dishes as follows:

  • Takahashi takes the 1-st dish. Takahashi's happiness is now 3, and Aoki's is 0.
  • Takahashi takes the 2-nd dish. Takahashi's happiness is now 7, and Aoki's is 0.
  • Aoki takes the 3-rd dish. Takahashi's happiness is now 7, and Aoki's is 9.
  • Takahashi takes the 4-th dish. Takahashi's happiness is now 9, and Aoki's is 9.
  • Takahashi takes the 5-th dish. Takahashi's happiness is now 14, and Aoki's is 9.

This yields the maximum value, because one cannot make the final happiness of Takahashi 15 or greater while keeping the absolute difference of their happinesses not greater than 7.


Sample Input 2

5 3
3 1
4 1
5 9
2 6
5 3

Sample Output 2

10

They can distribute the dishes as follows:

  • Aoki takes the 1-st dish. Takahashi's happiness is now 0, and Aoki's is 1.
  • Aoki takes the 2-nd dish. Takahashi's happiness is now 0, and Aoki's is 2.
  • Takahashi takes the 3-rd dish. Takahashi's happiness is now 5, and Aoki's is 2.
  • Aoki takes the 4-th dish. Takahashi's happiness is now 5, and Aoki's is 8.
  • Takahashi takes the 5-th dish. Takahashi's happiness is now 10, and Aoki's is 8.

Sample Input 3

5 2
3 1
4 1
5 9
2 6
5 3

Sample Output 3

-1

Any distribution will make the absolute difference of the happinesses greater than 2 at some moment.


Sample Input 4

20 70
22 75
26 45
72 81
47 29
97 2
75 25
82 84
17 56
32 2
28 37
57 39
18 11
79 6
40 68
68 16
40 63
93 49
91 10
55 68
31 80

Sample Output 4

496
M - Boost Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

AtCoder 国には 1 から N の番号がついた N 個の街と、1 から M の番号がついた M 本の道があります。 i 番目の道は街 X_iY_i を双方向に結んでおり、長さは C_i です。

高橋君ははじめ街 1 におり、スピード1 です。高橋君はスピードが s であるとき、長さ C の道を移動するのに \frac{C}{s} 時間かかります。

K 個の街 A_1,\ldots,A_K にはジムがあり、高橋君はジムで一回 T 時間トレーニングするごとにスピードを 1 上げることができます。
高橋君は訪れたそれぞれのジムで、0 回以上の好きな回数のトレーニングを行うことができます。

高橋君が街 N にたどり着くまでにかかる時間の最小値を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq T \leq 10^6
  • 1 \leq X_i,Y_i \leq N
  • 1 \leq C_i \leq 10^6
  • 1 \leq A_1 < \ldots < A_K \leq N
  • 1 から街 N へ移動可能
  • 入力は全て整数である

入力

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

N M K T
X_1 Y_1 C_1
\vdots
X_M Y_M C_M
A_1 \dots A_K

出力

答えを出力せよ。

真の解との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。


入力例 1

3 2 1 15
1 2 100
2 3 100
2

出力例 1

163.333333333333343

次のように行動することで \frac{490}{3}=163.333333\dots 時間で街 1 から街 3 へ移動することができます。

  • 道路 1 を使って街 1 から街 2 へ移動する。100 時間かかる。
  • 2 のジムでトレーニングする。15 時間かかり、スピードが 2 になる。
  • 2 のジムでトレーニングする。15 時間かかり、スピードが 3 になる。
  • 道路 2 を使って街 2 から街 3 へ移動する。\frac{100}{3} 時間かかる。

真の解との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定されるため、163.33321098 などの出力でも正解とみなされます。


入力例 2

5 5 3 2
1 3 1
3 2 10
2 5 10
1 4 10
4 5 10
2 3 4

出力例 2

11.666666666666668

入力例 3

5 5 3 2
1 3 1
3 2 10
2 5 10
1 4 8
4 5 10
2 3 4

出力例 3

11.333333333333332

Problem Statement

AtCoder Republic has N towns numbered 1 through N, and M roads numbered 1 through M. Road i connects towns X_i and Y_i bidirectionally, and its length is C_i.

Takahashi is initially at town 1 with speed 1. When his speed is s, traveling along a length-C road takes \frac{C}{s} hours.

K towns, towns A_1,\ldots,A_K, have a gym. Every time he works out once for T hours in a gym, his speed increases by 1.
He can work out any number of times (possibly zero) in each gym.

Find the minimum hours required for him to reach town N.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq T \leq 10^6
  • 1 \leq X_i,Y_i \leq N
  • 1 \leq C_i \leq 10^6
  • 1 \leq A_1 < \ldots < A_K \leq N
  • It is reachable from town 1 to town N.
  • All input values are integers.

Input

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

N M K T
X_1 Y_1 C_1
\vdots
X_M Y_M C_M
A_1 \dots A_K

Output

Print the answer.

Your answer is considered correct if the absolute or relative error from the true value is at most 10^{-6}.


Sample Input 1

3 2 1 15
1 2 100
2 3 100
2

Sample Output 1

163.333333333333343

The following moves allow him to travel from town 1 to 3 in \frac{490}{3}=163.333333\dots hours.

  • Travel from town 1 to 2 via road 1, spending 100 hours.
  • Work out in the gym at town 2 for 15 hours. Now his speed is 2.
  • Work out in the gym at town 2 for 15 hours. Now his speed is 3.
  • Travel from town 2 to 3 via road 2, spending \frac{100}{3} hours.

Sample Input 2

5 5 3 2
1 3 1
3 2 10
2 5 10
1 4 10
4 5 10
2 3 4

Sample Output 2

11.666666666666668

Sample Input 3

5 5 3 2
1 3 1
3 2 10
2 5 10
1 4 8
4 5 10
2 3 4

Sample Output 3

11.333333333333332
N - Sorting Substrings

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

整数 N,K と長さ N の英小文字からなる文字列 S が与えられます。 S\displaystyle \frac{N(N+1)}2 個の非空な連続部分文字列を辞書順に並べた時、小さい方から K 番目の文字列を求めてください。

制約

  • 1\le N\le 10^6
  • \displaystyle 1\le K\le \frac{N(N+1)}2
  • S は英小文字からなる長さ N の文字列

入力

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

N K
S

出力

答えを出力せよ。


入力例 1

3 3
aba

出力例 1

ab

S= aba の非空な部分文字列は a, ab, aba, b, ba, a6 個で、これらを辞書順に並べると a, a, ab, aba, b, ba となります。小さい方から 3 番目は ab なので、 ab を出力してください。


入力例 2

5 8
wwwww

出力例 2

ww

入力例 3

8 28
chokudai

出力例 3

ok

入力例 4

22 30
atcoderbeginnercontest

出力例 4

beginner

Problem Statement

You are given integers N and K, and a length-N string S consisting of lowercase English letters. When the \displaystyle \frac{N(N+1)}2 nonempty contiguous substrings of S are arranged in lexicographical order, find the K-th smallest string.

Constraints

  • 1\le N\le 10^6
  • \displaystyle 1\le K\le \frac{N(N+1)}2
  • S is a string of length N consisting of lowercase English letters.

Input

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

N K
S

Output

Print the answer.


Sample Input 1

3 3
aba

Sample Output 1

ab

The nonempty substrings of S= aba are these six: a, ab, aba, b, ba, a. In lexicographical order, they are a, a, ab, aba, b, ba. Print the 3-rd smallest string ab.


Sample Input 2

5 8
wwwww

Sample Output 2

ww

Sample Input 3

8 28
chokudai

Sample Output 3

ok

Sample Input 4

22 30
atcoderbeginnercontest

Sample Output 4

beginner
O - Grid Maximize

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

HW 列からなるマス目があります。

上から i 行目左から j 列目のマスには、A_{i,j}\neq -1 のとき A_{i,j} が書かれており、A_{i,j}=-1 のとき何も書かれていません。
ここで、A_{i,j}-1 以上 9 以下の整数です。

あなたは、次の条件を満たすように、何も書かれていないマスにそれぞれ 0 以上 9 以下のいずれかの整数を書き込みます。

  • 辺を共有するどの 2 マスも、その 2 マスに書かれている数の和が D 以下である

書き込む数の和として考えられる最大値を求めてください。

制約

  • 1 \leq H,W \leq 15
  • 0 \leq D \leq 18
  • -1 \leq A_{i,j} \leq 9
  • 入力は全て整数である
  • 条件を満たすような書き込み方が存在する

入力

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

H W D
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}

出力

答えを出力せよ。


入力例 1

2 3 10
5 -1 3
2 4 -1

出力例 1

11

上から 1 行目・左から 2 列目のマスに 5、上から 2 行目・左から 3 列目のマスに 6 を書き込むと、書き込む数の和は 11 であり、辺を共有するどの 2 マスも和が 10 以下になります。

書き込む数の和を 12 以上にすることはできないため、これが最大です。


入力例 2

2 1 18
-1
0

出力例 2

9

書き込むことができるのは 0 以上 9 以下の整数であることに注意してください。


入力例 3

3 2 10
4 4
4 4
4 4

出力例 3

0

何も書かれていないマスが存在しないこともあります。


入力例 4

8 9 6
2 -1 3 -1 3 2 3 -1 3
-1 2 2 3 -1 3 -1 3 2
-1 3 -1 -1 3 2 3 2 -1
-1 -1 -1 2 2 3 2 -1 2
-1 3 1 -1 -1 2 3 2 2
-1 -1 -1 1 4 1 -1 -1 3
3 -1 -1 4 1 2 -1 -1 2
2 -1 -1 1 -1 -1 2 2 1

出力例 4

91

Problem Statement

There is a grid with H horizontal rows and W vertical columns.

The cell in row i (counted from the top) and column j (from the left) has A_{i,j} written on it if A_{i,j}\neq -1, and is empty if A_{i,j}=-1.
Here, A_{i,j} is an integer between -1 and 9.

You will write an integer between 0 and 9, inclusive, to each empty cell so that:

  • For any pair of cells that share a side, the numbers written on the two cells sum to D or less.

Find the maximum possible sum of the numbers newly written.

Constraints

  • 1 \leq H,W \leq 15
  • 0 \leq D \leq 18
  • -1 \leq A_{i,j} \leq 9
  • All input values are integers.
  • There exists a conforming assignment of numbers.

Input

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

H W D
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
\vdots
A_{H,1} A_{H,2} \ldots A_{H,W}

Output

Print the answer.


Sample Input 1

2 3 10
5 -1 3
2 4 -1

Sample Output 1

11

By writing 5 to the cell in row 1 and column 2, and 6 to the cell in row 2 and column 3, the newly written numbers sum to 11, and any adjacent cells sum to 10 or less.

This is the maximum, because one cannot make the sum of newly written numbers 12 or greater.


Sample Input 2

2 1 18
-1
0

Sample Output 2

9

Note that we can only write integers between 0 and 9.


Sample Input 3

3 2 10
4 4
4 4
4 4

Sample Output 3

0

There may be no empty cell.


Sample Input 4

8 9 6
2 -1 3 -1 3 2 3 -1 3
-1 2 2 3 -1 3 -1 3 2
-1 3 -1 -1 3 2 3 2 -1
-1 -1 -1 2 2 3 2 -1 2
-1 3 1 -1 -1 2 3 2 2
-1 -1 -1 1 4 1 -1 -1 3
3 -1 -1 4 1 2 -1 -1 2
2 -1 -1 1 -1 -1 2 2 1

Sample Output 4

91