A - アルバイトの給料計算

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

配点 : 200

問題文

高橋君はある会社の経理担当です。この会社では N 人のアルバイトスタッフが働いており、それぞれ番号 1 から N が付けられています。

ある月に、スタッフ iC_i 回のシフトに入りました。

会社は、各スタッフに対してシフト 1 回あたり P 円の基本給を支払います。ただし、月間シフト回数が K 回以上であるスタッフには、そのスタッフの全シフトに対して、基本給に加えてシフト 1 回あたり B 円の特別手当が上乗せされます。すなわち、月間シフト回数が K 回以上のスタッフにはシフト 1 回あたり合計 P + B 円が、K 回未満のスタッフにはシフト 1 回あたり P 円が支払われます。

N 人のスタッフ全員に支払う給料の合計額を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq P \leq 1000
  • 1 \leq B \leq 1000
  • 1 \leq K \leq 31
  • 1 \leq C_i \leq 31 (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N P B K
C_1 C_2 \ldots C_N
  • 1 行目には、スタッフの人数を表す N 、シフト 1 回あたりの基本給を表す P 、シフト 1 回あたりの特別手当の額を表す B 、特別手当が適用される月間シフト回数の閾値を表す K が、スペース区切りで与えられる。
  • 2 行目には、各スタッフの月間シフト回数を表す C_1, C_2, \ldots, C_N が、スペース区切りで与えられる。

出力

会社が N 人のスタッフ全員に支払う給料の合計額を 1 行で出力せよ。


入力例 1

3 100 50 3
5 2 3

出力例 1

1400

入力例 2

3 200 100 10
3 5 2

出力例 2

2000

入力例 3

10 500 300 15
20 10 15 8 25 14 16 5 30 12

出力例 3

109300

入力例 4

20 1000 500 20
25 18 31 20 10 15 22 28 19 30 5 12 21 27 16 8 24 31 14 20

出力例 4

535500

入力例 5

1 1 1 1
1

出力例 5

2

Score : 200 pts

Problem Statement

Takahashi is an accountant at a company. This company has N part-time staff members, each numbered from 1 to N.

In a certain month, staff member i worked C_i shifts.

The company pays each staff member a base pay of P yen per shift. However, for staff members whose monthly shift count is K or more, a special bonus of B yen per shift is added on top of the base pay for all of that staff member's shifts. In other words, staff members with K or more monthly shifts are paid a total of P + B yen per shift, while staff members with fewer than K shifts are paid P yen per shift.

Find the total salary to be paid to all N staff members.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq P \leq 1000
  • 1 \leq B \leq 1000
  • 1 \leq K \leq 31
  • 1 \leq C_i \leq 31 (1 \leq i \leq N)
  • All input values are integers

Input

N P B K
C_1 C_2 \ldots C_N
  • The first line contains N representing the number of staff members, P representing the base pay per shift, B representing the special bonus amount per shift, and K representing the threshold of monthly shift count for the special bonus to apply, separated by spaces.
  • The second line contains C_1, C_2, \ldots, C_N representing the monthly shift count of each staff member, separated by spaces.

Output

Print the total salary the company pays to all N staff members on a single line.


Sample Input 1

3 100 50 3
5 2 3

Sample Output 1

1400

Sample Input 2

3 200 100 10
3 5 2

Sample Output 2

2000

Sample Input 3

10 500 300 15
20 10 15 8 25 14 16 5 30 12

Sample Output 3

109300

Sample Input 4

20 1000 500 20
25 18 31 20 10 15 22 28 19 30 5 12 21 27 16 8 24 31 14 20

Sample Output 4

535500

Sample Input 5

1 1 1 1
1

Sample Output 5

2
B - 倉庫の荷物管理

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

配点 : 266

問題文

高橋君は物流センターで倉庫の管理を担当しています。

物流センターには N 個の倉庫があり、各倉庫には 1 から N までの番号が付けられています。はじめ、倉庫 i (1 \leq i \leq N) には重さ V_i の荷物が 1 つだけ保管されています。

青木君は配送担当で、Q 個の指示を順番に高橋君に伝えます。各指示は次の 2 種類のいずれかです。

  • 種類 1:「倉庫 a にある荷物をすべて、倉庫 b に移動する」(a \neq b)。倉庫 a に現在ある荷物をすべて倉庫 b に移動させます。移動後、倉庫 a は空になります。倉庫 b にはもともと保管されていた荷物と、倉庫 a から移動してきた荷物の両方が保管されます。倉庫 a が空の場合、何も起こりません。
  • 種類 2:「倉庫 c にある荷物の重さの合計を報告する」。倉庫 c に現在保管されているすべての荷物の重さの合計を出力します。倉庫 c が空の場合は 0 を出力します。

高橋君は指示を 1 番目から順に処理していきます。種類 2 の指示それぞれについて、出力すべき値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • 各指示は種類 1 または種類 2 のいずれかである
  • 種類 1 の指示において、1 \leq a \leq N, 1 \leq b \leq N, a \neq b
  • 種類 2 の指示において、1 \leq c \leq N
  • 入力はすべて整数である

入力

N Q
V_1 V_2 \ldots V_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
  • 1 行目には、倉庫の数を表す整数 N と指示の数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、初期状態で倉庫 i に保管されている荷物の重さを表す整数 V_1, V_2, \ldots, V_N が、スペース区切りで与えられる。
  • 続く Q 行にわたって、指示の情報が 1 つずつ与えられる。各指示 \text{query}_i は以下のいずれかの形式である。
  • 種類 1 の場合:1 a b(倉庫 a の荷物をすべて倉庫 b に移動する)
  • 種類 2 の場合:2 c(倉庫 c の荷物の重さの合計を求める)

出力

種類 2 の指示それぞれについて、指示が与えられた順に、出力すべき値を 1 行ずつ出力せよ。種類 2 の指示が存在しない場合は何も出力しない。


入力例 1

3 5
10 20 30
1 1 2
2 2
2 1
1 3 2
2 2

出力例 1

30
0
60

入力例 2

4 6
5 15 25 35
2 4
1 1 3
1 2 3
2 3
1 1 4
2 1

出力例 2

35
45
0

入力例 3

8 12
100 200 300 400 500 600 700 800
1 1 2
1 3 2
2 2
1 4 5
1 6 5
2 5
1 2 5
2 5
1 7 8
1 8 5
2 5
2 7

出力例 3

600
1500
2100
3600
0

入力例 4

10 15
1000000000 999999999 888888888 777777777 666666666 555555555 444444444 333333333 222222222 111111111
1 1 2
1 3 2
1 4 2
2 2
1 5 6
1 7 6
1 8 6
2 6
1 2 10
1 6 10
2 10
1 9 10
2 10
2 1
2 5

出力例 4

3666666664
1999999998
5777777773
5999999995
0
0

入力例 5

1 1
1000000000
2 1

出力例 5

1000000000

Score : 266 pts

Problem Statement

Takahashi is in charge of warehouse management at a logistics center.

The logistics center has N warehouses, each numbered from 1 to N. Initially, warehouse i (1 \leq i \leq N) stores exactly one item with weight V_i.

Aoki is in charge of delivery and gives Q instructions to Takahashi in order. Each instruction is one of the following two types:

  • Type 1: "Move all items in warehouse a to warehouse b" (a \neq b). All items currently in warehouse a are moved to warehouse b. After the move, warehouse a becomes empty. Warehouse b will then store both the items it originally had and the items moved from warehouse a. If warehouse a is empty, nothing happens.
  • Type 2: "Report the total weight of items in warehouse c". Output the total weight of all items currently stored in warehouse c. If warehouse c is empty, output 0.

Takahashi processes the instructions in order starting from the first. For each Type 2 instruction, determine the value that should be output.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • Each instruction is either Type 1 or Type 2
  • For Type 1 instructions, 1 \leq a \leq N, 1 \leq b \leq N, a \neq b
  • For Type 2 instructions, 1 \leq c \leq N
  • All input values are integers

Input

N Q
V_1 V_2 \ldots V_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
  • The first line contains two space-separated integers: N, the number of warehouses, and Q, the number of instructions.
  • The second line contains N space-separated integers V_1, V_2, \ldots, V_N, where V_i represents the weight of the item initially stored in warehouse i.
  • The following Q lines each contain one instruction. Each instruction \text{query}_i is in one of the following formats:
  • Type 1: 1 a b (move all items from warehouse a to warehouse b)
  • Type 2: 2 c (find the total weight of items in warehouse c)

Output

For each Type 2 instruction, output the corresponding value on a separate line, in the order the instructions were given. If there are no Type 2 instructions, output nothing.


Sample Input 1

3 5
10 20 30
1 1 2
2 2
2 1
1 3 2
2 2

Sample Output 1

30
0
60

Sample Input 2

4 6
5 15 25 35
2 4
1 1 3
1 2 3
2 3
1 1 4
2 1

Sample Output 2

35
45
0

Sample Input 3

8 12
100 200 300 400 500 600 700 800
1 1 2
1 3 2
2 2
1 4 5
1 6 5
2 5
1 2 5
2 5
1 7 8
1 8 5
2 5
2 7

Sample Output 3

600
1500
2100
3600
0

Sample Input 4

10 15
1000000000 999999999 888888888 777777777 666666666 555555555 444444444 333333333 222222222 111111111
1 1 2
1 3 2
1 4 2
2 2
1 5 6
1 7 6
1 8 6
2 6
1 2 10
1 6 10
2 10
1 9 10
2 10
2 1
2 5

Sample Output 4

3666666664
1999999998
5777777773
5999999995
0
0

Sample Input 5

1 1
1000000000
2 1

Sample Output 5

1000000000
C - 最強ペアの結成

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

配点 : 300

問題文

高橋君はプログラミングコンテストのチーム戦に向けて、メンバーの中から異なる 2 人を選んでペアを 1 組作ることにしました。

チームには N 人のメンバーがおり、i 番目(1 \leq i \leq N)のメンバーはプログラミング能力を表す正の整数値 A_i を持っています。なお、異なるメンバーが同じ能力値を持つこともありますが、別人であれば区別してペアに選ぶことができます。

i 番目のメンバーと j 番目のメンバー(1 \leq i < j \leq N)からなるペアの「総合力」は、その 2 人の能力値の和 A_i + A_j として定義されます。

高橋君は、総合力が最大となるようにペアを選びたいと考えています。

ペアの総合力としてあり得る最大値を求めてください。

制約

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

入力

N
A_1 A_2 \ldots A_N
  • 1 行目には、メンバーの人数を表す整数 N が与えられる。
  • 2 行目には、各メンバーの能力値を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。

出力

ペアの総合力としてあり得る最大値を 1 行で出力せよ。


入力例 1

5
3 1 4 1 5

出力例 1

9

入力例 2

3
100 200 150

出力例 2

350

入力例 3

10
500000000 1000000000 250000000 750000000 100000000 999999999 300000000 600000000 400000000 800000000

出力例 3

1999999999

Score : 300 pts

Problem Statement

Takahashi is preparing for a team programming contest and has decided to select 2 different people from the members to form one pair.

The team has N members, and the i-th member (1 \leq i \leq N) has a positive integer value A_i representing their programming ability. Note that different members may have the same ability value, but as long as they are different people, they can be distinguished and selected for a pair.

The "combined strength" of a pair consisting of the i-th member and the j-th member (1 \leq i < j \leq N) is defined as the sum of their ability values, A_i + A_j.

Takahashi wants to select a pair that maximizes the combined strength.

Find the maximum possible value of the pair's combined strength.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All inputs are integers

Input

N
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of members.
  • The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the ability values of each member.

Output

Print the maximum possible value of the pair's combined strength on one line.


Sample Input 1

5
3 1 4 1 5

Sample Output 1

9

Sample Input 2

3
100 200 150

Sample Output 2

350

Sample Input 3

10
500000000 1000000000 250000000 750000000 100000000 999999999 300000000 600000000 400000000 800000000

Sample Output 3

1999999999
D - ネットワーク敷設

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

配点 : 366

問題文

高橋君は通信会社のネットワークエンジニアとして、光ファイバーケーブルの敷設計画を担当しています。

市内には N 個の中継局があり、 1 から N までの番号が付けられています。また、 M 本の地下トンネルがあり、それぞれのトンネルは異なる2つの中継局を双方向に結んでいます。 i 番目のトンネル (1 \leq i \leq M) は中継局 U_i と中継局 V_i を結び、幅は W_i センチメートルです。

高橋君は、中継局 1 にあるデータセンターから中継局 N にある顧客ビルまで、幅 K センチメートルの光ファイバーケーブル束を敷設したいと考えています。ケーブル束をトンネルに通すためには、そのトンネルの幅がケーブル束の幅 K センチメートル以上でなければなりません。したがって、敷設経路上のすべてのトンネルの幅が K センチメートル以上である必要があります。

ここで、中継局 1 から中継局 N への経路とは、中継局の列 v_0, v_1, \ldots, v_LL \geq 1 )であって、以下の条件をすべて満たすものを指します。

  • v_0 = 1 かつ v_L = N である。
  • j1 \leq j \leq L )について、中継局 v_{j-1} と中継局 v_j を結ぶトンネルが存在する。
  • v_0, v_1, \ldots, v_L はすべて異なる(同じ中継局を2度以上通らない)。

この経路は L 本のトンネルを通過します。この L を、その経路が通過するトンネルの本数と呼びます。

コスト削減のため、高橋君は通過するトンネルの本数をできるだけ少なくしたいと考えています。

経路上のすべてのトンネルの幅が K センチメートル以上である経路が存在するかどうかを判定し、存在する場合はそのような経路の中で通過するトンネルの本数の最小値を出力してください。存在しない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq U_i < V_i \leq N (1 \leq i \leq M)
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq M)
  • 同じ中継局の組を結ぶトンネルは高々1本である
  • 入力はすべて整数である

入力

N M K
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M

1 行目には、中継局の数 N 、トンネルの数 M 、ケーブル束の幅 K (センチメートル)が、スペース区切りで与えられる。

続く M 行のうち i 行目 (1 \leq i \leq M) には、 i 番目のトンネルが結ぶ2つの中継局の番号 U_i , V_i と、そのトンネルの幅 W_i (センチメートル)が、スペース区切りで与えられる。

出力

中継局 1 から中継局 N まで、経路上のすべてのトンネルの幅が K センチメートル以上である経路が存在する場合は、そのような経路の中で通過するトンネルの本数の最小値を 1 行で出力してください。

そのような経路が存在しない場合は、 -1 を出力してください。


入力例 1

5 6 5
1 2 3
1 3 7
2 5 10
3 4 6
4 5 8
1 5 2

出力例 1

3

入力例 2

4 4 10
1 2 15
1 3 5
2 3 8
3 4 12

出力例 2

-1

入力例 3

8 10 4
1 2 3
1 3 10
1 4 7
2 5 6
3 5 5
4 6 8
5 7 2
5 8 9
6 7 12
7 8 6

出力例 3

3

Score : 366 pts

Problem Statement

Takahashi is working as a network engineer at a telecommunications company and is in charge of planning the installation of fiber optic cables.

There are N relay stations in the city, numbered from 1 to N. There are also M underground tunnels, each connecting two different relay stations bidirectionally. The i-th tunnel (1 \leq i \leq M) connects relay station U_i and relay station V_i, and has a width of W_i centimeters.

Takahashi wants to install a fiber optic cable bundle of width K centimeters from the data center at relay station 1 to the customer building at relay station N. In order to pass the cable bundle through a tunnel, the width of that tunnel must be at least K centimeters. Therefore, all tunnels along the installation route must have a width of at least K centimeters.

Here, a path from relay station 1 to relay station N is a sequence of relay stations v_0, v_1, \ldots, v_L (L \geq 1) satisfying all of the following conditions:

  • v_0 = 1 and v_L = N.
  • For each j (1 \leq j \leq L), there exists a tunnel connecting relay station v_{j-1} and relay station v_j.
  • v_0, v_1, \ldots, v_L are all distinct (no relay station is visited more than once).

This path passes through L tunnels. We call this L the number of tunnels traversed by the path.

To reduce costs, Takahashi wants to minimize the number of tunnels traversed.

Determine whether there exists a path where all tunnels along the path have a width of at least K centimeters. If such a path exists, output the minimum number of tunnels traversed among all such paths. If no such path exists, output -1.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq U_i < V_i \leq N (1 \leq i \leq M)
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq M)
  • There is at most one tunnel connecting any pair of relay stations
  • All input values are integers

Input

N M K
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M

The first line contains the number of relay stations N, the number of tunnels M, and the width of the cable bundle K (in centimeters), separated by spaces.

The i-th of the following M lines (1 \leq i \leq M) contains the numbers of the two relay stations U_i, V_i connected by the i-th tunnel, and the width W_i (in centimeters) of that tunnel, separated by spaces.

Output

If there exists a path from relay station 1 to relay station N where all tunnels along the path have a width of at least K centimeters, output the minimum number of tunnels traversed among all such paths on a single line.

If no such path exists, output -1.


Sample Input 1

5 6 5
1 2 3
1 3 7
2 5 10
3 4 6
4 5 8
1 5 2

Sample Output 1

3

Sample Input 2

4 4 10
1 2 15
1 3 5
2 3 8
3 4 12

Sample Output 2

-1

Sample Input 3

8 10 4
1 2 3
1 3 10
1 4 7
2 5 6
3 5 5
4 6 8
5 7 2
5 8 9
6 7 12
7 8 6

Sample Output 3

3
E - 巡回配達

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

配点 : 433

問題文

高橋君は配達員として働いています。担当エリアには N 個の地点があり、地点 1 から地点 N まで番号が付けられています。地点と地点の間には M 本の一方通行の道路があり、i 番目の道路 (1 \leq i \leq M) は地点 U_i から地点 V_i へのみ通行できる道路で、通行には正のコスト W_i がかかります。高橋君はこれらの道路のみを使って地点間を移動します。

高橋君は今、地点 S にある営業所にいます。今日の業務として、K 個の届け先 T_1, T_2, \ldots, T_K に荷物を届ける必要があります。これらの届け先は互いに異なり、いずれも地点 S とは異なる地点です。

高橋君は地点 S を出発し、K 個すべての届け先を好きな順序で訪れて荷物を届けた後、最終的に地点 S の営業所に戻らなければなりません。ここで、高橋君の移動経路とは、地点 S から出発して道路を1本ずつたどり(各道路の終点が次の道路の始点と一致するように)、最終的に地点 S に到達するような道路の列のことです。出発から帰還までの経路の中で、ある届け先の地点を少なくとも 1 回通りさえすれば、その届け先に荷物を届けたことになります。すなわち、届け先の地点を通過するだけでも構いません。

移動の途中で、同じ道路や同じ地点を複数回通ることは許されます。ただし、同じ道路を複数回通った場合は、通るたびにその道路のコストがかかります。

高橋君が地点 S を出発し、K 個すべての届け先を訪れて地点 S に戻るまでの経路について、通った道路のコストの延べ総和(同じ道路を複数回通った場合は通過回数分を加算)の最小値を求めてください。すべての届け先を訪れて地点 S に戻ることが不可能な場合は -1 を出力してください。

制約

  • 2 \leq N \leq 300
  • 1 \leq M \leq N(N-1)
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • 同じ始点・終点の組 (U_i, V_i) を持つ道路は存在しない(多重辺なし)
  • 1 \leq W_i \leq 10^6
  • 1 \leq S \leq N
  • 1 \leq K \leq 15
  • 1 \leq T_j \leq N (1 \leq j \leq K)
  • T_j はすべて異なる
  • T_j \neq S (1 \leq j \leq K)
  • 入力はすべて整数である

入力

N M
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
S K
T_1 T_2 \ldots T_K
  • 1 行目には、地点の数 N と道路の数 M が、スペース区切りで与えられる。
  • 続く M 行のうち i 行目 (1 \leq i \leq M) には、i 番目の道路の始点 U_i、終点 V_i、コスト W_i が、スペース区切りで与えられる。
  • 次の行には、高橋君の出発地点 S と届け先の数 K が、スペース区切りで与えられる。
  • 次の行には、届け先の地点 T_1, T_2, \ldots, T_K が、スペース区切りで与えられる。

出力

高橋君がすべての届け先を訪れて地点 S に戻るために必要な、通った道路のコストの延べ総和の最小値を 1 行で出力してください。すべての届け先を訪れて地点 S に戻ることが不可能な場合は -1 を出力してください。


入力例 1

4 5
1 2 3
2 3 2
3 1 4
1 3 10
2 1 5
1 2
2 3

出力例 1

9

入力例 2

3 2
1 2 1
2 3 1
1 1
2

出力例 2

-1

入力例 3

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

出力例 3

20

入力例 4

10 20
1 2 4
2 3 3
3 4 5
4 5 2
5 6 7
6 7 1
7 8 6
8 9 3
9 10 4
10 1 8
1 5 20
5 1 15
2 6 10
6 2 12
3 7 8
7 3 9
4 8 6
8 4 7
1 3 11
9 1 5
1 5
2 4 6 8 10

出力例 4

43

入力例 5

2 2
1 2 1000000
2 1 1000000
1 1
2

出力例 5

2000000

Score : 433 pts

Problem Statement

Takahashi works as a delivery person. His assigned area has N locations, numbered from location 1 to location N. There are M one-way roads between locations, and the i-th road (1 \leq i \leq M) can only be traversed from location U_i to location V_i, with a positive cost of W_i. Takahashi moves between locations using only these roads.

Takahashi is currently at the office located at location S. As today's task, he needs to deliver packages to K destinations T_1, T_2, \ldots, T_K. These destinations are all distinct from each other and all different from location S.

Takahashi departs from location S, visits all K destinations in any order he likes to deliver the packages, and must finally return to the office at location S. Here, Takahashi's route is a sequence of roads starting from location S, traversing roads one by one (such that the endpoint of each road matches the starting point of the next road), and ultimately arriving back at location S. A package is considered delivered to a destination as long as Takahashi passes through that destination's location at least once during the route from departure to return. That is, merely passing through a destination location is sufficient.

During the journey, it is permitted to traverse the same road or visit the same location multiple times. However, if the same road is traversed multiple times, its cost is incurred each time.

Find the minimum total cost of roads traversed (where the cost of a road traversed multiple times is counted for each traversal) over all possible routes where Takahashi departs from location S, visits all K destinations, and returns to location S. If it is impossible to visit all destinations and return to location S, output -1.

Constraints

  • 2 \leq N \leq 300
  • 1 \leq M \leq N(N-1)
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • No two roads share the same pair of start and end points (U_i, V_i) (no parallel edges)
  • 1 \leq W_i \leq 10^6
  • 1 \leq S \leq N
  • 1 \leq K \leq 15
  • 1 \leq T_j \leq N (1 \leq j \leq K)
  • All T_j are distinct
  • T_j \neq S (1 \leq j \leq K)
  • All input values are integers

Input

N M
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
S K
T_1 T_2 \ldots T_K
  • The first line contains the number of locations N and the number of roads M, separated by a space.
  • The following M lines each contain, for the i-th road (1 \leq i \leq M), the starting point U_i, the ending point V_i, and the cost W_i, separated by spaces.
  • The next line contains Takahashi's starting location S and the number of destinations K, separated by a space.
  • The next line contains the destination locations T_1, T_2, \ldots, T_K, separated by spaces.

Output

Output in a single line the minimum total cost of roads traversed for Takahashi to visit all destinations and return to location S. If it is impossible to visit all destinations and return to location S, output -1.


Sample Input 1

4 5
1 2 3
2 3 2
3 1 4
1 3 10
2 1 5
1 2
2 3

Sample Output 1

9

Sample Input 2

3 2
1 2 1
2 3 1
1 1
2

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

20

Sample Input 4

10 20
1 2 4
2 3 3
3 4 5
4 5 2
5 6 7
6 7 1
7 8 6
8 9 3
9 10 4
10 1 8
1 5 20
5 1 15
2 6 10
6 2 12
3 7 8
7 3 9
4 8 6
8 4 7
1 3 11
9 1 5
1 5
2 4 6 8 10

Sample Output 4

43

Sample Input 5

2 2
1 2 1000000
2 1 1000000
1 1
2

Sample Output 5

2000000