実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君はある図書館の司書です。最近、図書館の利用状況を分析するため、D 日間にわたって来館者の記録を取ることにしました。
この図書館の利用登録者は N 人おり、それぞれ 1 から N までの利用者番号が付いています。高橋君は D 日間の各日について、その日に来館した利用者の番号を記録しました。j 日目 (1 \leq j \leq D) に来館した利用者は M_j 人であり、その利用者番号は S_{j,1}, S_{j,2}, \ldots, S_{j,M_j} です。なお、同じ日に同じ利用者が複数回記録されることはありません。また、各日の来館記録内で利用者番号は特定の順序で並んでいるとは限りません。
高橋君は、D 日間のうち来館した日数が K 日以上である利用者を「常連利用者」と定義し、常連利用者の一覧を作成したいと考えています。ここで、ある利用者の来館した日数とは、その利用者の番号が来館記録に含まれている日の数を指します。
D 日間の来館記録が与えられるので、常連利用者と判定される利用者の番号を昇順にすべて出力してください。常連利用者が 1 人もいない場合は -1 を出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq D \leq 100
- 1 \leq K \leq D
- 0 \leq M_j \leq N (1 \leq j \leq D)
- 1 \leq S_{j,i} \leq N (1 \leq j \leq D, \; 1 \leq i \leq M_j)
- 同じ日の記録内に同じ利用者番号が複数回現れることはない
- 入力はすべて整数である
入力
N D K
M_1 S_{1,1} S_{1,2} \ldots S_{1,M_1}
M_2 S_{2,1} S_{2,2} \ldots S_{2,M_2}
\vdots
M_D S_{D,1} S_{D,2} \ldots S_{D,M_D}
- 1 行目には、利用登録者の人数 N、記録日数 D、常連利用者と判定する基準日数 K が、スペース区切りで与えられる。
- 2 行目から第 (D+1) 行目までの D 行にわたって、各日の来館記録が与えられる。
- 第 (j+1) 行目 (1 \leq j \leq D) では、j 日目に来館した利用者の人数 M_j と、その利用者の番号 S_{j,1}, S_{j,2}, \ldots, S_{j,M_j} がスペース区切りで与えられる。M_j = 0 の場合は、0 のみが与えられる。
出力
常連利用者と判定された利用者が 1 人以上いる場合は、その利用者の番号を昇順にスペース区切りで 1 行で出力してください。常連利用者が 1 人もいない場合は -1 を 1 行で出力してください。
入力例 1
5 3 2 3 1 2 3 3 2 3 4 2 3 5
出力例 1
2 3
入力例 2
4 3 3 2 1 2 2 3 4 2 1 3
出力例 2
-1
入力例 3
10 5 3 5 1 2 3 4 5 5 2 4 6 8 10 5 1 2 3 6 7 4 2 4 6 8 5 1 3 5 7 9
出力例 3
1 2 3 4 6
入力例 4
15 7 3 8 1 3 5 7 9 11 13 15 7 2 4 6 8 10 12 14 7 1 2 3 4 5 6 7 8 8 9 10 11 12 13 14 15 5 1 4 7 10 13 5 2 5 8 11 14 5 3 6 9 12 15
出力例 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
入力例 5
1 1 1 0
出力例 5
-1
Score : 266 pts
Problem Statement
Takahashi is a librarian at a certain library. Recently, he decided to record visitor data over D days in order to analyze the library's usage.
The library has N registered users, each assigned a user ID from 1 to N. For each of the D days, Takahashi recorded the user IDs of the visitors who came that day. On day j (1 \leq j \leq D), there were M_j visitors, and their user IDs are S_{j,1}, S_{j,2}, \ldots, S_{j,M_j}. Note that the same user is never recorded multiple times on the same day. Also, the user IDs within each day's record are not necessarily in any particular order.
Takahashi defines a "regular user" as a user who visited the library on at least K of the D days, and he wants to compile a list of all regular users. Here, the number of days a user visited is the number of days on which that user's ID appears in the visitor records.
Given the visitor records for D days, output all user IDs that are identified as regular users in ascending order. If there are no regular users, output -1.
Constraints
- 1 \leq N \leq 100
- 1 \leq D \leq 100
- 1 \leq K \leq D
- 0 \leq M_j \leq N (1 \leq j \leq D)
- 1 \leq S_{j,i} \leq N (1 \leq j \leq D, \; 1 \leq i \leq M_j)
- The same user ID does not appear multiple times within the same day's record
- All input values are integers
Input
N D K
M_1 S_{1,1} S_{1,2} \ldots S_{1,M_1}
M_2 S_{2,1} S_{2,2} \ldots S_{2,M_2}
\vdots
M_D S_{D,1} S_{D,2} \ldots S_{D,M_D}
- The first line contains the number of registered users N, the number of recorded days D, and the threshold number of days K for determining regular users, separated by spaces.
- The following D lines (from line 2 to line (D+1)) contain the visitor records for each day.
- Line (j+1) (1 \leq j \leq D) contains the number of visitors on day j, M_j, followed by their user IDs S_{j,1}, S_{j,2}, \ldots, S_{j,M_j}, separated by spaces. If M_j = 0, only 0 is given.
Output
If there is at least one regular user, output their user IDs in ascending order, separated by spaces, on a single line. If there are no regular users, output -1 on a single line.
Sample Input 1
5 3 2 3 1 2 3 3 2 3 4 2 3 5
Sample Output 1
2 3
Sample Input 2
4 3 3 2 1 2 2 3 4 2 1 3
Sample Output 2
-1
Sample Input 3
10 5 3 5 1 2 3 4 5 5 2 4 6 8 10 5 1 2 3 6 7 4 2 4 6 8 5 1 3 5 7 9
Sample Output 3
1 2 3 4 6
Sample Input 4
15 7 3 8 1 3 5 7 9 11 13 15 7 2 4 6 8 10 12 14 7 1 2 3 4 5 6 7 8 8 9 10 11 12 13 14 15 5 1 4 7 10 13 5 2 5 8 11 14 5 3 6 9 12 15
Sample Output 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Sample Input 5
1 1 1 0
Sample Output 5
-1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 333 点
問題文
高橋君は広大な植物園の管理者です。植物園には N 個の花壇があり、それぞれの花壇は平面上の互いに異なる位置に設置されています。花壇 i(1 \leq i \leq N)の座標は (X_i, Y_i) です。
なお、異なる花壇が同じ X 座標を持つことや、同じ Y 座標を持つことはありえます。
高橋君は、すべての花壇の位置をまとめた配置図を作成することにしました。配置図は以下のように定義されるグリッドです。
N 個の花壇の X 座標のうち相異なる値の個数を C_x、Y 座標のうち相異なる値の個数を C_y とします。X 座標の相異なる値を小さい順に x_1 < x_2 < \cdots < x_{C_x}、Y 座標の相異なる値を小さい順に y_1 < y_2 < \cdots < y_{C_y} とします。
配置図は C_x \times C_y 個のセルからなるグリッドです。各セルはインデックスの組 (p, q)(1 \leq p \leq C_x, 1 \leq q \leq C_y)に一対一に対応し、セル (p, q) は座標 (x_p, y_q) を表します。
各花壇 i の座標 (X_i, Y_i) について、X_i = x_p, Y_i = y_q を満たす p, q がそれぞれちょうど1つ存在するので、花壇 i はセル (p, q) に配置されます。花壇の座標は互いに異なるため、各花壇は異なるセルに配置され、花壇が配置されているセルの数はちょうど N 個です。
配置図の 空きセル数 を、セルの総数から花壇の数を引いた値、すなわち C_x \times C_y - N と定義します。
ここで、青木君(高橋君の同僚)は、高橋君に追加の課題を出しました。N 個の花壇から 1 つを選んで取り除いたとき、残りの N - 1 個の花壇で同様に配置図を作成した場合の空きセル数を、取り除く花壇ごとにそれぞれ求めよ、というものです。
すなわち、各 i = 1, 2, \ldots, N について、花壇 i を除いた残りの N - 1 個の花壇に対して上記の手順で配置図を作成してください。残りの N - 1 個の花壇の X 座標のうち相異なる値の個数を C'_x、Y 座標のうち相異なる値の個数を C'_y とすると、空きセル数は C'_x \times C'_y - (N - 1) です。この値を各 i について求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- -10^9 \leq X_i \leq 10^9
- -10^9 \leq Y_i \leq 10^9
- (X_i, Y_i) \neq (X_j, Y_j) (i \neq j)
- 入力はすべて整数である
入力
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
- 1 行目には、花壇の個数を表す整数 N が与えられる。
- 2 行目から N + 1 行目では、各花壇の座標が与えられる。
- 1 + i 行目には、花壇 i の X 座標 X_i と Y 座標 Y_i が、スペース区切りで与えられる。
出力
N 行出力せよ。i 行目には、花壇 i を除外した場合の空きセル数を出力せよ。
入力例 1
4 1 1 1 2 2 1 3 3
出力例 1
6 3 3 1
入力例 2
3 0 0 1 1 2 2
出力例 2
2 2 2
入力例 3
10 1 3 1 5 2 3 2 7 3 1 3 5 4 1 4 7 5 3 5 5
出力例 3
11 11 11 11 11 11 11 11 11 11
入力例 4
20 -10 4 -10 7 -5 2 -5 4 -5 9 0 1 0 4 0 7 0 9 3 2 3 7 3 9 6 1 6 2 6 4 8 1 8 7 8 9 10 2 10 4
出力例 4
16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16
入力例 5
2 1000000000 -1000000000 -1000000000 1000000000
出力例 5
0 0
Score : 333 pts
Problem Statement
Takahashi is the manager of a vast botanical garden. The garden has N flower beds, each placed at a distinct position on a plane. The coordinates of flower bed i (1 \leq i \leq N) are (X_i, Y_i).
Note that different flower beds may share the same X coordinate or the same Y coordinate.
Takahashi has decided to create a layout map summarizing the positions of all flower beds. The layout map is a grid defined as follows.
Let C_x be the number of distinct values among the X coordinates of the N flower beds, and C_y be the number of distinct values among the Y coordinates. Let the distinct X coordinate values in ascending order be x_1 < x_2 < \cdots < x_{C_x}, and the distinct Y coordinate values in ascending order be y_1 < y_2 < \cdots < y_{C_y}.
The layout map is a grid consisting of C_x \times C_y cells. Each cell corresponds one-to-one with an index pair (p, q) (1 \leq p \leq C_x, 1 \leq q \leq C_y), where cell (p, q) represents the coordinates (x_p, y_q).
For each flower bed i with coordinates (X_i, Y_i), there exists exactly one p and one q such that X_i = x_p and Y_i = y_q, so flower bed i is placed in cell (p, q). Since all flower bed coordinates are distinct, each flower bed is placed in a different cell, and the number of cells containing a flower bed is exactly N.
The number of empty cells in the layout map is defined as the total number of cells minus the number of flower beds, namely C_x \times C_y - N.
Now, Aoki (Takahashi's colleague) has given Takahashi an additional task: for each flower bed, determine the number of empty cells in the layout map created from the remaining N - 1 flower beds when that flower bed is removed.
Specifically, for each i = 1, 2, \ldots, N, create the layout map using the above procedure for the remaining N - 1 flower beds after removing flower bed i. Let C'_x be the number of distinct values among the X coordinates and C'_y be the number of distinct values among the Y coordinates of the remaining N - 1 flower beds. The number of empty cells is C'_x \times C'_y - (N - 1). Compute this value for each i.
Constraints
- 2 \leq N \leq 2 \times 10^5
- -10^9 \leq X_i \leq 10^9
- -10^9 \leq Y_i \leq 10^9
- (X_i, Y_i) \neq (X_j, Y_j) (i \neq j)
- All input values are integers
Input
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
- The first line contains an integer N, the number of flower beds.
- The 2nd through (N + 1)-th lines give the coordinates of each flower bed.
- The (1 + i)-th line contains the X coordinate X_i and Y coordinate Y_i of flower bed i, separated by a space.
Output
Print N lines. The i-th line should contain the number of empty cells when flower bed i is removed.
Sample Input 1
4 1 1 1 2 2 1 3 3
Sample Output 1
6 3 3 1
Sample Input 2
3 0 0 1 1 2 2
Sample Output 2
2 2 2
Sample Input 3
10 1 3 1 5 2 3 2 7 3 1 3 5 4 1 4 7 5 3 5 5
Sample Output 3
11 11 11 11 11 11 11 11 11 11
Sample Input 4
20 -10 4 -10 7 -5 2 -5 4 -5 9 0 1 0 4 0 7 0 9 3 2 3 7 3 9 6 1 6 2 6 4 8 1 8 7 8 9 10 2 10 4
Sample Output 4
16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16
Sample Input 5
2 1000000000 -1000000000 -1000000000 1000000000
Sample Output 5
0 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君はデパートの閉店セールに行く予定でしたが、うっかり昼寝をしてしまい、到着が遅れてしまいました。
デパートのセールは時刻 0 分に開始され、時刻 T 分に閉店します。高橋君がデパートに到着するのは時刻 S 分です( 0 \leq S \leq T )。高橋君は到着した時刻 S 分から閉店時刻 T 分までの残り T - S 分間で買い物をすることができます。
セール会場には N 個の商品があります。 i 番目の商品( 1 \leq i \leq N )を購入するには C_i 分の購入手続き時間がかかり、購入すると P_i 円のお得さを得られます。各商品は最大 1 個まで購入できます。
高橋君は一度に 1 つの商品の購入手続きしか進めることができません。ある商品の購入手続きが完了したら直ちに次の商品の購入手続きを開始できます。購入手続き以外に時間はかかりません。また、購入手続きを途中で中断することはできず、手続きは閉店時刻までに完了しなければなりません。
したがって、購入する商品の集合を A としたとき、購入手続き時間の合計が残り時間以下、すなわち \displaystyle\sum_{i \in A} C_i \leq T - S を満たす必要があります。
この条件のもとで、高橋君が得られるお得さの合計 \displaystyle\sum_{i \in A} P_i の最大値を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq T \leq 10^4
- 0 \leq S \leq T
- 1 \leq C_i \leq 10^4
- 1 \leq P_i \leq 10^5
- 入力はすべて整数である
入力
N T S C_1 P_1 C_2 P_2 \vdots C_N P_N
- 1 行目には、商品の個数を表す整数 N 、閉店時刻(分)を表す整数 T 、高橋君の到着時刻(分)を表す整数 S が、スペース区切りで与えられる。
- 2 行目から N 行では、各商品の情報が与えられる。
- 1 + i 行目では、 i 番目の商品の購入手続きにかかる時間(分)を表す整数 C_i と、得られるお得さ(円)を表す整数 P_i が、スペース区切りで与えられる。
出力
高橋君が得られるお得さの合計の最大値を、整数で 1 行に出力せよ。
入力例 1
3 10 7 2 100 3 150 4 200
出力例 1
150
入力例 2
5 100 85 5 300 8 500 10 600 3 200 7 450
出力例 2
950
入力例 3
8 5000 4970 12 800 9 550 15 900 6 400 8 600 10 700 5 350 3 200
出力例 3
2100
Score : 366 pts
Problem Statement
Takahashi was planning to go to a department store's closing sale, but he accidentally took a nap and arrived late.
The department store sale starts at time 0 minutes and the store closes at time T minutes. Takahashi arrives at the department store at time S minutes (0 \leq S \leq T). He can shop during the remaining T - S minutes from his arrival time S until the closing time T.
There are N items at the sale venue. Purchasing the i-th item (1 \leq i \leq N) requires C_i minutes of purchase processing time, and buying it yields a savings value of P_i yen. Each item can be purchased at most once.
Takahashi can only process the purchase of one item at a time. Once the purchase processing of an item is completed, he can immediately start processing the next item. No time is required other than purchase processing. Also, purchase processing cannot be interrupted midway, and all processing must be completed by the closing time.
Therefore, if A is the set of items to purchase, the total purchase processing time must not exceed the remaining time, i.e., \displaystyle\sum_{i \in A} C_i \leq T - S must be satisfied.
Under this condition, find the maximum total savings value \displaystyle\sum_{i \in A} P_i that Takahashi can obtain.
Constraints
- 1 \leq N \leq 100
- 1 \leq T \leq 10^4
- 0 \leq S \leq T
- 1 \leq C_i \leq 10^4
- 1 \leq P_i \leq 10^5
- All inputs are integers
Input
N T S C_1 P_1 C_2 P_2 \vdots C_N P_N
- The first line contains three space-separated integers: N representing the number of items, T representing the closing time (in minutes), and S representing Takahashi's arrival time (in minutes).
- The next N lines contain the information for each item.
- The (1 + i)-th line contains two space-separated integers: C_i representing the purchase processing time (in minutes) for the i-th item, and P_i representing the savings value (in yen) obtained from it.
Output
Output the maximum total savings value that Takahashi can obtain, as an integer on a single line.
Sample Input 1
3 10 7 2 100 3 150 4 200
Sample Output 1
150
Sample Input 2
5 100 85 5 300 8 500 10 600 3 200 7 450
Sample Output 2
950
Sample Input 3
8 5000 4970 12 800 9 550 15 900 6 400 8 600 10 700 5 350 3 200
Sample Output 3
2100
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君は N 件の借金を抱えています。
各借金 i(1 \leq i \leq N)には、初期残高 H_i 円と、毎ターン加算される固定の利息額 G_i 円が設定されています。
高橋君はこれらの借金をターン制で返済していきます。各ターンの開始時に、高橋君は P 円を受け取ります。この P 円はそのターン内でのみ使用でき、ターン終了時に未使用分は消滅します。
各ターンは以下の手順で進行します:
- 返済フェーズ:高橋君は、まだ完済していない借金の中から 1 件を選んで完済するか、何もしないかを選択します。ただし、選べるのはその時点での残高が P 以下である借金に限ります。借金を完済する場合、その借金の残高と同額を支払い、その借金は以後消滅します(残高が 0 になります)。P 円のうち支払いに使わなかった分はそのまま消滅し、次のターンには持ち越せません。部分的な返済(残高の一部だけを支払うこと)はできません。1 ターンに完済できるのは最大 1 件です。
- 利息フェーズ:返済フェーズの終了後、まだ完済されていない全ての借金 i について、残高がそれぞれ G_i 円増加します。
最初のターンの返済フェーズ開始時点において、各借金 i の残高は初期残高 H_i 円です。
高橋君が最適な順番で借金を返済するとき、有限回のターン内に全ての借金を完済できるかどうかを判定してください。完済できる場合は Yes を、できない場合は No を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq P \leq 10^9
- 1 \leq H_i \leq 10^9
- 1 \leq G_i \leq 10^9
- 入力はすべて整数
入力
N P H_1 G_1 H_2 G_2 \vdots H_N G_N
- 1 行目には、借金の件数を表す整数 N と、高橋君が毎ターン受け取る金額を表す整数 P が、スペース区切りで与えられる。
- 続く N 行のうち i 行目(1 \leq i \leq N)には、i 番目の借金の初期残高 H_i と毎ターン加算される固定の利息額 G_i が、スペース区切りで与えられる。
出力
全ての借金を完済できる場合は Yes を、できない場合は No を 1 行で出力せよ。
入力例 1
3 10 6 2 2 1 8 1
出力例 1
Yes
入力例 2
3 10 9 2 10 1 3 1
出力例 2
No
入力例 3
10 100 80 5 70 10 95 1 40 15 10 10 60 4 99 1 50 8 30 7 20 20
出力例 3
Yes
入力例 4
25 1000000 851860 12345 1000000 99999 200008 33333 725932 45678 622226 22222 700000 150000 200000 40000 111115 98765 555556 111111 185185 54321 76000 42000 222223 777777 340000 60000 430000 30000 904947 13579 57000 41000 250000 250000 90000 65000 111120 88888 382720 123456 200008 44444 90000 70000 200008 99999 55000 45000 111120 55555
出力例 4
Yes
入力例 5
1 1000000000 1000000000 1000000000
出力例 5
Yes
Score : 400 pts
Problem Statement
Takahashi has N debts.
Each debt i (1 \leq i \leq N) has an initial balance of H_i yen and a fixed interest amount of G_i yen that is added every turn.
Takahashi repays these debts in a turn-based manner. At the beginning of each turn, Takahashi receives P yen. This P yen can only be used within that turn, and any unused portion disappears at the end of the turn.
Each turn proceeds as follows:
- Repayment Phase: Takahashi chooses one debt that has not yet been fully repaid and pays it off completely, or chooses to do nothing. However, he can only choose a debt whose current balance is at most P. When paying off a debt, he pays an amount equal to that debt's balance, and that debt disappears thereafter (its balance becomes 0). Any portion of the P yen not used for payment simply disappears and cannot be carried over to the next turn. Partial repayment (paying only a portion of the balance) is not allowed. At most 1 debt can be fully repaid per turn.
- Interest Phase: After the repayment phase ends, for every debt i that has not yet been fully repaid, its balance increases by G_i yen.
At the start of the repayment phase of the first turn, the balance of each debt i is the initial balance H_i yen.
Determine whether Takahashi can fully repay all debts within a finite number of turns if he repays them in the optimal order. If he can, output Yes; otherwise, output No.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq P \leq 10^9
- 1 \leq H_i \leq 10^9
- 1 \leq G_i \leq 10^9
- All inputs are integers
Input
N P H_1 G_1 H_2 G_2 \vdots H_N G_N
- The first line contains an integer N representing the number of debts and an integer P representing the amount Takahashi receives each turn, separated by a space.
- The i-th of the following N lines (1 \leq i \leq N) contains the initial balance H_i and the fixed interest amount G_i added each turn for the i-th debt, separated by a space.
Output
If all debts can be fully repaid, output Yes; otherwise, output No on a single line.
Sample Input 1
3 10 6 2 2 1 8 1
Sample Output 1
Yes
Sample Input 2
3 10 9 2 10 1 3 1
Sample Output 2
No
Sample Input 3
10 100 80 5 70 10 95 1 40 15 10 10 60 4 99 1 50 8 30 7 20 20
Sample Output 3
Yes
Sample Input 4
25 1000000 851860 12345 1000000 99999 200008 33333 725932 45678 622226 22222 700000 150000 200000 40000 111115 98765 555556 111111 185185 54321 76000 42000 222223 777777 340000 60000 430000 30000 904947 13579 57000 41000 250000 250000 90000 65000 111120 88888 382720 123456 200008 44444 90000 70000 200008 99999 55000 45000 111120 55555
Sample Output 4
Yes
Sample Input 5
1 1000000000 1000000000 1000000000
Sample Output 5
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 433 点
問題文
高橋君は山の麓から山頂の展望台を目指して登山をします。
この山には N 個のチェックポイントがあり、それぞれに 1 から N までの番号が付けられています。また、麓を地点 0、山頂の展望台を地点 N+1 と表します。
高橋君は地点 0(麓)からスタートし、N 個のチェックポイントの中からいくつかを選んで立ち寄りながら登り、最終的に地点 N+1(山頂の展望台)に到達します。高橋君は常に地点番号が増加する方向にのみ移動します(したがって、各チェックポイントには高々 1 回しか立ち寄りません)。すなわち、立ち寄るチェックポイントの番号を昇順に p_1 < p_2 < \cdots < p_s とすると、高橋君は
0 \to p_1 \to p_2 \to \cdots \to p_s \to N+1
の順に移動します。s = 0(どのチェックポイントにも立ち寄らない)の場合は、地点 0 から地点 N+1 へ直接移動します。
0 \leq a < b \leq N+1 を満たす任意の 2 地点の組 (a, b) に対して、地点 a から地点 b へ途中のチェックポイントを経由せずに直接移動するためのコスト C_{a,b} が定められています。すなわち、番号が隣接していない 2 地点間であっても直接移動することができ、そのコストが与えられます。移動経路全体の消費体力は、経路上で連続する 2 地点間のコストの総和です。具体的には、上記の経路における消費体力は
C_{0,\,p_1} + C_{p_1,\,p_2} + \cdots + C_{p_{s-1},\,p_s} + C_{p_s,\,N+1}
です(s = 0 の場合は C_{0,\,N+1})。
立ち寄るチェックポイントの選び方には以下の 2 つのルールがあります。
- 最小個数の条件: 立ち寄るチェックポイントの個数 s は K 個以上でなければなりません。
- 地形バランスの条件: 各チェックポイントは 1 から M までの番号で表される M 種類の地形タイプのうちちょうど 1 つに分類されています。1 以上 M 以下の各整数 j について、地形タイプ j に属するチェックポイントのうち立ち寄るものの個数は偶数でなければなりません(0 も偶数とみなします)。
高橋君は、上記のルールをすべて満たすようなチェックポイントの選び方のうち、消費体力を最小化したいと考えています。
ルールを満たす選び方が存在する場合は消費体力の最小値を、存在しない場合は -1 を出力してください。
制約
- 1 \leq N \leq 150
- 1 \leq M \leq 8
- N^3 \cdot 2^M \leq 3 \times 10^8
- 0 \leq K \leq N
- 1 \leq t_i \leq M(1 \leq i \leq N)
- 1 \leq C_{a,b} \leq 10^9(0 \leq a < b \leq N+1)
- 入力はすべて整数である。
入力
N M K
t_1 t_2 \cdots t_N
C_{0,1} C_{0,2} \cdots C_{0,N+1}
C_{1,2} C_{1,3} \cdots C_{1,N+1}
\vdots
C_{N,N+1}
- 1 行目には、チェックポイントの数 N、地形タイプの数 M、立ち寄るチェックポイントの最小個数 K が、スペース区切りで与えられる。
- 2 行目には、各チェックポイントの地形タイプを表す t_1, t_2, \ldots, t_N がスペース区切りで与えられる。t_i はチェックポイント i の地形タイプを表す 1 以上 M 以下の整数である。M 種類すべての地形タイプが入力に現れるとは限らない。
- 続く N + 1 行にわたって、移動コストが与えられる。これらのうち k 行目(k = 1, 2, \ldots, N+1)には、地点 k-1 を出発地点として、地点 k, k+1, \ldots, N+1 への各移動コスト C_{k-1,\,k},\, C_{k-1,\,k+1},\, \ldots,\, C_{k-1,\,N+1} がスペース区切りで与えられる。
出力
ルールを満たす立ち寄り方が存在する場合は消費体力の最小値を、存在しない場合は -1 を 1 行で出力せよ。
入力例 1
3 2 2 1 2 1 5 8 4 20 6 3 7 4 2 9
出力例 1
17
入力例 2
1 1 1 1 3 10 4
出力例 2
-1
入力例 3
8 3 3 1 2 3 1 2 3 1 2 4 9 15 7 20 18 25 30 12 6 5 11 14 9 21 17 23 8 13 4 16 19 10 22 7 12 15 6 18 20 9 3 14 11 16 5 10 8 13 6 4 9 7 5 8
出力例 3
28
入力例 4
15 5 6 1 2 3 4 5 1 2 3 4 5 1 2 3 4 1 11 25 18 40 33 52 47 60 58 75 69 88 80 95 90 110 14 9 27 22 41 36 55 50 68 63 82 77 96 91 105 13 26 12 39 31 45 44 62 57 70 66 84 79 98 8 21 34 16 48 30 53 47 65 59 78 72 90 17 11 29 24 42 37 56 51 69 64 83 78 10 23 15 36 28 49 41 54 52 71 67 19 14 32 27 46 40 58 53 72 68 12 25 20 38 33 51 45 63 60 16 9 30 22 43 35 57 49 18 13 31 26 44 39 61 7 24 19 37 32 50 15 28 10 41 34 20 12 35 29 6 27 21 18 14 9
出力例 4
100
入力例 5
1 1 0 1 1000000000 1 999999999
出力例 5
1
Score : 433 pts
Problem Statement
Takahashi is climbing a mountain from the foot to the observatory at the summit.
There are N checkpoints on this mountain, numbered 1 to N. Additionally, the foot of the mountain is represented as point 0, and the observatory at the summit is represented as point N+1.
Takahashi starts at point 0 (the foot), visits some chosen checkpoints among the N checkpoints as he climbs, and finally reaches point N+1 (the observatory at the summit). Takahashi always moves in the direction of increasing point numbers (thus, he visits each checkpoint at most once). That is, if the indices of the visited checkpoints in ascending order are p_1 < p_2 < \cdots < p_s, Takahashi moves in the following order:
0 \to p_1 \to p_2 \to \cdots \to p_s \to N+1
If s = 0 (no checkpoints are visited), he moves directly from point 0 to point N+1.
For any pair of points (a, b) satisfying 0 \leq a < b \leq N+1, a cost C_{a,b} is defined for moving directly from point a to point b without passing through any checkpoints in between. That is, direct movement is possible even between two non-adjacent points, and its cost is given. The total stamina consumption of the entire route is the sum of the costs between consecutive points on the route. Specifically, the stamina consumption for the route above is:
C_{0,\,p_1} + C_{p_1,\,p_2} + \cdots + C_{p_{s-1},\,p_s} + C_{p_s,\,N+1}
(or C_{0,\,N+1} if s = 0).
There are two rules for choosing which checkpoints to visit:
- Minimum Count Condition: The number of visited checkpoints s must be at least K.
- Terrain Balance Condition: Each checkpoint is classified into exactly one of M terrain types, represented by integers from 1 to M. For each integer j (1 \leq j \leq M), the number of visited checkpoints belonging to terrain type j must be even (where 0 is also considered even).
Takahashi wants to find a way to choose checkpoints that satisfies all the rules above while minimizing the total stamina consumption.
If there exists a valid choice of checkpoints, output the minimum stamina consumption. If no such choice exists, output -1.
Constraints
- 1 \leq N \leq 150
- 1 \leq M \leq 8
- N^3 \cdot 2^M \leq 3 \times 10^8
- 0 \leq K \leq N
- 1 \leq t_i \leq M (1 \leq i \leq N)
- 1 \leq C_{a,b} \leq 10^9 (0 \leq a < b \leq N+1)
- All input values are integers.
Input
N M K
t_1 t_2 \cdots t_N
C_{0,1} C_{0,2} \cdots C_{0,N+1}
C_{1,2} C_{1,3} \cdots C_{1,N+1}
\vdots
C_{N,N+1}
- The first line contains the number of checkpoints N, the number of terrain types M, and the minimum number of checkpoints to visit K, separated by spaces.
- The second line contains t_1, t_2, \ldots, t_N, separated by spaces, representing the terrain types of the checkpoints. t_i is an integer between 1 and M representing the terrain type of checkpoint i. Not all M terrain types are guaranteed to appear in the input.
- The next N + 1 lines provide the movement costs. The k-th of these lines (k = 1, 2, \ldots, N+1) contains the movement costs from point k-1 to points k, k+1, \ldots, N+1, represented as C_{k-1,\,k},\, C_{k-1,\,k+1},\, \ldots,\, C_{k-1,\,N+1}, separated by spaces.
Output
If there exists a valid way to visit checkpoints, output the minimum stamina consumption. If no such way exists, output -1 in a single line.
Sample Input 1
3 2 2 1 2 1 5 8 4 20 6 3 7 4 2 9
Sample Output 1
17
Sample Input 2
1 1 1 1 3 10 4
Sample Output 2
-1
Sample Input 3
8 3 3 1 2 3 1 2 3 1 2 4 9 15 7 20 18 25 30 12 6 5 11 14 9 21 17 23 8 13 4 16 19 10 22 7 12 15 6 18 20 9 3 14 11 16 5 10 8 13 6 4 9 7 5 8
Sample Output 3
28
Sample Input 4
15 5 6 1 2 3 4 5 1 2 3 4 5 1 2 3 4 1 11 25 18 40 33 52 47 60 58 75 69 88 80 95 90 110 14 9 27 22 41 36 55 50 68 63 82 77 96 91 105 13 26 12 39 31 45 44 62 57 70 66 84 79 98 8 21 34 16 48 30 53 47 65 59 78 72 90 17 11 29 24 42 37 56 51 69 64 83 78 10 23 15 36 28 49 41 54 52 71 67 19 14 32 27 46 40 58 53 72 68 12 25 20 38 33 51 45 63 60 16 9 30 22 43 35 57 49 18 13 31 26 44 39 61 7 24 19 37 32 50 15 28 10 41 34 20 12 35 29 6 27 21 18 14 9
Sample Output 4
100
Sample Input 5
1 1 0 1 1000000000 1 999999999
Sample Output 5
1