A - 数値列の一致

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

配点 : 233 点

問題文

高橋君は長さ N の整数列 A = (A_1, A_2, \dots, A_N) を、青木君は同じく長さ N の整数列 B = (B_1, B_2, \dots, B_N) を持っています。

高橋君と青木君は、2人の整数列を各要素ごとに一致させたいと考えています。そのために、以下の操作を何度でも繰り返し行うことができます。なお、同じインデックスを複数回選ぶこともできます。

1回の操作では、インデックス i(1 \leq i \leq N)を1つ選び、以下の4つのうちちょうど1つを行います。

  • A_i の値を 1 増やす
  • A_i の値を 1 減らす
  • B_i の値を 1 増やす
  • B_i の値を 1 減らす

すべての i(1 \leq i \leq N)について A_i = B_i となる状態にするために必要な操作回数の最小値を求めてください。

制約

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

入力

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
  • 1 行目には、整数列の長さを表す整数 N が与えられる。
  • 2 行目には、高橋君の整数列の要素 A_1, A_2, \dots, A_N がスペース区切りで与えられる。
  • 3 行目には、青木君の整数列の要素 B_1, B_2, \dots, B_N がスペース区切りで与えられる。

出力

すべての i について A_i = B_i とするために必要な操作回数の最小値を 1 行で出力せよ。


入力例 1

3
1 3 5
2 1 5

出力例 1

3

入力例 2

5
3 -1 0 7 -5
-2 4 0 10 -5

出力例 2

13

入力例 3

8
1000000000 -999999999 500000000 0 -100 42 -7 123456789
-1000000000 999999999 -500000000 0 200 -58 7 -987654321

出力例 3

6111111522

Score : 233 pts

Problem Statement

Takahashi has an integer sequence of length N: A = (A_1, A_2, \dots, A_N), and Aoki has an integer sequence of the same length N: B = (B_1, B_2, \dots, B_N).

Takahashi and Aoki want to make their two integer sequences match element by element. To achieve this, they can perform the following operation any number of times. The same index may be chosen multiple times.

In one operation, choose an index i (1 \leq i \leq N) and perform exactly one of the following four actions:

  • Increase the value of A_i by 1
  • Decrease the value of A_i by 1
  • Increase the value of B_i by 1
  • Decrease the value of B_i by 1

Find the minimum number of operations required to reach a state where A_i = B_i for all i (1 \leq i \leq N).

Constraints

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

Input

N
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
  • The first line contains an integer N representing the length of the integer sequences.
  • The second line contains the elements of Takahashi's integer sequence A_1, A_2, \dots, A_N, separated by spaces.
  • The third line contains the elements of Aoki's integer sequence B_1, B_2, \dots, B_N, separated by spaces.

Output

Print the minimum number of operations required to make A_i = B_i for all i, on a single line.


Sample Input 1

3
1 3 5
2 1 5

Sample Output 1

3

Sample Input 2

5
3 -1 0 7 -5
-2 4 0 10 -5

Sample Output 2

13

Sample Input 3

8
1000000000 -999999999 500000000 0 -100 42 -7 123456789
-1000000000 999999999 -500000000 0 200 -58 7 -987654321

Sample Output 3

6111111522
B - 果樹園の収穫

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

配点 : 333 点

問題文

高橋君は N 個の果樹園を管理しています。

果樹園 i (1 \le i \le N) には現在 A_i 個の果物が実っています。

高橋君は、明日から K 日間、毎日ちょうど 1 つの果樹園を選んで収穫作業を行います。同じ果樹園を複数の日に選ぶこともでき、果物が 0 個の果樹園を選ぶことも許されます。

果樹園 i で 1 日収穫作業を行うと、その果樹園に現在残っている果物の数と B_i のうち小さい方の個数だけ果物を収穫できます。すなわち、作業前にその果樹園に r 個の果物が残っているとき、\min(r, B_i) 個を収穫し、残りは r - \min(r, B_i) 個になります。

果物が新たに実ることはなく、各果樹園の果物は収穫によってのみ減少します。

高橋君が K 日間の作業先を最適に選んだとき、合計で最大何個の果物を収穫できるか求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{18}
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq B_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N K
A_1 B_1
A_2 B_2
\vdots
A_N B_N

1 行目には、果樹園の数 N と作業日数 K がスペース区切りで与えられます。

続く N 行のうち i 行目 (1 \leq i \leq N) には、果樹園 i の果物の数 A_i と 1 日あたりの収穫可能数 B_i がスペース区切りで与えられます。

出力

収穫できる果物の個数の合計の最大値を 1 行で出力してください。


入力例 1

3 4
5 2
3 5
10 4

出力例 1

13

入力例 2

2 7
4 3
2 1

出力例 2

6

入力例 3

8 15
12 3
7 10
25 4
9 2
14 7
30 6
5 1
18 5

出力例 3

82

入力例 4

20 60
45 6
120 15
78 9
33 4
200 25
17 3
96 8
150 20
64 7
89 11
210 30
58 5
132 12
41 10
73 6
185 18
27 2
99 13
160 16
52 14

出力例 4

1143

入力例 5

1 1000000000000000000
1000000000 1000000000

出力例 5

1000000000

Score : 333 pts

Problem Statement

Takahashi manages N orchards.

Orchard i (1 \le i \le N) currently has A_i fruits growing on its trees.

Starting from tomorrow, Takahashi will perform harvesting work over K days, choosing exactly 1 orchard each day. He may choose the same orchard on multiple days, and he is also allowed to choose an orchard with 0 fruits.

When harvesting work is performed at orchard i for one day, Takahashi can harvest a number of fruits equal to the smaller of the number of fruits currently remaining in that orchard and B_i. In other words, if there are r fruits remaining in the orchard before the work, he harvests \min(r, B_i) fruits, and the remaining number of fruits becomes r - \min(r, B_i).

No new fruits grow, and the number of fruits in each orchard only decreases through harvesting.

When Takahashi optimally chooses which orchards to work at over the K days, find the maximum total number of fruits he can harvest.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{18}
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq B_i \leq 10^9 (1 \leq i \leq N)
  • All input values 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 orchards N and the number of work days K, separated by a space.

The following N lines, where the i-th line (1 \leq i \leq N), contain the number of fruits A_i in orchard i and the number of fruits harvestable per day B_i, separated by a space.

Output

Print the maximum total number of fruits that can be harvested, on a single line.


Sample Input 1

3 4
5 2
3 5
10 4

Sample Output 1

13

Sample Input 2

2 7
4 3
2 1

Sample Output 2

6

Sample Input 3

8 15
12 3
7 10
25 4
9 2
14 7
30 6
5 1
18 5

Sample Output 3

82

Sample Input 4

20 60
45 6
120 15
78 9
33 4
200 25
17 3
96 8
150 20
64 7
89 11
210 30
58 5
132 12
41 10
73 6
185 18
27 2
99 13
160 16
52 14

Sample Output 4

1143

Sample Input 5

1 1000000000000000000
1000000000 1000000000

Sample Output 5

1000000000
C - カードの重ね置き

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

配点 : 366 点

問題文

高橋君はカードゲームが好きで、数字が書かれたカードを机の上に重ねて置いていきます。

カードにはそれぞれ正の整数が書かれています。高橋君はカードを積み重ねて山を作ります。新しいカードを置くときは山の一番上に重ね、カードを取り除くときは一番上(最後に置いたもの)からしか取れません。取り除いたカードは捨てられ、以降の操作には一切関与しません。

はじめ、山にはカードが 1 枚もありません。高橋君は N 回の操作を順番に行います。各操作は以下の 3 種類のいずれかです。

  • 追加操作 PUT c:正の整数 c が書かれたカードを山の一番上に置く。
  • 除去操作 REMOVE:山の一番上のカードを 1 枚取り除いて捨てる。この操作が行われる時点で、山にカードが 1 枚以上あることが保証される。
  • 確認操作 LOOK:現在の山に積まれているすべてのカードに書かれた数字の ビットごとの XOR(排他的論理和)を求める。この操作が行われる時点で、山にカードが 1 枚以上あることが保証される。山にカードが 1 枚だけの場合、XOR の値はそのカードに書かれた数字そのものである。

N 回の操作のうち確認操作だけを出現順に抜き出し、1, 2, 3, \ldots, K と番号を付けます(K は確認操作の総回数です)。i 回目の確認操作で得られた XOR 値を v_i とします。

各 i = 1, 2, \ldots, K について、v_j = v_i かつ j < i を満たす最大の整数 j を求めてください。すなわち、i 回目の確認操作と同じ XOR 値が得られた直近の過去の確認操作の番号を答えてください。そのような j が存在しない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 各追加操作で与えられる c は 1 \leq c \leq 10^9 を満たす。
  • REMOVE 操作が行われる時点で、山にカードが 1 枚以上ある。
  • LOOK 操作が行われる時点で、山にカードが 1 枚以上ある。
  • N 回の操作のうち、LOOK 操作は 1 回以上含まれる(すなわち K \geq 1)。
  • 入力で与えられる数値はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N
\mathrm{event}_1
\mathrm{event}_2
\vdots
\mathrm{event}_N
  • 1 行目には、操作の総回数を表す整数 N が与えられる。
  • 続く N 行のうち i 行目には、i 番目の操作 \mathrm{event}_i が以下のいずれかの形式で与えられる。
  • 追加操作の場合:PUT c(文字列 PUT の後にスペース区切りで正の整数 c が続く)
  • 除去操作の場合:REMOVE
  • 確認操作の場合:LOOK

出力

確認操作の総回数を K とする。K 行を出力せよ。

i 行目 (1 \leq i \leq K) には、i 回目の確認操作で得られた XOR 値を v_i として、v_j = v_i かつ j < i を満たす最大の整数 j を出力せよ。そのような j が存在しなければ -1 を出力せよ。


入力例 1

7
PUT 3
PUT 5
LOOK
PUT 6
LOOK
REMOVE
LOOK

出力例 1

-1
-1
1

入力例 2

6
PUT 1
LOOK
PUT 2
LOOK
PUT 4
LOOK

出力例 2

-1
-1
-1

入力例 3

15
PUT 10
PUT 7
LOOK
REMOVE
LOOK
PUT 3
LOOK
PUT 9
LOOK
REMOVE
REMOVE
LOOK
PUT 5
LOOK
REMOVE

出力例 3

-1
-1
-1
-1
2
-1

入力例 4

24
PUT 1
PUT 2
PUT 4
LOOK
REMOVE
LOOK
REMOVE
LOOK
PUT 8
LOOK
PUT 6
LOOK
REMOVE
LOOK
REMOVE
LOOK
PUT 3
LOOK
PUT 2
LOOK
REMOVE
LOOK
REMOVE
LOOK

出力例 4

-1
-1
-1
-1
-1
4
3
-1
-1
8
7

入力例 5

2
PUT 1000000000
LOOK

出力例 5

-1

Score : 366 pts

Problem Statement

Takahashi loves card games and places cards with numbers written on them by stacking them on a desk.

Each card has a positive integer written on it. Takahashi builds a pile by stacking cards. When placing a new card, he puts it on top of the pile, and when removing a card, he can only take the topmost one (the one placed most recently). Removed cards are discarded and are not involved in any subsequent operations.

Initially, the pile contains no cards. Takahashi performs N operations in order. Each operation is one of the following three types:

  • Put operation PUT c: Place a card with the positive integer c written on it on top of the pile.
  • Remove operation REMOVE: Remove the topmost card from the pile and discard it. It is guaranteed that the pile contains at least one card when this operation is performed.
  • Look operation LOOK: Compute the bitwise XOR (exclusive or) of the numbers written on all cards currently in the pile. It is guaranteed that the pile contains at least one card when this operation is performed. If the pile contains exactly one card, the XOR value is the number written on that card itself.

Extract only the look operations from the N operations in the order they appear, and number them 1, 2, 3, \ldots, K (where K is the total number of look operations). Let v_i be the XOR value obtained by the i-th look operation.

For each i = 1, 2, \ldots, K, find the largest integer j such that v_j = v_i and j < i. In other words, find the number of the most recent past look operation that yielded the same XOR value as the i-th look operation. If no such j exists, output -1.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • The value c given in each put operation satisfies 1 \leq c \leq 10^9.
  • When a REMOVE operation is performed, the pile contains at least one card.
  • When a LOOK operation is performed, the pile contains at least one card.
  • Among the N operations, at least one LOOK operation is included (i.e., K \geq 1).
  • All numerical values given in the input are integers.

Input

The input is given from standard input in the following format:

N
\mathrm{event}_1
\mathrm{event}_2
\vdots
\mathrm{event}_N
  • The first line contains an integer N representing the total number of operations.
  • Among the following N lines, the i-th line contains the i-th operation \mathrm{event}_i in one of the following formats:
  • For a put operation: PUT c (the string PUT followed by a positive integer c separated by a space)
  • For a remove operation: REMOVE
  • For a look operation: LOOK

Output

Let K be the total number of look operations. Output K lines.

On the i-th line (1 \leq i \leq K), let v_i be the XOR value obtained by the i-th look operation, and output the largest integer j such that v_j = v_i and j < i. If no such j exists, output -1.


Sample Input 1

7
PUT 3
PUT 5
LOOK
PUT 6
LOOK
REMOVE
LOOK

Sample Output 1

-1
-1
1

Sample Input 2

6
PUT 1
LOOK
PUT 2
LOOK
PUT 4
LOOK

Sample Output 2

-1
-1
-1

Sample Input 3

15
PUT 10
PUT 7
LOOK
REMOVE
LOOK
PUT 3
LOOK
PUT 9
LOOK
REMOVE
REMOVE
LOOK
PUT 5
LOOK
REMOVE

Sample Output 3

-1
-1
-1
-1
2
-1

Sample Input 4

24
PUT 1
PUT 2
PUT 4
LOOK
REMOVE
LOOK
REMOVE
LOOK
PUT 8
LOOK
PUT 6
LOOK
REMOVE
LOOK
REMOVE
LOOK
PUT 3
LOOK
PUT 2
LOOK
REMOVE
LOOK
REMOVE
LOOK

Sample Output 4

-1
-1
-1
-1
-1
4
3
-1
-1
8
7

Sample Input 5

2
PUT 1000000000
LOOK

Sample Output 5

-1
D - 街歩きと通行料

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

配点 : 400 点

問題文

高橋君は、ある都市の街並みを散策しています。この都市には N 個の交差点があり、交差点同士は双方向に通行可能な道路で結ばれています。道路は全部で M 本あり、それぞれの道路には移動にかかる所要時間が設定されています。同じ交差点の組を結ぶ道路が複数存在することもありますが、同じ交差点を両端とする道路(自己ループ)は存在しません。

この都市には K 箇所の料金所があり、それぞれ異なる交差点に設置されています。j 番目の料金所は交差点 L_j に設置されており、通行料は C_j です。料金所が設置されていない交差点では通行料はかかりません。

高橋君は交差点 1 から交差点 N まで移動したいと考えています。高橋君は道路を通って交差点から交差点へ移動しますが、同じ交差点や同じ道路を複数回通ってもかまいません。

高橋君の移動経路は、交差点の列 v_0, v_1, v_2, \ldots, v_s(ただし v_0 = 1、v_s = N)として表されます。ここで、連続する 2 つの交差点 v_{k-1} と v_k(1 \leq k \leq s)は道路で直接結ばれていなければなりません。

移動にかかる総コストは、以下の 2 つの合計です。

  • 通った道路の所要時間の合計。すなわち、v_{k-1} と v_k を結ぶ道路として選んだ道路の所要時間を k = 1, 2, \ldots, s について合計したもの。同じ道路を複数回通った場合はその回数分加算する。
  • 経路上の各交差点に設置されている料金所の通行料の合計。すなわち、v_0, v_1, \ldots, v_s の各交差点について、その交差点に料金所がある場合は通行料を加算する。同じ交差点が経路上に複数回現れる場合はその回数分加算する。出発地点(交差点 1)および到着地点(交差点 N)も対象に含まれる。

高橋君が交差点 1 から交差点 N まで移動するときの総コストの最小値を求めてください。なお、交差点 1 から交差点 N へ到達可能であることは保証されます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
  • U_i \neq V_i (1 \leq i \leq M)
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq M)
  • 1 \leq L_j \leq N (1 \leq j \leq K)
  • L_j はすべて異なる (1 \leq j \leq K)
  • 1 \leq C_j \leq 10^9 (1 \leq j \leq K)
  • 交差点 1 から交差点 N へ到達可能である
  • 入力はすべて整数である

入力

N M K
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
L_1 C_1
L_2 C_2
\vdots
L_K C_K
  • 1 行目には、交差点の数を表す N 、道路の数を表す M 、料金所の数を表す K が、スペース区切りで与えられる。
  • 2 行目から M + 1 行目では、道路の情報が M 行で与えられる。
  • 1 + i 行目では、 i 番目の道路が結ぶ 2 つの交差点 U_i , V_i と、その道路の所要時間 W_i がスペース区切りで与えられる。道路は双方向に通行可能である。
  • M + 2 行目から M + K + 1 行目では、料金所の情報が K 行で与えられる。
  • M + 1 + j 行目では、 j 番目の料金所が設置されている交差点 L_j と、その通行料 C_j がスペース区切りで与えられる。
  • K = 0 の場合、この部分は存在しない。

出力

高橋君が交差点 1 から交差点 N まで移動するときの総コストの最小値を 1 行で出力せよ。


入力例 1

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

出力例 1

10

入力例 2

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

出力例 2

15

入力例 3

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

出力例 3

26

Score : 400 pts

Problem Statement

Takahashi is strolling through the streets of a city. The city has N intersections, and the intersections are connected by bidirectional roads. There are M roads in total, and each road has a required travel time. Multiple roads may connect the same pair of intersections, but there are no roads connecting an intersection to itself (no self-loops).

The city has K toll gates, each installed at a distinct intersection. The j-th toll gate is installed at intersection L_j, and its toll fee is C_j. No toll fee is charged at intersections without a toll gate.

Takahashi wants to travel from intersection 1 to intersection N. He moves from intersection to intersection via roads, and he may pass through the same intersection or the same road multiple times.

Takahashi's route is represented as a sequence of intersections v_0, v_1, v_2, \ldots, v_s (where v_0 = 1 and v_s = N). Here, each pair of consecutive intersections v_{k-1} and v_k (1 \leq k \leq s) must be directly connected by a road.

The total cost of the trip is the sum of the following two components:

  • The total travel time of the roads traversed. That is, the sum of the travel times of the roads chosen to connect v_{k-1} and v_k for k = 1, 2, \ldots, s. If the same road is traversed multiple times, it is counted that many times.
  • The total toll fees at toll gates located at intersections along the route. That is, for each intersection in v_0, v_1, \ldots, v_s, if that intersection has a toll gate, its toll fee is added. If the same intersection appears multiple times in the route, the fee is charged each time. Both the starting point (intersection 1) and the destination (intersection N) are included.

Find the minimum total cost for Takahashi to travel from intersection 1 to intersection N. It is guaranteed that intersection N is reachable from intersection 1.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq N
  • 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
  • U_i \neq V_i (1 \leq i \leq M)
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq M)
  • 1 \leq L_j \leq N (1 \leq j \leq K)
  • All L_j are distinct (1 \leq j \leq K)
  • 1 \leq C_j \leq 10^9 (1 \leq j \leq K)
  • Intersection N is reachable from intersection 1
  • 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
L_1 C_1
L_2 C_2
\vdots
L_K C_K
  • The first line contains N (the number of intersections), M (the number of roads), and K (the number of toll gates), separated by spaces.
  • From the 2nd line to the (M + 1)-th line, road information is given over M lines.
  • The (1 + i)-th line gives the two intersections U_i, V_i connected by the i-th road and its travel time W_i, separated by spaces. The road is bidirectionally traversable.
  • From the (M + 2)-th line to the (M + K + 1)-th line, toll gate information is given over K lines.
  • The (M + 1 + j)-th line gives the intersection L_j where the j-th toll gate is installed and its toll fee C_j, separated by spaces.
  • If K = 0, this part does not exist.

Output

Print the minimum total cost for Takahashi to travel from intersection 1 to intersection N in a single line.


Sample Input 1

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

Sample Output 1

10

Sample Input 2

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

Sample Output 2

15

Sample Input 3

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

Sample Output 3

26
E - 最小コスト警備計画

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

配点 : 433 点

問題文

高橋君は M 箇所の施設を管理する責任者です。すべての施設に警備をつけるため、N 社の警備会社の中からいくつかを選んで契約しようとしています。

各警備会社 i(1 \leq i \leq N)には契約費用 C_i が定められています。また、警備会社 i が施設 j を警備できるかどうかを表す値 E_{i,j} が与えられます。E_{i,j} = 1 のとき警備会社 i は施設 j を警備でき、E_{i,j} = 0 のとき警備できません。

高橋君は N 社の中から 1 社以上を選んで契約します。ただし、同じ会社を重複して選ぶことはできません。選んだ警備会社の集合を S としたとき、すべての施設 j(1 \leq j \leq M)に対して、S の中に施設 j を警備できる会社が少なくとも 1 つ存在しなければなりません。

この条件を満たす S の選び方のうち、契約費用の合計 \displaystyle\sum_{i \in S} C_i の最小値を求めてください。条件を満たす選び方が存在しない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 50
  • 1 \leq M \leq 18
  • 1 \leq C_i \leq 10^9(1 \leq i \leq N)
  • E_{i,j} \in \{0, 1\}(1 \leq i \leq N, 1 \leq j \leq M)
  • 入力はすべて整数である

入力

N M
C_1 C_2 \ldots C_N
E_{1,1} E_{1,2} \ldots E_{1,M}
E_{2,1} E_{2,2} \ldots E_{2,M}
\vdots
E_{N,1} E_{N,2} \ldots E_{N,M}
  • 1 行目には、警備会社の数 N と施設の数 M がスペース区切りで与えられる。
  • 2 行目には、各警備会社の契約費用 C_1, C_2, \ldots, C_N がスペース区切りで与えられる。
  • 3 行目から N+2 行目までの各行には、警備会社 i(1 \leq i \leq N)が各施設を警備できるかどうかを表す E_{i,1}, E_{i,2}, \ldots, E_{i,M} がスペース区切りで与えられる。

出力

すべての施設を警備できる警備会社の選び方における契約費用の合計の最小値を 1 行で出力せよ。条件を満たす選び方が存在しない場合は -1 を出力せよ。


入力例 1

4 3
4 6 5 11
1 1 0
0 1 1
1 0 0
1 1 1

出力例 1

10

入力例 2

4 4
5 2 7 4
1 0 0 0
0 1 1 0
1 0 1 0
0 1 0 0

出力例 2

-1

入力例 3

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

出力例 3

13

入力例 4

12 10
14 9 20 11 8 7 13 6 15 10 12 5
1 0 1 0 0 1 0 0 0 0
0 1 0 1 0 0 1 0 0 0
1 1 0 0 1 0 0 1 0 0
0 0 1 1 0 1 0 0 1 0
1 0 0 0 1 0 1 0 0 1
0 1 1 0 0 0 0 1 1 0
0 0 0 1 1 1 0 0 0 1
1 0 0 0 0 0 1 1 0 0
0 1 0 1 0 1 0 1 0 1
0 0 1 0 1 0 1 0 1 0
1 0 0 1 0 0 1 0 1 0
0 1 0 0 1 0 0 1 0 1

出力例 4

22

入力例 5

1 1
1000000000
1

出力例 5

1000000000

Score : 433 pts

Problem Statement

Takahashi is the manager responsible for M facilities. To assign security to all facilities, he plans to select and contract with some of the N security companies.

Each security company i (1 \leq i \leq N) has a contract cost C_i. Additionally, a value E_{i,j} is given that indicates whether security company i can guard facility j. When E_{i,j} = 1, security company i can guard facility j, and when E_{i,j} = 0, it cannot.

Takahashi selects 1 or more companies from the N companies to contract with. However, the same company cannot be selected more than once. Let S be the set of selected security companies. For every facility j (1 \leq j \leq M), there must be at least one company in S that can guard facility j.

Among all ways of choosing S that satisfy this condition, find the minimum total contract cost \displaystyle\sum_{i \in S} C_i. If no valid selection exists, output -1.

Constraints

  • 1 \leq N \leq 50
  • 1 \leq M \leq 18
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • E_{i,j} \in \{0, 1\} (1 \leq i \leq N, 1 \leq j \leq M)
  • All input values are integers

Input

N M
C_1 C_2 \ldots C_N
E_{1,1} E_{1,2} \ldots E_{1,M}
E_{2,1} E_{2,2} \ldots E_{2,M}
\vdots
E_{N,1} E_{N,2} \ldots E_{N,M}
  • The first line contains the number of security companies N and the number of facilities M, separated by a space.
  • The second line contains the contract costs C_1, C_2, \ldots, C_N of each security company, separated by spaces.
  • From the 3rd line to the (N+2)-th line, each line contains E_{i,1}, E_{i,2}, \ldots, E_{i,M} separated by spaces, indicating whether security company i (1 \leq i \leq N) can guard each facility.

Output

Output in one line the minimum total contract cost among all ways of selecting security companies that can guard all facilities. If no valid selection exists, output -1.


Sample Input 1

4 3
4 6 5 11
1 1 0
0 1 1
1 0 0
1 1 1

Sample Output 1

10

Sample Input 2

4 4
5 2 7 4
1 0 0 0
0 1 1 0
1 0 1 0
0 1 0 0

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

13

Sample Input 4

12 10
14 9 20 11 8 7 13 6 15 10 12 5
1 0 1 0 0 1 0 0 0 0
0 1 0 1 0 0 1 0 0 0
1 1 0 0 1 0 0 1 0 0
0 0 1 1 0 1 0 0 1 0
1 0 0 0 1 0 1 0 0 1
0 1 1 0 0 0 0 1 1 0
0 0 0 1 1 1 0 0 0 1
1 0 0 0 0 0 1 1 0 0
0 1 0 1 0 1 0 1 0 1
0 0 1 0 1 0 1 0 1 0
1 0 0 1 0 0 1 0 1 0
0 1 0 0 1 0 0 1 0 1

Sample Output 4

22

Sample Input 5

1 1
1000000000
1

Sample Output 5

1000000000