A - Attractions

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 9

問題文

高橋君は遊園地に行きました。
この遊園地のアトラクションに乗るには、以下の条件を全て満たさなければなりません。

  • 身長 H cm 以上
  • 体重 W kg 以下

高橋君は身長 h cm、体重 w kg です。
高橋君はアトラクションに乗ることができますか?

制約

  • 100 \leq H,h \leq 200
  • 50 \leq W,w \leq 100
  • 入力は全て整数

入力

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

H W
h w

出力

アトラクションに乗ることができるならば Yes と、できないならば No と出力せよ。


入力例 1

150 90
100 70

出力例 1

No

身長の条件を満たしていません。


入力例 2

123 85
199 99

出力例 2

No

体重の条件を満たしていません。


入力例 3

100 100
200 50

出力例 3

Yes

Score : 9 points

Problem Statement

Takahashi has come to an amusement park. To get on the ride in this park, one must meet all of the following restrictions.

  • Must be at least H centimeters tall.
  • Must weigh at most W kilograms.

Takahashi is h centimeters tall and weighs w kilograms. Can he get on the ride?

Constraints

  • 100 \leq H,h \leq 200
  • 50 \leq W,w \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
h w

Output

If Takahashi can get on the ride, print Yes; if he cannot, print No.


Sample Input 1

150 90
100 70

Sample Output 1

No

He does not meet the height restriction.


Sample Input 2

123 85
199 99

Sample Output 2

No

He does not meet the weight restriction.


Sample Input 3

100 100
200 50

Sample Output 3

Yes
B - Perforated Coin

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 8

問題文

日本で現在使われている硬貨には 1 円玉、5 円玉、10 円玉、50 円玉、100 円玉、500 円玉の 6 種類あります。
これらのうち、穴が開いているものは 5 円玉と 50 円玉の 2 種類です。

高橋君は日本のとある場所で店を経営しており、ある日、彼の店には N 人の客が訪れました。i \, (1 \leq i \leq N) 番目に訪れた客は A_i 円の品物に対して B_i 円を支払い、高橋くんはそれぞれの客に対し硬貨の総数が最小になるようにお釣りを出しました。

その日にお釣りとして用いられた硬貨のうち、穴が開いているものは何枚か求めてください。

ただし、高橋くんは 6 種類の硬貨をそれぞれ十分な枚数持っているとします。

制約

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

入力

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

N
A_1 B_1
\vdots
A_N B_N

出力

答えを出力せよ。


入力例 1

2
40 100
105 120

出力例 1

2

1 人目の客には 60 円のお釣りを出すので、硬貨の総数が最小となるように 10 円玉 1 枚と 50 円玉 1 枚を用います。

2 人目の客には 15 円のお釣りを出すので、硬貨の総数が最小となるように 5 円玉 1 枚と 10 円玉 1 枚を用います。

これらのうち穴が開いている硬貨は 2 枚です。


入力例 2

6
1 2
1 6
1 11
1 51
1 101
1 501

出力例 2

2

Score : 8 points

Problem Statement

There are six kinds of coins used in Japan now: 1-yen, 5-yen, 10-yen, 50-yen, 100-yen, and 500-yen coins. Two of them, 5-yen and 50-yen coins, have holes in them.

Takahashi runs a shop somewhere in Japan. One day, N customers visited his shop. The i-th customer paid B_i yen for a product worth A_i yen, and Takahashi gave each customer the change using the minimum number of coins.

Among the coins used as the changes on that day, how many had holes in them?

Here, assume that Takahashi had enough of each kind of coin.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq B_i \leq 10^3
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
\vdots
A_N B_N

Output

Print the answer.


Sample Input 1

2
40 100
105 120

Sample Output 1

2

To the 1-st customer, he gives 60 yen as the change, using one 10-yen coin and one 50-yen coin to minimize the number of coins.

To the 2-nd customer, he gives 15 yen as the change, using one 5-yen coin and one 10-yen coin to minimize the number of coins.

Among these coins, 2 of them have holes in them.


Sample Input 2

6
1 2
1 6
1 11
1 51
1 101
1 501

Sample Output 2

2
C - Fastest Answer

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 8

問題文

AtCoder 上で行われたあるコンテストにおいては、問題 A , B , C , D , E , F6 問の問題が出題され、全部で N 件の提出がありました。 N 件の提出はすべて異なる時刻に行われ、提出された時間が早いものから順に 1 , 2, \ldots , N と ID がつけられました。 また、全ての提出の結果は AC または WA であり、全ての問題について、その問題に対する提出であって、 結果が AC であるようなものが 1 件以上存在しました。 具体的には、 ID が i である提出は問題 P_i ( P_iA , B , C , D , E , F のいずれか ) に対する提出であり、その提出結果は V_i ( V_iAC , WA のいずれか ) でした。

問題 A から問題 F までの各問題について、その問題を最初に AC した提出の ID 、 すなわちその問題に対する提出で結果が AC であるようなものであって、 提出 ID が最小であるようなものを求めてください。

制約

  • 6 \leq N\leq 1000
  • P_iA , B , C , D , E , F のいずれか
  • V_iAC , WA のいずれか
  • 各問題について、結果が AC であるような提出が 1 つ以上存在する。

入力

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

N
P_1 V_1
P_2 V_2
:
P_N V_N

出力

6 行出力せよ。
1 行目には、問題 A を最初に AC した提出の ID 、
2 行目には、問題 B を最初に AC した提出の ID 、
:
6 行目には、問題 F を最初に AC した提出の ID を出力せよ。


入力例 1

8
A AC
B WA
C AC
B AC
A AC
D AC
E AC
F AC

出力例 1

1
4
3
6
7
8

問題 A , B , C に対する提出は最初の 5 つであり、それぞれについては以下の通りです。

  • ID 1 の提出は問題 AAC しており、これが問題 A の最初の AC です。
  • ID 2 の提出は問題 B に対する提出ですが、 WA のため、これは問題 B の最初の AC ではありません。
  • ID 3 の提出は問題 CAC しており、これが問題 C の最初の AC です。
  • ID 4 の提出は問題 BAC しており、これが問題 B の最初の AC です。
  • ID 5 の提出は問題 AAC していますが、 ID 1 の提出がすでに問題 AAC しているため、これは問題 A の最初の AC ではありません。

これらと問題 D , E , F に対する提出をあわせて、各問題を最初に AC した提出の ID はそれぞれ、 1 , 4 , 3 , 6 , 7 , 8 となります。

Score : 8 points

Problem Statement

In some contest hold on AtCoder, there were 6 problems: A , B , C , D , E , and F, and a total of N submissions. These N submissions, which were all made at distinct times, got assigned IDs 1, 2, \ldots, N in chronological order. The verdict of every submission is AC or WA, and for every problem, there was at least one submission with the verdict AC. Specifically, the submission with ID i is a submission to Problem P_i (where P_i is A , B , C , D , E , or F), with the verdict V_i (where V_i is AC or WA).

For each problem from A through F, find the ID of the first submission that got AC on that problem, that is, find the submission with the minimum ID among the ones made to that problem that got the verdict AC.

Constraints

  • 6 \leq N\leq 1000
  • P_i is A , B , C , D , E , or F.
  • V_i is AC or WA.
  • For each problem, there is at least one submission with the verdict AC.

Input

Input is given from Standard Input in the following format:

N
P_1 V_1
P_2 V_2
:
P_N V_N

Output

Print 6 lines.
The 1-st line should contain the ID of the first submission that got AC on Problem A.
The 2-nd line should contain the ID of the first submission that got AC on Problem B.
: The 6-th line should contain the ID of the first submission that got AC on Problem F.


Sample Input 1

8
A AC
B WA
C AC
B AC
A AC
D AC
E AC
F AC

Sample Output 1

1
4
3
6
7
8

The first five submissions were made to Problems A, B, and C, as follows.

  • The submission with the ID 1 got AC on Problem A, which was the first AC on this problem.
  • The submission with the ID 2 was made to Problem B but got WA, so this was not the first AC on this problem.
  • The submission with the ID 3 got AC on Problem C, which was the first AC on this problem.
  • The submission with the ID 4 got AC on Problem B, which was the first AC on this problem.
  • The submission with the ID 5 got AC on Problem A, but the submission with the ID 1 already got AC on this problem, so this was not the first AC on it.

Summing up these and the submissions to Problems D, E, F, the IDs of the first submissions that got AC on the respective problems are 1, 4, 3, 6, 7, 8.

D - Examination

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

問題文

AtCoder 高校には N 人の生徒が在籍し、生徒 1 ,生徒 2 , \ldots ,生徒 N と番号付けられています。 ある日、全員が数学と英語のテストを受け、 生徒 i (1 \leq i \leq N) は数学で A_i 点、英語で B_i 点を取りました。AtCoder 高校では、次のようにして成績の順位がつけられます。

  • 数学と英語の合計点が高い生徒が上位
  • 合計点が等しいとき、数学の点が高い生徒が上位
  • 合計点および数学の点が等しいとき、番号が小さい生徒が上位

成績が上位の方から順に、N 人の生徒番号を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_i \leq 10^9
  • 入力は全て整数である。

入力

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

以下の形式で、1 行に出力せよ。

X_1 X_2 \ldots X_N

ただし、X_i は、N 人の生徒を成績が上位の方から順に並べたとき、i 番目にくる生徒の番号である。


入力例 1

5
5 10 10 12 5
10 10 5 0 10

出力例 1

2 3 1 5 4

順位は次のように判定されます。

  • 数学と英語の合計点は順に、15 , 20 , 15 , 12 , 15 であるので、生徒 2 が成績順で 1 番目に、生徒 1 , 3 , 5 がそれぞれ 2 , 3 , 4 番目のいずれかに、生徒 45 番目に来ます。
  • 生徒 1 , 3 , 5 は数学と英語の合計点が同じであり、数学の点数は順に 5 , 10 , 5 であるので、生徒 3 が成績順で 2 番目に、生徒 1 , 5 がそれぞれ 3 , 4 番目のいずれかに来ます。
  • 生徒 1 , 5 は数学と英語の合計点および数学の点が同じであるので、番号の小さい生徒 1 が成績順で 3 番目に、大きい生徒 54 番目に来ます。

よって、成績が上位の方から順に、生徒 2 , 3 , 1 , 5 , 4 となります。


入力例 2

2
0 1000000000
0 1000000000

出力例 2

2 1

Score : 7 points

Problem Statement

There are N students in AtCoder High School, given student IDs and called Student 1, Student 2, \ldots, Student N. One day, all of them took tests in math and English, and Student i (1 \leq i \leq N) scored A_i in math and B_i in English. In this school, the students are ranked as follows.

  • A student with a higher total score in math and English ranks higher.
  • When students have the same total score, a student with a higher score in math ranks higher.
  • When students have both the same total score and the same score in math, a student with a smaller student ID ranks higher.

Print the student IDs of the N students in descending order of rank.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Print a line in the following format:

X_1 X_2 \ldots X_N

Here, X_i should be the student ID of the student who ranks i-th from the top.


Sample Input 1

5
5 10 10 12 5
10 10 5 0 10

Sample Output 1

2 3 1 5 4

The students are ranked as follows.

  • The respective students' total scores in math and English are 15, 20, 15, 12, 15, so Student 2 comes 1-st, Students 1, 3, 5 come 2-nd, 3-rd, 4-th in some order, and Student 4 comes 5-th.
  • Students 1, 3, 5, with the same total score in math and English, scored 5, 10, 5 in math, respectively, so Student 3 comes 2-nd, and Students 1, 5 come 3-rd, 4-th in some order.
  • Since Students 1 and 5 have both the same total score and the same score in math, Student 1, with the smaller student ID, comes 3-rd, and Student 5, with the larger student ID, comes 4-th.

Therefore, the students are ranked in the order 2, 3, 1, 5, 4 from top to bottom.


Sample Input 2

2
0 1000000000
0 1000000000

Sample Output 2

2 1
E - Keyboard

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

問題文

高橋君は整数 N をキーボードを使って入力しようと考えています。
高橋君は各桁の数字のうち、1 , 2 , 3 , 4 , 5 は左手を用いて、6 , 7 , 8 , 9 , 0 は右手を用いて入力します。
高橋君が各桁の数字を打ち込むのにかかる時間は以下の通りです。

  • 打ち込みたい数字が整数の先頭の桁であるとき、数字によらず 500ms かかる。
  • そうでなく、打ち込みたい数字が直前の数字と同じであるとき、301ms かかる。
  • 上の 2 つに該当せず、打ち込みたい数字が直前の数字を打ち込むのに用いた手と同一の手を用いるとき、210ms かかる。
  • それ以外のとき、100ms かかる。

整数を入力するのにかかる時間は各桁を打ち込むのにかかる時間の総和です。高橋君が整数 N を入力するのにかかる時間が何 ms であるか求めてください。

制約

  • 1 \leq N < 10^{200000}
  • N の先頭の桁は 0 ではない。
  • N は整数

入力

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

N

出力

答えを出力せよ。


入力例 1

1120

出力例 1

1111
  • まず、先頭の桁 1 を打ち込むのに 500ms かかります。
  • 次の桁も 1 であり、これは直前の数字と同じ数字であるため、これを打ち込むのに 301ms かかります。
  • その次の桁は 2 であり、これは直前の数字と異なる数字ですが、どちらも左手を用いて打ち込む数字であるため、これを打ち込むのに 210ms かかります。
  • 最後の桁は 0 であり、これは直前の数字と異なり、打ち込むのに用いる手も異なるため、これを打ち込むのに 100ms かかります。

よって、1120 を打ち込むのには合計で 500+301+210+100=1111ms かかります。


入力例 2

8

出力例 2

500

N1 桁のみからなるため、打ち込むのにかかる時間は先頭の桁の数字を打ち込むのにかかる時間と等しく、500ms となります。


入力例 3

12345678901234567890

出力例 3

4160

Score : 7 points

Problem Statement

Takahashi will enter an integer N using a keyboard.
When entering the digits, he will use the left hand for 1, 2, 3, 4, 5, and the right hand for 6, 7, 8, 9, 0.
The time it takes for him to enter each digit is as follows.

  • If the digit to enter is the top digit of the integer, it takes 500 milliseconds, regardless of what that digit is.
  • Otherwise, if the digit to enter is the same as the previous digit, it takes 301 milliseconds.
  • If none of the above applies, and the digit to enter is entered by the same hand as the hand used to enter the previous digit, it takes 210 milliseconds.
  • If none of the above applies, it takes 100 milliseconds.

The total time to enter the integer is the sum of the times to enter the respective digits. Print the time it takes for Takahashi to enter the integer N, in milliseconds.

Constraints

  • 1 \leq N < 10^{200000}
  • The top digit of N is not 0.
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

1120

Sample Output 1

1111
  • First, it takes 500 milliseconds to enter the top digit 1.
  • The next digit is 1, which is the same digit as the previous, so it takes 301 milliseconds to enter it.
  • The next digit is 2, which is a different digit from the previous but entered by the left hand again, so it takes 210 milliseconds to enter it.
  • The last digit is 0, which is a different digit from the previous and entered by the other hand, so it takes 100 milliseconds to enter it.

Therefore, it takes a total of 500+301+210+100=1111 milliseconds to enter 1120.


Sample Input 2

8

Sample Output 2

500

N has just one digit, so the time it takes to enter it is equal to the time it takes to enter the top digit, which is 500 milliseconds.


Sample Input 3

12345678901234567890

Sample Output 3

4160
F - Like As Shogi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

問題文

9 行、横 9 列からなるマス目があり、上から i 行目、左から j 列目のマスを (i,j) と表すことにします。

はじめ、マス (A,B) にコマが一つ置かれています。その他のマスにはコマは置かれていません。

コマは、自身が存在するマスの 8 近傍に位置するマスのうちいくつかに一手で移動することができます。移動することのできるマスの集合の具体的な情報は長さがそれぞれ 3 である 3 つの文字列 S_1,S_2,S_3 によって表され、コマがあるマス (x,y) に存在するとき、

  • S_ij 文字目が # ならマス (x+i-2,y+j-2) に一手で移動することが可能
  • S_ij 文字目が . ならマス (x+i-2,y+j-2) に一手で移動することは不可能

です。ただし、マス目の外に出るような移動をすることはできません。

0 回以上の移動によってコマが辿り着けるマスの個数を求めてください。

制約

  • 1 \leq A,B \leq 9
  • S_1,S_2,S_3 はそれぞれ #. のみからなる長さ 3 の文字列
  • S_22 文字目は .
  • A,B は整数

入力

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

A B
S_1
S_2
S_3

出力

0 回以上の移動によってコマが辿り着けるマスの個数を出力せよ。


入力例 1

2 2
###
#..
...

出力例 1

5

コマはマス (1,1)、マス (1,2)、マス (1,3)、マス (2,1)、マス (2,2) の計 5 マスに辿り着くことができます。


入力例 2

5 4
###
#.#
###

出力例 2

81

コマは全てのマスに辿り着くことができます。


入力例 3

9 9
...
...
..#

出力例 3

1

はじめに置かれているマス (9,9) から他のマスに移動することができません。

Score : 7 points

Problem Statement

We have a grid with 9 rows arranged vertically and 9 columns arranged horizontally. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.

Initially, there is a piece on (A, B), and no piece on the other squares.

In one move, the piece can move to some of the squares vertically, horizontally, or diagonally adjacent to the square it is on now. The specific set of squares that the piece can move to is described by three strings S_1, S_2, S_3 of length 3 each. When the piece is on some square (x, y),

  • if the j-th character of S_i is #, it can move to (x+i-2,y+j-2) in one move;
  • if the j-th character of S_i is ., it cannot move to (x+i-2,y+j-2) in one move.

Here, it is not allowed to exit the grid.

Find the number of squares that the piece can reach by zero or more moves.

Constraints

  • 1 \leq A,B \leq 9
  • Each of S_1,S_2,S_3 is a string of length 3 consisting of # and ..
  • The 2-nd character of S_2 is ..
  • A,B are integers.

Input

Input is given from Standard Input in the following format:

A B
S_1
S_2
S_3

Output

Print the number of squares that the piece can reach by zero or more moves.


Sample Input 1

2 2
###
#..
...

Sample Output 1

5

The piece can reach the following five squares: (1,1), (1,2), (1,3), (2,1), and (2,2).


Sample Input 2

5 4
###
#.#
###

Sample Output 2

81

The piece can reach all squares.


Sample Input 3

9 9
...
...
..#

Sample Output 3

1

The piece cannot move from the starting square (9, 9).

G - Connectivity

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

N 頂点からなる無向グラフがあり、 最初、どの 2 つの頂点も辺で結ばれていません。
Q 個のクエリが与えられるので与えられた順番に処理してください。
クエリは次の 2 種類のいずれかです。

  • 1 u v : 頂点 u と頂点 v を直接結ぶ辺が無いならばその間に無向辺を追加する。頂点 u と頂点 v を直接結ぶ辺があるならば、その辺を削除する。

  • 2 u v : 頂点 u から頂点 v へ辺を辿って移動することができるならば Yes を、そうでないならば No を出力する。

制約

  • 2 \leq N \leq 100
  • 1 \leq Q \leq 10000
  • 1 \leq u,v \leq N
  • u \neq v
  • 2 種類目のクエリが 1 回以上与えられる。
  • 入力は全て整数である。

入力

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

N Q
query_1
query_2
\vdots
query_Q

2 行目から Q+1 行目の各 query_i は次のいずれかの形で与えられる。

1 u v
2 u v

出力

2 種類目のクエリに対する答えを与えられた順に改行区切りで出力せよ。


入力例 1

3 5
1 1 2
1 3 2
2 1 3
1 2 1
2 1 3

出力例 1

Yes
No
  • 1 個目のクエリの直前の時点で、頂点 1 と頂点 2 は辺で結ばれていないため、辺を追加します。
  • 2 個目のクエリの直前の時点で、頂点 3 と頂点 2 は辺で結ばれていないため、辺を追加します。
  • 3 個目のクエリの時点で、1 , 2 個目のクエリで追加された辺を辿って、頂点 1 \to 頂点 2 \to 頂点 3 と移動できるため、Yes を出力します。
  • 4 個目のクエリの直前の時点で、頂点 2 と頂点 3 を直接結ぶ辺は存在するため、これを削除します。
  • 5 個目のクエリの時点では、頂点 1 と頂点 2 を結ぶ辺しか存在せず、頂点 1 から頂点 3 へは辿り着けないため、 No を出力します。

Score : 6 points

Problem Statement

There is an undirected graph with N vertices. Initially, no two vertices are connected by an edge.
Process the Q queries in the order they are given.
There are two kinds of queries shown below.

  • 1 u v: If there is no edge directly connecting Vertex u and Vertex v, add an undirected edge connecting them; if there is such an edge, delete it.

  • 1 u v: If it is possible to get from Vertex u to Vertex v by traversing edges, print Yes; otherwise, print No.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq Q \leq 10000
  • 1 \leq u,v \leq N
  • u \neq v
  • There is at least one query of the second kind.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
query_1
query_2
\vdots
query_Q

Each query_i in the 2-nd through (Q+1)-th lines is in one of the following formats:

1 u v
2 u v

Output

Print the responses to the queries of the second kind, in the order they are given, separated by newlines.


Sample Input 1

3 5
1 1 2
1 3 2
2 1 3
1 2 1
2 1 3

Sample Output 1

Yes
No
  • Just before the 1-st query, Vertex 1 and Vertex 2 are not connected by an edge, so we add an edge.
  • Just before the 2-nd query, Vertex 3 and Vertex 2 are not connected by an edge, so we add an edge.
  • At the time of the 3-rd query, we can traverse the edges added in the 1-st and 2-nd queries to go Vertex 1 \to Vertex 2 \to Vertex 3, so we print Yes.
  • Just before the 4-th query, Vertex 3 and Vertex 2 are directly connected by an edge, so we delete it.
  • At the time of the 5-th query, only the edge connecting Vertex 1 and Vertex 2 exists, which does not enable us to get from Vertex 1 to Vertex 3, so we print No.
H - Longest Non-common Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

英小文字からなる 2 つの文字列 S,T が与えられます。
S の連続とは限らない部分列 S'T の連続とは限らない部分列 T' を、 |S'| = |T'| となるように取ります。
このとき、 S'i 文字目と T'i 文字目が異なる i (1 \leq i \leq |S'|) の個数としてあり得る最大値を答えてください。

制約

  • S, T は英小文字からなる文字列である。
  • 1 \leq |S|, |T| \leq 5000

入力

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

S
T

出力

S'i 文字目と T'i 文字目が異なる i の個数の最大値を出力せよ。


入力例 1

abc
aba

出力例 1

2

文字列 S' として ab を、 T' として ba を取ると、S'i 文字目と T'i 文字目が異なる i (1 \leq i \leq |S'|) の個数は 2 個になり、これが最大値です。


入力例 2

aaaaaa
a

出力例 2

0

入力例 3

abra
cadabra

出力例 3

4

Score : 6 points

Problem Statement

Given are two strings S and T consisting of lowercase English letters.
Let us choose a (not necessarily contiguous) subsequence S' of S and a (not necessarily contiguous) subsequence T' of T so that |S'| = |T'|.
Here, find the maximum possible number of indices i (1 \leq i \leq |S'|) such that the i-th character of S' and the i-th character of T' are different.

Constraints

  • S and T are strings consisting of lowercase English letters.
  • 1 \leq |S|, |T| \leq 5000

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the maximum possible number of indices i (1 \leq i \leq |S'|) such that the i-th character of S' and the i-th character of T' are different.


Sample Input 1

abc
aba

Sample Output 1

2

If we choose ab as the string S' and ba as T', there are two indices i (1 \leq i \leq |S'|) such that the i-th character of S' and the i-th character of T' are different, which is the maximum possible.


Sample Input 2

aaaaaa
a

Sample Output 2

0

Sample Input 3

abra
cadabra

Sample Output 3

4
I - Direct Elevator

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 6

問題文

N 階建てのビルがあり、階段と M 台の直通エレベーターが設置されています。

階段を用いると、任意の i \, (1 \leq i \leq N - 1) について、i 階と i+1 階の間を双方向に 1 分で移動することができます。
また、各 i \, (1 \leq i \leq M) について、i 台目のエレベーターを用いると、A_i 階と B_i 階の間を双方向に C_i 分で移動することができます。

1 階から N 階まで移動するために最短で何分かかるか求めてください。

制約

  • 2 \leq N \leq 10^{18}
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \lt B_i \leq N
  • 1 \leq C_i \leq 10^{18}
  • 入力は全て整数である。

入力

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

N M
A_1 B_1 C_1
\vdots
A_M B_M C_M

出力

答えの値を出力せよ。


入力例 1

10 2
2 5 1
4 10 3

出力例 1

6

次のように移動するのが最適です。

  • 1 階から 2 階まで階段を使って 1 分で移動する。
  • 2 階から 5 階まで 1 台目のエレベーターを使って 1 分で移動する。
  • 5 階から 4 階まで階段を使って 1 分で移動する。
  • 4 階から 10 階まで 2 台目のエレベーターを使って 3 分で移動する。

入力例 2

1000000000000000000 1
1 1000000000000000000 1000000000000000000

出力例 2

999999999999999999

Score : 6 points

Problem Statement

There is an N-story building, with stairs and M direct elevators. (Here, the lowest floor is called the 1-st floor.)

Using stairs, it is possible to travel between the i-th and (i+1)-th floors in either direction in 1 minute, for every i \, (1 \leq i \leq N - 1).
Additionally, using the i-th elevator, it is possible to travel between the A_i-th and B_i-th floors in either direction in C_i minutes, for each i \, (1 \leq i \leq M).

Find the minimum number of minutes needed to get from the 1-st floor to the N-th floor.

Constraints

  • 2 \leq N \leq 10^{18}
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \lt B_i \leq N
  • 1 \leq C_i \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
\vdots
A_M B_M C_M

Output

Print the value representing the answer.


Sample Input 1

10 2
2 5 1
4 10 3

Sample Output 1

6

The optimal way to get there is as follows.

  • Use stairs to get from the 1-st floor to the 2-nd floor in 1 minute.
  • Use the 1-st elevator to get from the 2-nd floor to the 5-th floor in 1 minute.
  • Use stairs to get from the 5-th floor to the 4-th floor in 1 minute.
  • Use the 2-nd elevator to get from the 4-th floor to the 10-th floor in 3 minutes.

Sample Input 2

1000000000000000000 1
1 1000000000000000000 1000000000000000000

Sample Output 2

999999999999999999
J - Rotate and Reverse

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

NN 列のマス目があります。最初は全てのマスが白く塗られています。

Q 個のクエリ \text{Query}_1,\text{Query}_2,\ldots,\text{Query}_Q が与えられるので、順に処理してください。

クエリには 3 種類あり、入力形式とクエリの内容は以下の通りです。

  • 1 x y : 上から x 行目、左から y 列目のマスが白なら黒く、黒なら白く塗り替える。
  • 2 c : グリッド全体を、cA なら時計回りに、B なら反時計回りに 90 度回す。
  • 3 c : グリッド全体を、cA なら上下反転、B なら左右反転させる。

全てのクエリを処理した後のマス目の状態を求めてください。

制約

  • 1 \leq N \leq 300
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq x,y \leq N
  • cAB のいずれか
  • 各クエリは問題文中の 3 種類のいずれか

入力

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

N Q
\text{Query}_1
\text{Query}_2
\vdots
\text{Query}_Q

出力

N 行出力せよ。

i\,(1 \leq i \leq N) 行目には、01 のみからなる長さ N の文字列 S を出力せよ。Sj 文字目は、最終的なマス目の上から i 行目、左から j 列目のマスの色を表し、0 は白、1 は黒に対応する。


入力例 1

3 2
1 1 1
2 A

出力例 1

001
000
000

上から 1 行目、左から 1 列目のマスを黒く塗り替えた後、グリッド全体を時計回りに 90 度回転させます。


入力例 2

3 3
1 1 1
3 A
3 B

出力例 2

000
000
001

入力例 3

4 8
2 A
1 4 2
2 A
1 2 2
3 B
3 B
3 A
1 3 1

出力例 3

0000
0000
0100
0000

Score : 6 points

Problem Statement

We have a grid with N rows and N columns. Initially, all squares are painted white.

Process Q queries \text{Query}_1,\text{Query}_2,\ldots,\text{Query}_Q in the order they are given.

There are three kinds of queries, with the following input formats and specifications.

  • 1 x y : If the square at the x-th row from the top and y-th column from the left is white, repaint it black; if it is black, repaint it white.

  • 2 c : Rotate the whole grid 90 degrees clockwise if c is A and counter-clockwise if c is B.

  • 3 c : Flip the whole grid vertically if c is A and horizontally if c is B.

Find the colors of the squares after processing all queries.

Constraints

  • 1 \leq N \leq 300
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq x,y \leq N
  • c is A or B.
  • Each query is of one of the three kinds described in Problem Statement.

Input

Input is given from Standard Input in the following format:

N Q
\text{Query}_1
\text{Query}_2
\vdots
\text{Query}_Q

Output

Print N lines.

The i-th line (1 \leq i \leq N) should contain a string of length N consisting of 0 and 1, where the j-th character of S represents the color of the square at the i-th row from the top and j-th column from the left in the final grid; use 0 for white and 1 for black.


Sample Input 1

3 2
1 1 1
2 A

Sample Output 1

001
000
000

We repaint the square at the 1-st row from the top and 1-st column from the left and then rotate the whole grid 90 degrees clockwise.


Sample Input 2

3 3
1 1 1
3 A
3 B

Sample Output 2

000
000
001

Sample Input 3

4 8
2 A
1 4 2
2 A
1 2 2
3 B
3 B
3 A
1 3 1

Sample Output 3

0000
0000
0100
0000
K - Gas Station

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 6

問題文

高橋王国は N 個の街と M 本の道路からなります。

街には 1,2,\ldots,N の番号がついており、i\,(1 \leq i \leq M) 本目の道路は街 u_i と街 v_i を双方向に結んでいます。どの街からどの街へもいくつかの道路をたどることで到達できることが保証されます。

また、街 a_1,a_2,\ldots,a_K にはガソリンスタンドがあります。

Q 個のクエリに答えてください。i\,(1 \leq i \leq Q) 番目のクエリは以下の内容です。

  • s_i を出発し、ガソリンスタンドのある街を 1 つ以上通った後、街 t_i に行くとき、道を通る回数の最小値を求めよ。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M,Q \leq 2 \times 10^5
  • 1 \leq K \leq \min(N-1,20)
  • 1 \leq a_i \leq N
  • a_i \neq a_j\,(i \neq j)
  • 1 \leq u_i < v_i \leq N
  • (u_i,v_i) \neq (u_j,v_j) \,(i \neq j)
  • 1 \leq s_i , t_i \leq N
  • s_i,t_i にはガソリンスタンドがない
  • どの街からどの街へもいくつかの道路をたどることで到達できる
  • 入力は全て整数

入力

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

N M Q K
a_1 a_2 \ldots a_K
u_1 v_1
u_2 v_2
\vdots
u_M v_M
s_1 t_1
s_2 t_2
\vdots
s_Q t_Q

出力

Q 行出力せよ。i\,(1 \leq i \leq Q) 行目には、i 番目のクエリに対する答えを出力せよ。


入力例 1

3 2 1 1
1
1 2
2 3
3 2

出力例 1

3

3 を出発し、街 2、街 1、街 2 の順に移動すると移動回数は 3 回です。これより少ない移動回数は実現できないので 3 を出力します。


入力例 2

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

出力例 2

2
3

Score : 6 points

Problem Statement

The Kingdom of Takahashi has N towns and M roads.

The towns are numbered 1,2,\ldots,N, and the i-th road (1 \leq i \leq M) connects Town u_i and Town v_i bidirectionally. It is guaranteed that one can get from every town to every town by traversing some roads.

Additionally, there are gas stations in Towns a_1,a_2,\ldots,a_K.

Answer Q queries. The i-th query (1 \leq i \leq Q) is as follows.

  • Find the minimum number of times one needs to traverse a road when getting from Town s_i to Town t_i via a town with a gas station.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M,Q \leq 2 \times 10^5
  • 1 \leq K \leq \min(N-1,20)
  • 1 \leq a_i \leq N
  • a_i \neq a_j\,(i \neq j)
  • 1 \leq u_i < v_i \leq N
  • (u_i,v_i) \neq (u_j,v_j) \,(i \neq j)
  • 1 \leq s_i , t_i \leq N
  • Nither s_i nor t_i has a gas station.
  • It is possible to get from every town to every town by traversing some roads.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q K
a_1 a_2 \ldots a_K
u_1 v_1
u_2 v_2
\vdots
u_M v_M
s_1 t_1
s_2 t_2
\vdots
s_Q t_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th query.


Sample Input 1

3 2 1 1
1
1 2
2 3
3 2

Sample Output 1

3

After starting in Town 3, we can go to Town 2, Town 1, Town 2 in this order to complete the trip with three moves. It is impossible to do it with fewer moves, so we print 3.


Sample Input 2

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

Sample Output 2

2
3
L - Honest students

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

N 人の生徒がテストを受け、それぞれ 0 以上 P 以下の整数の得点を得ました。各生徒の得た得点は相異なり、得点の高い順に順位付けされています。

高橋君がそれぞれ生徒に得点を聞いたところ、i 位の生徒は自分の得点を A_i 点だと教えてくれました。
しかし、生徒たちのうち何人かは嘘をついているかもしれません。

少なくとも何人の生徒が嘘をついているか求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq P \leq 10^9
  • 0 \leq A_i \leq P
  • 入力に含まれる値は全て整数である

入力

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

N P
A_1 A_2 \ldots A_N

出力

嘘をついている生徒の人数として考えられる最小値を出力せよ。


入力例 1

5 10
7 5 10 3 2

出力例 1

1

3 位の生徒だけが嘘をついており、3 位の生徒の本当の得点が 4 点だった場合は得点と順位が符合します。

生徒が全員本当のことを言っていたとすると、3 位の生徒の得点が 2 位の生徒の得点より高くなってしまうため、そのようなことはありません。

よって嘘を付いている生徒の人数として考えられる最小値は 1 です。


入力例 2

5 10
9 8 7 7 5

出力例 2

1

生徒たちの本当の得点は相異なります。


入力例 3

11 1000000000
0 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000

出力例 3

10

Score : 6 points

Problem Statement

N students took a test, each getting a score of a positive integer between 0 and P (inclusive). All students got different scores, and they were ranked in descending order of score.

Takahashi asked each student their score, and the student ranked i-th told him they scored A_i.
However, some of the students might have told a lie.

Find the minimum possible number of students who told a lie.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq P \leq 10^9
  • 0 \leq A_i \leq P
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N P
A_1 A_2 \ldots A_N

Output

Print the minimum possible number of students who told a lie.


Sample Input 1

5 10
7 5 10 3 2

Sample Output 1

1

If just the student ranked 3-rd told a lie when the true score was 4, the students' scores and ranks would be consistent.

If all students had told the truth, the student ranked 3-rd would have scored higher than the student ranked 2-nd, which could not happen.

Therefore, the minimum possible number of students who told a lie is 1.


Sample Input 2

5 10
9 8 7 7 5

Sample Output 2

1

Note that the true scores of the students are distinct.


Sample Input 3

11 1000000000
0 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000

Sample Output 3

10
M - Deadnames

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 6

問題文

1 から N までの番号が振られた、N 人の人がいます。はじめ、人 i\ (1 \leq i \leq N) の名前は S_i です。

i=1,2,\ldots,Q について順に以下のクエリを処理したあと、最終的な各人の名前を出力してください。

  • N 人を名前の辞書順で小さい順(名前が同じ場合は番号の若い順)に整列させ、前から X_i 人目の人の名前を T_i に変える。

制約

  • 1 \leq N,Q \leq 10^5
  • S_i は英小文字のみからなる長さ 1 以上 5 以下の文字列
  • 1 \leq X_i \leq N
  • T_i は英小文字のみからなる長さ 1 以上 5 以下の文字列
  • N, Q, X_i は整数

入力

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

N Q
S_1 S_2 \ldots S_N
X_1 T_1
X_2 T_2
\vdots
X_Q T_Q

出力

クエリをすべて順に処理したあとの各人の名前を、空白区切りで番号の昇順に出力せよ。


入力例 1

3 3
taro jiro sabro
1 taro
2 jiro
3 taro

出力例 1

jiro taro sabro

はじめ、N=3 人の名前は番号順に taro, jiro, sabro です。

  • i=1 のクエリにおいて、各人を名前の辞書順で小さい順に整列させると人 2、人 3、人 1 となります。前から X_1=1 人目である人 2 の名前が T_1 に変更され、3 人の名前は番号順に taro, taro, sabro となります。
  • i=2 のクエリにおいて、各人を名前の辞書順で小さい順に整列させると人 3、人 1、人 2 となります。前から X_2=2 人目である人 1 の名前が T_2 に変更され、3 人の名前は番号順に jiro, taro, sabro となります。
  • i=3 のクエリにおいて、各人を名前の辞書順で小さい順に整列させると人 1、人 3、人 2 となります。前から X_3=3 人目である人 2 の名前が T_3 に変更され、3 人の名前は番号順に jiro, taro, sabro となります。

この入出力例における i=3 のクエリのように、クエリによる変更の前後で X_i 人目の名前に変化がない場合も存在することに注意してください。


入力例 2

10 10
yo gaaj uab y reah y dyg ix rwq p
10 tvbvr
8 ax
7 xr
9 vw
10 ky
10 wlxtw
2 guoux
10 wbct
9 zbuxx
5 gq

出力例 2

tvbvr gaaj ax zbuxx reah gq guoux ix wbct p

Score : 6 points

Problem Statement

There are N people, with IDs from 1 to N. Initially, Person i (1 \leq i \leq N) is named S_i.

Print the name of each person after processing the following query for each i=1, 2, \ldots, Q in order.

  • Make the people stand in a row in lexicographically ascending order of name (in case of the same name, ascending order of ID), and rename the X_i-th person from the front to T_i.

Constraints

  • 1 \leq N,Q \leq 10^5
  • S_i is a string of length between 1 and 5 (inclusive) consisting of lowercase English letters.
  • 1 \leq X_i \leq N
  • T_i is a string of length between 1 and 5 (inclusive) consisting of lowercase English letters.
  • N, Q, and X_i are integers.

Input

Input is given from Standard Input in the following format:

N Q
S_1 S_2 \ldots S_N
X_1 T_1
X_2 T_2
\vdots
X_Q T_Q

Output

Print the names of the N people in ascending order of ID, separated by spaces, after processing all queries in order.


Sample Input 1

3 3
taro jiro sabro
1 taro
2 jiro
3 taro

Sample Output 1

jiro taro sabro

Initially, the N=3 people are named taro, jiro, sabro in ascending order of ID.

  • In the query with i=1, when they stand in a row in lexicographically ascending order of name, they are in the order 2, 3, 1. The X_1=1-st person from the front, Person 2, is renamed to T_1. Now, the people are named taro, taro, sabro in ascending order of ID.
  • In the query with i=2, when they stand in a row in lexicographically ascending order of name, they are in the order 3, 1, 2. The X_2=2-nd person from the front, Person 1, is renamed to T_2. Now, the people are named jiro, taro, sabro in ascending order of ID.
  • In the query with i=3, when they stand in a row in lexicographically ascending order of name, they are in the order 1, 3, 2. The X_3=3-rd person from the front, Person 2, is renamed to T_3. Now, the people are named jiro, taro, sabro in ascending order of ID.

Note that, as seen in the query with i=3, the name of the X_i-th person may remain unchanged in a query.


Sample Input 2

10 10
yo gaaj uab y reah y dyg ix rwq p
10 tvbvr
8 ax
7 xr
9 vw
10 ky
10 wlxtw
2 guoux
10 wbct
9 zbuxx
5 gq

Sample Output 2

tvbvr gaaj ax zbuxx reah gq guoux ix wbct p
N - Intersection

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

2 次元平面上の凸多角形 S, T が与えられます。

SN 個の頂点からなり、頂点の座標は反時計回りに (a_1, b_1), \dots, (a_N, b_N) です。
TM 個の頂点からなり、頂点の座標は反時計回りに (c_1, d_1), \dots, (c_M, d_M) です。

S, T の共通部分の面積を求めてください。

制約

  • 3 \leq N, M \leq 10^3
  • 0 \leq a_i, b_i \leq 3 \times 10^4 \, (1 \leq i \leq N)
  • 0 \leq c_i, d_i \leq 3 \times 10^4 \, (1 \leq i \leq M)
  • (a_1, b_1), \dots, (a_N, b_N)S の頂点を反時計回りに並べたものである。
  • (c_1, d_1), \dots, (c_M, d_M)T の頂点を反時計回りに並べたものである。
  • 入力は全て整数である。

入力

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

N M
a_1 b_1 
\vdots
a_N b_N 
c_1 d_1 
\vdots
c_M d_M 

出力

答えを出力せよ。 なお、想定解答との絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。


入力例 1

3 4
3 0
3 4
0 3
1 2
4 2
5 4
4 5

出力例 1

2.00000000000000000000

S, T の共通部分は下図の灰色部分であり、その面積は 2 です。


入力例 2

3 3
0 0
3 0
0 3
3 3
0 3
3 0

出力例 2

0.00000000000000000000

入力例 3

3 3
1 3
0 2
2 0
3 1
2 3
0 1

出力例 3

0.79166666666666662966

Score : 6 points

Problem Statement

Given are convex polygons S and T in a two-dimensional plane.

S has N vertices, with the coordinates (a_1, b_1), \dots, (a_N, b_N) in counter-clockwise order.
T has M vertices, with the coordinates (c_1, d_1), \dots, (c_M, d_M) in counter-clockwise order.

Find the area of the intersection of S and T.

Constraints

  • 3 \leq N, M \leq 10^3
  • 0 \leq a_i, b_i \leq 3 \times 10^4 \, (1 \leq i \leq N)
  • 0 \leq c_i, d_i \leq 3 \times 10^4 \, (1 \leq i \leq M)
  • (a_1, b_1), \dots, (a_N, b_N) are the vertices of S listed in counter-clockwise order.
  • (c_1, d_1), \dots, (c_M, d_M) are the vertices of T listed in counter-clockwise order.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1 
\vdots
a_N b_N 
c_1 d_1 
\vdots
c_M d_M 

Output

Print the answer. Your output will be considered correct when the absolute or relative error from our answer is at most 10^{-6}.


Sample Input 1

3 4
3 0
3 4
0 3
1 2
4 2
5 4
4 5

Sample Output 1

2.00000000000000000000

The intersection of S and T, shown gray in the figure below, has an area of 2.


Sample Input 2

3 3
0 0
3 0
0 3
3 3
0 3
3 0

Sample Output 2

0.00000000000000000000

Sample Input 3

3 3
1 3
0 2
2 0
3 1
2 3
0 1

Sample Output 3

0.79166666666666662966
O - Pair Score

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

問題文

1 から N の番号がついた N 人の人がいます。人 i は整数 B_i が書かれたゼッケンをつけています。

この N 人を横一列に並べる方法は N! 通りありますが、各並びについて、以下で定めるスコアを求め、その総和を 998244353 で割った余りを求めてください。

  • 左から i 番目の人のゼッケンにかかれている整数を A_i とする。f(i,j) を、A_j-A_i=j-i であるとき S_{j-i}、それ以外のとき 0 とする。1\leq i < j \leq N を満たす全ての組 (i,j) についての f(i,j) の総和をスコアとする

制約

  • 2 \leq N \leq 10^5
  • 1 \leq B_i \leq 10^5
  • 1 \leq S_i \leq 998244352
  • 入力に含まれる値は全て整数である

入力

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

N
B_1 \ldots B_N
S_1 \ldots S_N

出力

答えを出力せよ。


入力例 1

3
3 2 1
1 10 100

出力例 1

14
  • 左から人 1,2,3 と並べたとき、A=(3,2,1) となり、スコアは f(1,2)+f(1,3)+f(2,3)=0+0+0=0
  • 左から人 1,3,2 と並べたとき、A=(3,1,2) となり、スコアは f(1,2)+f(1,3)+f(2,3)=0+0+1=1
  • 左から人 2,1,3 と並べたとき、A=(2,3,1) となり、スコアは f(1,2)+f(1,3)+f(2,3)=1+0+0=1
  • 左から人 2,3,1 と並べたとき、A=(2,1,3) となり、スコアは f(1,2)+f(1,3)+f(2,3)=0+0+0=0
  • 左から人 3,1,2 と並べたとき、A=(1,3,2) となり、スコアは f(1,2)+f(1,3)+f(2,3)=0+0+0=0
  • 左から人 3,2,1 と並べたとき、A=(1,2,3) となり、スコアは f(1,2)+f(1,3)+f(2,3)=1+10+1=12

となるため、答えは 0+1+1+0+0+12=14 になります。


入力例 2

3
3 3 1
1 10 100

出力例 2

20
  • 左から人 1,2,3 と並べたとき、A=(3,3,1) となり、スコアは f(1,2)+f(1,3)+f(2,3)=0+0+0=0
  • 左から人 1,3,2 と並べたとき、A=(3,1,3) となり、スコアは f(1,2)+f(1,3)+f(2,3)=0+0+0=0
  • 左から人 2,1,3 と並べたとき、A=(3,3,1) となり、スコアは f(1,2)+f(1,3)+f(2,3)=0+0+0=0
  • 左から人 2,3,1 と並べたとき、A=(3,1,3) となり、スコアは f(1,2)+f(1,3)+f(2,3)=0+0+0=0
  • 左から人 3,1,2 と並べたとき、A=(1,3,3) となり、スコアは f(1,2)+f(1,3)+f(2,3)=0+10+0=10
  • 左から人 3,2,1 と並べたとき、A=(1,3,3) となり、スコアは f(1,2)+f(1,3)+f(2,3)=0+10+0=10

となるため、答えは 0+0+0+0+10+10=20 になります。


入力例 3

9
3 1 4 15 9 2 6 5 35
1 12 123 1234 12345 123456 1234567 12345678 123456789

出力例 3

146471953

998244353 で割った余りを求めてください。

Score : 6 points

Problem Statement

There are N people numbered 1 to N. Person i wears an integer B_i.

There are N! ways to make them stand in a row from left to right. The score for each of these ways is defined as follows. Find the sum of all those scores, modulo 998244353.

  • Let A_i denote the integer worn by the i-th person from the left. Let f(i,j) be S_{j-i} if A_j-A_i=j-i and 0 otherwise. The score is the sum of f(i,j) over all pairs (i,j) such that 1\leq i < j \leq N.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq B_i \leq 10^5
  • 1 \leq S_i \leq 998244352
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
B_1 \ldots B_N
S_1 \ldots S_N

Output

Print the answer.


Sample Input 1

3
3 2 1
1 10 100

Sample Output 1

14
  • When arranging them in the order 1, 2, 3 from left to right, we have A=(3,2,1), for a score of f(1,2)+f(1,3)+f(2,3)=0+0+0=0.
  • When arranging them in the order 1, 3, 2 from left to right, we have A=(3,1,2), for a score of f(1,2)+f(1,3)+f(2,3)=0+0+1=1.
  • When arranging them in the order 2, 1, 3 from left to right, we have A=(3,1,2), for a score of f(1,2)+f(1,3)+f(2,3)=1+0+0=1.
  • When arranging them in the order 2, 3, 1 from left to right, we have A=(3,1,2), for a score of f(1,2)+f(1,3)+f(2,3)=0+0+0=0.
  • When arranging them in the order 3, 1, 2 from left to right, we have A=(3,1,2), for a score of f(1,2)+f(1,3)+f(2,3)=0+0+0=0.
  • When arranging them in the order 3, 2, 1 from left to right, we have A=(3,1,2), for a score of f(1,2)+f(1,3)+f(2,3)=1+10+1=12.

Therefore, the answer is 0+1+1+0+0+12=14.


Sample Input 2

3
3 3 1
1 10 100

Sample Output 2

20
  • When arranging them in the order 1, 2, 3 from left to right, we have A=(3,3,1), for a score of f(1,2)+f(1,3)+f(2,3)=0+0+0=0.
  • When arranging them in the order 1, 3, 2 from left to right, we have A=(3,1,3), for a score of f(1,2)+f(1,3)+f(2,3)=0+0+0=0.
  • When arranging them in the order 2, 1, 3 from left to right, we have A=(3,3,1), for a score of f(1,2)+f(1,3)+f(2,3)=0+0+0=0.
  • When arranging them in the order 2, 3, 1 from left to right, we have A=(3,1,3), for a score of f(1,2)+f(1,3)+f(2,3)=0+0+0=0.
  • When arranging them in the order 3, 1, 2 from left to right, we have A=(1,3,3), for a score of f(1,2)+f(1,3)+f(2,3)=0+10+0=10.
  • When arranging them in the order 3, 2, 1 from left to right, we have A=(1,3,3), for a score of f(1,2)+f(1,3)+f(2,3)=0+10+0=10.

Therefore, the answer is 0+0+0+0+10+10=20.


Sample Input 3

9
3 1 4 15 9 2 6 5 35
1 12 123 1234 12345 123456 1234567 12345678 123456789

Sample Output 3

146471953

Be sure to find it modulo 998244353.