A - Entering the PIN

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君は銀行のATMで暗証番号を入力しようとしています。このATMでは、暗証番号は L(左ボタン)と R(右ボタン)の2種類のボタンを使って入力します。

高橋君は正しい暗証番号を覚えているつもりでしたが、緊張のあまり何度か間違えてしまうかもしれません。ATMの画面には、入力した各桁が正しいかどうかがリアルタイムで表示されます。

高橋君はこれから N 桁の暗証番号を順番に入力していきます。正しい暗証番号と、高橋君が実際に入力したボタンの列が与えられるので、高橋君が連続して正しく入力できた最大の桁数を求めてください。

ここで「連続して正しく入力できた桁数」とは、間違えずに連続して正しいボタンを押せた回数のことを指します。

制約

  • 1 \leq N \leq 2 \times 10^5
  • S は長さ N の文字列である
  • T は長さ N の文字列である
  • S の各文字は L または R である
  • T の各文字は L または R である

入力

N
S
T
  • 1 行目には、暗証番号の桁数を表す整数 N が与えられる。
  • 2 行目には、正しい暗証番号を表す長さ N の文字列 S が与えられる。各文字は L または R である。
  • 3 行目には、高橋君が入力したボタンの列を表す長さ N の文字列 T が与えられる。各文字は L または R である。

出力

高橋君が連続して正しく入力できた最大の桁数を 1 行で出力してください。


入力例 1

5
LLRRL
LLRLL

出力例 1

3

入力例 2

4
LLLL
RRRR

出力例 2

0

入力例 3

10
LRLRLRLRLR
LRLRLLRLRR

出力例 3

5

入力例 4

20
RLRLRLRLRLRLRLRLRLRL
RLRLRLRLRLRLRLRLRLRL

出力例 4

20

入力例 5

1
L
R

出力例 5

0

Score : 233 pts

Problem Statement

Takahashi is trying to enter his PIN at a bank ATM. On this ATM, the PIN is entered using two types of buttons: L (left button) and R (right button).

Takahashi thought he remembered the correct PIN, but due to nervousness, he might have made mistakes several times. The ATM screen displays in real-time whether each entered digit is correct or not.

Takahashi will now enter an N-digit PIN one digit at a time. Given the correct PIN and the sequence of buttons that Takahashi actually pressed, find the maximum number of consecutive digits that Takahashi entered correctly.

Here, "the number of consecutive correctly entered digits" refers to the number of consecutive times he pressed the correct button without making a mistake.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • S is a string of length N
  • T is a string of length N
  • Each character of S is L or R
  • Each character of T is L or R

Input

N
S
T
  • The first line contains an integer N representing the number of digits in the PIN.
  • The second line contains a string S of length N representing the correct PIN. Each character is L or R.
  • The third line contains a string T of length N representing the sequence of buttons that Takahashi pressed. Each character is L or R.

Output

Print on a single line the maximum number of consecutive digits that Takahashi entered correctly.


Sample Input 1

5
LLRRL
LLRLL

Sample Output 1

3

Sample Input 2

4
LLLL
RRRR

Sample Output 2

0

Sample Input 3

10
LRLRLRLRLR
LRLRLLRLRR

Sample Output 3

5

Sample Input 4

20
RLRLRLRLRLRLRLRLRLRL
RLRLRLRLRLRLRLRLRLRL

Sample Output 4

20

Sample Input 5

1
L
R

Sample Output 5

0
B - Harvest from the Field

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

高橋君は農場の管理者です。高橋君の農場には N 個の畑があり、それぞれの畑には 1 から N までの番号が付けられています。

各畑 i には、収穫に必要な作業量 D_i が決まっています。はじめ、各畑の累計作業量は 0 です。畑 i の累計作業量が D_i 以上になると、その畑の作物は収穫できる状態になります。

高橋君の農場には M 人の作業員がいます。作業員は 1 人あたり 1 日に 1 だけの作業量をこなすことができます。

高橋君は毎日、M 人の作業員全員をそれぞれちょうど 1 つの畑に割り当てて作業させなければなりません(1 つの畑に複数人を割り当てることもできます)。作業員を休ませたり、どの畑にも割り当てないということはできません。ある日に畑 i に k 人の作業員が割り当てられると、その日に畑 i の累計作業量が k だけ増加します。すでに収穫可能になった畑にも作業員を割り当てることができますが、その畑の累計作業量が増えるだけで、収穫には影響しません。

作業員の割り当ては日ごとに独立に決めることができます。

高橋君が作業員の割り当てを最適に行ったとき、すべての畑の作物が収穫できる状態になるまでに必要な最小の日数を求めてください。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 10^9
  • 1 \leq D_i \leq 10^9
  • 入力はすべて整数である

入力

N M
D_1 D_2 \ldots D_N
  • 1 行目には、畑の数を表す整数 N と、作業員の人数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各畑の収穫に必要な作業量を表す整数 D_1, D_2, \ldots, D_N が、スペース区切りで与えられる。

出力

すべての畑の作物が収穫できる状態になるまでに必要な最小の日数を 1 行で出力せよ。


入力例 1

3 2
1 2 3

出力例 1

3

入力例 2

4 5
2 1 4 3

出力例 2

2

入力例 3

10 7
5 12 8 20 3 15 7 11 9 15

出力例 3

15

入力例 4

30 13
41 27 63 18 52 36 75 29 44 58 31 69 22 47 55 38 61 24 73 19 46 57 33 66 28 49 54 37 62 21

出力例 4

103

入力例 5

1 1000000000
1000000000

出力例 5

1

Score : 300 pts

Problem Statement

Takahashi is a farm manager. Takahashi's farm has N fields, each numbered from 1 to N.

Each field i has a required amount of work D_i needed for harvest. Initially, the cumulative work done on each field is 0. When the cumulative work on field i reaches D_i or more, the crops in that field become ready for harvest.

Takahashi's farm has M workers. Each worker can perform exactly 1 unit of work per day.

Every day, Takahashi must assign all M workers, each to exactly one field (multiple workers can be assigned to the same field). Workers cannot rest or be left unassigned to any field. If k workers are assigned to field i on a given day, the cumulative work on field i increases by k on that day. Workers can be assigned to fields that are already ready for harvest, but this only increases the cumulative work on that field and has no effect on the harvest.

The assignment of workers can be decided independently each day.

Determine the minimum number of days required for all fields to become ready for harvest when Takahashi assigns workers optimally.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq M \leq 10^9
  • 1 \leq D_i \leq 10^9
  • All input values are integers

Input

N M
D_1 D_2 \ldots D_N
  • The first line contains an integer N representing the number of fields and an integer M representing the number of workers, separated by a space.
  • The second line contains integers D_1, D_2, \ldots, D_N representing the required amount of work for each field, separated by spaces.

Output

Print the minimum number of days required for all fields to become ready for harvest, in a single line.


Sample Input 1

3 2
1 2 3

Sample Output 1

3

Sample Input 2

4 5
2 1 4 3

Sample Output 2

2

Sample Input 3

10 7
5 12 8 20 3 15 7 11 9 15

Sample Output 3

15

Sample Input 4

30 13
41 27 63 18 52 36 75 29 44 58 31 69 22 47 55 38 61 24 73 19 46 57 33 66 28 49 54 37 62 21

Sample Output 4

103

Sample Input 5

1 1000000000
1000000000

Sample Output 5

1
C - Organizing the Network

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

高橋君は、N 台のコンピュータ 1, 2, \ldots, N と N 本のケーブルからなるネットワークを管理しています。i 番目のケーブル (1 \leq i \leq N) はコンピュータ A_i とコンピュータ B_i を双方向に接続しています。同じコンピュータ同士を接続するケーブル(自己ループ、すなわち A_i = B_i)や、同じ 2 台のコンピュータを結ぶケーブルが複数存在すること(多重辺)もありえます。

高橋君は、N 本のケーブルの中から互いに異なるケーブルを K 本以下選んで取り外すことができます。

適切にケーブルを選んで取り外すことで、ネットワークを閉路を含まない状態(すなわち森)にできるか判定してください。

ここで、ネットワークが閉路を含むとは、あるコンピュータ v から出発し、1 本以上のケーブルをたどって v に戻る経路であって、同じケーブルを 2 度通らないものが存在することを指します。特に、自己ループは単独で閉路をなし、多重辺(同じ 2 台のコンピュータを結ぶ 2 本以上のケーブル)も閉路を含みます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq A_i, B_i \leq N
  • 入力はすべて整数である。

入力

N K
A_1 B_1
A_2 B_2
\vdots
A_N B_N

1 行目には、コンピュータの台数 N と、取り外せるケーブルの本数の上限 K がスペース区切りで与えられる。続く N 行のうち i 行目には、i 番目のケーブルが結ぶ 2 台のコンピュータの番号 A_i, B_i がスペース区切りで与えられる。

出力

適切にケーブルを選んで取り外すことで、ネットワークを閉路を含まない状態にできるなら Yes を、できないなら No を出力せよ。


入力例 1

4 1
1 2
2 3
3 1
3 4

出力例 1

Yes

入力例 2

4 0
1 2
2 3
3 1
3 4

出力例 2

No

入力例 3

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

出力例 3

Yes

入力例 4

25 5
1 2
2 3
3 4
4 1
4 5
5 6
7 8
7 8
8 9
9 10
10 11
12 12
12 13
13 14
14 15
15 16
17 18
18 19
19 20
20 17
21 22
22 23
23 21
23 24
24 25

出力例 4

Yes

入力例 5

1 1
1 1

出力例 5

Yes

Score : 366 pts

Problem Statement

Takahashi manages a network consisting of N computers 1, 2, \ldots, N and N cables. The i-th cable (1 \leq i \leq N) connects computer A_i and computer B_i bidirectionally. There may exist cables connecting a computer to itself (self-loops, i.e., A_i = B_i), as well as multiple cables connecting the same pair of computers (multi-edges).

Takahashi can choose and remove at most K distinct cables from the N cables.

Determine whether it is possible to make the network cycle-free (i.e., a forest) by appropriately choosing and removing cables.

Here, a network contains a cycle means that there exists a path starting from some computer v, traversing one or more cables, and returning to v, without passing through the same cable twice. In particular, a self-loop forms a cycle by itself, and multi-edges (two or more cables connecting the same pair of computers) also contain a cycle.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq A_i, B_i \leq N
  • All inputs are integers.

Input

N K
A_1 B_1
A_2 B_2
\vdots
A_N B_N

The first line contains the number of computers N and the maximum number of cables that can be removed K, separated by a space. The i-th of the following N lines contains the numbers A_i, B_i of the two computers connected by the i-th cable, separated by a space.

Output

If it is possible to make the network cycle-free by appropriately choosing and removing cables, print Yes; otherwise, print No.


Sample Input 1

4 1
1 2
2 3
3 1
3 4

Sample Output 1

Yes

Sample Input 2

4 0
1 2
2 3
3 1
3 4

Sample Output 2

No

Sample Input 3

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

Sample Output 3

Yes

Sample Input 4

25 5
1 2
2 3
3 4
4 1
4 5
5 6
7 8
7 8
8 9
9 10
10 11
12 12
12 13
13 14
14 15
15 16
17 18
18 19
19 20
20 17
21 22
22 23
23 21
23 24
24 25

Sample Output 4

Yes

Sample Input 5

1 1
1 1

Sample Output 5

Yes
D - Place Signposts to Guide the Way

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

N 個の交差点と M 本の双方向の道路からなる街があります。交差点には 1 から N までの番号が付けられています。

高橋君は交差点 N にいる迷子の子猫を、交差点 1 にある自宅まで誘導したいと考えています。子猫は人見知りで直接連れて帰ることができないため、高橋君はいくつかの交差点に道しるべ(光る目印)を設置し、子猫を自宅まで導くことにしました。

各交差点に設置できる道しるべは高々 1 個です。高橋君は子猫が移動を開始する前に、道しるべを設置するすべての交差点を決定します。子猫の移動中に道しるべを追加・撤去・移動することはできません。

道しるべにはそれぞれ「機能している」「機能していない(倒れている)」の 2 つの状態があります。設置された道しるべは最初すべて「機能している」状態です。一度倒れた道しるべが再び機能することはありません。

一方、青木君は高橋君の邪魔をしようとしています。青木君は K 個の交差点にバリケードを設置しており、高橋君はバリケードが設置された交差点に道しるべを置くことができません。ただし、交差点 1 と交差点 N にはバリケードは設置されていません。高橋君は、バリケードが設置されていない任意の交差点(交差点 1 や交差点 N を含む)に道しるべを設置できます。

子猫の移動ルール

子猫は交差点 N から出発し、以下の手順を繰り返します。

  1. 到着判定: 子猫が交差点 1 にいる場合、誘導は成功となり、移動は即座に終了します。
  2. 移動先の決定: 子猫は、現在いる交差点に道路で直接つながっている(隣接する)交差点のうち、道しるべが設置されておりかつ機能しているものがあるか確認します。
  • そのような隣接交差点が 1 つ以上ある場合、子猫はその中で番号が最も小さい交差点へ移動します。
  • そのような隣接交差点が 1 つもない場合、子猫はその場から動けず、移動は終了します(誘導は失敗です)。
  1. 道しるべの状態変化: 子猫が移動した先の交差点に道しるべが設置されており、かつその交差点が交差点 1 でない場合、到着した直後にその道しるべは倒れて機能しなくなります。交差点 1 に設置された道しるべは倒れることはありません。その後、手順 1 に戻ります。

補足:

  • 子猫の出発地点は交差点 N です。出発時点では子猫はどこからも「移動してきた」わけではないので、交差点 N に道しるべが設置されていてもそれは倒れず、機能したままです。
  • 交差点 1 の道しるべは倒れないため、隣接する交差点から子猫を交差点 1 へ誘導する目印として機能します。ただし、交差点 1 に到着した時点で誘導成功となるため、その先の移動には影響しません。
  • 交差点 1 以外の道しるべは到着時に倒れるため、子猫が交差点 1 以外の同じ交差点に 2 回以上誘導されることはありません。したがって、子猫の移動は有限回で必ず終了します。

子猫を交差点 N から交差点 1 まで誘導するために必要な道しるべの最小個数を求めてください。どのように道しるべを設置しても子猫を交差点 1 に誘導することができない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N - 2
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • 同じ交差点の組を結ぶ道路は高々 1 本である
  • 1 \leq B_j \leq N (1 \leq j \leq K)
  • B_j \neq 1 かつ B_j \neq N (1 \leq j \leq K)
  • B_1, B_2, \ldots, B_K はすべて異なる
  • 入力はすべて整数である

入力

N M K
U_1 V_1
U_2 V_2
\vdots
U_M V_M
B_1 B_2 \ldots B_K
  • 1 行目には、交差点の数を表す N 、道路の数を表す M 、バリケードが設置された交差点の数を表す K が、スペース区切りで与えられる。
  • 2 行目から M + 1 行目では、道路の情報が与えられる。
  • 1 + i 行目では、 i 番目の道路が結ぶ 2 つの交差点の番号 U_i と V_i がスペース区切りで与えられる。
  • K \geq 1 の場合、M + 2 行目に、バリケードが設置された交差点の番号 B_1, B_2, \ldots, B_K がスペース区切りで与えられる。K = 0 の場合、この行は存在しない。

出力

子猫を交差点 N から交差点 1 まで誘導するために必要な道しるべの最小個数を 1 行で出力せよ。誘導することが不可能な場合は -1 を出力せよ。


入力例 1

4 3 0
4 3
3 2
2 1

出力例 1

3

入力例 2

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

出力例 2

-1

入力例 3

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

出力例 3

4

入力例 4

30 45 5
1 2
1 3
2 4
2 5
3 6
3 7
4 8
5 8
5 9
6 10
6 11
7 11
8 12
9 13
10 14
11 15
12 16
12 18
13 17
14 18
15 19
16 20
17 21
18 24
19 23
20 24
21 25
22 26
23 27
24 30
25 29
26 30
27 30
28 29
29 30
4 14
7 17
9 22
10 20
11 21
13 23
16 26
18 28
19 29
2 12
14 17 22 25 28

出力例 4

5

入力例 5

2 1 0
1 2

出力例 5

1

Score : 400 pts

Problem Statement

There is a town consisting of N intersections and M bidirectional roads. The intersections are numbered 1 to N.

Takahashi wants to guide a lost kitten, currently at intersection N, to his home at intersection 1. Since the kitten is shy and cannot be carried directly, Takahashi decides to place signposts (glowing markers) at some intersections to guide the kitten to his home.

At most one signpost can be placed at each intersection. Takahashi decides all the intersections to place signposts before the kitten starts moving. He cannot add, remove, or move signposts while the kitten is moving.

Each signpost has two states: "active" and "inactive (fallen)". All placed signposts are initially in the "active" state. Once a signpost falls, it never becomes active again.

Meanwhile, Aoki wants to obstruct Takahashi. Aoki has placed barricades at K intersections, and Takahashi cannot place a signpost at any intersection with a barricade. However, no barricades are placed at intersection 1 and intersection N. Takahashi can place a signpost at any intersection without a barricade (including intersection 1 and intersection N).

Kitten's Movement Rules

The kitten starts from intersection N and repeats the following steps:

  1. Arrival Check: If the kitten is at intersection 1, the guidance is successful, and the movement ends immediately.
  2. Determining the Destination: The kitten checks if there are any intersections directly connected (adjacent) to its current intersection by a road that have an active signpost.
  • If there is at least one such adjacent intersection, the kitten moves to the one with the smallest intersection number.
  • If there are no such adjacent intersections, the kitten cannot move further, and the movement ends (guidance fails).
  1. State Change of Signposts: If a signpost is placed at the intersection the kitten has just moved to, and that intersection is not intersection 1, the signpost falls and becomes inactive immediately upon arrival. A signpost placed at intersection 1 never falls. After this, return to Step 1.

Notes:

  • The kitten's starting point is intersection N. At the start, the kitten has not "moved from" anywhere, so even if a signpost is placed at intersection N, it does not fall and remains active.
  • Since the signpost at intersection 1 does not fall, it functions as a marker to guide the kitten to intersection 1 from adjacent intersections. However, once the kitten arrives at intersection 1, the guidance succeeds, so it does not affect any subsequent movement.
  • Since signposts at intersections other than intersection 1 fall upon arrival, the kitten will never be guided to the same intersection (excluding intersection 1) more than once. Therefore, the kitten's movement is guaranteed to end in a finite number of steps.

Find the minimum number of signposts required to guide the kitten from intersection N to intersection 1. If it is impossible to guide the kitten to intersection 1 no matter how the signposts are placed, output -1.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N - 2
  • 1 \leq U_i, V_i \leq N
  • U_i \neq V_i
  • There is at most one road connecting the same pair of intersections.
  • 1 \leq B_j \leq N (1 \leq j \leq K)
  • B_j \neq 1 and B_j \neq N (1 \leq j \leq K)
  • B_1, B_2, \ldots, B_K are all distinct.
  • All input values are integers.

Input

N M K
U_1 V_1
U_2 V_2
\vdots
U_M V_M
B_1 B_2 \ldots B_K
  • The first line contains N, the number of intersections, M, the number of roads, and K, the number of intersections with barricades, separated by spaces.
  • The next M lines, from the 2nd line to the (M + 1)-th line, contain the information about the roads.
  • The (1 + i)-th line contains two space-separated integers U_i and V_i, representing the two intersections connected by the i-th road.
  • If K \geq 1, the (M + 2)-th line contains B_1, B_2, \ldots, B_K, the indices of the intersections with barricades, separated by spaces. If K = 0, this line is omitted.

Output

Print the minimum number of signposts required to guide the kitten from intersection N to intersection 1 in a single line. If it is impossible to guide the kitten, print -1.


Sample Input 1

4 3 0
4 3
3 2
2 1

Sample Output 1

3

Sample Input 2

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

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

4

Sample Input 4

30 45 5
1 2
1 3
2 4
2 5
3 6
3 7
4 8
5 8
5 9
6 10
6 11
7 11
8 12
9 13
10 14
11 15
12 16
12 18
13 17
14 18
15 19
16 20
17 21
18 24
19 23
20 24
21 25
22 26
23 27
24 30
25 29
26 30
27 30
28 29
29 30
4 14
7 17
9 22
10 20
11 21
13 23
16 26
18 28
19 29
2 12
14 17 22 25 28

Sample Output 4

5

Sample Input 5

2 1 0
1 2

Sample Output 5

1
E - Order of Dishes

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466 点

問題文

高橋君は N 種類の料理を作ろうとしています。

料理には 1 から N までの番号が付けられており、料理 i を完成させると、満足度 A_i を得ます(満足度は負の値になることもあります)。

各料理には 0 個以上の「失敗条件」が設定されています。同じ料理に対して複数の失敗条件が設定されることもあります。

失敗条件は、対象となる料理 p と、料理の集合 S(p を含まない)の組 (p, S) で表され、次の意味を持ちます。

  • 料理 p を作り始める時点で、集合 S に含まれるすべての料理がすでに完成済みであるならば、料理 p はこの失敗条件に該当し、完成できない。

特に、S が空集合の場合、この失敗条件は無条件に該当するため、料理 p は決して完成できません。

高橋君は N 種類の料理の中から好きな料理を好きな個数だけ選び(1 つも選ばなくてもよい)、好きな順番で 1 つずつ作ります。各料理は最大 1 回しか作れません。ただし、ある料理を作り始める時点でその料理に関する失敗条件のうち 1 つでも該当している場合、その料理は完成しません。高橋君は事前に計画を立て、確実に完成する料理だけを選んで順に作ることにします。

形式的には、高橋君は相異なる料理の列 a_1, a_2, \dots, a_k(0 \leq k \leq N)を選びます。この列が 実行可能 であるとは、すべての t = 1, 2, \dots, k について、料理 a_t に関するどの失敗条件 (a_t, S) も

S \nsubseteq \{ a_1, a_2, \dots, a_{t-1} \}

を満たすことをいいます。ここで \{a_1, \dots, a_{t-1}\} は料理 a_t より前に完成した料理の集合であり、t = 1 のときは空集合です(料理 a_t 自身はこの集合に含まれません)。

実行可能な列を 1 つ選んだとき、得られる満足度の合計は

A_{a_1} + A_{a_2} + \cdots + A_{a_k}

です(k = 0 のとき満足度は 0 とします)。

実行可能な列すべてのうち、得られる満足度の合計の最大値を求めてください。

制約

  • 1 \leq N \leq 19
  • 0 \leq M \leq 10^5
  • -10^9 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq p_q \leq N (1 \leq q \leq M)
  • 0 \leq c_q \leq N - 1 (1 \leq q \leq M)
  • 1 \leq x_{q,j} \leq N (1 \leq q \leq M,\ 1 \leq j \leq c_q)
  • x_{q,j} \neq p_q (1 \leq q \leq M,\ 1 \leq j \leq c_q)
  • 同じ失敗条件内の x_{q,1}, x_{q,2}, \dots, x_{q,c_q} はすべて異なる
  • \displaystyle \sum_{q=1}^{M} c_q \leq 2 \times 10^5
  • 入力はすべて整数である

入力

N M
A_1 A_2 \cdots A_N
p_1 c_1 x_{1,1} x_{1,2} \cdots x_{1,c_1}
p_2 c_2 x_{2,1} x_{2,2} \cdots x_{2,c_2}
\vdots
p_M c_M x_{M,1} x_{M,2} \cdots x_{M,c_M}
  • 1 行目には、料理の種類数 N と失敗条件の個数 M が、スペース区切りで与えられる。
  • 2 行目には、各料理の満足度 A_1, A_2, \dots, A_N が、スペース区切りで与えられる。
  • 続く M 行のうち q 行目(1 \leq q \leq M)には、q 番目の失敗条件の内容が与えられる。M = 0 の場合、この部分は存在しない。
  • まず対象の料理番号 p_q と集合の要素数 c_q が与えられ、続いて c_q 個の整数 x_{q,1}, x_{q,2}, \dots, x_{q,c_q} が与えられる。すべてスペース区切りである。
  • これは失敗条件 (p_q,\ \{x_{q,1}, x_{q,2}, \dots, x_{q,c_q}\}) を表す。すなわち、料理 p_q を作り始める時点で、料理 x_{q,1}, x_{q,2}, \dots, x_{q,c_q} のすべてがすでに完成済みであれば、料理 p_q はこの失敗条件に該当する。
  • c_q = 0 の場合、集合 S は空集合であり、この失敗条件は無条件に該当する(料理 p_q は決して完成できない)。

出力

実行可能な列によって得られる満足度の合計の最大値を 1 行で出力せよ。


入力例 1

3 2
5 10 -3
2 1 1
3 0

出力例 1

15

入力例 2

4 3
-5 7 4 6
4 2 2 3
2 1 1
3 1 1

出力例 2

17

入力例 3

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

出力例 3

41

入力例 4

15 25
20 -7 35 14 -12 8 50 -30 22 5 -3 18 40 -25 11
7 2 1 3
3 1 7
13 3 2 4 6
5 0
8 1 9
9 2 10 11
12 4 1 5 7 9
15 2 13 14
2 1 15
4 3 2 8 12
10 2 6 7
11 1 10
6 5 1 2 3 4 5
14 2 7 13
1 4 4 8 12 15
13 1 7
7 3 5 9 11
9 0
15 5 1 3 5 7 9
12 2 10 14
4 1 1
6 2 8 11
2 3 3 4 5
10 4 1 9 13 15
11 2 6 12

出力例 4

201

入力例 5

1 1
1000000000
1 0

出力例 5

0

Score : 466 pts

Problem Statement

Takahashi is trying to make N types of dishes.

The dishes are numbered from 1 to N, and completing dish i gives him a satisfaction of A_i (satisfaction can be negative).

Each dish has 0 or more "failure conditions" set for it. Multiple failure conditions may be set for the same dish.

A failure condition is represented by a pair (p, S) of a target dish p and a set of dishes S (not containing p), with the following meaning:

  • If, at the time dish p is started, all dishes in set S have already been completed, then dish p meets this failure condition and cannot be completed.

In particular, if S is the empty set, the failure condition is unconditionally met, so dish p can never be completed.

Takahashi selects any number of dishes he likes from the N types (he may select none), and makes them one by one in any order he likes. Each dish can be made at most once. However, if at the time he starts making a dish, even one of the failure conditions for that dish is met, the dish cannot be completed. Takahashi plans ahead and chooses to make only dishes that will certainly be completed, in order.

Formally, Takahashi selects a sequence of distinct dishes a_1, a_2, \dots, a_k (0 \leq k \leq N). This sequence is feasible if, for all t = 1, 2, \dots, k, every failure condition (a_t, S) related to dish a_t satisfies

S \nsubseteq \{ a_1, a_2, \dots, a_{t-1} \}

Here, \{a_1, \dots, a_{t-1}\} is the set of dishes completed before dish a_t, and when t = 1, it is the empty set (dish a_t itself is not included in this set).

When a feasible sequence is chosen, the total satisfaction obtained is

A_{a_1} + A_{a_2} + \cdots + A_{a_k}

(When k = 0, the satisfaction is 0.)

Find the maximum total satisfaction over all feasible sequences.

Constraints

  • 1 \leq N \leq 19
  • 0 \leq M \leq 10^5
  • -10^9 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq p_q \leq N (1 \leq q \leq M)
  • 0 \leq c_q \leq N - 1 (1 \leq q \leq M)
  • 1 \leq x_{q,j} \leq N (1 \leq q \leq M,\ 1 \leq j \leq c_q)
  • x_{q,j} \neq p_q (1 \leq q \leq M,\ 1 \leq j \leq c_q)
  • Within the same failure condition, x_{q,1}, x_{q,2}, \dots, x_{q,c_q} are all distinct
  • \displaystyle \sum_{q=1}^{M} c_q \leq 2 \times 10^5
  • All input values are integers

Input

N M
A_1 A_2 \cdots A_N
p_1 c_1 x_{1,1} x_{1,2} \cdots x_{1,c_1}
p_2 c_2 x_{2,1} x_{2,2} \cdots x_{2,c_2}
\vdots
p_M c_M x_{M,1} x_{M,2} \cdots x_{M,c_M}
  • The first line gives the number of dish types N and the number of failure conditions M, separated by a space.
  • The second line gives the satisfaction values A_1, A_2, \dots, A_N for each dish, separated by spaces.
  • The following M lines give the failure conditions. The q-th line (1 \leq q \leq M) gives the content of the q-th failure condition. If M = 0, this part does not exist.
  • First, the target dish number p_q and the number of elements in the set c_q are given, followed by c_q integers x_{q,1}, x_{q,2}, \dots, x_{q,c_q}. All are separated by spaces.
  • This represents the failure condition (p_q,\ \{x_{q,1}, x_{q,2}, \dots, x_{q,c_q}\}). That is, if at the time dish p_q is started, all of dishes x_{q,1}, x_{q,2}, \dots, x_{q,c_q} have already been completed, then dish p_q meets this failure condition.
  • If c_q = 0, the set S is the empty set, and this failure condition is unconditionally met (dish p_q can never be completed).

Output

Output in one line the maximum total satisfaction obtainable by a feasible sequence.


Sample Input 1

3 2
5 10 -3
2 1 1
3 0

Sample Output 1

15

Sample Input 2

4 3
-5 7 4 6
4 2 2 3
2 1 1
3 1 1

Sample Output 2

17

Sample Input 3

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

Sample Output 3

41

Sample Input 4

15 25
20 -7 35 14 -12 8 50 -30 22 5 -3 18 40 -25 11
7 2 1 3
3 1 7
13 3 2 4 6
5 0
8 1 9
9 2 10 11
12 4 1 5 7 9
15 2 13 14
2 1 15
4 3 2 8 12
10 2 6 7
11 1 10
6 5 1 2 3 4 5
14 2 7 13
1 4 4 8 12 15
13 1 7
7 3 5 9 11
9 0
15 5 1 3 5 7 9
12 2 10 14
4 1 1
6 2 8 11
2 3 3 4 5
10 4 1 9 13 15
11 2 6 12

Sample Output 4

201

Sample Input 5

1 1
1000000000
1 0

Sample Output 5

0