A - Lexicographic Order

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

配点 : 100

問題文

相異なる二つの文字列 S, T が与えられます。
ST よりも辞書順で小さい場合は Yes を、大きい場合は No を出力してください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。

以下では「 Si 文字目の文字」を S_i のように表します。また、 ST より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. ST のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_iT_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_jT_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、 ST の長さを比較して、ST より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

なお、主要なプログラミング言語の多くでは、文字列の辞書順による比較は標準ライブラリに含まれる関数や演算子として実装されています。詳しくは各言語のリファレンスをご参照ください。

制約

  • S, T は英小文字からなる長さ 1 以上 10 以下の相異なる文字列である。

入力

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

S T

出力

ST より辞書順で小さい場合は Yes を、大きい場合は No を出力せよ。


入力例 1

abc atcoder

出力例 1

Yes

abcatcoder1 文字目が同じで 2 文字目が異なります。 アルファベットの bt よりもアルファベット順で先に来るので、 abc の方が atcoder よりも辞書順で小さいことがわかります。


入力例 2

arc agc

出力例 2

No

入力例 3

a aa

出力例 3

Yes

Score : 100 points

Problem Statement

You are given two different strings S and T.
If S is lexicographically smaller than T, print Yes; otherwise, print No.

What is the lexicographical order?

Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.

Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.

  1. Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
  3. If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.

Note that many major programming languages implement lexicographical comparison of strings as operators or functions in standard libraries. For more detail, see your language's reference.

Constraints

  • S and T are different strings, each of which consists of lowercase English letters and has a length of between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

S T

Output

If S is lexicographically smaller than T, print Yes; otherwise, print No.


Sample Input 1

abc atcoder

Sample Output 1

Yes

abc and atcoder begin with the same character, but their second characters are different. Since b comes earlier than t in alphabetical order, we can see that abc is lexicographically smaller than atcoder.


Sample Input 2

arc agc

Sample Output 2

No

Sample Input 3

a aa

Sample Output 3

Yes
B - AtCoder Quiz

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

配点 : 200

問題文

AtCoder では現在、 ABC , ARC , AGC , AHC4 つのコンテストが定期的に開催されています。

AtCoder で現在定期的に開催されているコンテストは S_1 , S_2 , S_3 とあと 1 つは何ですか?

制約

  • S_1 , S_2 , S_3 はそれぞれ、 ABC , ARC , AGC , AHC のいずれかである。
  • S_1 , S_2 , S_3 は相異なる。

入力

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

S_1
S_2
S_3

出力

答えを出力せよ。


入力例 1

ARC
AGC
AHC

出力例 1

ABC

ARC , AGC , AHC3つが入力として与えられているので、 残りの 1 つはABC です。


入力例 2

AGC
ABC
ARC

出力例 2

AHC

Score : 200 points

Problem Statement

AtCoder currently holds four series of regular contests: ABC, ARC, AGC, and AHC.

What is the series of regular contests currently held by AtCoder in addition to S_1, S_2, and S_3?

Constraints

  • Each of S_1, S_2, and S_3 is ABC, ARC, AGC, or AHC.
  • S_1, S_2, and S_3 are pairwise different.

Input

Input is given from Standard Input in the following format:

S_1
S_2
S_3

Output

Print the answer.


Sample Input 1

ARC
AGC
AHC

Sample Output 1

ABC

Given in input are ARC, AGC, and AHC. The rest is ABC.


Sample Input 2

AGC
ABC
ARC

Sample Output 2

AHC
C - Inverse of Permutation

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

配点 : 300

問題文

1,2,\dots,N1 回ずつ現れる長さ N の数列を「長さ N の順列」と呼びます。
長さ N の順列 P = (p_1, p_2,\dots,p_N) が与えられるので、以下の条件を満たす長さ N の順列 Q = (q_1,\dots,q_N) を出力してください。

  • 全ての i (1 \leq i \leq N) に対して Qp_i 番目の要素が i である。

ただし、条件を満たす Q は必ずただ 1 つ存在することが証明できます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • (p_1,p_2,\dots,p_N) は長さ N の順列である。
  • 入力は全て整数である。

入力

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

N
p_1 p_2 \dots p_N

出力

数列 Q を空白区切りで 1 行で出力せよ。

q_1 q_2 \dots q_N

入力例 1

3
2 3 1

出力例 1

3 1 2

以下に説明する通り、 Q=(3,1,2) は条件を満たす順列です。

  • i = 1 のとき p_i = 2, q_2 = 1
  • i = 2 のとき p_i = 3, q_3 = 2
  • i = 3 のとき p_i = 1, q_1 = 3

入力例 2

3
1 2 3

出力例 2

1 2 3

全ての i (1 \leq i \leq N) に対して p_i = i が成り立つときは P = Q になります。


入力例 3

5
5 3 2 4 1

出力例 3

5 3 2 4 1

Score : 300 points

Problem Statement

We will call a sequence of length N where each of 1,2,\dots,N occurs once as a permutation of length N.
Given a permutation of length N, P = (p_1, p_2,\dots,p_N), print a permutation of length N, Q = (q_1,\dots,q_N), that satisfies the following condition.

  • For every i (1 \leq i \leq N), the p_i-th element of Q is i.

It can be proved that there exists a unique Q that satisfies the condition.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • (p_1,p_2,\dots,p_N) is a permutation of length N (defined in Problem Statement).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 \dots p_N

Output

Print the sequence Q in one line, with spaces in between.

q_1 q_2 \dots q_N

Sample Input 1

3
2 3 1

Sample Output 1

3 1 2

The permutation Q=(3,1,2) satisfies the condition, as follows.

  • For i = 1, we have p_i = 2, q_2 = 1.
  • For i = 2, we have p_i = 3, q_3 = 2.
  • For i = 3, we have p_i = 1, q_1 = 3.

Sample Input 2

3
1 2 3

Sample Output 2

1 2 3

If p_i = i for every i (1 \leq i \leq N), we will have P = Q.


Sample Input 3

5
5 3 2 4 1

Sample Output 3

5 3 2 4 1
D - Cutting Woods

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

配点 : 400

問題文

長さ L メートルの直線状の木材があります。
x = 1, 2, \dots, L - 1 に対して、木材の左端から x メートルの地点には目印として線 x が引かれています。

Q 個のクエリが与えられます。 i 番目のクエリは数の組 (c_i, x_i) によって表されます。
以下の説明に従ってクエリを i の昇順に処理してください。

  • c_i = 1 のとき : 線 x_i がある地点で木材を 2 つに切る。
  • c_i = 2 のとき : 線 x_i を含む木材を選び、その長さを出力する。

ただし c_i = 1, 2 の両方に対して、線 x_i はクエリを処理する時点で切られていないことが保証されます。

制約

  • 1 \leq L \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • c_i = 1, 2 (1 \leq i \leq Q)
  • 1 \leq x_i \leq L - 1 (1 \leq i \leq Q)
  • 全ての i (1 \leq i \leq Q) に対して次が成り立つ: 1 \leq j \lt i かつ (c_j,x_j) = (1, x_i) を満たす j は存在しない。
  • 入力は全て整数である。

入力

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

L Q
c_1 x_1
c_2 x_2
\vdots
c_Q x_Q

出力

c_i = 2 を満たすクエリの回数と等しい行数だけ出力せよ。 j 行目では j 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

5 3
2 2
1 3
2 2

出力例 1

5
3

1 番目のクエリ時点では木材は一度も切られていないので、線 2 を含む木材の長さは 5 メートルです。よって 5 を出力します。
2 番目のクエリによって、木材は 3 メートルの木材と 2 メートルの木材に分割されます。
3 番目のクエリ時点では 線 2 を含む木材の長さは 3 メートルなので、3 を出力します。


入力例 2

5 3
1 2
1 4
2 3

出力例 2

2

入力例 3

100 10
1 31
2 41
1 59
2 26
1 53
2 58
1 97
2 93
1 23
2 84

出力例 3

69
31
6
38
38

Score : 400 points

Problem Statement

We have a long piece of timber with a length of L meters.
For each x = 1, 2, \dots, L - 1, there is a mark called Mark x at x meters from the left end of the piece.

You are given Q queries, the i-th of which is represented as a pair of numbers (c_i, x_i).
Process the queries in ascending order of i as described below.

  • If c_i = 1: cut the piece at Mark x_i into two.
  • If c_i = 2: choose the piece with Mark x_i on it and print its length.

Here, for both kinds of queries c_i = 1, 2, it is guaranteed that there will have been no cut at Mark x_i when the query is to be processed.

Constraints

  • 1 \leq L \leq 10^9
  • 1 \leq Q \leq 2 \times 10^5
  • c_i = 1, 2 (1 \leq i \leq Q)
  • 1 \leq x_i \leq L - 1 (1 \leq i \leq Q)
  • For every i (1 \leq i \leq Q), the following holds: there is no j such that 1 \leq j \lt i and (c_j,x_j) = (1, x_i).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

L Q
c_1 x_1
c_2 x_2
\vdots
c_Q x_Q

Output

Print the number of lines equal to the number of queries c_i = 2. In the j-th line, print the response to the j-th such query.


Sample Input 1

5 3
2 2
1 3
2 2

Sample Output 1

5
3

At the time of the first query, no cut has been made, so the piece with Mark 2 has a length of 5 meters. Thus, you should print 5.
In the second query, the piece is cut into two pieces with lengths of 3 and 2 meters.
At the time of the third query, the piece with Mark 2 has a length of 3 meters, so you should print 3.


Sample Input 2

5 3
1 2
1 4
2 3

Sample Output 2

2

Sample Input 3

100 10
1 31
2 41
1 59
2 26
1 53
2 58
1 97
2 93
1 23
2 84

Sample Output 3

69
31
6
38
38
E - Sorting Queries

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

配点 : 500

問題文

空の列 A があります。クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。

  • 1 x : A の最後尾に x を追加する。
  • 2 : A の最初の要素を出力する。その後、その要素を削除する。このクエリが与えられるとき、A は空でないことが保証される。
  • 3 : A を昇順にソートする。

制約

  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq x \leq 10^9
  • クエリ 2 が与えられるとき、A は空でない。
  • 入力は全て整数である。

入力

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

Q
\mathrm{query} 1
\mathrm{query} 2
\vdots
\mathrm{query} Q

i 番目のクエリ \mathrm{query} i では、まずクエリの種類 c_i1, 2, 3 のいずれか)が与えられる。 c_i = 1 の場合はさらに整数 x が追加で与えられる。

すなわち、各クエリは以下に示す 3 つの形式のいずれかである。

1 x
2
3

出力

c_i = 2 を満たすクエリの回数を q として q 行出力せよ。
j (1 \leq j \leq q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

8
1 4
1 3
1 2
1 1
3
2
1 0
2

出力例 1

1
2

入力例 1 において、 i 番目のクエリを処理した後の A の状態を i 行目に示すと以下のようになります。

  • (4)
  • (4, 3)
  • (4, 3, 2)
  • (4, 3, 2, 1)
  • (1, 2, 3, 4)
  • (2, 3, 4)
  • (2, 3, 4, 0)
  • (3, 4, 0)

入力例 2

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

出力例 2

5
3
5

入力例 2 において、 i 番目のクエリを処理した後の A の状態を i 行目に示すと以下のようになります。

  • (5)
  • (5, 5)
  • (5, 5, 3)
  • (5, 3)
  • (3, 5)
  • (5)
  • (5, 6)
  • (5, 6)
  • (6)

Score : 500 points

Problem Statement

We have an empty sequence A. You will be given Q queries, which should be processed in the order they are given. Each query is of one of the three kinds below:

  • 1 x : Append x to the end of A.
  • 2 : Print the element at the beginning of A. Then, delete that element. It is guaranteed that A will not empty when this query is given.
  • 3 : Sort A in ascending order.

Constraints

  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq x \leq 10^9
  • A will not be empty when a query 2 is given.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
\mathrm{query} 1
\mathrm{query} 2
\vdots
\mathrm{query} Q

The i-th query, \mathrm{query} i, begins with the kind of query c_i (1, 2, or 3). If c_i = 1, the line additionally has an integer x.

In other words, each query is in one of the three formats below.

1 x
2
3

Output

Print q lines, where q is the number of queries with c_i = 2.
The j-th line (1 \leq j \leq q) should contain the response for the j-th such query.


Sample Input 1

8
1 4
1 3
1 2
1 1
3
2
1 0
2

Sample Output 1

1
2

The i-th line below shows the contents of A after the i-th query is processed in Sample Input 1.

  • (4)
  • (4, 3)
  • (4, 3, 2)
  • (4, 3, 2, 1)
  • (1, 2, 3, 4)
  • (2, 3, 4)
  • (2, 3, 4, 0)
  • (3, 4, 0)

Sample Input 2

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

Sample Output 2

5
3
5

The i-th line below shows the contents of A after the i-th query is processed in Sample Input 2.

  • (5)
  • (5, 5)
  • (5, 5, 3)
  • (5, 3)
  • (3, 5)
  • (5)
  • (5, 6)
  • (5, 6)
  • (6)
F - Make Pair

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

配点 : 500

問題文

2N 人の生徒が一列に並んでおり、左から順に生徒 1 , 生徒 2 , \ldots , 生徒 2N と番号が付いています。 2N 人の生徒はどの 2 人も互いに仲が良いか悪いかのどちらかであり、 具体的には 1\leq i\leq M について生徒 A_i と生徒 B_i の仲が良く、これら以外のどの 2 人も仲が悪いです。

先生は以下の操作を N 回繰り返して、 2 人組のペアを N 組作ることにしました。

  • 隣り合う 2 人であって仲が良いような 2 人をペアとして選び、列から抜く。
  • 抜けた 2 人が列の端でなかったならば、その後、列を詰める。すなわち、抜けた 2 人の左右にいた 2 人が新しく隣り合う。

このとき、 N 回の操作方法としてあり得るものの数を 998244353 で割った余りを求めてください。 ただし N 回の操作方法が異なるとは、ある 1\leq i\leq N が存在して、 i 回目の操作で 選ばれた 2 人組がペアとして異なることをいいます。

制約

  • 1 \leq N \leq 200
  • 0 \leq M \leq N(2N-1)
  • 1 \leq A_i < B_i \leq 2N
  • (A_i, B_i) はすべて異なる。
  • 入力は全て整数である。

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

出力

操作方法としてあり得るものの数を 998244353 で割った余りを出力せよ。


入力例 1

2 3
1 2
1 4
2 3

出力例 1

1

1 度目の操作で生徒 2 と生徒 3 を選び、 2 度目の操作で生徒 1 と生徒 4 を選ぶのが 唯一の操作方法です。 1 度目の操作で生徒 1 と生徒 2 を選ぶと、 生徒 3 と生徒 4 が残りますが、この 2 人は仲が悪いため 2 度目の操作でペアにすることができません。
よって、 1 を出力します。


入力例 2

2 2
1 2
3 4

出力例 2

2

1 度目の操作で生徒 1 と生徒 2 を選び、 2 度目の操作で生徒 3 と生徒 4 を選ぶのが 1 通り、 1 度目の操作で生徒 3 と生徒 4 を選び、 2 度目の操作で生徒 1 と生徒 2 を選ぶのが 1 通りであわせて 2 通りあります。 この 2 つが区別されることに注意してください。


入力例 3

2 2
1 3
2 4

出力例 3

0

1 度目の操作で選べるペアが無いため条件をみたす操作方法は無く、 0 を出力します。

Score : 500 points

Problem Statement

There are 2N students standing in a row, numbered 1, 2, \ldots, 2N from left to right. For all pairs of two students, they are on good or bad terms. Specifically, for each 1\leq i\leq M, Student A_i and Student B_i are on good terms; for the remaining pairs of two students, they are on bad terms.

The teacher is going to do the following operation N times to form N pairs of two students.

  • Choose two adjacent students who are on good terms, pair them, and remove them from the row.
  • If the removed students were not at an end of the row, close up the gap so that the two students who were to the left and right of the removed students are now adjacent.

Find the number, modulo 998244353, of possible ways to do the operation N times. Two ways to do the operation N times are considered different when there exists 1\leq i\leq N such that the pair of students chosen in the i-th operation is different in those two ways.

Constraints

  • 1 \leq N \leq 200
  • 0 \leq M \leq N(2N-1)
  • 1 \leq A_i < B_i \leq 2N
  • All pairs (A_i, B_i) are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

Print the number of possible ways to complete the procedure, modulo 998244353.


Sample Input 1

2 3
1 2
1 4
2 3

Sample Output 1

1

The only way to complete the procedure is to choose Students 2 and 3 in the first and Students 1 and 4 in the second. If Students 1 and 2 are chosen in the first operation, Students 3 and 4 will remain, who are on bad terms and thus cannot be paired in the second operation.
Thus, you should print 1.


Sample Input 2

2 2
1 2
3 4

Sample Output 2

2

There are two ways to complete the procedure: one way is to choose Students 1 and 2 in the first operation and Students 3 and 4 in the second, and the other way is to choose Students 3 and 4 in the first operation and Students 1 and 2 in the second. Note that these two ways are distinguished.


Sample Input 3

2 2
1 3
2 4

Sample Output 3

0

Since no pair can be chosen in the first operation, there is no way to complete the procedure, so you should print 0.

G - Groups

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

配点 : 600

問題文

正の整数 N,M が与えられます。k=1,\ldots,N のそれぞれについて、次の問題を解いてください。

  • 問題:1 から N の番号がついた N 人を、空でない k 個のグループに分けます。 ただし、番号を M で割ったあまりが等しい人は同じグループになることができません。
    そのようなグループ分けの方法は何通りありますか?
    答えは非常に大きくなる可能性があるので 998244353 で割ったあまりを求めてください。

ここで、グループ分けが異なるとは、一方では人 x,y が同じグループであり、他方では異なるグループであるような (x,y) が存在することを指すものとします。

制約

  • 2 \leq N \leq 5000
  • 2 \leq M \leq N
  • 入力は全て整数である

入力

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

N M

出力

N 行出力せよ。

i 行目には k=i の問題の答えを出力せよ。


入力例 1

4 2

出力例 1

0
2
4
1

番号を 2 で割ったあまりが等しい人、すなわち、人 13、人 24 を同じグループにすることはできません。

  • 1 個のグループにすることはできません。
  • 2 個のグループにする方法は \{\{1,2\},\{3,4\}\},\{\{1,4\},\{2,3\}\}2 通りです。
  • 3 個のグループにする方法は \{\{1,2\},\{3\},\{4\}\},\{\{1,4\},\{2\},\{3\}\},\{\{1\},\{2,3\},\{4\}\},\{\{1\},\{2\},\{3,4\}\}4 通りです。
  • 4 個のグループにする方法は \{\{1\},\{2\},\{3\},\{4\}\}1 通りです。

入力例 2

6 6

出力例 2

1
31
90
65
15
1

自由にグループ分けすることができます。


入力例 3

20 5

出力例 3

0
0
0
331776
207028224
204931064
814022582
544352515
755619435
401403040
323173195
538468102
309259764
722947327
162115584
10228144
423360
10960
160
1

答えを 998244353 で割ったあまりを求めてください。

Score : 600 points

Problem Statement

You are given positive integers N and M. For each k=1,\ldots,N, solve the following problem.

  • Problem: We will divide N people with ID numbers 1 through N into k non-empty groups. Here, people whose ID numbers are equal modulo M cannot belong to the same group.
    How many such ways are there to divide people into groups?
    Since the answer may be enormous, find it modulo 998244353.

Here, two ways to divide people into groups are considered different when there is a pair (x, y) such that Person x and Person y belong to the same group in one way but not in the other.

Constraints

  • 2 \leq N \leq 5000
  • 2 \leq M \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print N lines.

The i-th line should contain the answer for the problem with k=i.


Sample Input 1

4 2

Sample Output 1

0
2
4
1

People whose ID numbers are equal modulo 2 cannot belong to the same group. That is, Person 1 and Person 3 cannot belong to the same group, and neither can Person 2 and Person 4.

  • There is no way to divide the four into one group.
  • There are two ways to divide the four into two groups: \{\{1,2\},\{3,4\}\} and \{\{1,4\},\{2,3\}\}.
  • There are four ways to divide the four into three groups: \{\{1,2\},\{3\},\{4\}\}, \{\{1,4\},\{2\},\{3\}\}, \{\{1\},\{2,3\},\{4\}\}, and \{\{1\},\{2\},\{3,4\}\}.
  • There is one way to divide the four into four groups: \{\{1\},\{2\},\{3\},\{4\}\}.

Sample Input 2

6 6

Sample Output 2

1
31
90
65
15
1

You can divide them as you like.


Sample Input 3

20 5

Sample Output 3

0
0
0
331776
207028224
204931064
814022582
544352515
755619435
401403040
323173195
538468102
309259764
722947327
162115584
10228144
423360
10960
160
1

Be sure to find the answer modulo 998244353.

H - Snuketoon

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

配点 : 600

問題文

AtCoder 社が開発したゲーム『スヌケトゥーン』は、プレイヤーがすぬけ君を操作して水鉄砲から飛んでくる水を回避するゲームです。

ゲームのステージは無限に続く数直線からなり、ゲーム開始時点ですぬけ君は地点 0 にいます。
ゲーム開始直後から、すぬけ君は 1 秒ごとに「 1 小さい地点に移動」「 1 大きい地点に移動」「動かない」の 3 択から行動を選べます。より厳密には、すぬけ君がゲーム開始後 t(t \geq 0, t は整数) の時点で地点 p にいるとき、 t+1 秒の時点では地点 p-1 ・地点 p ・地点 p+13 ヵ所のいずれかに行くことができます。

すぬけ君は水鉄砲から発射された水を浴びるとダメージを受けてしまいます。水鉄砲は N 回発射されて、 i 回目の発射は T_i, D_i, X_i を用いて次のように表されます。

  • ゲーム開始から T_i 秒後に左右いずれかから水が発射されます。すぬけ君が T_i 秒の時点でいる地点を p としたとき、ダメージを受ける範囲および値は次の通りです。
    • D_i = 0 のとき、p \lt X_i の範囲にいると X_i - p のダメージを受ける。
    • D_i = 1 のとき、X_i \lt p の範囲にいると p - X_i のダメージを受ける。

プロゲーマーの高橋君は、攻略情報をツイートするために N 回目の水鉄砲の発射が終わった後のすぬけ君の合計ダメージを最小化することにしました。高橋君が合計ダメージを最小化するようにすぬけ君を操作したときの合計ダメージを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T_1 \lt T_2 \lt \dots \lt T_N \leq 10^9
  • D_i (1 \leq i \leq N)0 または 1
  • -10^9 \leq X_i \leq 10^9 (1 \leq i \leq N)
  • 入力は全て整数である。

入力

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

N
T_1 D_1 X_1
T_2 D_2 X_2
\vdots
T_N D_N X_N

出力

高橋君が合計ダメージを最小化するようにすぬけ君を操作したときの合計ダメージを出力せよ。


入力例 1

3
1 0 3
3 1 0
4 0 6

出力例 1

7

便宜上 t をゲーム開始から経過した秒数を表す変数とします。全ての水鉄砲の発射が終了するまでのすぬけ君の最適な動きは以下の通りです。

  • t = 0 のときすぬけ君は地点 0 にいます。すぬけ君は 1 大きい地点に移動します。
  • t = 1 のときすぬけ君は地点 1 にいて、 1 回目の水鉄砲の発射により 2 のダメージを受けます。すぬけ君は 1 小さい地点に移動します。
  • t = 2 のときすぬけ君は地点 0 にいます。すぬけ君は移動しません。
  • t = 3 のときすぬけ君は地点 0 にいて、 2 回目の水鉄砲の発射によるダメージを受けません。すぬけ君は 1 大きい地点に移動します。
  • t = 4 のときすぬけ君は地点 1 にいて、 3 回目の水鉄砲の発射により 5 のダメージを受けます。

このときすぬけ君は合計で 7 のダメージを受けるので、 7 を出力します。


入力例 2

3
1 0 1
6 1 1
8 0 -1

出力例 2

0

入力例 3

5
1 0 1000000000
2 1 -1000000000
3 0 1000000000
4 1 -1000000000
5 0 1000000000

出力例 3

4999999997

Score : 600 points

Problem Statement

In a game called Snuketoon, developed by AtCoder, Inc., the player plays as Snuke to dodge water from water guns.

The platform consists of an infinite number line, and Snuke is at the point 0 at the beginning of the game.
Starting from the beginning of the game, Snuke can make one of the three moves in each second: move 1 in the negative direction, move 1 in the positive direction, or remain still. More formally, if Snuke's position is p at t seconds (t \geq 0, t is an integer) from the beginning of the game, his position at t+1 seconds can be p-1, p, or p+1.

Snuke takes damage from getting soaked by water guns. The water guns shoot N times, and the i-th shot is represented by T_i, D_i, and X_i as follows.

  • At T_i seconds from the beginning of the game, water is shot from the left or right. Let p be Snuke's position at that time. He takes the following damage if he is in the following range.
    • When D_i = 0, he takes X_i - p points of damage if he is in the range p \lt X_i.
    • When D_i = 1, he takes p - X_i points of damage if he is in the range X_i \lt p.

Takahashi, a pro gamer, wants to minimize the total damage taken by Snuke after the end of the N-th shot, to post a tweet on this game. Find the total damage taken by Snuke when the game is played to minimize it.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq T_1 \lt T_2 \lt \dots \lt T_N \leq 10^9
  • D_i (1 \leq i \leq N) is 0 or 1.
  • -10^9 \leq X_i \leq 10^9 (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
T_1 D_1 X_1
T_2 D_2 X_2
\vdots
T_N D_N X_N

Output

Print the number of points of damage taken by Snuke when the game is played to minimize it.


Sample Input 1

3
1 0 3
3 1 0
4 0 6

Sample Output 1

7

For convenience, let t denote the number of seconds elapsed from the beginning of the game. The optimal course of movements for Snuke until all the shots are done is as follows.

  • When t = 0, Snuke is at the point 0. He moves 1 in the positive direction.
  • When t = 1, Snuke is at the point 1 and takes 2 points of damage from the first shot. He moves 1 in the negative direction.
  • When t = 2, Snuke is at the point 0. He remains still.
  • When t = 3, Snuke is at the point 0 and takes no damage from the second shot. He moves 1 in the positive direction.
  • When t = 4, Snuke is at the point 1 and takes 5 points of damage from the third shot.

Here, Snuke takes a total of 7 points of damage, so you should print 7.


Sample Input 2

3
1 0 1
6 1 1
8 0 -1

Sample Output 2

0

Sample Input 3

5
1 0 1000000000
2 1 -1000000000
3 0 1000000000
4 1 -1000000000
5 0 1000000000

Sample Output 3

4999999997