A - Kyu in AtCoder

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 100

問題文

M 君は AtCoder に参加している選手の 1 人です。彼の最高レーティングは X です。
AtCoder では、最高レーティングに応じて選手に級位が与えられます。レーティング 400 以上 1999 以下については、以下の通りです。

  • 400 以上 599 以下:8
  • 600 以上 799 以下:7
  • 800 以上 999 以下:6
  • 1000 以上 1199 以下:5
  • 1200 以上 1399 以下:4
  • 1400 以上 1599 以下:3
  • 1600 以上 1799 以下:2
  • 1800 以上 1999 以下:1

M 君は何級を持っていますか。

制約

  • 400 \leq X \leq 1999
  • X は整数

入力

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

X

出力

M 君が持っている級を整数として出力してください。
例えば 8 級の場合は 8 と出力してください。


入力例 1

725

出力例 1

7

M 君の最高レーティングは 725 であり、7 級に対応します。
よって、 7 と出力すれば正解となります。


入力例 2

1600

出力例 2

2

M 君の最高レーティングは 1600 であり、2 級に対応します。
よって、2 と出力すれば正解となります。

Score: 100 points

Problem Statement

M-kun is a competitor in AtCoder, whose highest rating is X.
In this site, a competitor is given a kyu (class) according to his/her highest rating. For ratings from 400 through 1999, the following kyus are given:

  • From 400 through 599: 8-kyu
  • From 600 through 799: 7-kyu
  • From 800 through 999: 6-kyu
  • From 1000 through 1199: 5-kyu
  • From 1200 through 1399: 4-kyu
  • From 1400 through 1599: 3-kyu
  • From 1600 through 1799: 2-kyu
  • From 1800 through 1999: 1-kyu

What kyu does M-kun have?

Constraints

  • 400 \leq X \leq 1999
  • X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the kyu M-kun has, as an integer. For example, if he has 8-kyu, print 8.


Sample Input 1

725

Sample Output 1

7

M-kun's highest rating is 725, which corresponds to 7-kyu.
Thus, 7 is the correct output.


Sample Input 2

1600

Sample Output 2

2

M-kun's highest rating is 1600, which corresponds to 2-kyu.
Thus, 2 is the correct output.

B - Magic 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 200

問題文

M 君は、以下の 3 枚のカードを持っています。

  • 整数 A が書かれた赤のカード
  • 整数 B が書かれた緑のカード
  • 整数 C が書かれた青のカード

彼は天才的な魔術師なので、以下の操作を K 回まで行うことができます。

  • 3 枚のうちいずれか 1 枚のカードを選び、書かれた整数を 2 倍する。

操作を行った後、以下の条件が同時に満たされれば、魔術は成功です。

  • 緑のカードに書かれている整数は、赤のカードに書かれている整数より真に大きい。
  • 青のカードに書かれている整数は、緑のカードに書かれている整数より真に大きい。

魔術を成功させることができるかどうか判定してください。

制約

  • 1 \leq A, B, C \leq 7
  • 1 \leq K \leq 7
  • 入力はすべて整数

入力

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

A B C
K

出力

魔術を成功させることができる場合は、Yes と出力してください。
そうでない場合は、No と出力してください。


入力例 1

7 2 5
3

出力例 1

Yes

例えば、以下のように操作を行った場合、魔術を成功させることができます。

  • 1 回目:青のカードを選ぶ。赤のカードには 7、緑には 2、青には 10 が書かれている状態になる。
  • 2 回目:緑のカードを選ぶ。赤のカードには 7、緑には 4、青には 10 が書かれている状態になる。
  • 3 回目:緑のカードを選ぶ。赤のカードには 7、緑には 8、青には 10 が書かれている状態になる。

入力例 2

7 4 2
3

出力例 2

No

M 君がどのように操作を行っても、3 回以内の操作で魔術を成功させることはできません。

Score: 200 points

Problem Statement

M-kun has the following three cards:

  • A red card with the integer A.
  • A green card with the integer B.
  • A blue card with the integer C.

He is a genius magician who can do the following operation at most K times:

  • Choose one of the three cards and multiply the written integer by 2.

His magic is successful if both of the following conditions are satisfied after the operations:

  • The integer on the green card is strictly greater than the integer on the red card.
  • The integer on the blue card is strictly greater than the integer on the green card.

Determine whether the magic can be successful.

Constraints

  • 1 \leq A, B, C \leq 7
  • 1 \leq K \leq 7
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C
K

Output

If the magic can be successful, print Yes; otherwise, print No.


Sample Input 1

7 2 5
3

Sample Output 1

Yes

The magic will be successful if, for example, he does the following operations:

  • First, choose the blue card. The integers on the red, green, and blue cards are now 7, 2, and 10, respectively.
  • Second, choose the green card. The integers on the red, green, and blue cards are now 7, 4, and 10, respectively.
  • Third, choose the green card. The integers on the red, green, and blue cards are now 7, 8, and 10, respectively.

Sample Input 2

7 4 2
3

Sample Output 2

No

He has no way to succeed in the magic with at most three operations.

C - Marks

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 300

問題文

M 君は青木高校の生徒です。青木高校では、1 年間の課程を N 個の学期に分割する N 学期制が用いられています。
各学期には期末テストが 1 回行われ、その点数に応じて、以下のように各学期の評点が付けられます。

  • 1 学期から K-1 学期までの評点:付けられない。
  • K 学期から N 学期までの評点:その学期を含めた直近 K 回の期末テストの点数を掛け算したもの。

M 君は i 学期の期末テストで A_i 点を取りました。
K+1 \leq i \leq N を満たすそれぞれの i について、i 学期の評点が i-1 学期の評点より真に高かったかどうか判定してください。

制約

  • 2 \leq N \leq 200000
  • 1 \leq K \leq N-1
  • 1 \leq A_i \leq 10^{9}
  • 入力はすべて整数

入力

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

N K
A_1 A_2 A_3 \ldots A_N

出力

答えを N-K 行に出力してください。
i 行目には、K+i 学期の評点が K+i-1 学期の評点より高い場合は Yes を、そうでない場合は No を出力してください。


入力例 1

5 3
96 98 95 100 20

出力例 1

Yes
No

M 君の各学期の評点は、以下のように計算されます。

  • 3 学期:(96 \times 98 \times 95) = 893760
  • 4 学期:(98 \times 95 \times 100) = 931000
  • 5 学期:(95 \times 100 \times 20) = 190000

入力例 2

3 2
1001 869120 1001

出力例 2

No

3 学期の評点と 2 学期の評点が同じ場合、No と出力しなければならないことに注意してください。


入力例 3

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

出力例 3

Yes
Yes
No
Yes
Yes
No
Yes
Yes

Score: 300 points

Problem Statement

M-kun is a student in Aoki High School, where a year is divided into N terms.
There is an exam at the end of each term. According to the scores in those exams, a student is given a grade for each term, as follows:

  • For the first through (K-1)-th terms: not given.
  • For each of the K-th through N-th terms: the multiplication of the scores in the last K exams, including the exam in the graded term.

M-kun scored A_i in the exam at the end of the i-th term.
For each i such that K+1 \leq i \leq N, determine whether his grade for the i-th term is strictly greater than the grade for the (i-1)-th term.

Constraints

  • 2 \leq N \leq 200000
  • 1 \leq K \leq N-1
  • 1 \leq A_i \leq 10^{9}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 A_3 \ldots A_N

Output

Print the answer in N-K lines.
The i-th line should contain Yes if the grade for the (K+i)-th term is greater than the grade for the (K+i-1)-th term, and No otherwise.


Sample Input 1

5 3
96 98 95 100 20

Sample Output 1

Yes
No

His grade for each term is computed as follows:

  • 3-rd term: (96 \times 98 \times 95) = 893760
  • 4-th term: (98 \times 95 \times 100) = 931000
  • 5-th term: (95 \times 100 \times 20) = 190000

Sample Input 2

3 2
1001 869120 1001

Sample Output 2

No

Note that the output should be No if the grade for the 3-rd term is equal to the grade for the 2-nd term.


Sample Input 3

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

Sample Output 3

Yes
Yes
No
Yes
Yes
No
Yes
Yes
D - Road to Millionaire

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 400

問題文

M 君は億万長者を目指して、明日から N 日間は投資で稼ごうと考えました。現在の彼の所持金は 1000 円であり、株は持っていません。なお、M 君の住んでいる国で発行されている株は一種類です。

彼は全国に知られる未来予知能力者であり、今後 N 日間の株価が以下のようになることをすでに知っています。

  • 1 日目の株価は A_1 円、2 日目の株価は A_2 円、・・・、N 日目の株価は A_N

i 日目には、その時点で所持する金と株の範囲内で、M 君は次の取引を何回でも行えます。何も取引しない日があっても構いません。

  • 株式購入:A_i 円を支払って、1 株を受け取る。
  • 株式売却:1 株を売却し、A_i 円を受け取る。

さて、M 君がうまく取引を行ったとき、彼の最終的な所持金は最大でいくらになるでしょうか?

制約

  • 2 \leq N \leq 80
  • 100 \leq A_i \leq 200
  • 入力はすべて整数

入力

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

N
A_1 A_2 \cdots A_N

出力

M 君の最終的な所持金としてありうる最大の金額を整数として出力してください。


入力例 1

7
100 130 130 130 115 115 150

出力例 1

1685

この入力例では、M 君は 7 日間にわたって株の取引を行うことになります。例えば、次の方法で最終的な所持金を 1685 円とすることができます。

  • 最初、M 君は 1000 円を持っており、株は持っていない。
  • 1 日目:株式購入を 10 回行う。1000 円を支払って 10 株を購入し、所持金は 0 円となる。
  • 2 日目:株式売却を 7 回行う。7 株を売却して 910 円を受け取り、所持金は 910 円となる。
  • 3 日目:株式売却を 3 回行う。3 株を売却して 390 円を受け取り、所持金は 1300 円となる。
  • 4 日目:何もしない。
  • 5 日目:株式購入を 1 回行う。115 円を支払って 1 株を購入し、所持金は 1185 円となる。
  • 6 日目:株式購入を 10 回行う。1150 円を支払って 10 株を購入し、所持金は 35 円となる。
  • 7 日目:株式売却を 11 回行う。11 株を売却して 1650 円を受け取り、所持金は 1685 円となる。

また、どのように取引を行っても、最終的な所持金を 1686 円以上にすることはできないため、答えは 1685 となります。


入力例 2

6
200 180 160 140 120 100

出力例 2

1000

この入力例では、6 日間何もしないのが最適です。このとき、最終的な所持金は 1000 円となります。


入力例 3

2
157 193

出力例 3

1216

この入力例では、1 日目に 6 株を購入し、2 日目に 6 株を売却すると、最終的な所持金が 1216 円となり、最適です。

Score: 400 points

Problem Statement

To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.

He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:

  • A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.

In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.

  • Buy stock: Pay A_i yen and receive one stock.
  • Sell stock: Sell one stock for A_i yen.

What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

Constraints

  • 2 \leq N \leq 80
  • 100 \leq A_i \leq 200
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the maximum possible amount of money that M-kun can have in the end, as an integer.


Sample Input 1

7
100 130 130 130 115 115 150

Sample Output 1

1685

In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:

  • Initially, he has 1000 yen and no stocks.
  • Day 1: Buy 10 stocks for 1000 yen. Now he has 0 yen.
  • Day 2: Sell 7 stocks for 910 yen. Now he has 910 yen.
  • Day 3: Sell 3 stocks for 390 yen. Now he has 1300 yen.
  • Day 4: Do nothing.
  • Day 5: Buy 1 stock for 115 yen. Now he has 1185 yen.
  • Day 6: Buy 10 stocks for 1150 yen. Now he has 35 yen.
  • Day 7: Sell 11 stocks for 1650 yen. Now he has 1685 yen.

There is no way to have 1686 yen or more in the end, so the answer is 1685.


Sample Input 2

6
200 180 160 140 120 100

Sample Output 2

1000

In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.


Sample Input 3

2
157 193

Sample Output 3

1216

In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2, after which we will have 1216 yen.

E - M's Solution

Time Limit: 3 sec / Memory Limit: 1024 MB

配点: 500

問題文

新 AtCoder 市は以下のように、無限に広がる碁盤の目のような構造になっています。

  • 新 AtCoder 市の中心には時計台があり、この座標を (0, 0) とする。
  • 時計台を通る東西方向に一直線の道路があり、これを「東西大通り」とする。これは 2 次元座標平面では x 軸に相当する。
  • そのほかにも、東西大通りに平行な道路が、距離 1 ずつ間隔をあけて無限本存在する。これらは、直線 y = (0 \ 以外の整数) に相当する。
  • 時計台を通る南北方向に一直線の道路があり、これを「南北大通り」とする。これは 2 次元座標平面では y 軸に相当する。
  • そのほかにも、南北大通りに平行な道路が、距離 1 ずつ間隔をあけて無限本存在する。これらは、直線 x = (0 \ 以外の整数) に相当する。

新 AtCoder 市には N 個の集落があります。i 個目の集落は、座標 (X_i, Y_i) の交差点上にあり、人口は P_i です。各市民は、これらの集落のうちいずれかひとつに住んでいます。

現時点で、鉄道路線は東西大通りに沿って無限に延びるものと、南北大通りに沿って無限に延びるものの 2 本しかありません。
市長の M 君は、これでは通勤に不便だと考えたので、新たに K 本の通りを選んで、それぞれに沿って無限に延びる鉄道路線を建設することにしました。

ここで、新 AtCoder 市の各市民の「歩行距離」を、住んでいる集落から最も近い鉄道路線までの距離とします。
このとき、全ての市民の歩行距離の合計 S が最も小さくなるように鉄道路線を建設したいです。

さて、K = 0, 1, 2, \dots, N のそれぞれについて、鉄道路線建設後の S の最小値はいくつでしょうか?

制約

  • 1 \leq N \leq 15
  • -10 \ 000 \leq X_i \leq 10 \ 000
  • -10 \ 000 \leq Y_i \leq 10 \ 000
  • 1 \leq P_i \leq 1 \ 000 \ 000
  • N 個の集落の場所 (X_i, Y_i) はすべて異なる
  • 入力はすべて整数

入力

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

N
X_1 Y_1 P_1
X_2 Y_2 P_2
 :  :  :
X_N Y_N P_N

出力

答えを N+1 行に出力してください。
i 行目 (i = 1, \ldots, N+1) には、K = i - 1 の場合の鉄道路線建設後の S の最小値を整数として出力してください。


入力例 1

3
1 2 300
3 3 600
1 4 800

出力例 1

2900
900
0
0

K = 0 の場合、集落 1, 2, 3 の住民は、鉄道路線にたどり着くためには、それぞれ距離 1, 3, 1 歩く必要があります。
したがって、全ての市民の歩行距離の合計 S1 \times 300 + 3 \times 600 + 1 \times 800 = 2900 となります。

K = 1 の場合、東西大通りから距離 4 だけ北の道路に鉄道路線を建設すると、集落 1, 2, 3 の住民の歩行距離は 1, 1, 0 になります。
すると、S = 1 \times 300 + 1 \times 600 + 0 \times 800 = 900 となります。
他にも多くの候補から建設場所を選べますが、そのうちどれも S900 より小さくすることはできません。

K = 2 の場合、南北大通りから距離 1, 3 だけ東の道路に鉄道路線を建設すると、新 AtCoder 市のすべての住民が歩行距離 0 で鉄道路線にたどり着くことができるので、S = 0 となります。また、K = 3 の場合も、S = 0 とすることができます。

K = 0, 1, 2 の場合の最適な鉄道路線の建設方法を下図に示します。
青く塗られた道路が鉄道路線を敷設する道路を表します。


入力例 2

5
3 5 400
5 3 700
5 5 1000
5 7 700
7 5 400

出力例 2

13800
1600
0
0
0
0

K = 1, 2 の場合の最適な鉄道路線の建設方法を下図に示します。


入力例 3

6
2 5 1000
5 2 1100
5 5 1700
-2 -5 900
-5 -2 600
-5 -5 2200

出力例 3

26700
13900
3200
1200
0
0
0

K = 3 の場合の最適な鉄道路線の建設方法を下図に示します。


入力例 4

8
2 2 286017
3 1 262355
2 -2 213815
1 -3 224435
-2 -2 136860
-3 -1 239338
-2 2 217647
-1 3 141903

出力例 4

2576709
1569381
868031
605676
366338
141903
0
0
0

K = 4 の場合の最適な鉄道路線の建設方法を下図に示します。

Score: 500 points

Problem Statement

New AtCoder City has an infinite grid of streets, as follows:

  • At the center of the city stands a clock tower. Let (0, 0) be the coordinates of this point.
  • A straight street, which we will call East-West Main Street, runs east-west and passes the clock tower. It corresponds to the x-axis in the two-dimensional coordinate plane.
  • There are also other infinitely many streets parallel to East-West Main Street, with a distance of 1 between them. They correspond to the lines \ldots, y = -2, y = -1, y = 1, y = 2, \ldots in the two-dimensional coordinate plane.
  • A straight street, which we will call North-South Main Street, runs north-south and passes the clock tower. It corresponds to the y-axis in the two-dimensional coordinate plane.
  • There are also other infinitely many streets parallel to North-South Main Street, with a distance of 1 between them. They correspond to the lines \ldots, x = -2, x = -1, x = 1, x = 2, \ldots in the two-dimensional coordinate plane.

There are N residential areas in New AtCoder City. The i-th area is located at the intersection with the coordinates (X_i, Y_i) and has a population of P_i. Each citizen in the city lives in one of these areas.

The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.
M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose K streets and build a railroad stretching infinitely along each of those streets.

Let the walking distance of each citizen be the distance from his/her residential area to the nearest railroad.
M-kun wants to build railroads so that the sum of the walking distances of all citizens, S, is minimized.

For each K = 0, 1, 2, \dots, N, what is the minimum possible value of S after building railroads?

Constraints

  • 1 \leq N \leq 15
  • -10 \ 000 \leq X_i \leq 10 \ 000
  • -10 \ 000 \leq Y_i \leq 10 \ 000
  • 1 \leq P_i \leq 1 \ 000 \ 000
  • The locations of the N residential areas, (X_i, Y_i), are all distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1 P_1
X_2 Y_2 P_2
 :  :  :
X_N Y_N P_N

Output

Print the answer in N+1 lines.
The i-th line (i = 1, \ldots, N+1) should contain the minimum possible value of S after building railroads for the case K = i-1.


Sample Input 1

3
1 2 300
3 3 600
1 4 800

Sample Output 1

2900
900
0
0

When K = 0, the residents of Area 1, 2, and 3 have to walk the distances of 1, 3, and 1, respectively, to reach a railroad.
Thus, the sum of the walking distances of all citizens, S, is 1 \times 300 + 3 \times 600 + 1 \times 800 = 2900.

When K = 1, if we build a railroad along the street corresponding to the line y = 4 in the coordinate plane, the walking distances of the citizens of Area 1, 2, and 3 become 1, 1, and 0, respectively.
Then, S = 1 \times 300 + 1 \times 600 + 0 \times 800 = 900.
We have many other options for where we build the railroad, but none of them makes S less than 900.

When K = 2, if we build a railroad along the street corresponding to the lines x = 1 and x = 3 in the coordinate plane, all citizens can reach a railroad with the walking distance of 0, so S = 0. We can also have S = 0 when K = 3.

The figure below shows the optimal way to build railroads for the cases K = 0, 1, 2.
The street painted blue represents the roads along which we build railroads.


Sample Input 2

5
3 5 400
5 3 700
5 5 1000
5 7 700
7 5 400

Sample Output 2

13800
1600
0
0
0
0

The figure below shows the optimal way to build railroads for the cases K = 1, 2.


Sample Input 3

6
2 5 1000
5 2 1100
5 5 1700
-2 -5 900
-5 -2 600
-5 -5 2200

Sample Output 3

26700
13900
3200
1200
0
0
0

The figure below shows the optimal way to build railroads for the case K = 3.


Sample Input 4

8
2 2 286017
3 1 262355
2 -2 213815
1 -3 224435
-2 -2 136860
-3 -1 239338
-2 2 217647
-1 3 141903

Sample Output 4

2576709
1569381
868031
605676
366338
141903
0
0
0

The figure below shows the optimal way to build railroads for the case K = 4.

F - Air Safety

Time Limit: 3 sec / Memory Limit: 1024 MB

配点: 600

問題文

M 君は立派な航空管制官です。

今、彼が管理するレーダーのディスプレイの中では、番号 1, 2, ..., NN 機の飛行機が、全て同じ高度を飛行しています。
各飛行機は秒速 0.1 という一定の速度で一定の方向に飛行しており、番号 i の飛行機の現在の座標は (X_i, Y_i)、進行方向は以下の通りです。

  • U_iU の場合:y 座標の正の方向に進む。
  • U_iR の場合:x 座標の正の方向に進む。
  • U_iD の場合:y 座標の負の方向に進む。
  • U_iL の場合:x 座標の負の方向に進む。

M 君の仕事を助けるために、このままでは衝突してしまう飛行機の組が存在するかどうか判定してください。
もし存在する場合は、最も早いもので今から何秒後に衝突してしまうかを求めてください。
ただし、すべての飛行機は無視できるほど小さく、衝突は 2 つの飛行機が同じ座標に同時に到達した場合にのみ起こるものとします。

制約

  • 1 \leq N \leq 200000
  • 0 \leq X_i, Y_i \leq 200000
  • U_iURDL のいずれかである
  • N 機の飛行機の現在位置 (X_i, Y_i) はすべて異なる
  • N, X_i, Y_i は整数

入力

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

N
X_1 Y_1 U_1
X_2 Y_2 U_2
X_3 Y_3 U_3
:
X_N Y_N U_N

出力

このままでは衝突してしまう飛行機の組が存在する場合は、最も早いもので今から何秒後に衝突するかを整数で出力してください。
存在しない場合は、SAFE と出力してください。


入力例 1

2
11 1 U
11 47 D

出力例 1

230

このままでは、230 秒後に、番号 1 の飛行機と番号 2 の飛行機が同時に座標 (11, 24) に到達し、衝突してしまいます。


入力例 2

4
20 30 U
30 20 R
20 10 D
10 20 L

出力例 2

SAFE

衝突してしまう飛行機の組はひとつもありません。


入力例 3

8
168 224 U
130 175 R
111 198 D
121 188 L
201 116 U
112 121 R
145 239 D
185 107 L

出力例 3

100

Score: 600 points

Problem Statement

M-kun is a brilliant air traffic controller.

On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude.
Each of the airplanes flies at a constant speed of 0.1 per second in a constant direction. The current coordinates of the airplane numbered i are (X_i, Y_i), and the direction of the airplane is as follows:

  • if U_i is U, it flies in the positive y direction;
  • if U_i is R, it flies in the positive x direction;
  • if U_i is D, it flies in the negative y direction;
  • if U_i is L, it flies in the negative x direction.

To help M-kun in his work, determine whether there is a pair of airplanes that will collide with each other if they keep flying as they are now.
If there is such a pair, find the number of seconds after which the first collision will happen.
We assume that the airplanes are negligibly small so that two airplanes only collide when they reach the same coordinates simultaneously.

Constraints

  • 1 \leq N \leq 200000
  • 0 \leq X_i, Y_i \leq 200000
  • U_i is U, R, D, or L.
  • The current positions of the N airplanes, (X_i, Y_i), are all distinct.
  • N, X_i, and Y_i are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1 U_1
X_2 Y_2 U_2
X_3 Y_3 U_3
:
X_N Y_N U_N

Output

If there is a pair of airplanes that will collide with each other if they keep flying as they are now, print an integer representing the number of seconds after which the first collision will happen.
If there is no such pair, print SAFE.


Sample Input 1

2
11 1 U
11 47 D

Sample Output 1

230

If the airplanes keep flying as they are now, two airplanes numbered 1 and 2 will reach the coordinates (11, 24) simultaneously and collide.


Sample Input 2

4
20 30 U
30 20 R
20 10 D
10 20 L

Sample Output 2

SAFE

No pair of airplanes will collide.


Sample Input 3

8
168 224 U
130 175 R
111 198 D
121 188 L
201 116 U
112 121 R
145 239 D
185 107 L

Sample Output 3

100