A - Clock

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

高橋君の持っている時計は本当の時刻より X 分進んでいます。ただし、 X<0 なら |X| 分遅れているということを意味します。

高橋君の時計が 12:00 を指している時、本当の時刻が何時何分か求めてください。

制約

  • X-10 以上 10 以下の整数

入力

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

X

出力

本当の時刻を xy 分としたときに、x:y をこの順に出力せよ。ただし、 y10 未満である場合は y の先頭に 0 を付けて出力せよ。


入力例 1

5

出力例 1

11:55

高橋君の持っている時計は本当の時刻より 5 分進んでいます。よって、本当の時刻は 11:55 です。


入力例 2

0

出力例 2

12:00

高橋君の持っている時計は正しい時刻を示しています。


入力例 3

-3

出力例 3

12:03

高橋君の持っている時計は本当の時刻より 3 分遅れています。

Problem Statement

Takahashi's clock is X minutes ahead of the actual time. Here, X<0 implies it is |X| minutes behind.

When his clock shows twelve o'clock, what is the actual time?

Constraints

  • X is an integer between -10 and 10, inclusive.

Input

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

X

Output

If the actual time is y minutes past x, print x, :, and y in this order. If y is strictly less than 10, prepend 0 when printing y.


Sample Input 1

5

Sample Output 1

11:55

Takahashi's clock is five minutes ahead of the actual time. Thus, the actual time is 11:55.


Sample Input 2

0

Sample Output 2

12:00

Takahashi's clock shows the actual time.


Sample Input 3

-3

Sample Output 3

12:03

Takahashi's clock is three minutes behind the actual time.

B - Separation

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

英大文字と英小文字からなる文字列 S が与えられます。
ここで、S には英大文字と英小文字がそれぞれ少なくとも 1 つ以上ずつ含まれることが保証されます。

S から英大文字のみを順番を保って抜き出した文字列と、S から英小文字のみを順番を保って抜き出した文字列をそれぞれ出力してください。

制約

  • S は英大文字と英小文字からなる長さ 2 以上 100 以下の文字列
  • S は英大文字と英小文字をそれぞれ 1 つ以上ずつ含む。

入力

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

S

出力

2 行出力せよ。
1 行目には S から英大文字のみを順番を保って抜き出した文字列を、 2 行目には S から英小文字のみを順番を保って抜き出した文字列を出力せよ。


入力例 1

aFGdbcE

出力例 1

FGE
adbc

S= aFGdbcE から英大文字のみを順番を保って抜き出した文字列は FGE、英小文字のみを順番を保って抜き出した文字列は adbc です。
よって、1 行目には FGE を、2 行目には adbc を出力します。


入力例 2

zZyZz

出力例 2

ZZ
zyz

Problem Statement

You are given a string S consisting of uppercase and lowercase English letters.
Here, S is guaranteed to have at least one uppercase and one lowercase letter.

Print the string obtained by extracting the uppercase characters in S, and the string obtained by extracting the lowercase characters in S, preserving the order.

Constraints

  • S is a string of length between 2 and 100, inclusive, consisting of uppercase and lowercase English letters.
  • S contains at least one uppercase character, and at least one lowercase character.

Input

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

S

Output

Print two lines.
The first line should contain the string obtained by extracting the uppercase letters in S preserving the order. The second line should contain the string obtained by extracting the lowercase letters in S preserving the order.


Sample Input 1

aFGdbcE

Sample Output 1

FGE
adbc

The uppercase letters extracted from S= aFGdbcE form the string FGE when joined in order, and the lowercase letters form adbc.
Thus, the first line should contain FGE, and the second should contain adbc.


Sample Input 2

zZyZz

Sample Output 2

ZZ
zyz
C - Spring Thief

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N 本の桜の木があります。木 i は時刻 S_i に花が咲き、時刻 T_i に(その時点で咲いていれば)散ります。

K の倍数の時刻に雨が振り、その時点で咲いている桜の花はすべて散ります。各 i=1,2,\dots,N について、木 i の花が咲いている時間の長さを求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq S_i < T_i \leq 10^9
  • 2 \leq K \leq 10^9
  • S_iK の倍数でない
  • 入力はすべて整数

入力

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

N K
S_1 T_1
S_2 T_2
\vdots
S_N T_N

出力

i=1,2,\dots,N に対する答えを、この順に空白区切りで 1 行で出力せよ。


入力例 1

3 5
1 3
2 5
4 10

出力例 1

2 3 1
  • 1:時刻 1 に咲き、時刻 3 に散ります。答えは 2 です。
  • 2:時刻 2 に咲き、時刻 5 に散ります。答えは 3 です。
  • 3:時刻 4 に咲き、時刻 5 に雨が降って散ります。答えは 1 です。

Problem Statement

There are N cherry blossom trees. Tree i blooms at time S_i, but sheds its blossoms (if any) at time T_i.

It rains each time that is a multiple of K, causing all blossoms blooming at that time to shed. For each i=1,2,\dots,N, find the duration for which tree i blooms.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq S_i < T_i \leq 10^9
  • 2 \leq K \leq 10^9
  • S_i is not a multiple of K.
  • All input values are integers.

Input

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

N K
S_1 T_1
S_2 T_2
\vdots
S_N T_N

Output

Print the answers for trees i=1,2,\dots,N in one line, in this order, with spaces in between.


Sample Input 1

3 5
1 3
2 5
4 10

Sample Output 1

2 3 1
  • Tree 1: blooms at time 1 and sheds its blossoms at time 3. The answer is 2.
  • Tree 2: blooms at time 2 and sheds its blossoms at time 5. The answer is 3.
  • Tree 3: blooms at time 4 and sheds its blossoms at time 5 because of the rain. The answer is 1.
D - Remove Duplicated

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

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

S に複数回現れる文字を削除し、残った文字を順序を保って連結することで得られる文字列を求めてください。

制約

  • S は長さ 1 以上 100 以下の英小文字からなる文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

takahashi

出力例 1

tksi

S に複数回現れる文字は a, h2 つです。S から a, h を削除して残った文字を順序を保って連結すると tksi となります。


入力例 2

abba

出力例 2


出力するべき文字列が空文字列になることもあります。


入力例 3

atcoder

出力例 3

atcoder

Problem Statement

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

Find the string obtained by removing the characters that occur multiple times in S and joining the remaining ones in order.

Constraints

  • S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters.

Input

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

S

Output

Print the answer.


Sample Input 1

takahashi

Sample Output 1

tksi

The characters that occur multiple times in S are a and h. By removing a and h from S and joining the remaining characters in order, we obtain tksi.


Sample Input 2

abba

Sample Output 2


The string to print may be empty.


Sample Input 3

atcoder

Sample Output 3

atcoder
E - Broken Computer

Time Limit: 3 sec / Memory Limit: 1024 MiB

問題文

あなたは壊れかけのコンピュータを入手しました。 このコンピュータは足し算をすることができますが、ある特定の N 組の数のペアについては、その和をいつも誤って計算してしまいます。 具体的には、各 i\ (1\leq i\leq N) に対して、A_i+B_i および B_i+A_i の値を C_i と計算してしまいます。 逆に、これらの数のペア以外の和については正しく計算することができます。

あなたは、このコンピュータの中に、いくつかの数の和を計算するためのプログラムが保存されているのを見つけました。 このプログラムに M 個の数 x_1,x_2,\dots,x_M を入力すると、コンピュータは以下の手順に従ってそれらの和を計算します。

  1. 変数 sx_1 で初期化する。
  2. i=2,3,\dots,M の順に以下を行う:
    • s+x_i の値を計算し、その結果を s に代入する。
  3. 最終的な s の値を出力する。

このプログラムに M 個の数 X_1,X_2,\dots,X_M を入力したとき、コンピュータが何を出力するか求めてください。

制約

  • 1\leq N \leq 2\times 10^5
  • 2\leq M \leq 2\times 10^5
  • 0\leq A_i,B_i,C_i,X_i \leq 10^9
  • A_i \leq B_i
  • i\neq j ならば (A_i,B_i)\neq (A_j,B_j)
  • A_i+B_i \neq C_i
  • 入力は全て整数

入力

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

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N
X_1 X_2 \dots X_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

5

このコンピュータは以下のようにして 1,3,4,2 の和を計算します。

  1. 変数 s1 で初期化する。
  2. 1+3\ (=A_2+B_2) の値を誤って 2\ (=C_2) と計算し、これを s に代入する。
  3. 2+4 の値を正しく 6 と計算し、これを s に代入する。
  4. 6+2\ (=B_3+A_3) の値を誤って 5\ (=C_3) と計算し、これを s に代入する。
  5. 最終的な s の値である 5 を出力する。

入力例 2

1 3
1 1 1
1 2 3

出力例 2

6

このコンピュータは 1+1 の値を正しく計算することができませんが、1,2,3 の和を計算する過程では 1+1 という式は出てこないので、正しい答えを求めることができます。


入力例 3

8 10
5 27 27
8 72 27
2 27 12
3 32 12
57 60 3
35 57 27
12 41 72
12 64 57
57 60 32 64 0 60 32 64 0 60

出力例 3

3

Problem Statement

You acquired a malfunctioning computer. This computer is capable of computing addition, but always reports a wrong sum for specific N pairs. Specifically, for all i\ (1\leq i\leq N), it evaluates A_i+B_i and B_i+A_i to C_i. Conversely, it finds correct sums for any other pairs of numbers.

You found a program that computes the sum of several numbers. When M numbers x_1,x_2,\dots and x_M are input to this program, this computer finds their sum by the following procedure.

  1. Initialize a variable s with x_1.
  2. For each i=2,3,\dots,M in order:
    • evaluate s+x_i, and assign the result to s.
  3. Print the final value of s.

Find what will be printed when M numbers X_1,X_2,\dots, and X_M are input to this program.

Constraints

  • 1\leq N \leq 2\times 10^5
  • 2\leq M \leq 2\times 10^5
  • 0\leq A_i,B_i,C_i,X_i \leq 10^9
  • A_i \leq B_i
  • If i\neq j, then (A_i,B_i)\neq (A_j,B_j).
  • A_i+B_i \neq C_i
  • All input values are integers.

Input

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

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N
X_1 X_2 \dots X_M

Output

Print the answer.


Sample Input 1

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

Sample Output 1

5

This computer finds the sum of 1,3,4, and 2 as follows.

  1. Initialize a variable s with 1.
  2. Evaluate 1+3\ (=A_2+B_2) wrongly to 2\ (=C_2), and assign it to s.
  3. Evaluate 2+4 correctly to 6, and assign it to s.
  4. Evaluate 6+2\ (=B_3+A_3) wrongly to 5\ (=C_3), and assign it to s.
  5. Print the final value of s, that is, 5.

Sample Input 2

1 3
1 1 1
1 2 3

Sample Output 2

6

While this computer always wrongly evaluates 1+1, this is never evaluated when finding the sum of 1,2, and 3, so it prints the correct sum.


Sample Input 3

8 10
5 27 27
8 72 27
2 27 12
3 32 12
57 60 3
35 57 27
12 41 72
12 64 57
57 60 32 64 0 60 32 64 0 60

Sample Output 3

3
F - Maximum Concatenation

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

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

A の異なる 2 つの要素を選びそれらを文字列として結合して得られる整数の最大値を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^8
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4
2 12 323 50

出力例 1

50323

50, 323 をこの順に結合して得られる 50323 が最大値となります。


入力例 2

2
100000000 100000000

出力例 2

100000000100000000

数列 A 内の異なる場所から選ばれた 2 要素であれば、それらの値が等しくても異なる要素とみなされます。


入力例 3

8
3 14 15 9 26 53 5 89

出力例 3

8953

Problem Statement

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

Find the maximum integer that can be obtained by choosing two distinct elements from A and concatenating them as strings.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^8
  • 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

4
2 12 323 50

Sample Output 1

50323

The maximum value can be obtained by concatenating 50 and 323 in this order: 50323.


Sample Input 2

2
100000000 100000000

Sample Output 2

100000000100000000

Two elements are considered distinct if they are chosen from different positions in the sequence A, even if they have the same value.


Sample Input 3

8
3 14 15 9 26 53 5 89

Sample Output 3

8953
G - Car Safety

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

左右に伸びる数直線上を N 台の車が走っています。座標は右向きが正の向きに定められています。

i 番目の車は、座標 A_i に現在いて、正方向へ B_i の速度で動いています。ここで、車の座標は全て相異なります。

i 番目の車は以下の条件を満たすとき、安全であるといいます。

  • i 番目の車より右にいるどの車とも座標が B_i\times K 以上離れている。

安全でない車の番号を全て昇順に出力してください。

制約

  • 2\leq N\leq 2\times 10^5
  • 1\leq K\leq 10^9
  • 1\leq A_i,B_i\leq 10^9
  • A_i\neq A_j\ (i\neq j)
  • 入力される数値は全て整数

入力

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

N K
A_1 B_1
\vdots
A_N B_N

出力

2 行出力せよ。1 行目には安全でない車の個数を出力せよ。

2 行目には安全でない車の番号全てを昇順に空白区切りで出力せよ。


入力例 1

4 2
1 1
10 5
3 3
7 2

出力例 1

2
3 4

1 番目の車は、その右にいるどの車とも座標が 1\times 2=2 以上離れています。よって安全です。

一方、3 番目の車は、その右にいる 4 番目の車との座標差は 7-3=4 であり、これは 3\times 2=6 未満です。よって安全ではありません。

4 番目の車も同様に安全ではありません。

車の番号を昇順に出力することに注意してください。


入力例 2

10 2
25 9
22 10
7 5
5 7
27 10
6 7
21 6
2 1
9 3
24 10

出力例 2

7
1 2 3 4 6 7 10

Problem Statement

There are N cars on a number line extending left and right. The positive coordinate direction points to the right.

The i-th car, whose ID is i, is currently at coordinate A_i and moving at a speed of B_i in the positive direction. The coordinates of the cars are pairwise distinct.

Car i is considered safe if and only if:

  • every car to the right of car i is distant by at least B_i\times K from car i.

Print the IDs of the cars that are not safe in ascending order.

Constraints

  • 2\leq N\leq 2\times 10^5
  • 1\leq K\leq 10^9
  • 1\leq A_i,B_i\leq 10^9
  • A_i\neq A_j\ (i\neq j)
  • 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 B_N

Output

Print two lines. The first line should contain the number of cars that are not safe.

The second line should contain the IDs of the cars that are not safe in ascending order, separated by spaces.


Sample Input 1

4 2
1 1
10 5
3 3
7 2

Sample Output 1

2
3 4

Car 1 is distant by at least 1\times 2=2 from every car to its right, so it is safe.

Meanwhile, car 3 is distant from car 4 to its right by 7-3=4, which is less than 3\times 2=6, so car 3 is not safe.

Likewise, car 4 is not safe either.

Note that car IDs should be printed in ascending order.


Sample Input 2

10 2
25 9
22 10
7 5
5 7
27 10
6 7
21 6
2 1
9 3
24 10

Sample Output 2

7
1 2 3 4 6 7 10
H - Cleaning Service

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

高橋君は N 日間ホテルに滞在します。
ホテルには清掃サービスが存在し、高橋君は N 日間のうちに K 回この清掃サービスを利用しようと考えました。
具体的には滞在を始めてから D_1, D_2, \ldots, D_K 日目に利用しようと考えました。

しかし、実際には清掃サービスはどの連続する L 日間についても M 回までしか利用できないことが判明しました。
より厳密には、1\leq i\leq N-L+1 をみたすすべての整数 i について、 i 日目から (i+L-1) 日目までの間に高々 M 回しかサービスは利用できません。

高橋君は D_1, D_2, \ldots, D_K 日目のうちいくつかの日に利用することを取りやめることによって、この条件をみたすようにしようと思いました。 (1 日も取りやめなくて良い可能性もあります。) 条件をみたすようにするためには、高橋君は K 回のうち最小で何回の利用を取りやめる必要があるか求めてください。

制約

  • 1\leq L \leq N \leq 10^{18}
  • 1\leq M \leq K \leq 2\times 10^5
  • 1\leq D_1<D_2<\cdots<D_K\leq N
  • 入力はすべて整数

入力

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

N K L M
D_1 D_2 \ldots D_K

出力

問題文の条件をみたすために、高橋君は何回清掃サービスの利用を取りやめる必要があるか出力せよ。


入力例 1

10 6 5 2
1 2 3 6 8 10

出力例 1

2

高橋君はどの連続する 5 日間についても清掃サービスを高々 2 回しか利用できません。
2,10 日目の利用を取りやめ、1,3,6,8 日目に利用するようにすることで問題文の条件をみたすようにすることができます。
6 回のうちどの 1 回を取りやめるだけでも問題文の条件をみたすようにできないため、2 を出力します。


入力例 2

10 3 3 1
1 5 10

出力例 2

0

高橋君はどの日もサービスの利用を取りやめる必要はありません。


入力例 3

1000000000000000000 2 1000000000000000000 1
1 1000000000000000000

出力例 3

1

Problem Statement

Takahashi is going to stay at a hotel for N days.
The hotel has a cleaning service, which he is planning to use K times, on day D_1, day D_2, \ldots, and day D_K, during his N-day visit.

However, it turned out that the service is available at most M times within any consecutive L days.
More formally, for all integers i with 1\leq i\leq N-L+1, the service is available at most M times from day i through day (i+L-1).

He has decided to cancel some of his plans of using the service on days D_1, D_2, \ldots, and D_K, to meet these conditions. (He may not need to cancel his plan at all.) At least how many days among the K days does he need to cancel his plan of using it to meet the conditions?

Constraints

  • 1\leq L \leq N \leq 10^{18}
  • 1\leq M \leq K \leq 2\times 10^5
  • 1\leq D_1<D_2<\cdots<D_K\leq N
  • All input values are integers.

Input

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

N K L M
D_1 D_2 \ldots D_K

Output

Print at least how many times he needs to cancel his plan of using the cleaning service in order to meet the conditions.


Sample Input 1

10 6 5 2
1 2 3 6 8 10

Sample Output 1

2

The cleaning service is available at most twice within any five consecutive days.
The conditions can be met by canceling his plan of using it on day 2 and 10, and using it on day 1,3,6, and 8.
Canceling it only once never satisfies the conditions regardless of which of the six to choose, so print 2.


Sample Input 2

10 3 3 1
1 5 10

Sample Output 2

0

He does not need to cancel his plan at all.


Sample Input 3

1000000000000000000 2 1000000000000000000 1
1 1000000000000000000

Sample Output 3

1
I - Find Permutations

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) と整数 K が与えられます。 A の連続部分列であって、1,2,\dots,K の並べ替えであるものの個数を求めてください。 すなわち、1 以上 N-K+1 以下の整数 i であって、(A_{i},A_{i+1},\dots,A_{i+K-1})1,2,\dots,K の並べ替えであるようなものの個数を求めてください。

制約

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

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

5 3
1 2 1 3 2

出力例 1

2

A の長さ 3 の連続部分列のうち、1,2,3 の並べ替えであるものは (A_2,A_3,A_4)=(2,1,3),(A_3,A_4,A_5)=(1,3,2)2 つです。


入力例 2

5 3
3 1 1 2 2

出力例 2

0

入力例 3

4 2
1 2 1 2

出力例 3

3

入力例 4

1 1
1

出力例 4

1

Problem Statement

Given a length-N integer sequence A=(A_1,A_2,\dots,A_N) and an integer K, find the number of (consecutive) subarrays of A that are permutations of 1,2,\dots,K. In other words, find the number of integers between 1 and N-K+1, inclusive, such that (A_{i},A_{i+1},\dots,A_{i+K-1}) is a permutation of 1,2,\dots,K.

Constraints

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

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

5 3
1 2 1 3 2

Sample Output 1

2

Among the length-3 subarrays of A, these two are permutations of 1,2,3: (A_2,A_3,A_4)=(2,1,3),(A_3,A_4,A_5)=(1,3,2).


Sample Input 2

5 3
3 1 1 2 2

Sample Output 2

0

Sample Input 3

4 2
1 2 1 2

Sample Output 3

3

Sample Input 4

1 1
1

Sample Output 4

1
J - Turn Right

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

N\times N のマス目が与えられます。 上から i 行目 (1\leq i\leq N)、左から j 列目 (1\leq j\leq N) のマスをマス (i,j) と呼ぶことにします。

それぞれのマスは空きマスか壁マスのどちらかです。 マスの状態は N 個の文字列 S _ 1,S _ 2,\ldots,S _ N で与えられ、S _ ij 文字目が . のときマス (i,j) は空きマス、# のときマス (i,j) は壁マスです。 ここで、マス目の一番外側のマス、すなわちマス (1,j),(i,1),(N,j),(i,N)\ (1\leq i\leq N,1\leq j\leq N) はすべて壁マスであることが保証されます。

高橋くんは、ある空きマス (x,y) で上下左右のいずれかの方向を向いているところから始めて次の操作を繰り返したところ、(始点を含む)ある時点でマス (2,2) を訪れました。

  • 現在向いている方向に隣り合っているマスが空きマスならば、向いている方向を変えないまま、そのマスに移動する。 そうでなければ、同じマスにとどまり、時計回りに 90 度回転する。

たとえば、高橋くんがマス (3,5) で上を向いている状態で操作を行った場合、マス (2,5) が空きマスならばマス (2,5) に移動し、壁マスならば右を向きます。

高橋くんが操作を始めたマスとしてありえる空きマスがいくつあるか求めてください。

制約

  • 3\leq N\leq1000
  • N は整数
  • S _ i. および # からなる長さ N の文字列 (1\leq i\leq N)
  • S _ 1,S _ N# のみからなる文字列
  • S _ i1 文字目と N 文字目はどちらも # と等しい (1\leq i\leq N)
  • S _ 22 文字目は . と等しい

入力

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

N
S _ 1
S _ 2
\vdots
S _ N

出力

答えを出力せよ。


入力例 1

6
######
#....#
#....#
#.#..#
#...##
######

出力例 1

13

与えられたマス目は以下のようになっています。

14 個の空きマスのうち、マス (4,5) 以外の 13 マスについては、適切な向きで操作を始めることでマス (2,2) を訪れることができます。

たとえば、マス (4,4) で下を向いている状態から操作を始めると、高橋くんは以下のように移動してマス (2,2) を訪れます。


入力例 2

4
####
#.##
##.#
####

出力例 2

1

入力例 3

13
#############
#.....####..#
#....#....#.#
#...#......##
#...#..###.##
#...#.#..#.##
#...#.#..#.##
#...#.#....##
#...#..#.##.#
#....#......#
#.....##.##.#
#...........#
#############

出力例 3

85

Problem Statement

There is an N\times N grid. The cell in the i-th row (1\leq i\leq N) from the top and j-th column (1\leq j\leq N) from the left is called cell (i,j).

Each cell is either an empty cell or a wall cell. The state of the grid is represented by N strings S _ 1,S _ 2,\ldots, and S _ N: if the j-th character of S _ i is ., then cell (i,j) is an empty cell; if it is #, it is a wall cell. Here, the outermost cells, namely cells (1,j),(i,1),(N,j),(i,N)\ (1\leq i\leq N,1\leq j\leq N), are guaranteed to be wall cells.

Initially located at an empty cell (x,y) facing one of the four directions (up, down, left, and right), Takahashi repeated the following move. During his tour, he visited cell (2,2) at some point (including the initial position):

  • If the adjacent cell in his facing direction is an empty cell, then advance to that cell without changing his direction. Otherwise, rotate clockwise by 90 degrees while staying at the same cell.

For example, if he makes the move when he is facing up at cell (3,5), he will advance to cell (2, 5) if cell (2,5) is an empty cell, and turn right if it is a wall cell.

How many cells are there where he can have been initially located?

Constraints

  • 3\leq N\leq1000
  • N is an integer.
  • S _ i is a string of length N consisting of . and # (1\leq i\leq N).
  • S _ 1 and S _ N are strings consisting solely of #.
  • The 1-st and N-th characters of S _ i both equal # (1\leq i\leq N).
  • The 2-nd character of S _ 2 equals ..

Input

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

N
S _ 1
S _ 2
\vdots
S _ N

Output

Print the answer.


Sample Input 1

6
######
#....#
#....#
#.#..#
#...##
######

Sample Output 1

13

The given grid looks like this.

Among the 14 empty cells, if he starts at one of the 13 cells except for cell (4,5), he can always visit cell (2,2) if he initially faces in an appropriate direction.

For example, if he starts at cell (4,4) facing down, he will visit cell (2,2) as follows.


Sample Input 2

4
####
#.##
##.#
####

Sample Output 2

1

Sample Input 3

13
#############
#.....####..#
#....#....#.#
#...#......##
#...#..###.##
#...#.#..#.##
#...#.#..#.##
#...#.#....##
#...#..#.##.#
#....#......#
#.....##.##.#
#...........#
#############

Sample Output 3

85
K - Strange Equation

Time Limit: 4 sec / Memory Limit: 1024 MiB

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N),C=(C_1,C_2,\dots,C_N),D=(D_1,D_2,\dots,D_N),E=(E_1,E_2,\dots,E_N) が与えられます。

任意の i\ (1\leq i\leq N) について、f(i)\max(A_i, B_j-C_i)=D_j+E_i を満たす j\ (1\leq j\leq N) の個数と定めます。

f(1),f(2),\dots,f(N) を求めてください。

制約

  • 1\leq N \leq 2\times 10^5
  • -10^9\leq A_i,B_i,C_i,D_i,E_i \leq 10^9
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
C_1 C_2 \dots C_N
D_1 D_2 \dots D_N
E_1 E_2 \dots E_N

出力

f(1),f(2),\dots,f(N) をこの順に空白区切りで出力せよ。


入力例 1

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

出力例 1

2 0 1
  • i=1 のとき、\max(A_i, B_j-C_i)=D_j+E_i を満たす jj=1,3 です。 実際、j=3 のとき \max(4, 9-2)=4+3 が成り立ちます。
  • i=2 のとき、\max(A_i, B_j-C_i)=D_j+E_i を満たす j はありません。
  • i=3 のとき、\max(A_i, B_j-C_i)=D_j+E_i を満たす jj=3 です。

よって、f(1)=2,f(2)=0,f(3)=1 です。


入力例 2

3
4 6 -10
9 2 6
-5 -5 3
7 0 4
7 7 -1

出力例 2

3 3 3

すべての 1\leq i,j\leq 3 について \max(A_i, B_j-C_i)=D_j+E_i が成り立ちます。


入力例 3

8
68 108 176 112 146 156 80 82
-158 -59 72 108 -9 -158 51 95
-69 -192 -177 -61 -38 -157 -112 -83
-60 39 14 97 37 -60 97 97
-29 94 79 15 49 59 66 -15

出力例 3

0 1 0 1 3 0 2 0

Problem Statement

You are given length-N integer sequences A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N),C=(C_1,C_2,\dots,C_N),D=(D_1,D_2,\dots,D_N), and E=(E_1,E_2,\dots,E_N).

For an integer i\ (1\leq i\leq N), let f(i) be the number of indices j\ (1\leq j\leq N) such that \max(A_i, B_j-C_i)=D_j+E_i.

Find f(1),f(2),\dots,f(N).

Constraints

  • 1\leq N \leq 2\times 10^5
  • -10^9\leq A_i,B_i,C_i,D_i,E_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 \dots A_N
B_1 B_2 \dots B_N
C_1 C_2 \dots C_N
D_1 D_2 \dots D_N
E_1 E_2 \dots E_N

Output

Print f(1),f(2),\dots,f(N) in this order, with spaces in between.


Sample Input 1

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

Sample Output 1

2 0 1
  • For i=1, the indices j that satisfy \max(A_i, B_j-C_i)=D_j+E_i are j=1,3. Indeed, for j=3 we have \max(4, 9-2)=4+3.
  • For i=2, no indices j satisfy \max(A_i, B_j-C_i)=D_j+E_i.
  • For i=3, the indices j that satisfy \max(A_i, B_j-C_i)=D_j+E_i are j=3.

Therefore, f(1)=2,f(2)=0, and f(3)=1.


Sample Input 2

3
4 6 -10
9 2 6
-5 -5 3
7 0 4
7 7 -1

Sample Output 2

3 3 3

\max(A_i, B_j-C_i)=D_j+E_i holds for all 1\leq i,j\leq 3.


Sample Input 3

8
68 108 176 112 146 156 80 82
-158 -59 72 108 -9 -158 51 95
-69 -192 -177 -61 -38 -157 -112 -83
-60 39 14 97 37 -60 97 97
-29 94 79 15 49 59 66 -15

Sample Output 3

0 1 0 1 3 0 2 0
L - Power Station

Time Limit: 3 sec / Memory Limit: 1024 MiB

問題文

二次元平面上に 1, 2, \ldots, N の番号が付いた N 個の都市があり、都市 i は座標 (X_i, Y_i) にあります。

はじめ、すべての都市に発電所はなく、すべての都市間に電線は張られていません。

あなたは以下の操作を好きな回数行うことができます。

  • 都市 i に発電所を作る。この操作にはコストが P_i かかる。
  • 都市 i と都市 j の間に電線を張る。この操作にはコストが都市間のユークリッド距離、すなわち \sqrt{(X_i - X_j)^2 + (Y_i - Y_j)^2} かかる。

すべての都市が電線を 0 本以上辿ることで発電所のある都市に辿り着けるようにするためにかかるコストの合計の最小値を求めてください。

制約

  • 1 \leq N \leq 2000
  • 0 \leq X_i, Y_i \leq 10^9
  • (X_i, Y_i) \neq (X_j, Y_j) (i \neq j)
  • 1 \leq P_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
P_1 P_2 \ldots P_N

出力

答えを出力せよ。 出力は、真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。


入力例 1

3
0 0
1 0
2 2
1 2 1

出力例 1

3.00000000000000000000

都市 1, 3 に発電所を作り、都市 1 と都市 2 の間に電線を張ると、コストの合計は 3 となります。


入力例 2

4
0 0
1 1
10 10
50 50
10 10 10 10

出力例 2

31.41421356237309504833

入力例 3

5
0 100000
10000 1000000000
10000 100
1000000000 100000
1000000000 0
400000000 600000000 900000000 200000000 500000000

出力例 3

1200200399.25298526883125305176

Problem Statement

There are N cities 1, 2, \ldots, N on a two-dimensional plane. City i is located at coordinates (X_i, Y_i).

Initially, no city has a power plant, and there is no power line between the cities.

You can perform the following operations any number of times:

  • Build a power plant at city i for a cost of P_i.
  • Build a power line between cities i and j for a cost of \sqrt{(X_i - X_j)^2 + (Y_i - Y_j)^2}, the Euclidean distance between them.

Find the minimum total cost required for all cities to be reachable to a city with a power plant via zero or more power lines.

Constraints

  • 1 \leq N \leq 2000
  • 0 \leq X_i, Y_i \leq 10^9
  • (X_i, Y_i) \neq (X_j, Y_j) (i \neq j)
  • 1 \leq P_i \leq 10^9
  • All input values are integers.

Input

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
P_1 P_2 \ldots P_N

Output

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


Sample Input 1

3
0 0
1 0
2 2
1 2 1

Sample Output 1

3.00000000000000000000

By building a power plant at cities 1 and 3, and a power line between cities 1 and 2, the total cost will be 3.


Sample Input 2

4
0 0
1 1
10 10
50 50
10 10 10 10

Sample Output 2

31.41421356237309504833

Sample Input 3

5
0 100000
10000 1000000000
10000 100
1000000000 100000
1000000000 0
400000000 600000000 900000000 200000000 500000000

Sample Output 3

1200200399.25298526883125305176
M - Difference Restricted Sequence

Time Limit: 3 sec / Memory Limit: 1024 MiB

問題文

大きさ N の整数の集合 S=\{S_1,S_2,\ldots,S_N\} と長さ M-1 の数列 D=(D_1,D_2,\ldots,D_{M-1}) が与えられます。

S の要素からなる長さ M の数列 A のうち、|A_i-A_{i+1}| = D_i を満たす 1 以上 M-1 以下の整数 i の個数として考えられる最大値を求めてください。

制約

  • 1 \leq N \leq 2000
  • 2 \leq M \leq 2000
  • 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq D_i \leq 10^9 (1 \leq i \leq M-1)
  • S_i \neq S_j (1 \leq i < j \leq N)
  • 入力はすべて整数である

入力

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

N M
S_1 S_2 ... S_N
D_1 D_2 ... D_{M-1}

出力

答えを出力せよ。


入力例 1

5 5
1 2 3 4 5
3 1 4 1

出力例 1

4

A=(5,2,1,5,4) のとき、|A_i-A_{i+1}|=D_i を満たす i4 個あります。この値が 5 以上になることはないため、答えは 4 です。


入力例 2

10 7
22 75 26 45 72 81 47 29 97 2
0 30 34 0 18 50

出力例 2

5

Problem Statement

You are given an integer set S=\{S_1,S_2,\ldots,S_N\} of size N and a sequence D=(D_1,D_2,\ldots,D_{M-1}) of length (M-1).

Among the length-M sequences A consisting of the elements in S, find the maximum number of integers i between 1 and (M-1), inclusive, such that |A_i-A_{i+1}| = D_i.

Constraints

  • 1 \leq N \leq 2000
  • 2 \leq M \leq 2000
  • 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq D_i \leq 10^9 (1 \leq i \leq M-1)
  • S_i \neq S_j (1 \leq i < j \leq N)
  • All input values are integers.

Input

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

N M
S_1 S_2 ... S_N
D_1 D_2 ... D_{M-1}

Output

Print the answer.


Sample Input 1

5 5
1 2 3 4 5
3 1 4 1

Sample Output 1

4

When A=(5,2,1,5,4), there are four integers i with |A_i-A_{i+1}|=D_i. This count can never be 5 or larger, so the answer is 4.


Sample Input 2

10 7
22 75 26 45 72 81 47 29 97 2
0 30 34 0 18 50

Sample Output 2

5
N - Maximum Subarray Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

問題文

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

以下の形式のクエリが Q 個与えられるので、順に処理してください。

  • 整数 i, x が与えられる。A_i の値を x に変更する。その後、A の長さ K の連続部分列の値の和の最大値を求める。

制約

  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • 各クエリについて、1 \leq i \leq N
  • 各クエリについて、1 \leq x \leq 10^9
  • 入力される値はすべて整数

入力

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

N K
A_1 A_2 \ldots A_N
Q
\text{query}_{1}
\vdots
\text{query}_{Q}

ただし、\text{query}_{i}i 番目のクエリであり、以下の形式で与えられる。

i x

出力

Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

12
7
11

はじめ、A = (2, 3, 1, 1, 7, 4) です。

  • 1 番目のクエリでは、まず A_6 の値を 2 に変更するため、A = (2, 3, 1, 1, 7, 2) となります。A の長さ 4 の連続部分列の値の和の最大値は 3 + 1 + 1 + 7 = 12 です。

  • 2 番目のクエリでは、まず A_5 の値を 2 に変更するため、A = (2, 3, 1, 1, 2, 2) となります。A の長さ 4 の連続部分列の値の和の最大値は 2 + 3 + 1 + 1 = 7 です。

  • 3 番目のクエリでは、まず A_4 の値を 5 に変更するため、A = (2, 3, 1, 5, 2, 2) となります。A の長さ 4 の連続部分列の値の和の最大値は 3 + 1 + 5 + 2 = 11 です。


入力例 2

7 2
1 7 2 6 3 5 4
5
7 7
1 100
3 1000
5 10000
2 100000

出力例 2

12
107
1007
10006
101000

Problem Statement

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

Process the given Q queries in the following format in order.

  • Given integers i and x, set A_i to x, and find the maximum sum of the values in a length-K (contiguous) subarray of A.

Constraints

  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • For each query, 1 \leq i \leq N
  • For each query, 1 \leq x \leq 10^9
  • All input values are integers.

Input

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

N K
A_1 A_2 \ldots A_N
Q
\text{query}_{1}
\vdots
\text{query}_{Q}

Here, \text{query}_{i} denotes the i-th query, given in the following format:

i x

Output

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


Sample Input 1

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

Sample Output 1

12
7
11

Initially, A = (2, 3, 1, 1, 7, 4).

  • For the 1-st query, first set A_6 to 2, making A = (2, 3, 1, 1, 7, 2). Then, the maximum sum of the elements in a length-4 subarray of A is 3 + 1 + 1 + 7 = 12.
  • For the 2-st query, first set A_5 to 2, making A = (2, 3, 1, 1, 2, 2). Then, the maximum sum of the elements in a length-4 subarray of A is 2 + 3 + 1 + 1 = 7.
  • For the 3-st query, first set A_4 to 5, making A = (2, 3, 1, 5, 2, 2). Then, the maximum sum of the elements in a length-4 subarray of A is 3 + 1 + 5 + 2 = 11.

Sample Input 2

7 2
1 7 2 6 3 5 4
5
7 7
1 100
3 1000
5 10000
2 100000

Sample Output 2

12
107
1007
10006
101000
O - Manhattan Rope

Time Limit: 3 sec / Memory Limit: 1024 MiB

問題文

2 次元平面上に、N 個の点 \{(x_1,y_1),(x_2,y_2),\dots,(x_N,y_N)\} を、以下のすべての条件を満たすように配置します。

  • i=1,2,\dots,N について、点 (x_i,y_i) と点 (a_i,b_i)マンハッタン距離d_i 以下である
  • j=1,2,\dots,M について、点 (x_{u_j},y_{u_j}) と点 (x_{v_j},y_{v_j}) のマンハッタン距離が e_j 以下である

ただし、点 (p_1,q_1) と点 (p_2,q_2) のマンハッタン距離を |p_1-p_2|+|q_1-q_2| と定義します。また、座標 x_i,y_i は非整数の値を取りうることに注意してください。

以上の条件を満たすような N 個の点の配置(互いに相異なる必要はない)が存在するか判定し、存在するならば、原点 (0,0) と点 (x_P,y_P) のマンハッタン距離が取りうる最大値を求めてください。 本問題の制約において、答えは整数になることが証明できるので、答えを整数で出力してください。

制約

  • 1 \leq N \leq 2000
  • 0 \leq M \leq \min\{2000, N(N-1)/2\}
  • 1 \leq P \leq N
  • |a_i|,|b_i|\leq 10^8
  • 0 \leq d_i\leq 10^8
  • 1 \leq u_j < v_j\leq N
  • j\neq k ならば (u_j,v_j) \neq (u_k,v_k)
  • 0 \leq e_j\leq 10^8
  • 入力はすべて整数

入力

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

N M P
a_1 b_1 d_1
a_2 b_2 d_2
\vdots
a_N b_N d_N
u_1 v_1 e_1
u_2 v_2 e_2
\vdots
u_M v_M e_M

出力

条件を満たす点の配置が存在しないならば、-1 を出力せよ。

存在するならば、原点 (0,0) と点 (x_P,y_P) のマンハッタン距離が取りうる最大値を整数で出力せよ。


入力例 1

1 0 1
1 1 2

出力例 1

4

このサンプルの状況を下図に示します。丸が (a_1,b_1) を、正方形が |x-a_1|+|y-b_1|\leq d_1 を満たす領域を表します。例えば、(x_1,y_1)(2,2)(下図の星)に配置することで、(x_1,y_1) と原点のマンハッタン距離を 4 にすることができます。

他にも、例えば (x_1,y_1)(1.5, 2.5) に配置しても条件を満たし、原点とのマンハッタン距離を 4 にすることができます。


入力例 2

2 1 2
1 1 2
-2 2 3
1 2 1

出力例 2

3

例えば、(x_1,y_1)(0,2)(x_2,y_2)(-1,2) に配置することで、すべての条件を満たすことができます。このとき、(x_2,y_2) と原点のマンハッタン距離を 3 にでき、これが最大です。


入力例 3

2 1 1
2 0 1
-2 0 1
1 2 1

出力例 3

-1

(x_1,y_1),(x_2,y_2) を、それぞれ青、橙の正方形領域のどこにおいても、間のマンハッタン距離を 1 以下にすることができません。

Problem Statement

Consider placing N points \{(x_1,y_1),(x_2,y_2),\dots,(x_N,y_N)\} on a two-dimensional plane subject to the following conditions:

  • For all i=1,2,\dots,N, the Manhattan distance between point (x_i,y_i) and (a_i,b_i) is at most d_i.
  • For all j=1,2,\dots,M, the Manhattan distance between point (x_{u_j},y_{u_j}) and (x_{v_j},y_{v_j}) is at most e_j.

Here, the Manhattan distance between two points (p_1,q_1) and (p_2,q_2) is defined as |p_1-p_2|+|q_1-q_2|. Note that the coordinates x_i and y_i are not necessarily integers.

Determine if there exists such a placement of N (not necessarily distinct) points. If it exists, find the maximum possible Manhattan distance between the origin (0,0) and point (x_P,y_P). Under the constraints of this problem, the answer can be proven an integer, so print the answer as an integer.

Constraints

  • 1 \leq N \leq 2000
  • 0 \leq M \leq \min\{2000, N(N-1)/2\}
  • 1 \leq P \leq N
  • |a_i|,|b_i|\leq 10^8
  • 0 \leq d_i\leq 10^8
  • 1 \leq u_j < v_j\leq N
  • If j\neq k, then (u_j,v_j) \neq (u_k,v_k).
  • 0 \leq e_j\leq 10^8
  • All input values are integers.

Input

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

N M P
a_1 b_1 d_1
a_2 b_2 d_2
\vdots
a_N b_N d_N
u_1 v_1 e_1
u_2 v_2 e_2
\vdots
u_M v_M e_M

Output

Print -1 if there is no conforming placement.

If there is, print the maximum possible Manhattan distance between the origin (0,0) and point (x_P,y_P) as an integer.


Sample Input 1

1 0 1
1 1 2

Sample Output 1

4

The setting of this sample is shown in the figure below. The circle denotes point (a_1,b_1), and the square denotes the region that satisfies |x-a_1|+|y-b_1|\leq d_1. For example, by placing (x_1,y_1) at (2,2) (the star in the figure), the Manhattan distance between (x_1,y_1) and the origin can be 4.

Alternatively, placing (x_1,y_1) at (1.5, 2.5) also satisfies the conditions, resulting in the Manhattan distance of 4.


Sample Input 2

2 1 2
1 1 2
-2 2 3
1 2 1

Sample Output 2

3

For example, by placing (x_1,y_1) at (0,2) and (x_2,y_2) at (-1,2), all the conditions can be satisfied. In this case, the Manhattan distance between (x_2,y_2) and the origin is 3, which is maximum possible.


Sample Input 3

2 1 1
2 0 1
-2 0 1
1 2 1

Sample Output 3

-1

No matter where you place (x_1,y_1) and (x_2,y_2) in the blue and orange regions, respectively, the Manhattan distance between them can never be 1.