A - Parking Lot Organization

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君は、駐車場の管理人をしています。この駐車場は一直線に伸びた数直線上にあり、座標 0 から座標 M までの範囲が駐車可能エリアとなっています。

現在、 N 台の車が駐車されており、 i 番目の車は座標 A_i の位置にあります。しかし、いくつかの車は駐車可能エリアの外に停められてしまっている可能性があります。

高橋君は、すべての車を駐車可能エリア内(座標 0 以上 M 以下)に移動させる必要があります。各車は他の車とは独立に移動させることができ、 1 台の車を距離 1 だけ移動させるのに 1 円のコストがかかります。すなわち、座標 a にある車を座標 b に移動させるコストは |a - b| 円です。

すべての車を座標 0 以上 M 以下の範囲内に収めるために必要な最小の合計コストを求めてください。

ただし、移動前・移動後ともに、複数の車が同じ座標に存在することは許されます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^9
  • -10^9 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N M
A_1 A_2 \ldots A_N
  • 1 行目には、車の台数を表す整数 N と、駐車可能エリアの上限座標を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各車の現在の座標を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

すべての車を座標 0 以上 M 以下の範囲内に収めるために必要な最小の合計コストを 1 行で出力せよ。


入力例 1

3 10
-2 5 12

出力例 1

4

入力例 2

4 5
0 2 5 3

出力例 2

0

入力例 3

10 100
-200 -50 -1 0 20 60 99 100 101 150

出力例 3

302

入力例 4

50 1000
-5000 -1000 -1 0 1 2 10 50 200 500 999 1000 1001 1200 1500 2000 3000 10000 123 456 789 100 900 250 750 333 666 111 222 444 -250 -750 -333 -666 -111 -222 -444 0 1000 -1 1001 500 1500 500000000 -500000000 1000000000 -1000000000 999 1 1002

出力例 4

3000019982

入力例 5

1 1000000000
-1000000000

出力例 5

1000000000

Score : 233 pts

Problem Statement

Takahashi is a parking lot manager. The parking lot lies on a straight number line, and the parkable area ranges from coordinate 0 to coordinate M.

Currently, N cars are parked, and the i-th car is at coordinate A_i. However, some cars may have been parked outside the parkable area.

Takahashi needs to move all cars into the parkable area (coordinates between 0 and M, inclusive). Each car can be moved independently of the others, and it costs 1 yen to move one car a distance of 1. That is, the cost of moving a car from coordinate a to coordinate b is |a - b| yen.

Find the minimum total cost required to move all cars into the range of coordinates 0 or greater and M or less.

Note that multiple cars are allowed to occupy the same coordinate, both before and after moving.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^9
  • -10^9 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N M
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of cars and an integer M representing the upper limit coordinate of the parkable area, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing the current coordinates of each car, separated by spaces.

Output

Print on one line the minimum total cost required to move all cars into the range of coordinates 0 or greater and M or less.


Sample Input 1

3 10
-2 5 12

Sample Output 1

4

Sample Input 2

4 5
0 2 5 3

Sample Output 2

0

Sample Input 3

10 100
-200 -50 -1 0 20 60 99 100 101 150

Sample Output 3

302

Sample Input 4

50 1000
-5000 -1000 -1 0 1 2 10 50 200 500 999 1000 1001 1200 1500 2000 3000 10000 123 456 789 100 900 250 750 333 666 111 222 444 -250 -750 -333 -666 -111 -222 -444 0 1000 -1 1001 500 1500 500000000 -500000000 1000000000 -1000000000 999 1 1002

Sample Output 4

3000019982

Sample Input 5

1 1000000000
-1000000000

Sample Output 5

1000000000
B - Watering the Flower Bed

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

高橋君は一列に並んだ花壇の管理を任されています。花壇には N 個の区画が左から右へ一列に並んでおり、それぞれの区画には花が植えられているか、空いているかのどちらかです。左から i 番目の区画を区画 i と呼びます (1 \leq i \leq N)。

花壇の状態は長さ N の文字列 S で表されます。S の i 文字目 (1 \leq i \leq N) が o のとき区画 i には花が植えられており、. のとき区画 i は空です。

高橋君はスプリンクラーを使って水やりをします。スプリンクラーは区画 1 に固定されており、1 以上 N 以下の整数 K を1つ選んで設定します。スプリンクラーを動かすと、区画 1 を起点として間隔 K で等間隔に水をまきます。すなわち、水がまかれる区画は区画 1, 1+K, 1+2K, \ldots のうち区画番号が N 以下であるものすべてです。

水をまいた区画に花が植えられている場合、その花は水やりされます。空の区画に水をまいても何も起こりません。高橋君は K の値を1つだけ選び、その設定でスプリンクラーを1回だけ動かします。K の値を最適に選ぶことで、水やりされる花の数を最大化したいと考えています。

水やりされる花の数の最大値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • S は長さ N の文字列であり、o と . のみからなる。

入力

N
S
  • 1 行目には、花壇の区画数を表す正の整数 N が与えられる。
  • 2 行目には、花壇の状態を表す長さ N の文字列 S が与えられる。S は o と . のみからなる。

出力

最適な K を選んだときに水やりされる花の数の最大値を 1 行で出力せよ。


入力例 1

10
o.o.o.o.o.

出力例 1

5

入力例 2

15
oo.oo.oo.oo.oo.

出力例 2

10

入力例 3

50
o..o..o..o..o..o..o..o..o..o..o..o..o..o..o..o..o.

出力例 3

17

Score : 300 pts

Problem Statement

Takahashi is in charge of managing a flower bed arranged in a row. The flower bed has N sections lined up in a row from left to right, and each section either has a flower planted in it or is empty. The i-th section from the left is called section i (1 \leq i \leq N).

The state of the flower bed is represented by a string S of length N. If the i-th character (1 \leq i \leq N) of S is o, then section i has a flower planted in it; if it is ., then section i is empty.

Takahashi waters the flowers using a sprinkler. The sprinkler is fixed at section 1, and he chooses and sets one integer K where 1 \leq K \leq N. When the sprinkler is activated, it sprays water at equally spaced intervals of K starting from section 1. Specifically, the sections that receive water are all sections among 1, 1+K, 1+2K, \ldots whose section numbers are at most N.

If a section that receives water has a flower planted in it, that flower is watered. Nothing happens if water is sprayed on an empty section. Takahashi chooses exactly one value of K and activates the sprinkler exactly once with that setting. He wants to maximize the number of flowers that are watered by optimally choosing the value of K.

Find the maximum number of flowers that can be watered.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • S is a string of length N consisting only of o and ..

Input

N
S
  • The first line contains a positive integer N, representing the number of sections in the flower bed.
  • The second line contains a string S of length N, representing the state of the flower bed. S consists only of o and ..

Output

Print in one line the maximum number of flowers that can be watered when the optimal K is chosen.


Sample Input 1

10
o.o.o.o.o.

Sample Output 1

5

Sample Input 2

15
oo.oo.oo.oo.oo.

Sample Output 2

10

Sample Input 3

50
o..o..o..o..o..o..o..o..o..o..o..o..o..o..o..o..o.

Sample Output 3

17
C - Congestion of Evacuation Drill

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

高橋君は、N 個の広場からなる公園の避難訓練を管理しています。広場は 1 から N まで番号付けされており、N-1 本の道によって結ばれ、広場 1 を根とする木構造をなしています。各広場 i(2 \leq i \leq N)には親となる広場 P_i が定まっており、広場 i と広場 P_i は道で直接結ばれています。

各広場 i には混雑係数 A_i が設定されています。また、各広場 i には B_i 人がいます。

避難訓練では、各広場 i(2 \leq i \leq N)にいる B_i 人が、避難場所である広場 1 へ向かいます。各人は、広場 i から広場 1 への木上の一意な単純パスに沿って移動し、始点 i と終点 1 を含むパス上のすべての広場を通過します。

広場 1 にいる B_1 人はすでに避難場所にいるため移動せず、どの広場も通過しないものとします。

避難訓練の混雑度を、すべての移動する人について、その人が通過するすべての広場の混雑係数の総和として定義します。すなわち、

\text{混雑度} = \sum_{i=2}^{N} B_i \sum_{j \in \mathrm{path}(i,1)} A_j

です。ここで \mathrm{path}(i,1) は広場 i から広場 1 への単純パス上の広場の集合(両端を含む)を表します。

混雑度を求めてください。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq A_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq P_i < i (2 \leq i \leq N)
  • 0 \leq B_i \leq 10^4 (1 \leq i \leq N)
  • 入力はすべて整数である。
  • 答えは 2^{63}-1 以下であることが保証される。

入力

N
A_1 A_2 \dots A_N
P_2 P_3 \dots P_N
B_1 B_2 \dots B_N
  • 1 行目には、広場の数を表す整数 N が与えられる。
  • 2 行目には、各広場の混雑係数を表す N 個の整数 A_1, A_2, \dots, A_N がスペース区切りで与えられる。
  • 3 行目には、木構造における各広場の親を表す N-1 個の整数 P_2, P_3, \dots, P_N がスペース区切りで与えられる。広場 i(2 \leq i \leq N)の親は広場 P_i である。
  • 4 行目には、各広場にいる人数を表す N 個の整数 B_1, B_2, \dots, B_N がスペース区切りで与えられる。

出力

混雑度を 1 行に出力せよ。


入力例 1

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

出力例 1

33

入力例 2

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

出力例 2

68

入力例 3

8
5 3 6 1 4 0 2 8
1 1 2 2 3 3 6
9 2 3 4 0 5 1 6

出力例 3

267

入力例 4

20
4 7 1 3 9 0 2 6 5 8 1 4 7 3 2 9 0 5 6 1
1 1 2 2 3 3 4 4 5 5 6 7 7 8 9 10 11 13 13
3 1 4 0 2 5 1 6 0 3 2 4 1 7 0 2 5 1 3 4

出力例 4

811

入力例 5

1
10000

10000

出力例 5

0

Score : 366 pts

Problem Statement

Takahashi is managing an evacuation drill in a park consisting of N plazas. The plazas are numbered from 1 to N and connected by N-1 paths, forming a tree structure rooted at plaza 1. For each plaza i (2 \leq i \leq N), there is a designated parent plaza P_i, and plaza i and plaza P_i are directly connected by a path.

Each plaza i has a congestion coefficient A_i. Also, there are B_i people at each plaza i.

During the evacuation drill, the B_i people at each plaza i (2 \leq i \leq N) head toward plaza 1, which is the evacuation site. Each person moves along the unique simple path on the tree from plaza i to plaza 1, passing through all plazas on the path including the starting point i and the endpoint 1.

The B_1 people at plaza 1 are already at the evacuation site and do not move, so they do not pass through any plaza.

The congestion of the evacuation drill is defined as the sum, over all moving people, of the total congestion coefficients of all plazas that person passes through. That is,

\text{Congestion} = \sum_{i=2}^{N} B_i \sum_{j \in \mathrm{path}(i,1)} A_j

Here, \mathrm{path}(i,1) denotes the set of plazas on the simple path from plaza i to plaza 1 (including both endpoints).

Find the congestion.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 0 \leq A_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq P_i < i (2 \leq i \leq N)
  • 0 \leq B_i \leq 10^4 (1 \leq i \leq N)
  • All input values are integers.
  • It is guaranteed that the answer is at most 2^{63}-1.

Input

N
A_1 A_2 \dots A_N
P_2 P_3 \dots P_N
B_1 B_2 \dots B_N
  • The first line contains an integer N, the number of plazas.
  • The second line contains N integers A_1, A_2, \dots, A_N separated by spaces, representing the congestion coefficient of each plaza.
  • The third line contains N-1 integers P_2, P_3, \dots, P_N separated by spaces, representing the parent of each plaza in the tree structure. The parent of plaza i (2 \leq i \leq N) is plaza P_i.
  • The fourth line contains N integers B_1, B_2, \dots, B_N separated by spaces, representing the number of people at each plaza.

Output

Print the congestion on a single line.


Sample Input 1

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

Sample Output 1

33

Sample Input 2

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

Sample Output 2

68

Sample Input 3

8
5 3 6 1 4 0 2 8
1 1 2 2 3 3 6
9 2 3 4 0 5 1 6

Sample Output 3

267

Sample Input 4

20
4 7 1 3 9 0 2 6 5 8 1 4 7 3 2 9 0 5 6 1
1 1 2 2 3 3 4 4 5 5 6 7 7 8 9 10 11 13 13
3 1 4 0 2 5 1 6 0 3 2 4 1 7 0 2 5 1 3 4

Sample Output 4

811

Sample Input 5

1
10000

10000

Sample Output 5

0
D - Task Execution Order

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君は N 個のタスクをすべてこなす必要があります。それぞれのタスクには 1 から N までの番号が付けられており、各タスクはちょうど 1 回ずつ、1 つずつ順番に実行します。

タスクの間には前提条件があります。あるタスクを実行するには、それより先に別のタスクを完了していなければならない場合があります。この前提条件は M 個与えられ、各前提条件は「タスク A_i をタスク B_i より先に完了しなければならない」という形で表されます。

高橋君は、すべての前提条件を満たしながら N 個すべてのタスクを実行する順序を決めたいと考えています。そのような順序が存在する場合、実行するタスク番号を実行順に並べた列が辞書順最小となる実行順序を求めてください。前提条件に矛盾があり、すべてのタスクを実行することが不可能な場合は -1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M
  • N + M \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • A_i \neq B_i
  • (A_i, B_i) の組はすべて異なる
  • 入力はすべて整数

入力

N M
A_1 B_1
A_2 B_2
:
A_M B_M
  • 1 行目には、タスクの数を表す整数 N と、前提条件の数を表す整数 M が、スペース区切りで与えられる。
  • 続く M 行では、前提条件が与えられる。
  • i 行目 (1 \leq i \leq M) では、タスク A_i をタスク B_i より先に完了しなければならないことを表す整数 A_i と B_i が、スペース区切りで与えられる。

出力

すべてのタスクを実行できる場合、辞書順最小の実行順序として N 個のタスク番号をスペース区切りで 1 行に出力してください。実行が不可能な場合は -1 を出力してください。


入力例 1

4 3
1 3
2 3
2 4

出力例 1

1 2 3 4

入力例 2

3 3
1 2
2 3
3 1

出力例 2

-1

入力例 3

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

出力例 3

1 2 3 4 5 6 7 8

入力例 4

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

出力例 4

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

入力例 5

1 0

出力例 5

1

Score : 400 pts

Problem Statement

Takahashi needs to complete all N tasks. Each task is numbered from 1 to N, and each task is executed exactly once, one at a time, in some order.

There are prerequisite conditions between tasks. To execute a certain task, it may be necessary to have completed another task beforehand. There are M such prerequisite conditions given, each expressed in the form "Task A_i must be completed before Task B_i."

Takahashi wants to determine an order to execute all N tasks while satisfying all prerequisite conditions. If such an order exists, find the execution order such that the sequence of task numbers listed in execution order is lexicographically smallest. If the prerequisite conditions are contradictory and it is impossible to execute all tasks, output -1.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M
  • N + M \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • A_i \neq B_i
  • All pairs (A_i, B_i) are distinct
  • All input values are integers

Input

N M
A_1 B_1
A_2 B_2
:
A_M B_M
  • The first line contains an integer N representing the number of tasks and an integer M representing the number of prerequisite conditions, separated by a space.
  • The following M lines give the prerequisite conditions.
  • The i-th line (1 \leq i \leq M) contains integers A_i and B_i separated by a space, indicating that Task A_i must be completed before Task B_i.

Output

If it is possible to execute all tasks, output the N task numbers in the lexicographically smallest execution order on a single line, separated by spaces. If execution is impossible, output -1.


Sample Input 1

4 3
1 3
2 3
2 4

Sample Output 1

1 2 3 4

Sample Input 2

3 3
1 2
2 3
3 1

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

1 2 3 4 5 6 7 8

Sample Input 4

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

Sample Output 4

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

Sample Input 5

1 0

Sample Output 5

1
E - City Tour and Mission Completion

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

高橋君は 1 から N までの番号がついた N 個の都市がある国を旅行しています。高橋君は最初、都市 1 にいます。都市 1 は最初から訪問済みです。

都市間には M 本の一方通行の道路があり、 i 番目の道路を使うと都市 U_i から都市 V_i へ W_i 時間かけて移動できます。同じ2都市間に複数の道路が存在することもあります。高橋君は同じ都市や同じ道路を何度通っても構いません。

高橋君は都市 1 から出発し、道路を辿って移動します(一度も移動しなくてもよいです)。ただし、通った道路の移動時間の総和は T 以下でなければなりません。最終的にどの都市にいるかは問いません。一度でも到達した都市は訪問済みとして扱います(都市 1 も訪問済みに含まれます)。

この国では K 個のミッションが設定されています。 j 番目のミッションは都市の集合 S_j と報酬 P_j の組で表され、「S_j に含まれるすべての都市が訪問済みであれば、報酬 P_j を獲得できる」というものです。訪問の順序は問いません。条件を満たすミッションはすべて同時に達成でき、獲得する報酬はそれらの合計となります。

高橋君が獲得できる報酬の合計の最大値を求めてください。どのミッションの条件も満たせない場合、答えは 0 です。

制約

  • 2 \leq N \leq 10^4
  • 1 \leq M \leq 10^4
  • 1 \leq K \leq 100
  • 1 \leq T \leq 10^9
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • 1 \leq W_i \leq 10^6
  • 1 \leq |S_j| \leq 14
  • S_j の各要素 s_{j,k} は 1 \leq s_{j,k} \leq N を満たす
  • 各 S_j の要素はすべて相異なる
  • 異なるミッション間で S_j が同一の集合であることもありうる
  • \bigcup_{j=1}^{K} S_j の要素数は 14 以下である(すなわち、すべてのミッションに登場する都市の種類数は高々 14)
  • 1 \leq P_j \leq 10^9
  • 入力はすべて整数である

入力

N M K T
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
|S_1| s_{1,1} s_{1,2} \ldots s_{1,|S_1|} P_1
|S_2| s_{2,1} s_{2,2} \ldots s_{2,|S_2|} P_2
\vdots
|S_K| s_{K,1} s_{K,2} \ldots s_{K,|S_K|} P_K
  • 1 行目には、都市の数 N 、道路の数 M 、ミッションの数 K 、移動可能な合計時間 T がスペース区切りで与えられる。
  • 続く M 行のうち i 行目では、 i 番目の道路の始点 U_i 、終点 V_i 、移動時間 W_i がスペース区切りで与えられる。
  • 続く K 行のうち j 行目では、 j 番目のミッションが与えられる。まず集合 S_j の要素数 |S_j| が与えられ、続いて S_j に含まれる都市の番号 s_{j,1}, s_{j,2}, \ldots, s_{j,|S_j|} が与えられ、最後に報酬 P_j が与えられる。これらはすべてスペース区切りである。

出力

高橋君が獲得できる報酬の合計の最大値を 1 行で出力せよ。どのミッションの条件も満たせない場合は 0 を出力せよ。


入力例 1

4 4 3 7
1 2 3
2 3 3
1 4 10
3 4 1
1 2 100
2 2 3 300
1 4 500

出力例 1

900

入力例 2

3 1 2 5
1 2 10
1 2 100
1 3 200

出力例 2

0

入力例 3

12 18 8 25
1 2 4
1 3 6
2 4 5
3 4 2
4 5 4
2 6 10
5 6 3
6 7 2
7 8 4
3 9 8
9 10 3
10 5 2
8 11 5
11 12 1
5 3 7
6 2 6
4 10 6
10 8 4
1 2 100
2 3 4 250
3 2 5 6 500
2 8 10 700
1 11 900
4 2 4 6 8 1200
2 10 5 400
3 3 9 10 350

出力例 3

1850

入力例 4

30 50 15 120
1 2 5
1 3 12
1 4 7
2 5 8
2 6 20
3 6 6
4 3 3
4 7 15
5 8 10
6 8 5
6 9 9
7 10 8
8 11 4
9 11 3
10 12 6
11 12 5
12 13 7
13 14 2
14 15 5
15 16 8
16 18 6
13 18 15
18 20 9
20 22 10
22 25 12
25 30 14
12 20 25
8 15 18
5 10 22
3 5 20
2 3 4
11 7 11
15 8 16
18 12 13
20 15 7
22 18 5
25 22 9
30 1 50
9 17 10
17 19 7
19 21 4
21 23 6
23 24 3
24 26 8
26 27 4
27 28 5
28 29 3
29 30 2
10 20 30
6 18 40
1 1 100
1 2 150
2 2 5 400
3 3 8 12 900
2 15 18 1200
4 2 7 10 12 1300
3 20 22 25 2000
1 30 3000
5 1 3 5 8 15 1700
2 18 20 800
3 10 12 20 1100
4 15 20 22 25 1600
2 25 30 2200
6 2 3 5 7 8 10 2500
3 12 18 30 1800

出力例 4

14350

入力例 5

2 1 1 1
2 1 1000000
1 1 1000000000

出力例 5

1000000000

Score : 433 pts

Problem Statement

Takahashi is traveling in a country with N cities numbered from 1 to N. Takahashi is initially in city 1. City 1 is already considered visited from the start.

There are M one-way roads between cities. Using the i-th road, one can travel from city U_i to city V_i in W_i units of time. There may be multiple roads between the same pair of cities. Takahashi may pass through the same city or the same road any number of times.

Takahashi starts from city 1 and travels along roads (he may also choose not to move at all). However, the total travel time of all roads taken must not exceed T. It does not matter which city he ends up in. Any city that has been reached at least once is treated as visited (city 1 is also included as visited).

In this country, K missions are available. The j-th mission is represented by a pair of a set of cities S_j and a reward P_j, meaning "if all cities in S_j have been visited, reward P_j can be obtained." The order of visits does not matter. All missions whose conditions are satisfied can be completed simultaneously, and the total reward earned is the sum of their individual rewards.

Find the maximum total reward that Takahashi can earn. If no mission's condition can be satisfied, the answer is 0.

Constraints

  • 2 \leq N \leq 10^4
  • 1 \leq M \leq 10^4
  • 1 \leq K \leq 100
  • 1 \leq T \leq 10^9
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • 1 \leq W_i \leq 10^6
  • 1 \leq |S_j| \leq 14
  • Each element s_{j,k} of S_j satisfies 1 \leq s_{j,k} \leq N
  • All elements within each S_j are distinct
  • Different missions may have the same set S_j
  • The number of elements in \bigcup_{j=1}^{K} S_j is at most 14 (that is, the total number of distinct cities appearing across all missions is at most 14)
  • 1 \leq P_j \leq 10^9
  • All input values are integers

Input

N M K T
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
|S_1| s_{1,1} s_{1,2} \ldots s_{1,|S_1|} P_1
|S_2| s_{2,1} s_{2,2} \ldots s_{2,|S_2|} P_2
\vdots
|S_K| s_{K,1} s_{K,2} \ldots s_{K,|S_K|} P_K
  • The first line contains the number of cities N, the number of roads M, the number of missions K, and the maximum total travel time T, separated by spaces.
  • In the following M lines, the i-th line gives the starting city U_i, the ending city V_i, and the travel time W_i of the i-th road, separated by spaces.
  • In the following K lines, the j-th line gives the j-th mission. First, the number of elements |S_j| in set S_j is given, followed by the city numbers s_{j,1}, s_{j,2}, \ldots, s_{j,|S_j|} contained in S_j, and finally the reward P_j. All values are separated by spaces.

Output

Output in one line the maximum total reward that Takahashi can earn. If no mission's condition can be satisfied, output 0.


Sample Input 1

4 4 3 7
1 2 3
2 3 3
1 4 10
3 4 1
1 2 100
2 2 3 300
1 4 500

Sample Output 1

900

Sample Input 2

3 1 2 5
1 2 10
1 2 100
1 3 200

Sample Output 2

0

Sample Input 3

12 18 8 25
1 2 4
1 3 6
2 4 5
3 4 2
4 5 4
2 6 10
5 6 3
6 7 2
7 8 4
3 9 8
9 10 3
10 5 2
8 11 5
11 12 1
5 3 7
6 2 6
4 10 6
10 8 4
1 2 100
2 3 4 250
3 2 5 6 500
2 8 10 700
1 11 900
4 2 4 6 8 1200
2 10 5 400
3 3 9 10 350

Sample Output 3

1850

Sample Input 4

30 50 15 120
1 2 5
1 3 12
1 4 7
2 5 8
2 6 20
3 6 6
4 3 3
4 7 15
5 8 10
6 8 5
6 9 9
7 10 8
8 11 4
9 11 3
10 12 6
11 12 5
12 13 7
13 14 2
14 15 5
15 16 8
16 18 6
13 18 15
18 20 9
20 22 10
22 25 12
25 30 14
12 20 25
8 15 18
5 10 22
3 5 20
2 3 4
11 7 11
15 8 16
18 12 13
20 15 7
22 18 5
25 22 9
30 1 50
9 17 10
17 19 7
19 21 4
21 23 6
23 24 3
24 26 8
26 27 4
27 28 5
28 29 3
29 30 2
10 20 30
6 18 40
1 1 100
1 2 150
2 2 5 400
3 3 8 12 900
2 15 18 1200
4 2 7 10 12 1300
3 20 22 25 2000
1 30 3000
5 1 3 5 8 15 1700
2 18 20 800
3 10 12 20 1100
4 15 20 22 25 1600
2 25 30 2200
6 2 3 5 7 8 10 2500
3 12 18 30 1800

Sample Output 4

14350

Sample Input 5

2 1 1 1
2 1 1000000
1 1 1000000000

Sample Output 5

1000000000