A - 連鎖するバケツ

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

配点 : 266

問題文

高橋君は N 個のバケツを使って、雨水を集める実験をしています。バケツには 1 から N までの番号が付けられており、バケツ i の容量は C_i リットルです。最初、すべてのバケツは空です。

バケツは一列に連結されており、水は次のように流れます。高橋君はバケツ 1 に合計 W リットルの水を連続的に注ぎます。バケツ i (1 \leq i \leq N-1) に流れ込んだ水は、バケツ i の容量 C_i に達するまではバケツ i に溜まります。バケツ i が満杯(容量 C_i リットルちょうどの水が溜まっている状態)になった後、さらにバケツ i に流れ込む水はすべてバケツ i+1 へと流れ込みます。バケツ N が満杯になった後にさらに流れ込む水は、すべて外に溢れて失われます。

すべての水を注ぎ終えた時点で、満杯になっているバケツの個数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq W \leq 10^{18}
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N W
C_1 C_2 \cdots C_N
  • 1 行目には、バケツの個数を表す整数 N と、注がれる水の総量(リットル)を表す整数 W が、スペース区切りで与えられる。
  • 2 行目には、各バケツの容量を表す整数 C_1, C_2, \ldots, C_N が、スペース区切りで与えられる。
  • C_i はバケツ i の容量(リットル)を表す。

出力

満杯になったバケツの個数を 1 行で出力してください。


入力例 1

3 10
3 4 5

出力例 1

2

入力例 2

5 25
5 10 3 8 6

出力例 2

3

入力例 3

6 1000000000000000000
100000000 200000000 300000000 400000000 500000000 600000000

出力例 3

6

Score : 266 pts

Problem Statement

Takahashi is conducting an experiment to collect rainwater using N buckets. The buckets are numbered from 1 to N, and bucket i has a capacity of C_i liters. Initially, all buckets are empty.

The buckets are connected in a single line, and water flows as follows. Takahashi continuously pours a total of W liters of water into bucket 1. Water that flows into bucket i (1 \leq i \leq N-1) accumulates in bucket i until it reaches bucket i's capacity C_i. After bucket i becomes full (exactly C_i liters of water have accumulated), any additional water that flows into bucket i will all flow into bucket i+1. After bucket N becomes full, any additional water that flows in will all overflow and be lost.

Determine the number of buckets that are full after all the water has been poured.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq W \leq 10^{18}
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers

Input

N W
C_1 C_2 \cdots C_N
  • The first line contains an integer N representing the number of buckets and an integer W representing the total amount of water (in liters) to be poured, separated by a space.
  • The second line contains integers C_1, C_2, \ldots, C_N representing the capacity of each bucket, separated by spaces.
  • C_i represents the capacity (in liters) of bucket i.

Output

Print the number of buckets that are full in a single line.


Sample Input 1

3 10
3 4 5

Sample Output 1

2

Sample Input 2

5 25
5 10 3 8 6

Sample Output 2

3

Sample Input 3

6 1000000000000000000
100000000 200000000 300000000 400000000 500000000 600000000

Sample Output 3

6
B - 本の貸し出し

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

配点 : 333

問題文

高橋君は学校の図書委員です。今日は N 人の生徒が図書室に本を借りに来ています。

図書室では M 種類の本を貸し出しています。各本 i1 \leq i \leq M)には「面白さ」 S_i と「難易度」 R_i が設定されています。難易度とは、その本を読むために必要な読解力のレベルを表します。それぞれの種類の本は十分な冊数が用意されているため、同じ種類の本を複数の生徒に同時に貸し出すことができます。

各生徒 j1 \leq j \leq N)には「読解力」 T_j が定められており、生徒 j は難易度が T_j 以下の本のみ読むことができます。

高橋君は、各生徒に対して、その生徒が読める本の中から面白さが最も高い本を 1 冊選んで貸し出します。面白さが最も高い本が複数ある場合は、そのうちどれを選んでも構いません。読める本が 1 冊もない生徒には本を貸し出しません。

すべての生徒に対してこのように本を貸し出したとき、貸し出された本の面白さの合計を求めてください。本を借りなかった生徒については 0 として合計します。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9
  • 1 \leq R_i \leq 10^9
  • 1 \leq T_j \leq 10^9
  • 入力はすべて整数

入力

N M
S_1 R_1
S_2 R_2
\vdots
S_M R_M
T_1 T_2 \ldots T_N
  • 1 行目には、生徒の人数を表す N と、本の種類数を表す M が、スペース区切りで与えられる。
  • 2 行目から M + 1 行目では、各本の情報が与えられる。
  • 1 + i 行目では、本 i の面白さ S_i と難易度 R_i が、スペース区切りで与えられる。
  • M + 2 行目では、各生徒の読解力 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。

出力

すべての生徒に貸し出された本の面白さの合計を 1 行で出力してください。


入力例 1

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

出力例 1

18

入力例 2

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

出力例 2

57

入力例 3

10 8
100 50
30 10
80 40
50 20
200 100
60 30
90 60
10 5
5 15 25 35 45 55 70 100 3 50

出力例 3

730

Score : 333 pts

Problem Statement

Takahashi is a library committee member at his school. Today, N students have come to the library to borrow books.

The library lends out M types of books. Each book i (1 \leq i \leq M) has an "interestingness" S_i and a "difficulty" R_i. The difficulty represents the reading comprehension level required to read that book. There are sufficiently many copies of each type of book, so the same type of book can be lent to multiple students simultaneously.

Each student j (1 \leq j \leq N) has a "reading comprehension" T_j, and student j can only read books whose difficulty is at most T_j.

For each student, Takahashi selects and lends one book with the highest interestingness among the books that student can read. If there are multiple books with the highest interestingness, any of them may be chosen. Students who cannot read any books are not lent a book.

When books are lent to all students in this manner, find the total interestingness of the books that were lent out. For students who did not borrow a book, count their contribution as 0 in the total.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9
  • 1 \leq R_i \leq 10^9
  • 1 \leq T_j \leq 10^9
  • All input values are integers

Input

N M
S_1 R_1
S_2 R_2
\vdots
S_M R_M
T_1 T_2 \ldots T_N
  • The first line contains N, the number of students, and M, the number of types of books, separated by a space.
  • From the 2nd line to the (M + 1)-th line, the information for each book is given.
  • The (1 + i)-th line contains the interestingness S_i and difficulty R_i of book i, separated by a space.
  • The (M + 2)-th line contains the reading comprehension values T_1, T_2, \ldots, T_N of each student, separated by spaces.

Output

Print the total interestingness of the books lent to all students on a single line.


Sample Input 1

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

Sample Output 1

18

Sample Input 2

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

Sample Output 2

57

Sample Input 3

10 8
100 50
30 10
80 40
50 20
200 100
60 30
90 60
10 5
5 15 25 35 45 55 70 100 3 50

Sample Output 3

730
C - 水やり

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

配点 : 366

問題文

高橋君は植物園の管理人です。植物園には N 本の植物があり、植物には 1 から N までの番号が付けられています。これらの植物は木構造で管理されており、植物 1 を根とする根付き木を成しています。

各植物 i2 \leq i \leq N)について、植物 P_i は植物 i の親です。

各植物は水分量と呼ばれる値を持っており、すべての植物の水分量は最初 0 です。

高橋君は Q 回の水やり作業を行います。j 回目(1 \leq j \leq Q)の水やりでは、植物 X_j を根とする部分木に含まれるすべての植物の水分量を D_j だけ増加させます。ここで、植物 X_j を根とする部分木とは、植物 X_j 自身と、植物 X_j のすべての子孫からなる集合です。

Q 回の水やり作業がすべて行われた後の、各植物の水分量を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq P_i \leq i - 12 \leq i \leq N
  • 1 \leq X_j \leq N1 \leq j \leq Q
  • 1 \leq D_j \leq 10^91 \leq j \leq Q
  • 入力はすべて整数である。

入力

N Q
P_2 P_3 \ldots P_N
X_1 D_1
X_2 D_2
\vdots
X_Q D_Q
  • 1 行目には、植物の本数を表す整数 N と、水やり作業の回数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、植物 2 から植物 N までの親を表す N - 1 個の整数 P_2, P_3, \ldots, P_N が、スペース区切りで与えられる。P_i は植物 i の親の番号である。N = 1 のときは、この行には何も書かれていない(空行が与えられる)。
  • 3 行目から Q 行にわたって、水やり作業の内容が与えられる。2 + j 行目には、j 回目の水やり対象の植物の番号 X_j と水分増加量 D_j が、スペース区切りで与えられる。

出力

N 個の整数をスペース区切りで 1 行に出力せよ。i 番目の値は、すべての水やり作業実行後の植物 i の水分量である。


入力例 1

5 2
1 1 2 2
1 10
2 5

出力例 1

10 15 10 15 15

入力例 2

7 3
1 1 2 2 3 3
3 20
1 5
5 100

出力例 2

5 5 25 5 105 25 25

入力例 3

10 4
1 2 3 3 3 1 7 7 9
1 3
3 7
7 2
9 10

出力例 3

3 3 10 10 10 10 5 5 15 15

Score : 366 pts

Problem Statement

Takahashi is a caretaker of a botanical garden. The botanical garden has N plants, numbered from 1 to N. These plants are managed in a tree structure, forming a rooted tree with plant 1 as the root.

For each plant i (2 \leq i \leq N), plant P_i is the parent of plant i.

Each plant has a value called moisture level, and the moisture level of every plant is initially 0.

Takahashi performs Q watering operations. In the j-th operation (1 \leq j \leq Q), he increases the moisture level of all plants in the subtree rooted at plant X_j by D_j. Here, the subtree rooted at plant X_j is the set consisting of plant X_j itself and all descendants of plant X_j.

Determine the moisture level of each plant after all Q watering operations have been performed.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq P_i \leq i - 1 (2 \leq i \leq N)
  • 1 \leq X_j \leq N (1 \leq j \leq Q)
  • 1 \leq D_j \leq 10^9 (1 \leq j \leq Q)
  • All input values are integers.

Input

N Q
P_2 P_3 \ldots P_N
X_1 D_1
X_2 D_2
\vdots
X_Q D_Q
  • The first line contains an integer N representing the number of plants and an integer Q representing the number of watering operations, separated by a space.
  • The second line contains N - 1 integers P_2, P_3, \ldots, P_N separated by spaces, representing the parents of plants 2 through N. P_i is the number of the parent of plant i. When N = 1, nothing is written on this line (an empty line is given).
  • Over the next Q lines starting from the third line, the details of the watering operations are given. The (2 + j)-th line contains the plant number X_j targeted by the j-th watering operation and the moisture increase amount D_j, separated by a space.

Output

Output N integers separated by spaces on a single line. The i-th value should be the moisture level of plant i after all watering operations have been performed.


Sample Input 1

5 2
1 1 2 2
1 10
2 5

Sample Output 1

10 15 10 15 15

Sample Input 2

7 3
1 1 2 2 3 3
3 20
1 5
5 100

Sample Output 2

5 5 25 5 105 25 25

Sample Input 3

10 4
1 2 3 3 3 1 7 7 9
1 3
3 7
7 2
9 10

Sample Output 3

3 3 10 10 10 10 5 5 15 15
D - 展示会場の広告配置

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

配点 : 400

問題文

高橋君は、展示会場の長い廊下に広告パネルを配置する仕事を任されました。廊下は 1 メートルごとにスロットが区切られており、全長 L メートルです。つまり、スロット 1, 2, \ldots, L が一列に並んでいます。

高橋君は N 枚の広告パネルを候補として持っています。N 枚の広告パネルはすべて区別されており、それぞれ設置するかしないかを独立に選べます。各パネルは最大 1 回まで設置でき、設置する場合は廊下上の好きな位置に(後述の制約を満たす限り)自由に配置できます。パネルを配置する順序や番号順による制約はありません。

i 番目の広告パネル(1 \leq i \leq N)は連続する w_i 個のスロットを占有します。具体的には、i 番目の広告パネルの左端をスロット s に合わせて設置すると、スロット s, s+1, \ldots, s+w_i-1 を占有します(ただし 1 \leq s かつ s + w_i - 1 \leq L でなければなりません)。i 番目の広告パネルを設置した場合、集客効果 b_i が得られます。設置しなかったパネルからは集客効果は得られません。

広告パネル同士は重なって配置することはできません。すなわち、設置された異なる 2 つの広告パネルが同じスロットを同時に占有することはできません。重ならない限り、隣接して配置することは問題ありません。

選んだ広告パネルの配置が決まると、どの広告パネルにも占有されていないスロットが 空きスロット となります。1 枚も設置しない場合は、すべてのスロットが空きスロットです。

この廊下には防災上の規定があります。廊下の壁面には一定間隔で避難誘導灯が設置されており、空きスロットが長く続く区間では、来場者が誘導灯に注意を向けにくくなるおそれがあります。そのため、以下の 防災規定 を満たさなければなりません:

廊下上で空きスロットが K 個以上連続して存在してはならない。すなわち、連続する空きスロットからなる極大な区間の長さは、どの箇所においても K-1 以下でなければならない。

この制約は廊下の両端付近にも適用されます。例えば、最も左に設置された広告パネルの左端がスロット s であるとき、スロット 1 からスロット s-1 まではすべて空きスロットであり、これらは連続する s-1 個の空きスロットとなるため、s - 1 \leq K - 1 でなければなりません。同様に、最も右に設置された広告パネルの右端がスロット t であるとき、スロット t+1 からスロット L までの L-t 個の連続する空きスロットについても L - t \leq K - 1 でなければなりません。

広告パネルを 1 枚も設置しない場合は、スロット 1 からスロット L までの L 個すべてが連続する空きスロットとなります。したがって、L \leq K - 1(すなわち L < K)のときに限り、1 枚も設置しない配置は防災規定を満たします。このとき集客効果の合計は 0 です。

高橋君は、防災規定を満たしつつ、設置した広告パネルの集客効果の合計を最大化したいと考えています。

防災規定を満たすような広告パネルの選び方と配置が 1 つ以上存在するならば、集客効果の合計の最大値を出力してください。防災規定を満たす配置が 1 つも存在しない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 400
  • 1 \leq L \leq 1000
  • 1 \leq K \leq L
  • 1 \leq w_i \leq L1 \leq i \leq N
  • 1 \leq b_i \leq 10^61 \leq i \leq N
  • 入力はすべて整数である。

入力

N L K
w_1 b_1
w_2 b_2
\vdots
w_N b_N
  • 1 行目には、広告パネルの候補の枚数 N、廊下の全長 L、空きスロットの連続個数の上限に関する値 K が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各広告パネルの情報が与えられる。1 + i 行目(1 \leq i \leq N)には、i 番目の広告パネルが占有するスロット数 w_i と集客効果 b_i がスペース区切りで与えられる。

出力

防災規定を満たす配置が存在する場合は、集客効果の合計の最大値を 1 行で出力せよ。存在しない場合は -11 行で出力せよ。


入力例 1

4 8 3
2 5
3 8
1 2
4 7

出力例 1

17

入力例 2

3 5 1
2 10
4 20
4 30

出力例 2

-1

入力例 3

12 30 5
3 12
7 25
4 14
6 18
2 7
5 16
8 30
1 3
9 28
4 13
10 35
6 20

出力例 3

109

入力例 4

30 100 7
5 120
12 260
7 150
20 500
3 60
15 330
8 180
10 210
6 140
25 620
4 90
18 410
9 200
11 240
13 290
2 45
16 370
14 310
7 170
5 115
22 540
19 430
1 20
30 750
6 135
10 230
17 390
8 175
24 580
12 255

出力例 4

2475

入力例 5

1 1 1
1 100

出力例 5

100

Score : 400 pts

Problem Statement

Takahashi has been assigned the task of placing advertisement panels along a long corridor in an exhibition hall. The corridor is divided into slots every 1 meter, with a total length of L meters. That is, slots 1, 2, \ldots, L are lined up in a row.

Takahashi has N advertisement panels as candidates. All N advertisement panels are distinct, and he can independently choose whether or not to install each one. Each panel can be installed at most once, and if installed, it can be freely placed at any position along the corridor (as long as the constraints described below are satisfied). There are no constraints based on the order of placement or panel numbering.

The i-th advertisement panel (1 \leq i \leq N) occupies w_i consecutive slots. Specifically, if the i-th advertisement panel is installed with its left edge aligned to slot s, it occupies slots s, s+1, \ldots, s+w_i-1 (where 1 \leq s and s + w_i - 1 \leq L must hold). If the i-th advertisement panel is installed, it provides an attraction effect of b_i. Panels that are not installed provide no attraction effect.

Advertisement panels cannot overlap each other. That is, two different installed advertisement panels cannot occupy the same slot simultaneously. As long as they do not overlap, placing them adjacent to each other is acceptable.

Once the placement of the chosen advertisement panels is determined, any slot not occupied by any advertisement panel becomes a vacant slot. If no panels are installed, all slots are vacant slots.

This corridor has fire safety regulations. Emergency guidance lights are installed at regular intervals on the corridor walls, and in sections where vacant slots continue for a long stretch, there is a concern that visitors may not pay attention to the guidance lights. Therefore, the following fire safety regulation must be satisfied:

There must not be K or more consecutive vacant slots on the corridor. That is, the length of any maximal interval of consecutive vacant slots must be K-1 or less at every location.

This constraint also applies near both ends of the corridor. For example, if the left edge of the leftmost installed advertisement panel is at slot s, then slots 1 through s-1 are all vacant slots, forming s-1 consecutive vacant slots, so s - 1 \leq K - 1 must hold. Similarly, if the right edge of the rightmost installed advertisement panel is at slot t, then the L-t consecutive vacant slots from slot t+1 to slot L must also satisfy L - t \leq K - 1.

If no advertisement panels are installed, all L slots from slot 1 to slot L are consecutive vacant slots. Therefore, a placement with no panels installed satisfies the fire safety regulation only when L \leq K - 1 (i.e., L < K). In this case, the total attraction effect is 0.

Takahashi wants to maximize the total attraction effect of the installed advertisement panels while satisfying the fire safety regulation.

If there exists at least one way to select and place advertisement panels that satisfies the fire safety regulation, output the maximum total attraction effect. If no placement satisfying the fire safety regulation exists, output -1.

Constraints

  • 1 \leq N \leq 400
  • 1 \leq L \leq 1000
  • 1 \leq K \leq L
  • 1 \leq w_i \leq L (1 \leq i \leq N)
  • 1 \leq b_i \leq 10^6 (1 \leq i \leq N)
  • All input values are integers.

Input

N L K
w_1 b_1
w_2 b_2
\vdots
w_N b_N
  • The first line contains the number of candidate advertisement panels N, the total length of the corridor L, and the value K related to the upper limit on the number of consecutive vacant slots, separated by spaces.
  • From the 2nd line to the (N + 1)-th line, information about each advertisement panel is given. The (1 + i)-th line (1 \leq i \leq N) contains the number of slots w_i occupied by the i-th advertisement panel and its attraction effect b_i, separated by spaces.

Output

If a placement satisfying the fire safety regulation exists, output the maximum total attraction effect in one line. If no such placement exists, output -1 in one line.


Sample Input 1

4 8 3
2 5
3 8
1 2
4 7

Sample Output 1

17

Sample Input 2

3 5 1
2 10
4 20
4 30

Sample Output 2

-1

Sample Input 3

12 30 5
3 12
7 25
4 14
6 18
2 7
5 16
8 30
1 3
9 28
4 13
10 35
6 20

Sample Output 3

109

Sample Input 4

30 100 7
5 120
12 260
7 150
20 500
3 60
15 330
8 180
10 210
6 140
25 620
4 90
18 410
9 200
11 240
13 290
2 45
16 370
14 310
7 170
5 115
22 540
19 430
1 20
30 750
6 135
10 230
17 390
8 175
24 580
12 255

Sample Output 4

2475

Sample Input 5

1 1 1
1 100

Sample Output 5

100
E - グループ分けとウイルス感染

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

配点 : 466

問題文

高橋君は N 台のコンピュータからなる社内ネットワークの管理者です。各コンピュータには 1 から N までの番号が付いています。

このネットワークには M 種類のコンピュータウイルスが存在することが判明しました。各ウイルスには 1 から M までの番号が付いています。

ウイルス j1 \leq j \leq M)は、特定のコンピュータの集合 S_j に感染する能力を持っています。S_j は空であることもあります。

高橋君は、N 台のコンピュータをいくつかのサブネットワーク(グループ)に分割し、グループ間の通信を遮断するファイアウォールを設置することで被害を抑えようとしています。ここでグループ分けとは、N 台のコンピュータの集合を 1 つ以上の空でないグループに分けることであり、すべてのコンピュータがちょうど 1 つのグループに属するものとします。グループの数は 1 以上 N 以下の任意の数を選べます。

各ウイルスの被害は、以下のルールにより互いに独立に判定されます。

  • ウイルス j の感染対象 S_j に含まれるコンピュータがすべて同一のグループに属している場合、ウイルス j はファイアウォールに阻まれることなくグループ内で拡散し、S_j に含まれるすべてのコンピュータが被害を受けます。
  • ウイルス j の感染対象 S_j に含まれるコンピュータが 2 つ以上の異なるグループにまたがっている場合、ウイルス j はファイアウォールによって拡散が阻止され、S_j のどのコンピュータにも被害を与えません。

ここで、S_j が空の場合や S_j に含まれるコンピュータが 1 台のみの場合は、S_j のコンピュータはすべて同一のグループに属しているとみなします。(S_j が空の場合、被害を受けるコンピュータはありません。S_j1 台のみ含まれる場合、そのコンピュータはどのグループ分けでも必ず被害を受けます。)

すべての M 種類のウイルスによる被害を考えたとき、1 種類以上のウイルスから被害を受けるコンピュータの台数(同じコンピュータが複数のウイルスから被害を受けても 1 台と数えます)を最小化したいです。

最適なグループ分けを行ったときの、被害を受けるコンピュータの台数の最小値を求めてください。

制約

  • 1 \leq N \leq 15
  • 1 \leq M \leq 500
  • 0 \leq k_j \leq N1 \leq j \leq M
  • 1 \leq s_{j,i} \leq N1 \leq j \leq M, 1 \leq i \leq k_j
  • ウイルス j の感染対象のコンピュータ番号 s_{j,1}, s_{j,2}, \ldots, s_{j,k_j} はすべて異なる
  • 入力はすべて整数である

入力

N M
k_1 s_{1,1} s_{1,2} \ldots s_{1,k_1}
k_2 s_{2,1} s_{2,2} \ldots s_{2,k_2}
\vdots
k_M s_{M,1} s_{M,2} \ldots s_{M,k_M}
  • 1 行目には、コンピュータの台数を表す N と、ウイルスの種類数を表す M が、スペース区切りで与えられる。
  • 2 行目から M 行にわたって、各ウイルスの情報が与えられる。
  • 1 + j 行目では、ウイルス j の感染対象のコンピュータの台数 k_j と、それらのコンピュータの番号 s_{j,1}, s_{j,2}, \ldots, s_{j,k_j} がスペース区切りで与えられる。k_j = 0 の場合、その行には k_j のみが与えられる。

出力

最適なグループ分けを行ったときの、被害を受けるコンピュータの台数の最小値を 1 行で出力せよ。


入力例 1

3 3
2 1 2
2 2 3
1 1

出力例 1

1

入力例 2

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

出力例 2

0

入力例 3

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

出力例 3

1

入力例 4

15 25
0
1 1
1 15
2 1 2
2 2 3
3 1 3 5
3 4 5 6
4 1 4 7 10
4 2 5 8 11
5 3 6 9 12 15
5 1 5 9 13 14
6 2 4 6 8 10 12
6 3 5 7 9 11 13
7 1 2 3 4 5 6 7
7 9 10 11 12 13 14 15
8 1 3 5 7 9 11 13 15
8 2 4 6 8 10 12 14 15
9 1 2 4 5 7 8 10 11 13
10 3 4 5 6 7 8 9 10 11 12
11 1 2 3 5 6 7 9 10 11 13 14
12 1 2 3 4 6 7 8 9 11 12 13 14
13 1 2 3 4 5 7 8 9 10 11 13 14 15
14 1 2 3 4 5 6 8 9 10 11 12 13 14 15
15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
3 13 14 15

出力例 4

2

入力例 5

1 1
0

出力例 5

0

Score : 466 pts

Problem Statement

Takahashi is the administrator of a corporate network consisting of N computers. Each computer is numbered from 1 to N.

It has been discovered that M types of computer viruses exist in this network. Each virus is numbered from 1 to M.

Virus j (1 \leq j \leq M) has the ability to infect a specific set of computers S_j. S_j may be empty.

Takahashi wants to mitigate the damage by dividing the N computers into several subnetworks (groups) and installing firewalls to block communication between groups. Here, a grouping means partitioning the set of N computers into one or more non-empty groups, where every computer belongs to exactly one group. The number of groups can be any number from 1 to N.

The damage from each virus is determined independently according to the following rules:

  • If all computers in virus j's infection target S_j belong to the same group, virus j spreads within the group without being blocked by any firewall, and all computers in S_j are damaged.
  • If the computers in virus j's infection target S_j are spread across two or more different groups, virus j's spread is blocked by the firewall, and no computer in S_j is damaged.

Here, if S_j is empty or contains only one computer, all computers in S_j are considered to belong to the same group. (If S_j is empty, no computer is damaged. If S_j contains only one computer, that computer is always damaged regardless of the grouping.)

Considering the damage from all M types of viruses, we want to minimize the number of computers that are damaged by one or more viruses (even if the same computer is damaged by multiple viruses, it is counted as one).

Find the minimum number of damaged computers when the optimal grouping is chosen.

Constraints

  • 1 \leq N \leq 15
  • 1 \leq M \leq 500
  • 0 \leq k_j \leq N (1 \leq j \leq M)
  • 1 \leq s_{j,i} \leq N (1 \leq j \leq M, 1 \leq i \leq k_j)
  • The computer numbers s_{j,1}, s_{j,2}, \ldots, s_{j,k_j} in the infection target of virus j are all distinct
  • All input values are integers

Input

N M
k_1 s_{1,1} s_{1,2} \ldots s_{1,k_1}
k_2 s_{2,1} s_{2,2} \ldots s_{2,k_2}
\vdots
k_M s_{M,1} s_{M,2} \ldots s_{M,k_M}
  • The first line contains N, the number of computers, and M, the number of virus types, separated by a space.
  • The following M lines provide information about each virus.
  • The (1 + j)-th line contains k_j, the number of computers in the infection target of virus j, followed by the computer numbers s_{j,1}, s_{j,2}, \ldots, s_{j,k_j}, separated by spaces. If k_j = 0, only k_j is given on that line.

Output

Print in one line the minimum number of damaged computers when the optimal grouping is chosen.


Sample Input 1

3 3
2 1 2
2 2 3
1 1

Sample Output 1

1

Sample Input 2

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

Sample Output 2

0

Sample Input 3

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

Sample Output 3

1

Sample Input 4

15 25
0
1 1
1 15
2 1 2
2 2 3
3 1 3 5
3 4 5 6
4 1 4 7 10
4 2 5 8 11
5 3 6 9 12 15
5 1 5 9 13 14
6 2 4 6 8 10 12
6 3 5 7 9 11 13
7 1 2 3 4 5 6 7
7 9 10 11 12 13 14 15
8 1 3 5 7 9 11 13 15
8 2 4 6 8 10 12 14 15
9 1 2 4 5 7 8 10 11 13
10 3 4 5 6 7 8 9 10 11 12
11 1 2 3 5 6 7 9 10 11 13 14
12 1 2 3 4 6 7 8 9 11 12 13 14
13 1 2 3 4 5 7 8 9 10 11 13 14 15
14 1 2 3 4 5 6 8 9 10 11 12 13 14 15
15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
3 13 14 15

Sample Output 4

2

Sample Input 5

1 1
0

Sample Output 5

0