実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君は運送会社の配送計画担当者です。彼は N 台の配送トラックの中からちょうど 1 台を選び、配送業務を割り当てる必要があります。
各トラック i( 1 \leq i \leq N )には、燃費係数 E_i が設定されています。燃費係数が大きいトラックほど、同じ距離を走るのに多くの燃料を消費します。
配送ルートは M 個の区間から構成されており、 j 番目( 1 \leq j \leq M )の区間の距離は C_j です。選んだトラックはこれら M 個の区間をすべて走行します。トラック i が j 番目の区間を走行するときに消費する燃料は E_i \times C_j です。
トラック i を選んだときの燃料消費量の合計は
\displaystyle \sum_{j=1}^{M} E_i \times C_j
です。 N 台のトラックの中から燃料消費量の合計が最小となるようにちょうど 1 台を選んだときの、最小の燃料消費量を求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq E_i \leq 10^4( 1 \leq i \leq N )
- 1 \leq C_j \leq 10^4( 1 \leq j \leq M )
- 入力はすべて整数
- 答えは 10^{13} 以下であり、64ビット整数に収まる
入力
N M E_1 E_2 \ldots E_N C_1 C_2 \ldots C_M
- 1 行目には、トラックの台数を表す整数 N と、配送ルートの区間数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各トラックの燃費係数を表す N 個の整数 E_1, E_2, \ldots, E_N が、スペース区切りで与えられる。
- 3 行目には、各区間の距離を表す M 個の整数 C_1, C_2, \ldots, C_M が、スペース区切りで与えられる。
出力
最小の燃料消費量を 1 行で出力してください。
入力例 1
3 4 5 3 7 2 4 1 3
出力例 1
30
入力例 2
5 6 12 8 15 6 10 5 3 8 2 7 4
出力例 2
174
入力例 3
10 8 100 250 80 320 150 90 200 180 75 110 50 120 30 85 200 65 40 95
出力例 3
51375
Score : 200 pts
Problem Statement
Takahashi is a delivery planning manager at a shipping company. He needs to select exactly 1 truck out of N delivery trucks and assign it to a delivery task.
Each truck i (1 \leq i \leq N) has a fuel efficiency coefficient E_i. A truck with a larger fuel efficiency coefficient consumes more fuel to travel the same distance.
The delivery route consists of M segments, and the distance of the j-th segment (1 \leq j \leq M) is C_j. The selected truck will travel all M segments. The fuel consumed when truck i travels the j-th segment is E_i \times C_j.
The total fuel consumption when truck i is selected is
$\displaystyle \sum_{j=1}^{M} E_i \times C_j$
Find the minimum total fuel consumption when exactly 1 truck is chosen from the N trucks to minimize the total fuel consumption.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq E_i \leq 10^4 (1 \leq i \leq N)
- 1 \leq C_j \leq 10^4 (1 \leq j \leq M)
- All input values are integers
- The answer is at most 10^{13} and fits in a 64-bit integer
Input
N M E_1 E_2 \ldots E_N C_1 C_2 \ldots C_M
- The first line contains an integer N representing the number of trucks and an integer M representing the number of segments in the delivery route, separated by a space.
- The second line contains N integers E_1, E_2, \ldots, E_N representing the fuel efficiency coefficients of each truck, separated by spaces.
- The third line contains M integers C_1, C_2, \ldots, C_M representing the distance of each segment, separated by spaces.
Output
Print the minimum fuel consumption on a single line.
Sample Input 1
3 4 5 3 7 2 4 1 3
Sample Output 1
30
Sample Input 2
5 6 12 8 15 6 10 5 3 8 2 7 4
Sample Output 2
174
Sample Input 3
10 8 100 250 80 320 150 90 200 180 75 110 50 120 30 85 200 65 40 95
Sample Output 3
51375
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君は大学の研究支援センターで働いています。大学には N 個の研究室があり、それぞれの研究室はいくつかの研究テーマに取り組んでいます。研究室 i(1 \leq i \leq N)は M_i 個の研究テーマに取り組んでおり、それらのテーマはそれぞれ英小文字からなる文字列 W_{i,1}, W_{i,2}, \ldots, W_{i,M_i} で表されます。なお、同じ研究室内で同じ研究テーマが複数回現れることはありません。
大学では、共同研究を促進するために、「共同研究可能」な研究室のペアを調査することになりました。研究室 i が取り組んでいる研究テーマの集合を S_i = \{W_{i,1}, W_{i,2}, \ldots, W_{i,M_i}\} とします。2つの研究室 i, j(i \neq j)が「共同研究可能」であるとは、S_i と S_j に共通して含まれる研究テーマの数が K 個以上であること、すなわち |S_i \cap S_j| \geq K が成り立つことを指します。ここで、2つの研究テーマが同一であるかどうかは、テーマを表す文字列が完全に一致するかどうかで判定します。
高橋君は各研究室が取り組んでいる研究テーマのリストを持っています。これらの情報をもとに、「共同研究可能」な研究室のペアの数を求めてください。ただし、研究室 i と研究室 j のペアと研究室 j と研究室 i のペアは同一のペアとみなし、1 \leq i < j \leq N を満たす (i, j) の組のうち条件を満たすものの個数を答えてください。
制約
- 2 \leq N \leq 500
- 1 \leq K \leq 50
- 1 \leq M_i \leq 50(1 \leq i \leq N)
- W_{i,j} は英小文字のみからなる長さ 1 以上 20 以下の文字列である(1 \leq i \leq N, 1 \leq j \leq M_i)
- 同じ研究室内で同じ研究テーマが複数回現れることはない(すなわち、j \neq k ならば W_{i,j} \neq W_{i,k})
入力
入力は以下の形式で標準入力から与えられる。
N K
M_1
W_{1,1} W_{1,2} \ldots W_{1,M_1}
M_2
W_{2,1} W_{2,2} \ldots W_{2,M_2}
\vdots
M_N
W_{N,1} W_{N,2} \ldots W_{N,M_N}
最初の行には、研究室の数 N と「共同研究可能」の判定に用いる閾値 K が、スペース区切りで与えられる。
続いて、各研究室 i(i = 1, 2, \ldots, N)について、以下の 2 行が順に与えられる。
- 第 1 行には、研究室 i が取り組んでいる研究テーマの数 M_i が与えられる。
- 第 2 行には、研究室 i が取り組んでいる M_i 個の研究テーマを表す文字列 W_{i,1}, W_{i,2}, \ldots, W_{i,M_i} がスペース区切りで与えられる。
出力
「共同研究可能」な研究室のペアの数を 1 行で出力してください。
入力例 1
3 2 3 ai ml data 4 ml data web security 2 ai ml
出力例 1
2
入力例 2
4 1 3 biology chemistry physics 2 chemistry medicine 4 physics math statistics chemistry 3 biology ecology environment
出力例 2
4
入力例 3
5 3 5 deeplearning nlp vision robotics optimization 4 nlp vision speech recognition 6 deeplearning nlp vision gan transformer diffusion 3 robotics control automation 5 nlp vision deeplearning bert attention
出力例 3
3
Score : 266 pts
Problem Statement
Takahashi works at the university's research support center. The university has N laboratories, and each laboratory is working on several research themes. Laboratory i (1 \leq i \leq N) is working on M_i research themes, each represented by a string of lowercase English letters W_{i,1}, W_{i,2}, \ldots, W_{i,M_i}. Note that the same research theme does not appear more than once within the same laboratory.
To promote collaborative research, the university has decided to investigate pairs of laboratories that are "eligible for collaboration." Let S_i = \{W_{i,1}, W_{i,2}, \ldots, W_{i,M_i}\} be the set of research themes that laboratory i is working on. Two laboratories i, j (i \neq j) are said to be "eligible for collaboration" if the number of research themes common to both S_i and S_j is at least K, that is, |S_i \cap S_j| \geq K holds. Here, whether two research themes are identical is determined by whether their representing strings match exactly.
Takahashi has the list of research themes each laboratory is working on. Based on this information, find the number of pairs of laboratories that are "eligible for collaboration." Note that the pair of laboratory i and laboratory j is considered the same as the pair of laboratory j and laboratory i. Count the number of pairs (i, j) satisfying 1 \leq i < j \leq N that meet the condition.
Constraints
- 2 \leq N \leq 500
- 1 \leq K \leq 50
- 1 \leq M_i \leq 50 (1 \leq i \leq N)
- W_{i,j} is a string of length between 1 and 20 consisting only of lowercase English letters (1 \leq i \leq N, 1 \leq j \leq M_i)
- The same research theme does not appear more than once within the same laboratory (that is, if j \neq k then W_{i,j} \neq W_{i,k})
Input
The input is given from standard input in the following format.
N K
M_1
W_{1,1} W_{1,2} \ldots W_{1,M_1}
M_2
W_{2,1} W_{2,2} \ldots W_{2,M_2}
\vdots
M_N
W_{N,1} W_{N,2} \ldots W_{N,M_N}
The first line contains the number of laboratories N and the threshold K used for determining "eligibility for collaboration," separated by a space.
Then, for each laboratory i (i = 1, 2, \ldots, N), the following 2 lines are given in order:
- The first line contains the number of research themes M_i that laboratory i is working on.
- The second line contains the M_i strings W_{i,1}, W_{i,2}, \ldots, W_{i,M_i} representing the research themes of laboratory i, separated by spaces.
Output
Output the number of pairs of laboratories that are "eligible for collaboration" in a single line.
Sample Input 1
3 2 3 ai ml data 4 ml data web security 2 ai ml
Sample Output 1
2
Sample Input 2
4 1 3 biology chemistry physics 2 chemistry medicine 4 physics math statistics chemistry 3 biology ecology environment
Sample Output 2
4
Sample Input 3
5 3 5 deeplearning nlp vision robotics optimization 4 nlp vision speech recognition 6 deeplearning nlp vision gan transformer diffusion 3 robotics control automation 5 nlp vision deeplearning bert attention
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は、N 人の生徒が参加するクイズ大会の司会を担当しています。
各生徒には 1 から N までの出席番号が付けられており、生徒 i のクイズの実力値は A_i です。
大会の開始時点では N 人全員が残っており、以下のルールに従って「ラウンド」を繰り返します。
- 現在残っている生徒を出席番号の小さい順に並べ、その順序で先頭から K 人ずつのグループに分ける。すなわち、並べた列の 1 番目から K 番目までを第 1 グループ、K{+}1 番目から 2K 番目までを第 2 グループ、…とする。最後のグループの人数が K 人未満になる場合は、そのままの人数で 1 つのグループとする。
- 各グループについて、そのグループ内で実力値が最も高い生徒 1 人だけが勝ち残る。実力値が最も高い生徒が複数いる場合は、その中で出席番号が最も小さい生徒が勝ち残る。(グループの人数が 1 人の場合は、その生徒がそのまま勝ち残る。)
- 勝ち残った生徒が 1 人になったら、その生徒を優勝者とし大会を終了する。2 人以上残っている場合は、勝ち残った生徒のみを残して手順 1 に戻り、次のラウンドを行う。
各ラウンドでは残っている生徒が 2 人以上おり、K \geq 2 より少なくとも 1 つのグループには 2 人以上の生徒が含まれるため、ラウンドごとに残っている生徒の人数は必ず減少します。したがって、大会は有限回のラウンドで終了します。
最終的に優勝する生徒の出席番号を求めてください。
制約
- 2 \leq N \leq 5 \times 10^5
- 2 \leq K \leq N
- 1 \leq A_i \leq 10^9
- 入力はすべて整数である。
入力
N K A_1 A_2 \ldots A_N
- 1 行目には、生徒の人数を表す整数 N と、各グループの最大人数を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各生徒の実力値を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
優勝する生徒の出席番号を 1 行で出力せよ。
入力例 1
6 2 3 1 4 1 5 9
出力例 1
6
入力例 2
10 3 5 2 8 3 7 1 6 4 9 10
出力例 2
10
入力例 3
15 4 12 5 9 3 15 7 11 2 8 14 1 6 13 10 4
出力例 3
5
Score : 300 pts
Problem Statement
Takahashi is hosting a quiz tournament in which N students participate.
Each student is assigned a student ID number from 1 to N, and student i has a quiz skill level of A_i.
At the start of the tournament, all N students remain, and "rounds" are repeated according to the following rules:
- Arrange the currently remaining students in ascending order of their student ID numbers, and divide them into groups of K from the front. That is, the 1st through K-th students in the arranged sequence form group 1, the (K{+}1)-th through 2K-th form group 2, and so on. If the last group has fewer than K students, it remains as a single group with that number of students.
- For each group, only the 1 student with the highest skill level in that group survives. If there are multiple students with the highest skill level, the one with the smallest student ID number among them survives. (If a group has only 1 student, that student survives as is.)
- If only 1 student remains, that student is declared the champion and the tournament ends. If 2 or more students remain, only the surviving students are kept and we return to step 1 to conduct the next round.
In each round, there are at least 2 remaining students, and since K \geq 2, at least one group contains 2 or more students, so the number of remaining students strictly decreases with each round. Therefore, the tournament ends after a finite number of rounds.
Determine the student ID number of the student who ultimately wins the tournament.
Constraints
- 2 \leq N \leq 5 \times 10^5
- 2 \leq K \leq N
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
N K A_1 A_2 \ldots A_N
- The first line contains the integer N representing the number of students and the integer K representing the maximum number of students per group, separated by a space.
- The second line contains the integers A_1, A_2, \ldots, A_N representing each student's skill level, separated by spaces.
Output
Print the student ID number of the winning student on a single line.
Sample Input 1
6 2 3 1 4 1 5 9
Sample Output 1
6
Sample Input 2
10 3 5 2 8 3 7 1 6 4 9 10
Sample Output 2
10
Sample Input 3
15 4 12 5 9 3 15 7 11 2 8 14 1 6 13 10 4
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君と青木君は、N \times N マスの掲示板にそれぞれ広告を貼ろうとしています。掲示板の各マスは座標 (r, c)(1 \leq r \leq N, 1 \leq c \leq N)で表されます。ここで r は上から数えた行番号、c は左から数えた列番号です。
高橋君は A 個の長方形の広告を、青木君は B 個の長方形の広告を掲示板に貼る予定です。各広告は、左上のマス (r_1, c_1) と右下のマス (r_2, c_2) で指定される長方形の領域を覆います。すなわち、r_1 \leq r \leq r_2 かつ c_1 \leq c \leq c_2 を満たすすべてのマス (r, c) が覆われます。
掲示板の管理人は、高橋君の広告と青木君の広告が重なって貼られるマスがあると見栄えが悪くなるため、重なりが生じるマスの総数を事前に把握したいと考えています。
高橋君の広告のうち少なくとも 1 つに覆われ、かつ青木君の広告のうち少なくとも 1 つに覆われるマスの数を求めてください。同じマスが複数の広告の組み合わせで重複して覆われていても、1 マスとして 1 回だけ数えます。
制約
- 1 \leq N \leq 500
- 1 \leq A \leq 100
- 1 \leq B \leq 100
- 高橋君の各広告 i(1 \leq i \leq A)について、1 \leq r_{1,i} \leq r_{2,i} \leq N, 1 \leq c_{1,i} \leq c_{2,i} \leq N
- 青木君の各広告 j(1 \leq j \leq B)について、1 \leq r_{1,j} \leq r_{2,j} \leq N, 1 \leq c_{1,j} \leq c_{2,j} \leq N
- 入力はすべて整数である
入力
N A B
r_{1,1} c_{1,1} r_{2,1} c_{2,1}
r_{1,2} c_{1,2} r_{2,2} c_{2,2}
:
r_{1,A} c_{1,A} r_{2,A} c_{2,A}
r'_{1,1} c'_{1,1} r'_{2,1} c'_{2,1}
r'_{1,2} c'_{1,2} r'_{2,2} c'_{2,2}
:
r'_{1,B} c'_{1,B} r'_{2,B} c'_{2,B}
- 1 行目には、掲示板の一辺のマス数 N、高橋君の広告の数 A、青木君の広告の数 B がスペース区切りで与えられる。
- 続く A 行では、高橋君の広告が 1 行に 1 つずつ与えられる。
- i 番目の広告(1 \leq i \leq A)は、左上のマス (r_{1,i}, c_{1,i}) と右下のマス (r_{2,i}, c_{2,i}) の 4 つの整数がスペース区切りで与えられる。
- 続く B 行では、青木君の広告が 1 行に 1 つずつ与えられる。
- j 番目の広告(1 \leq j \leq B)は、左上のマス (r'_{1,j}, c'_{1,j}) と右下のマス (r'_{2,j}, c'_{2,j}) の 4 つの整数がスペース区切りで与えられる。
出力
高橋君の広告と青木君の広告が重なっているマスの数を 1 行で出力してください。
入力例 1
5 1 1 1 1 3 3 2 2 4 4
出力例 1
4
入力例 2
10 2 2 1 1 5 5 3 7 6 10 4 3 8 6 2 8 5 10
出力例 2
15
入力例 3
100 3 2 1 1 50 50 40 60 80 100 70 1 100 40 30 30 60 70 80 10 100 50
出力例 3
1323
Score : 400 pts
Problem Statement
Takahashi and Aoki are each planning to post advertisements on an N \times N grid bulletin board. Each cell of the bulletin board is represented by coordinates (r, c) (1 \leq r \leq N, 1 \leq c \leq N), where r is the row number counted from the top and c is the column number counted from the left.
Takahashi plans to post A rectangular advertisements, and Aoki plans to post B rectangular advertisements on the bulletin board. Each advertisement covers a rectangular region specified by its top-left cell (r_1, c_1) and bottom-right cell (r_2, c_2). That is, all cells (r, c) satisfying r_1 \leq r \leq r_2 and c_1 \leq c \leq c_2 are covered.
The bulletin board manager is concerned that cells covered by both Takahashi's and Aoki's advertisements will look unsightly, and wants to know the total number of such overlapping cells in advance.
Find the number of cells that are covered by at least one of Takahashi's advertisements and also covered by at least one of Aoki's advertisements. Even if the same cell is covered by multiple combinations of advertisements, it is counted only once.
Constraints
- 1 \leq N \leq 500
- 1 \leq A \leq 100
- 1 \leq B \leq 100
- For each of Takahashi's advertisements i (1 \leq i \leq A): 1 \leq r_{1,i} \leq r_{2,i} \leq N, 1 \leq c_{1,i} \leq c_{2,i} \leq N
- For each of Aoki's advertisements j (1 \leq j \leq B): 1 \leq r_{1,j} \leq r_{2,j} \leq N, 1 \leq c_{1,j} \leq c_{2,j} \leq N
- All input values are integers
Input
N A B
r_{1,1} c_{1,1} r_{2,1} c_{2,1}
r_{1,2} c_{1,2} r_{2,2} c_{2,2}
:
r_{1,A} c_{1,A} r_{2,A} c_{2,A}
r'_{1,1} c'_{1,1} r'_{2,1} c'_{2,1}
r'_{1,2} c'_{1,2} r'_{2,2} c'_{2,2}
:
r'_{1,B} c'_{1,B} r'_{2,B} c'_{2,B}
- The first line contains the board size N, the number of Takahashi's advertisements A, and the number of Aoki's advertisements B, separated by spaces.
- The following A lines each describe one of Takahashi's advertisements.
- The i-th advertisement (1 \leq i \leq A) is given as four space-separated integers representing the top-left cell (r_{1,i}, c_{1,i}) and the bottom-right cell (r_{2,i}, c_{2,i}).
- The following B lines each describe one of Aoki's advertisements.
- The j-th advertisement (1 \leq j \leq B) is given as four space-separated integers representing the top-left cell (r'_{1,j}, c'_{1,j}) and the bottom-right cell (r'_{2,j}, c'_{2,j}).
Output
Output the number of cells where Takahashi's advertisements and Aoki's advertisements overlap, on a single line.
Sample Input 1
5 1 1 1 1 3 3 2 2 4 4
Sample Output 1
4
Sample Input 2
10 2 2 1 1 5 5 3 7 6 10 4 3 8 6 2 8 5 10
Sample Output 2
15
Sample Input 3
100 3 2 1 1 50 50 40 60 80 100 70 1 100 40 30 30 60 70 80 10 100 50
Sample Output 3
1323
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 433 点
問題文
高橋君は大規模な倉庫で働く配送ロボットのプログラマーです。
倉庫の床は N \times N のグリッド状に区切られています。上から r 行目 (1 \leq r \leq N)、左から c 列目 (1 \leq c \leq N) のマスには番号 (r-1) \times N + c が付けられています。すなわち、1 行目の左から右へ 1, 2, \ldots, N、2 行目の左から右へ N+1, N+2, \ldots, 2N、……というように番号が振られています。グリッド上に壁や障害物はなく、すべてのマスは通行可能です。
配送ロボットは現在、マス S に待機しています。高橋君はロボットをマス T にある出荷エリアまで移動させる必要があります。S と T は異なるマスです。ロボットは 1 回の移動で上下左右に隣接するマスへ 1 マス移動できます。ただし、グリッドの外へ出ることはできません。
ロボットは移動の過程で M 個の指定された棚から荷物を回収しなければなりません。荷物を回収すべき棚のあるマスの番号 P_1, P_2, \ldots, P_M が与えられます。これらの M 個のマスはどのような順序で訪れてもかまいませんが、マス T に到達するまでにすべてのマスを少なくとも 1 回ずつ訪れる必要があります。なお、移動の途中で同じマスを複数回通ることは許されます。
ロボットがマス S から出発し、M 個の指定されたマスすべてを訪れたうえでマス T に到達するとき、必要な最小の移動回数を求めてください。ここで移動回数とは、隣接するマスへの移動を行った総回数のことです。
制約
- 1 \leq N \leq 10^5
- 0 \leq M \leq 15
- 1 \leq S \leq N^2
- 1 \leq T \leq N^2
- 1 \leq P_i \leq N^2 (1 \leq i \leq M)
- P_i \neq P_j (i \neq j)
- S, T, P_1, P_2, \ldots, P_M はすべて異なる
- 入力はすべて整数
入力
N M S T P_1 P_2 \cdots P_M
- 1 行目には、グリッドの一辺の長さを表す整数 N と、経由すべきマスの個数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、開始位置のマス番号を表す整数 S と、終了位置のマス番号を表す整数 T が、スペース区切りで与えられる。
- 3 行目には、経由すべきマスの番号を表す整数 P_1, P_2, \ldots, P_M が、スペース区切りで与えられる。ただし M = 0 のとき、3 行目は与えられない。
出力
ロボットがマス S から出発し、指定されたすべてのマスを訪れてマス T に到達するために必要な最小の移動回数を 1 行で出力してください。
入力例 1
3 2 1 9 5 3
出力例 1
6
入力例 2
5 0 1 25
出力例 2
8
入力例 3
100 5 1 10000 505 2550 7523 3001 9999
出力例 3
270
Score : 433 pts
Problem Statement
Takahashi is a programmer for delivery robots working in a large warehouse.
The warehouse floor is divided into an N \times N grid. The cell in the r-th row from the top (1 \leq r \leq N) and the c-th column from the left (1 \leq c \leq N) is assigned the number (r-1) \times N + c. That is, the cells are numbered 1, 2, \ldots, N from left to right in row 1, then N+1, N+2, \ldots, 2N from left to right in row 2, and so on. There are no walls or obstacles on the grid, and all cells are passable.
The delivery robot is currently waiting at cell S. Takahashi needs to move the robot to the shipping area at cell T. S and T are different cells. In one move, the robot can move one cell to an adjacent cell in one of the four directions (up, down, left, right). However, the robot cannot move outside the grid.
During its movement, the robot must collect packages from M designated shelves. The cell numbers P_1, P_2, \ldots, P_M where the shelves with packages to collect are located are given. These M cells may be visited in any order, but all of them must be visited at least once before reaching cell T. Note that passing through the same cell multiple times during movement is allowed.
Find the minimum number of moves required for the robot to start from cell S, visit all M designated cells, and reach cell T. Here, the number of moves refers to the total number of times the robot moves to an adjacent cell.
Constraints
- 1 \leq N \leq 10^5
- 0 \leq M \leq 15
- 1 \leq S \leq N^2
- 1 \leq T \leq N^2
- 1 \leq P_i \leq N^2 (1 \leq i \leq M)
- P_i \neq P_j (i \neq j)
- S, T, P_1, P_2, \ldots, P_M are all distinct
- All input values are integers
Input
N M S T P_1 P_2 \cdots P_M
- The first line contains an integer N representing the side length of the grid and an integer M representing the number of cells to visit, separated by a space.
- The second line contains an integer S representing the starting cell number and an integer T representing the destination cell number, separated by a space.
- The third line contains integers P_1, P_2, \ldots, P_M representing the cell numbers to visit, separated by spaces. If M = 0, the third line is not given.
Output
Print in one line the minimum number of moves required for the robot to start from cell S, visit all designated cells, and reach cell T.
Sample Input 1
3 2 1 9 5 3
Sample Output 1
6
Sample Input 2
5 0 1 25
Sample Output 2
8
Sample Input 3
100 5 1 10000 505 2550 7523 3001 9999
Sample Output 3
270