A - 合格者数の集計

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

配点 : 200

問題文

高橋君は学習塾の講師をしています。この塾では N クラスの生徒が模擬試験を受験しました。

各クラス i1 \leq i \leq N)には A_i 人の生徒が所属しており、それぞれの生徒には試験の点数が記録されています。クラス ij 番目(1 \leq j \leq A_i)の生徒の点数は B_{i,j} 点です。

高橋君は合格基準として合格ライン K 点を設定しました。点数が K 点以上の生徒を合格と判定します。

全てのクラスを通じて、合格と判定される生徒の総数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq K \leq 100
  • 1 \leq A_i \leq 100
  • \sum_{i=1}^{N} A_i \leq 10^6(全クラスの生徒数の合計は 10^6 以下)
  • 0 \leq B_{i,j} \leq 100
  • 入力はすべて整数

入力

N K
A_1 B_{1,1} B_{1,2} \ldots B_{1,A_1}
A_2 B_{2,1} B_{2,2} \ldots B_{2,A_2}
\vdots
A_N B_{N,1} B_{N,2} \ldots B_{N,A_N}
  • 1 行目には、クラスの数 N と合格ライン K がスペース区切りで与えられる。
  • i + 1 行(1 \leq i \leq N)には、クラス i に所属する生徒の人数 A_i と、そのクラスの各生徒の点数 B_{i,1}, B_{i,2}, \ldots, B_{i,A_i} がスペース区切りで与えられる。

出力

点数が K 点以上である生徒の総数を 1 行で出力せよ。


入力例 1

3 60
4 45 72 60 88
3 59 60 61
2 100 30

出力例 1

6

入力例 2

5 70
5 65 70 80 55 90
3 69 71 70
4 100 85 72 68
2 50 49
6 75 80 85 90 95 100

出力例 2

14

入力例 3

10 50
8 23 45 67 89 12 34 56 78
5 50 50 50 49 51
10 0 10 20 30 40 50 60 70 80 90
3 100 100 100
7 45 46 47 48 49 50 51
4 25 75 25 75
6 99 98 97 96 95 94
2 0 100
9 55 55 55 55 55 44 44 44 44
5 1 2 3 4 5

出力例 3

32

Score : 200 pts

Problem Statement

Takahashi is a lecturer at a cram school. At this school, students from N classes took a mock exam.

Each class i (1 \leq i \leq N) has A_i students, and each student's exam score has been recorded. The score of the j-th student (1 \leq j \leq A_i) in class i is B_{i,j} points.

Takahashi set a passing score of K points as the passing criterion. A student whose score is K points or higher is judged as passing.

Find the total number of students judged as passing across all classes.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq K \leq 100
  • 1 \leq A_i \leq 100
  • \sum_{i=1}^{N} A_i \leq 10^6 (the total number of students across all classes is at most 10^6)
  • 0 \leq B_{i,j} \leq 100
  • All input values are integers

Input

N K
A_1 B_{1,1} B_{1,2} \ldots B_{1,A_1}
A_2 B_{2,1} B_{2,2} \ldots B_{2,A_2}
\vdots
A_N B_{N,1} B_{N,2} \ldots B_{N,A_N}
  • The first line contains the number of classes N and the passing score K, separated by a space.
  • The (i + 1)-th line (1 \leq i \leq N) contains the number of students A_i in class i, followed by the scores B_{i,1}, B_{i,2}, \ldots, B_{i,A_i} of each student in that class, separated by spaces.

Output

Print the total number of students whose score is K points or higher, on a single line.


Sample Input 1

3 60
4 45 72 60 88
3 59 60 61
2 100 30

Sample Output 1

6

Sample Input 2

5 70
5 65 70 80 55 90
3 69 71 70
4 100 85 72 68
2 50 49
6 75 80 85 90 95 100

Sample Output 2

14

Sample Input 3

10 50
8 23 45 67 89 12 34 56 78
5 50 50 50 49 51
10 0 10 20 30 40 50 60 70 80 90
3 100 100 100
7 45 46 47 48 49 50 51
4 25 75 25 75
6 99 98 97 96 95 94
2 0 100
9 55 55 55 55 55 44 44 44 44
5 1 2 3 4 5

Sample Output 3

32
B - 奨学金の選考

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

配点 : 266

問題文

高橋君は大学の奨学金選考委員会の担当者です。今年度は N 種類の奨学金があり、それぞれについて応募した学生の中から受給者を決定する必要があります。

大学には M 人の応募者がおり、応募者 j (1 \leq j \leq M) には成績に基づいた評価点 P_j が設定されています。評価点は応募者ごとに固定の値であり、値が大きいほど高評価であることを意味します。異なる応募者の評価点が同じ値であることもあり得ます。

一人の応募者が複数の奨学金に応募することもあり得ます。各奨学金の受給者はそれぞれ独立に決定されるため、一人の応募者が複数の奨学金を受給することもあり得ます。

各奨学金 i (1 \leq i \leq N) には、その奨学金に応募した K_i 人の応募者の番号 C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} が与えられます。受給者は以下のルールで決まります:

  • 応募者がいない場合(K_i = 0)、その奨学金の受給者はいない。
  • 応募者がいる場合、評価点が最も高い応募者がその奨学金を受給する。評価点が最も高い応募者が複数いる場合は、その中で応募者番号が最も小さい応募者が受給する。

各奨学金について、最終的にその奨学金を受給する応募者の番号を求めてください。受給者がいない場合は 0 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq P_j \leq 10^9 (1 \leq j \leq M)
  • 0 \leq K_i \leq M (1 \leq i \leq N)
  • 1 \leq C_{i,k} \leq M (1 \leq i \leq N, 1 \leq k \leq K_i)
  • 同じ奨学金に対して同じ応募者が複数回現れることはない(C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} は相異なる)
  • \displaystyle \sum_{i=1}^{N} K_i \leq 2 \times 10^5
  • 入力はすべて整数

入力

N M
P_1 P_2 \ldots P_M
K_1 C_{1,1} C_{1,2} \ldots C_{1,K_1}
K_2 C_{2,1} C_{2,2} \ldots C_{2,K_2}
\vdots
K_N C_{N,1} C_{N,2} \ldots C_{N,K_N}
  • 1 行目には、奨学金の種類数を表す整数 N と、応募者の人数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各応募者の評価点を表す整数 P_1, P_2, \ldots, P_M が、スペース区切りで与えられる。P_j は応募者 j の評価点を表す。
  • 2 + i 行目 (1 \leq i \leq N) には、奨学金 i に応募した学生の情報が与えられる。まず応募した学生の人数を表す整数 K_i が与えられ、続いてその応募者の番号を表す整数 C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} がスペース区切りで与えられる。K_i = 0 の場合、その行には K_i のみが与えられる。

出力

N 行出力せよ。i 行目 (1 \leq i \leq N) には、奨学金 i を受給する応募者の番号を出力せよ。受給者がいない場合は 0 を出力せよ。


入力例 1

3 4
100 80 100 90
2 1 2
3 2 3 4
0

出力例 1

1
3
0

入力例 2

5 6
50 50 50 30 70 70
3 1 2 3
2 5 6
4 1 4 5 6
1 4
0

出力例 2

1
5
5
4
0

入力例 3

8 10
1000000000 999999999 500000000 750000000 1000000000 250000000 750000000 100000000 999999999 500000000
5 1 2 3 4 5
3 6 7 8
4 2 5 9 10
0
2 1 5
6 1 2 3 4 5 6
1 8
10 1 2 3 4 5 6 7 8 9 10

出力例 3

1
7
5
0
1
1
8
1

Score : 266 pts

Problem Statement

Takahashi is a member of the university's scholarship selection committee. This year, there are N types of scholarships, and for each one, a recipient must be determined from among the students who applied.

The university has M applicants, and applicant j (1 \leq j \leq M) has been assigned an evaluation score P_j based on their academic performance. The evaluation score is a fixed value for each applicant, and a higher value means a higher evaluation. It is possible for different applicants to have the same evaluation score.

A single applicant may apply for multiple scholarships. Since the recipient of each scholarship is determined independently, a single applicant may receive multiple scholarships.

For each scholarship i (1 \leq i \leq N), the applicant numbers C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} of the K_i applicants who applied for that scholarship are given. The recipient is determined by the following rules:

  • If there are no applicants (K_i = 0), there is no recipient for that scholarship.
  • If there are applicants, the applicant with the highest evaluation score receives the scholarship. If there are multiple applicants with the highest evaluation score, the one with the smallest applicant number among them receives it.

For each scholarship, determine the applicant number of the person who ultimately receives that scholarship. If there is no recipient, output 0.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq P_j \leq 10^9 (1 \leq j \leq M)
  • 0 \leq K_i \leq M (1 \leq i \leq N)
  • 1 \leq C_{i,k} \leq M (1 \leq i \leq N, 1 \leq k \leq K_i)
  • The same applicant does not appear multiple times for the same scholarship (C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} are distinct)
  • \displaystyle \sum_{i=1}^{N} K_i \leq 2 \times 10^5
  • All input values are integers

Input

N M
P_1 P_2 \ldots P_M
K_1 C_{1,1} C_{1,2} \ldots C_{1,K_1}
K_2 C_{2,1} C_{2,2} \ldots C_{2,K_2}
\vdots
K_N C_{N,1} C_{N,2} \ldots C_{N,K_N}
  • The first line contains an integer N representing the number of types of scholarships and an integer M representing the number of applicants, separated by a space.
  • The second line contains integers P_1, P_2, \ldots, P_M representing the evaluation scores of each applicant, separated by spaces. P_j represents the evaluation score of applicant j.
  • The (2 + i)-th line (1 \leq i \leq N) contains information about the students who applied for scholarship i. First, an integer K_i representing the number of applicants is given, followed by the integers C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} representing the applicant numbers, separated by spaces. If K_i = 0, only K_i is given on that line.

Output

Output N lines. The i-th line (1 \leq i \leq N) should contain the applicant number of the person who receives scholarship i. If there is no recipient, output 0.


Sample Input 1

3 4
100 80 100 90
2 1 2
3 2 3 4
0

Sample Output 1

1
3
0

Sample Input 2

5 6
50 50 50 30 70 70
3 1 2 3
2 5 6
4 1 4 5 6
1 4
0

Sample Output 2

1
5
5
4
0

Sample Input 3

8 10
1000000000 999999999 500000000 750000000 1000000000 250000000 750000000 100000000 999999999 500000000
5 1 2 3 4 5
3 6 7 8
4 2 5 9 10
0
2 1 5
6 1 2 3 4 5 6
1 8
10 1 2 3 4 5 6 7 8 9 10

Sample Output 3

1
7
5
0
1
1
8
1
C - お得なショッピングモール巡り

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

配点 : 333

問題文

高橋君は週末のセールで買い物をするため、郊外にある N 個のショッピングモールを巡ることを計画しています。

各ショッピングモール i1 \leq i \leq N)には M_i 個の店舗があり、それぞれの店舗ではセール品が販売されています。ショッピングモール ij 番目の店舗(1 \leq j \leq M_i)で買い物をすると、P_{i,j} 円のお得度を得ることができます。

ショッピングモール i を訪れるには、訪問費用として C_i 円を支払う必要があります。この費用はモールごとに決まった固定の値です。

高橋君は時間の都合上、N 個のモールの中から K 個以下のモールを選んで訪れることができます(1 つも訪れないことも可能です)。同じモールを複数回訪れることはできません。

訪れたモールでは、そのモールにある M_i 個の店舗の中から好きな店舗を 0 個以上選んで買い物をすることができます。同じ店舗で買い物をできるのは 1 回までです。なお、訪れたモールで 1 つも店舗を選ばなかった場合でも、訪問費用 C_i は支払う必要があります。

高橋君が得られる 合計利得 を最大化してください。合計利得は次のように定義されます。

\text{合計利得} = \sum_{\text{買い物をした店舗 } (i,j)} P_{i,j} - \sum_{\text{訪れたモール } i} C_i

すなわち、買い物をした全店舗のお得度の総和から、訪れた全モールの訪問費用の総和を引いた値です。1 つもモールを訪れない場合、合計利得は 0 です。したがって、合計利得の最大値は 0 以上になります。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq N
  • 1 \leq M_i \leq 100
  • \sum_{i=1}^{N} M_i \leq 10^5
  • 1 \leq C_i \leq 10^9
  • 1 \leq P_{i,j} \leq 10^9
  • 入力はすべて整数である

入力

N K
C_1 M_1 P_{1,1} P_{1,2} \ldots P_{1,M_1}
C_2 M_2 P_{2,1} P_{2,2} \ldots P_{2,M_2}
\vdots
C_N M_N P_{N,1} P_{N,2} \ldots P_{N,M_N}
  • 1 行目には、ショッピングモールの数 N と、訪れることができるモールの最大数 K が、スペース区切りで与えられる。
  • 続く N 行のうち i 行目(1 \leq i \leq N)には、モール i の訪問費用 C_i、店舗の数 M_i、および各店舗のお得度 P_{i,1}, P_{i,2}, \ldots, P_{i,M_i} が、スペース区切りで与えられる(2 + M_i 個の値からなる)。

出力

高橋君が得られる合計利得の最大値を 1 行で出力せよ。


入力例 1

3 2
500 3 200 300 100
300 2 400 150
800 4 250 250 250 250

出力例 1

450

入力例 2

4 3
100 2 50 60
200 3 300 100 50
150 1 100
80 2 90 85

出力例 2

355

入力例 3

5 2
1000000000 5 500000000 400000000 300000000 200000000 100000000
100 3 50 40 30
999999999 2 1000000000 1000000000
500 4 200 150 100 80
10000 10 5000 4000 3000 2000 1000 900 800 700 600 500

出力例 3

1500000001

Score : 333 pts

Problem Statement

Takahashi is planning to visit N shopping malls in the suburbs to shop during a weekend sale.

Each shopping mall i (1 \leq i \leq N) has M_i stores, each selling sale items. If Takahashi shops at the j-th store (1 \leq j \leq M_i) of shopping mall i, he can gain a benefit value of P_{i,j} yen.

To visit shopping mall i, he must pay a visiting cost of C_i yen. This cost is a fixed value determined for each mall.

Due to time constraints, Takahashi can choose and visit at most K malls out of the N malls (it is also possible to visit none). He cannot visit the same mall more than once.

At each mall he visits, he can choose any number (zero or more) of stores from the M_i stores in that mall to shop at. He can shop at each store at most once. Note that even if he does not choose any store at a mall he visits, he still must pay the visiting cost C_i.

Maximize the total profit that Takahashi can obtain. The total profit is defined as follows:

\text{Total profit} = \sum_{\text{stores } (i,j) \text{ where he shopped}} P_{i,j} - \sum_{\text{malls } i \text{ he visited}} C_i

In other words, it is the sum of benefit values of all stores where he shopped, minus the sum of visiting costs of all malls he visited. If he visits no malls, the total profit is 0. Therefore, the maximum total profit is at least 0.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq K \leq N
  • 1 \leq M_i \leq 100
  • \sum_{i=1}^{N} M_i \leq 10^5
  • 1 \leq C_i \leq 10^9
  • 1 \leq P_{i,j} \leq 10^9
  • All input values are integers

Input

N K
C_1 M_1 P_{1,1} P_{1,2} \ldots P_{1,M_1}
C_2 M_2 P_{2,1} P_{2,2} \ldots P_{2,M_2}
\vdots
C_N M_N P_{N,1} P_{N,2} \ldots P_{N,M_N}
  • The first line contains the number of shopping malls N and the maximum number of malls that can be visited K, separated by a space.
  • In the following N lines, the i-th line (1 \leq i \leq N) contains the visiting cost C_i of mall i, the number of stores M_i, and the benefit values P_{i,1}, P_{i,2}, \ldots, P_{i,M_i} of each store, separated by spaces (consisting of 2 + M_i values).

Output

Print the maximum total profit that Takahashi can obtain, in a single line.


Sample Input 1

3 2
500 3 200 300 100
300 2 400 150
800 4 250 250 250 250

Sample Output 1

450

Sample Input 2

4 3
100 2 50 60
200 3 300 100 50
150 1 100
80 2 90 85

Sample Output 2

355

Sample Input 3

5 2
1000000000 5 500000000 400000000 300000000 200000000 100000000
100 3 50 40 30
999999999 2 1000000000 1000000000
500 4 200 150 100 80
10000 10 5000 4000 3000 2000 1000 900 800 700 600 500

Sample Output 3

1500000001
D - チェックポイントラリー

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

配点 : 400

問題文

高橋君はオリエンテーリング大会に参加することになりました。この大会では、指定されたチェックポイントを順番に訪問し、ゴール地点まで到達する必要があります。

大会の会場には N 個の地点があり、それぞれ 1 から N までの番号が付けられています。これらの地点の間には M 本の双方向の道があり、i 番目の道 (1 \leq i \leq M) は地点 U_i と地点 V_i を結び、どちらの向きに通っても T_i 分の時間がかかります。同じ2つの地点を結ぶ道が複数存在することもあります。また、すべての地点の間を道で行き来できるとは限りません。

高橋君は現在、スタート地点である地点 1 にいます。大会のルールでは、K 個の指定されたチェックポイント P_1, P_2, \ldots, P_Kこの順番通りに訪問し、最後にゴール地点である地点 N に到着しなければなりません。具体的には、まず地点 1 を出発して地点 P_1 に到達し、次に地点 P_2 に到達し、\ldots、地点 P_K に到達した後、最後に地点 N に到着する必要があります。

チェックポイントの訪問に関するルールは以下のとおりです。

  • チェックポイントは P_1, P_2, \ldots, P_K の順番でのみ消化されます。ある地点に到達した時点で、その地点が次に訪問すべきチェックポイントであれば、即座にそのチェックポイントの訪問が完了します。移動にかかる時間以外の滞在時間等は一切発生しません。
  • 出発時点で高橋君は地点 1 にいるため、P_1 = 1 の場合は出発した時点で地点 P_1 の訪問は完了します。一般に、あるチェックポイントの訪問が完了した時点で、次に訪問すべきチェックポイントが現在いる地点と同じであれば、そのチェックポイントの訪問も即座に完了します。
  • まだ訪問の順番が来ていないチェックポイントの地点を途中で通過しても、そのチェックポイントは消化されません。順番が来たときに改めてその地点にいるか、再度訪れる必要があります。
  • 指定されたチェックポイントの番号は互いに異なるとは限らず、地点 1 や地点 N と一致することもあります。

移動の全体を通して、同じ地点や同じ道を何度通ってもかまいません。

高橋君は優勝を目指しているので、できるだけ短い時間でゴールしたいと考えています。地点 1 から出発し、P_1, P_2, \ldots, P_K の順に訪問した後、地点 N に到着するまでの最小の合計移動時間(分)を求めてください。ただし、条件を満たす経路が存在しない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 5 \times 10^4
  • 1 \leq M \leq 10^5
  • 1 \leq K \leq 10
  • 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
  • U_i \neq V_i (1 \leq i \leq M)
  • 同じ2つの地点を結ぶ道が複数存在することがある
  • 1 \leq T_i \leq 10^9 (1 \leq i \leq M)
  • 1 \leq P_j \leq N (1 \leq j \leq K)
  • P_j は互いに異なるとは限らない
  • グラフは連結とは限らない
  • 入力はすべて整数である

入力

N M K
U_1 V_1 T_1
U_2 V_2 T_2
\vdots
U_M V_M T_M
P_1 P_2 \ldots P_K
  • 1 行目には、地点の数 N、道の数 M、訪問すべきチェックポイントの数 K が、スペース区切りで与えられる。
  • 続く M 行のうち i 行目 (1 \leq i \leq M) には、i 番目の道が地点 U_i と地点 V_i を双方向に結び、通過に T_i 分かかることを表す整数 U_i, V_i, T_i がスペース区切りで与えられる。
  • 最後の行には、訪問すべき K 個のチェックポイントの番号 P_1, P_2, \ldots, P_K がスペース区切りで与えられる。この順番で訪問する必要がある。

出力

地点 1 から出発し、P_1, P_2, \ldots, P_K の順に訪問した後、地点 N に到着するまでの最小の合計移動時間(分)を整数で 1 行に出力せよ。ただし、条件を満たす経路が存在しない場合は -1 を出力せよ。

なお、答えが 10^{10} を超える場合があることに注意せよ。


入力例 1

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

出力例 1

11

入力例 2

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

出力例 2

15

入力例 3

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

出力例 3

13

Score : 400 pts

Problem Statement

Takahashi is participating in an orienteering competition. In this competition, he must visit designated checkpoints in order and reach the goal.

The competition venue has N locations, numbered from 1 to N. There are M bidirectional roads between these locations. The i-th road (1 \leq i \leq M) connects location U_i and location V_i, and takes T_i minutes to traverse in either direction. There may be multiple roads connecting the same pair of locations. Also, it is not guaranteed that all locations are reachable from each other via roads.

Takahashi is currently at location 1, the starting point. According to the competition rules, he must visit K designated checkpoints P_1, P_2, \ldots, P_K in this exact order, and finally arrive at location N, the goal. Specifically, he departs from location 1 and reaches location P_1, then reaches location P_2, \ldots, reaches location P_K, and finally arrives at location N.

The rules regarding checkpoint visits are as follows:

  • Checkpoints are completed only in the order P_1, P_2, \ldots, P_K. When arriving at a location, if that location is the next checkpoint to be visited, the visit to that checkpoint is completed immediately. No time other than travel time is incurred.
  • Since Takahashi starts at location 1, if P_1 = 1, the visit to checkpoint P_1 is completed at the moment of departure. In general, when a checkpoint visit is completed, if the next checkpoint to be visited is the same as the current location, that checkpoint's visit is also completed immediately.
  • Even if you pass through a location of a checkpoint whose turn has not yet come, that checkpoint is not completed. You must either already be at that location when its turn comes, or revisit it.
  • The designated checkpoint numbers are not necessarily distinct from each other, and may coincide with location 1 or location N.

Throughout the entire journey, you may pass through the same location or the same road any number of times.

Since Takahashi aims to win, he wants to reach the goal in the shortest possible time. Find the minimum total travel time (in minutes) to depart from location 1, visit P_1, P_2, \ldots, P_K in order, and then arrive at location N. If no valid route exists, output -1.

Constraints

  • 2 \leq N \leq 5 \times 10^4
  • 1 \leq M \leq 10^5
  • 1 \leq K \leq 10
  • 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
  • U_i \neq V_i (1 \leq i \leq M)
  • There may be multiple roads connecting the same pair of locations
  • 1 \leq T_i \leq 10^9 (1 \leq i \leq M)
  • 1 \leq P_j \leq N (1 \leq j \leq K)
  • The P_j values are not necessarily distinct
  • The graph is not necessarily connected
  • All input values are integers

Input

N M K
U_1 V_1 T_1
U_2 V_2 T_2
\vdots
U_M V_M T_M
P_1 P_2 \ldots P_K
  • The first line contains the number of locations N, the number of roads M, and the number of checkpoints to visit K, separated by spaces.
  • The following M lines each contain integers U_i, V_i, T_i separated by spaces, indicating that the i-th road (1 \leq i \leq M) bidirectionally connects location U_i and location V_i and takes T_i minutes to traverse.
  • The last line contains the K checkpoint numbers P_1, P_2, \ldots, P_K separated by spaces. They must be visited in this order.

Output

Output in a single line the minimum total travel time (in minutes) as an integer to depart from location 1, visit P_1, P_2, \ldots, P_K in order, and arrive at location N. If no valid route exists, output -1.

Note that the answer may exceed 10^{10}.


Sample Input 1

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

Sample Output 1

11

Sample Input 2

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

Sample Output 2

15

Sample Input 3

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

Sample Output 3

13
E - 畑の水やり計画

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

配点 : 466

問題文

高橋君は N 日間にわたって畑の水やりを行います。毎日、2 種類の水やり方法のうちどちらか 1 つを選んで実行します。

方法 A は大量の水を一気にまく方法です。この方法を選んだ日は基本的に a の成長量を得られますが、水をまきすぎるため土壌が水浸しになり、翌日に得られる成長量が半減してしまいます(翌々日以降への影響はありません)。

方法 B は少量の水をじっくりまく方法です。この方法を選んだ日は基本的に b の成長量を得られます。土壌への負担がないため、翌日への悪影響はありません。

具体的には、各日の成長量は以下のルールで決まります。

  • 前日に方法 B を実行していた場合、または 1 日目(前日が存在しない)の場合、半減は発生しない。方法 A を選んだなら成長量は a、方法 B を選んだなら成長量は b である。
  • 前日に方法 A を実行していた場合、その日の成長量は半減する。方法 A を選んだなら成長量は \lfloor a / 2 \rfloor、方法 B を選んだなら成長量は \lfloor b / 2 \rfloor となる。ここで \lfloor x \rfloorx 以下の最大の整数を表す。

半減は常に基本の値 a または b に対して適用されます。前日の成長量が半減されていたかどうかは関係なく、「前日に方法 A を実行したかどうか」のみで判定します。たとえば、3 日連続で方法 A を選んだ場合、1 日目の成長量は a、2 日目の成長量は \lfloor a/2 \rfloor、3 日目の成長量も \lfloor a/2 \rfloor です。

N 日間の水やり方法を最適に選んだとき、成長量の合計の最大値を求めてください。

制約

  • 1 \leq N \leq 10^{12}
  • 1 \leq a \leq 10^5
  • 1 \leq b \leq 10^5
  • 入力はすべて整数である。

入力

N a b
  • 1 行目には、水やりを行う日数 N、方法 A の基本成長量 a、方法 B の基本成長量 b が、スペース区切りで与えられる。

出力

成長量の合計の最大値を 1 行で出力せよ。


入力例 1

5 10 3

出力例 1

32

入力例 2

4 3 5

出力例 2

20

入力例 3

100 100 51

出力例 3

6276

入力例 4

1000000000000 100000 1

出力例 4

50000000000050000

入力例 5

1 1 1

出力例 5

1

Score : 466 pts

Problem Statement

Takahashi will water his field over N days. Each day, he chooses and executes exactly one of two watering methods.

Method A is a method that spreads a large amount of water all at once. On a day when this method is chosen, it basically yields a growth amount of a, but because too much water is spread, the soil becomes waterlogged, and the growth amount obtained on the next day is halved (there is no effect on days after that).

Method B is a method that carefully spreads a small amount of water. On a day when this method is chosen, it basically yields a growth amount of b. Since there is no burden on the soil, there is no negative effect on the next day.

Specifically, the growth amount for each day is determined by the following rules:

  • If Method B was used on the previous day, or if it is the first day (no previous day exists), no halving occurs. If Method A is chosen, the growth amount is a; if Method B is chosen, the growth amount is b.
  • If Method A was used on the previous day, the growth amount for that day is halved. If Method A is chosen, the growth amount is \lfloor a / 2 \rfloor; if Method B is chosen, the growth amount is \lfloor b / 2 \rfloor. Here, \lfloor x \rfloor denotes the largest integer not exceeding x.

The halving is always applied to the base values a or b. It does not matter whether the previous day's growth amount was itself halved; the determination is based solely on "whether Method A was used on the previous day." For example, if Method A is chosen for three consecutive days, the growth amounts are a on the first day, \lfloor a/2 \rfloor on the second day, and \lfloor a/2 \rfloor on the third day.

Find the maximum possible total growth amount when the watering methods over the N days are chosen optimally.

Constraints

  • 1 \leq N \leq 10^{18}
  • 1 \leq a \leq 10^9
  • 1 \leq b \leq 10^9
  • All input values are integers.

Input

N a b
  • The first line contains the number of days N, the base growth amount of Method A a, and the base growth amount of Method B b, separated by spaces.

Output

Output the maximum possible total growth amount in a single line.


Sample Input 1

5 10 3

Sample Output 1

32

Sample Input 2

4 3 5

Sample Output 2

20

Sample Input 3

100 100 51

Sample Output 3

6276

Sample Input 4

1000000000000 100000 1

Sample Output 4

50000000000050000

Sample Input 5

1 1 1

Sample Output 5

1