E - Peak

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

配点 : 300

問題文

高橋くんは数直線上に N 個のプレゼントを置きました。そのうち i 個目のプレゼントは座標 A_i に置かれました。

あなたは数直線上の長さ M の半開区間 [x,x+M) を選び、そこに含まれるプレゼントを全て獲得します。
より詳しくは、以下の手順でプレゼントを獲得します。

  • まず、実数 x をひとつ選択する。
  • その後、プレゼントのうち置かれている座標が x \le A_i < x+M を満たすものを全て獲得する。

最大でいくつのプレゼントを獲得することができますか?

制約

  • 入力は全て整数
  • 1 \le N \le 3 \times 10^5
  • 1 \le M \le 10^9
  • 0 \le A_i \le 10^9

入力

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

N M
A_1 A_2 \dots A_N

出力

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


入力例 1

8 6
2 3 5 7 11 13 17 19

出力例 1

4

例えば、半開区間 [1.5,7.5) を指定します。
このとき、座標 2,3,5,7 にある 4 つのプレゼントを全て獲得することができ、これが獲得可能な最大の個数です。


入力例 2

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

出力例 2

2

同一の座標に複数のプレゼントが置いてあることもあります。


入力例 3

10 998244353
100000007 0 1755647 998244353 495 1000000000 1755648 503 1755649 998244853

出力例 3

7

Score : 300 points

Problem Statement

Takahashi has placed N gifts on a number line. The i-th gift is placed at coordinate A_i.

You will choose a half-open interval [x,x+M) of length M on the number line and acquire all the gifts included in it.
More specifically, you acquire gifts according to the following procedure.

  • First, choose one real number x.
  • Then, acquire all the gifts whose coordinates satisfy x \le A_i < x+M.

What is the maximum number of gifts you can acquire?

Constraints

  • All input values are integers.
  • 1 \le N \le 3 \times 10^5
  • 1 \le M \le 10^9
  • 0 \le A_i \le 10^9

Input

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

N M
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

8 6
2 3 5 7 11 13 17 19

Sample Output 1

4

For example, specify the half-open interval [1.5,7.5).
In this case, you can acquire the four gifts at coordinates 2,3,5,7, the maximum number of gifts that can be acquired.


Sample Input 2

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

Sample Output 2

2

There may be multiple gifts at the same coordinate.


Sample Input 3

10 998244353
100000007 0 1755647 998244353 495 1000000000 1755648 503 1755649 998244853

Sample Output 3

7
F - Dash

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

配点 : 300

問題文

二次元平面の点 (0,0) に高橋君がいます。初め、高橋君の体力は H です。また、二次元平面には M 個の体力を回復するアイテムがあり、i 個目のアイテムは点 (x_i,y_i) に置いてあります。

高橋君は、これから N 回の移動をします。i 回目の移動は以下の方法で行われます。

  1. 今高橋君がいる点を (x,y) とする。体力を 1 消費し、Si 番目の文字 S_i に応じて以下の点に移動する。

    • S_iR のとき: (x+1,y)
    • S_iL のとき: (x-1,y)
    • S_iU のとき: (x,y+1)
    • S_iD のとき: (x,y-1)
  2. 高橋君の体力が負になった場合、高橋君は倒れてしまい、移動をやめる。そうでない場合、移動した点にアイテムがあり、かつ高橋君の体力が K 未満ならば、移動した点に置かれたアイテムを消費し、高橋君の体力が K になる。

高橋君が一度も倒れることなく N 回の移動を行えるか判定してください。

制約

  • 1\leq N,M,H,K\leq 2\times 10^5
  • SR, L, U, D からなる長さ N の文字列
  • |x_i|,|y_i| \leq 2\times 10^5
  • (x_i,y_i) は互いに異なる
  • S 以外の入力は全て整数

入力

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

N M H K
S
x_1 y_1
\vdots
x_M y_M

出力

高橋君が一度も倒れることなく N 回の移動を行える場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

4 2 3 1
RUDL
-1 -1
1 0

出力例 1

Yes

初め高橋君の体力は 3 です。以下で移動を説明します。

  • 1 回目の移動: S_iR なので点 (1,0) に移動する。高橋君の体力は 2 に減る。点 (1,0) にはアイテムが置いてあるが、高橋君の体力は K=1 以上なのでアイテムは消費されない。

  • 2 回目の移動: S_iU なので点 (1,1) に移動する。高橋君の体力は 1 に減る。

  • 3 回目の移動: S_iD なので点 (1,0) に移動する。高橋君の体力は 0 に減る。点 (1,0) にはアイテムが置いてあり、体力は K=1 未満なのでアイテムを消費し、体力が 1 になる。

  • 4 回目の移動: S_iL なので点 (0,0) に移動する。高橋君の体力は 0 に減る。

以上より、高橋君は倒れずに 4 回の移動を行えるので、Yes を出力してください。体力は 0 になってもいいことに注意してください。


入力例 2

5 2 1 5
LDRLD
0 0
-1 -1

出力例 2

No

初め高橋君の体力は 1 です。以下で移動を説明します。

  • 1 回目の移動: S_iL なので点 (-1,0) に移動する。高橋君の体力は 0 に減る。

  • 2 回目の移動: S_iD なので点 (-1,-1) に移動する。高橋君の体力は -1 に減る。体力が -1 になってしまったので、高橋君は倒れてしまい、移動をやめる。

以上より、高橋君は倒れてしまうので、No を出力してください。

高橋君がはじめいる点 (0,0) にはアイテムが置いてありますが、移動後にアイテムは消費されるので、1 回目の移動前にアイテムを消費しないことに注意してください。

Score : 300 points

Problem Statement

On a two-dimensional plane, Takahashi is initially at point (0, 0), and his initial health is H. M items to recover health are placed on the plane; the i-th of them is placed at (x_i,y_i).

Takahashi will make N moves. The i-th move is as follows.

  1. Let (x,y) be his current coordinates. He consumes a health of 1 to move to the following point, depending on S_i, the i-th character of S:

    • (x+1,y) if S_i is R;
    • (x-1,y) if S_i is L;
    • (x,y+1) if S_i is U;
    • (x,y-1) if S_i is D.
  2. If Takahashi's health has become negative, he collapses and stops moving. Otherwise, if an item is placed at the point he has moved to, and his health is strictly less than K, then he consumes the item there to make his health K.

Determine if Takahashi can complete the N moves without being stunned.

Constraints

  • 1\leq N,M,H,K\leq 2\times 10^5
  • S is a string of length N consisting of R, L, U, and D.
  • |x_i|,|y_i| \leq 2\times 10^5
  • (x_i, y_i) are pairwise distinct.
  • All values in the input are integers, except for S.

Input

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

N M H K
S
x_1 y_1
\vdots
x_M y_M

Output

Print Yes if he can complete the N moves without being stunned; print No otherwise.


Sample Input 1

4 2 3 1
RUDL
-1 -1
1 0

Sample Output 1

Yes

Initially, Takahashi's health is 3. We describe the moves below.

  • 1-st move: S_i is R, so he moves to point (1,0). His health reduces to 2. Although an item is placed at point (1,0), he do not consume it because his health is no less than K=1.

  • 2-nd move: S_i is U, so he moves to point (1,1). His health reduces to 1.

  • 3-rd move: S_i is D, so he moves to point (1,0). His health reduces to 0. An item is placed at point (1,0), and his health is less than K=1, so he consumes the item to make his health 1.

  • 4-th move: S_i is L, so he moves to point (0,0). His health reduces to 0.

Thus, he can make the 4 moves without collapsing, so Yes should be printed. Note that the health may reach 0.


Sample Input 2

5 2 1 5
LDRLD
0 0
-1 -1

Sample Output 2

No

Initially, Takahashi's health is 1. We describe the moves below.

  • 1-st move: S_i is L, so he moves to point (-1,0). His health reduces to 0.

  • 2-nd move: S_i is D, so he moves to point (-1,-1). His health reduces to -1. Now that the health is -1, he collapses and stops moving.

Thus, he will be stunned, so No should be printed.

Note that although there is an item at his initial point (0,0), he does not consume it before the 1-st move, because items are only consumed after a move.

G - Minimum Steiner Tree

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

配点 : 425

問題文

頂点に 1 から N の番号がついた N 頂点の木が与えられます。i 番目の辺は頂点 A_i と頂点 B_i を結んでいます。

このグラフからいくつかの(0 個でもよい)辺と頂点を削除してできる木のうち、指定された K 個の頂点、頂点 V_1,\ldots,V_K を全て含むようなものの頂点数の最小値を求めてください。

制約

  • 1 \leq K \leq N \leq 2\times 10^5
  • 1 \leq A_i,B_i \leq N
  • 1 \leq V_1 < V_2 < \ldots < V_K \leq N
  • 与えられるグラフは木である
  • 入力は全て整数

入力

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

N K
A_1 B_1
\vdots
A_{N-1} B_{N-1}
V_1 \ldots V_K

出力

答えを出力せよ。


入力例 1

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

出力例 1

4

与えられた木は下図左の通りであり、そこからいくつかの辺と頂点を削除してできる木のうち頂点 1,3,5 を全て含むような頂点数最小のものは下図右の通りです。

図


入力例 2

4 4
3 1
1 4
2 1
1 2 3 4

出力例 2

4

入力例 3

5 1
1 4
2 3
5 2
1 2
1

出力例 3

1

Score : 425 points

Problem Statement

You are given a tree with N vertices numbered 1 to N. The i-th edge connects vertices A_i and B_i.

Consider a tree that can be obtained by removing some (possibly zero) edges and vertices from this graph. Find the minimum number of vertices in such a tree that includes all of K specified vertices V_1,\ldots,V_K.

Constraints

  • 1 \leq K \leq N \leq 2\times 10^5
  • 1 \leq A_i,B_i \leq N
  • 1 \leq V_1 < V_2 < \ldots < V_K \leq N
  • The given graph is a tree.
  • All input values are integers.

Input

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

N K
A_1 B_1
\vdots
A_{N-1} B_{N-1}
V_1 \ldots V_K

Output

Print the answer.


Sample Input 1

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

Sample Output 1

4

The given tree is shown on the left in the figure below. The tree with the minimum number of vertices that includes all of vertices 1,3,5 is shown on the right.

Figure


Sample Input 2

4 4
3 1
1 4
2 1
1 2 3 4

Sample Output 2

4

Sample Input 3

5 1
1 4
2 3
5 2
1 2
1

Sample Output 3

1
H - Fraction Floor Sum

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

配点 : 500

問題文

正の整数 N が与えられます。 \displaystyle\sum_{i=1}^N \left[ \frac{N}{i} \right] の値を求めてください。

ただし、実数 x に対して [x]x 以下の最大の整数を表します。

制約

  • 1 \leq N \leq 10^{12}
  • N は整数である。

入力

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

N

出力

答えを出力せよ。


入力例 1

3

出力例 1

5

\left[ \frac{3}{1} \right]+\left[ \frac{3}{2} \right]+\left[ \frac{3}{3} \right]=3+1+1=5 です。


入力例 2

10000000000

出力例 2

231802823220

入力や出力が 32 bit 整数型に収まらないことがあることに注意してください。

Score : 500 points

Problem Statement

Given is a positive integer N. Find the value \displaystyle\sum_{i=1}^N \left[ \frac{N}{i} \right].

Here, for a real number x, [x] denotes the largest integer not exceeding x.

Constraints

  • 1 \leq N \leq 10^{12}
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

3

Sample Output 1

5

We have \left[ \frac{3}{1} \right]+\left[ \frac{3}{2} \right]+\left[ \frac{3}{3} \right]=3+1+1=5.


Sample Input 2

10000000000

Sample Output 2

231802823220

Note that the input and output may not fit into a 32-bit integer type.

I - ABCBAC

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

配点 : 500

問題文

長さ N の文字列 S および整数 i\ (0\leq i\leq N) に対して、f_i(S) を、

  • S の先頭 i 文字
  • S を反転した文字列
  • S の末尾 N-i 文字

をこの順に連結した文字列と定義します。 例えば、S= abci=2 のとき、f_i(S)= abcbac です。

長さ 2N の文字列 T が与えられます。 f_i(S)=T を満たす長さ N の文字列 S と整数 i\ (0\leq i\leq N) の組を 1 つ見つけてください。 そのような S,i の組が存在しない場合は、それを報告してください。

制約

  • 1\leq N \leq 10^6
  • N は整数
  • T は英小文字からなる長さ 2N の文字列

入力

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

N 
T

出力

条件を満たす S,i の組が存在しないならば、-1 と出力せよ。 存在するならば、S,i を改行区切りで出力せよ。 条件を満たす S,i の組が複数存在する場合は、そのどれを出力しても良い。


入力例 1

3
abcbac

出力例 1

abc
2

問題文中に書いた通り、S= abci=2 とすると f_i(S)= abcbac となって T に一致するため、abc2 を出力します。


入力例 2

4
abababab

出力例 2

abab
1

S= ababi=3 としても条件を満たします。


入力例 3

3
agccga

出力例 3

cga
0

S= agci=3 としても条件を満たします。


入力例 4

4
atcodeer

出力例 4

-1

条件を満たす S,i の組が存在しない場合は -1 を出力してください。

Score : 500 points

Problem Statement

For a string S of length N and an integer i\ (0\leq i\leq N), let us define the string f_i(S) as the concatenation of:

  • the first i characters of S,
  • the reversal of S, and
  • the last (N-i) characters of S,

in this order. For instance, if S= abc and i=2, we have f_i(S)= abcbac.

You are given a string T of length 2N. Find a pair of a string S of length N and an integer i\ (0\leq i\leq N) such that f_i(S)=T. If no such pair of S and i exists, report that fact.

Constraints

  • 1\leq N \leq 10^6
  • N is an integer.
  • T is a string of length 2N consisting of lowercase English letters.

Input

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

N 
T

Output

If no pair of S and i satisfies the condition, print -1. Otherwise, print S and i, separated by a newline. If multiple pairs of S and i satisfy the condition, you may print any of them.


Sample Input 1

3
abcbac

Sample Output 1

abc
2

As mentioned in the problem statement, if S= abc and i=2, we have f_i(S)= abcbac, which equals T, so you should print abc and 2.


Sample Input 2

4
abababab

Sample Output 2

abab
1

S= abab and i=3 also satisfy the condition.


Sample Input 3

3
agccga

Sample Output 3

cga
0

S= agc and i=3 also satisfy the condition.


Sample Input 4

4
atcodeer

Sample Output 4

-1

If no pair of S and i satisfies the condition, print -1.