A - Over Budget

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君は N 個のプロジェクトを担当するマネージャーです。各プロジェクトにはすでに予算が割り当てられていますが、期末の見直しで、いくつかのプロジェクトに予算を割り当てすぎていたことが判明しました。

i 番目のプロジェクト (1 \leq i \leq N) には A_i 万円の予算が割り当てられており、適正な予算額は B_i 万円です。A_i > B_i であるとき、そのプロジェクトは 予算オーバー であり、A_i - B_i 万円を返還する必要があります。A_i \leq B_i であるとき、そのプロジェクトは予算オーバーではなく、返還の必要はありません。

予算オーバーとなっているプロジェクトの数と、返還が必要な金額の合計(万円)をそれぞれ求めてください。

制約

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

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

1 行目にはプロジェクトの数 N が与えられる。続く N 行の i 行目 (1 \leq i \leq N) には、i 番目のプロジェクトに割り当てられた予算 A_i と適正な予算額 B_i が、スペース区切りで与えられる。

出力

予算オーバーとなっているプロジェクトの数と、返還が必要な金額の合計(万円)を、この順にスペース区切りで 1 行に整数で出力せよ。


入力例 1

3
100 80
50 50
30 60

出力例 1

1 20

入力例 2

5
500 300
200 400
150 100
600 600
800 350

出力例 2

3 700

入力例 3

10
1000000000 999999999
500000000 500000000
300000000 100000000
750000000 800000000
999999999 1
100 200
450000000 300000000
600000000 600000001
1 1000000000
888888888 777777777

出力例 3

5 1461111110

Score : 200 pts

Problem Statement

Takahashi is a manager in charge of N projects. Each project has already been allocated a budget, but during the end-of-term review, it was discovered that some projects had been allocated too much budget.

The i-th project (1 \leq i \leq N) has been allocated a budget of A_i ten-thousand yen, and the appropriate budget amount is B_i ten-thousand yen. When A_i > B_i, that project is over budget, and A_i - B_i ten-thousand yen must be returned. When A_i \leq B_i, that project is not over budget, and no return is necessary.

Find the number of projects that are over budget and the total amount of money (in ten-thousand yen) that needs to be returned.

Constraints

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

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

The first line gives the number of projects N. The i-th line (1 \leq i \leq N) of the following N lines gives the allocated budget A_i and the appropriate budget amount B_i for the i-th project, separated by a space.

Output

Output the number of projects that are over budget and the total amount of money (in ten-thousand yen) that needs to be returned, in this order, separated by a space, on a single line as integers.


Sample Input 1

3
100 80
50 50
30 60

Sample Output 1

1 20

Sample Input 2

5
500 300
200 400
150 100
600 600
800 350

Sample Output 2

3 700

Sample Input 3

10
1000000000 999999999
500000000 500000000
300000000 100000000
750000000 800000000
999999999 1
100 200
450000000 300000000
600000000 600000001
1 1000000000
888888888 777777777

Sample Output 3

5 1461111110
B - Exam Preparation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233

問題文

高橋君は N 科目の試験を受けることになりました。各科目 i1 \leq i \leq N)には、現在の実力で取れる得点 A_i があります。高橋君はすべての科目で合格点 T 以上の得点を取る必要があります。

高橋君は追加で勉強をすることで、各科目の得点を上げることができます。科目 i の得点を 1 点上げるには C_i 時間の勉強が必要です。すなわち、科目 i の得点を k 点上げるには k \times C_i 時間の勉強が必要です。なお、得点を下げることはできません。

現在の得点がすでに T 以上の科目については追加の勉強は不要であり、その科目にかかる勉強時間は 0 です。

高橋君がすべての科目の得点を T 以上にするために必要な合計勉強時間の最小値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq T \leq 10^4
  • 0 \leq A_i \leq 10^4
  • 1 \leq C_i \leq 10^4
  • 入力はすべて整数である。

入力

N T
A_1 C_1
A_2 C_2
\vdots
A_N C_N
  • 1 行目には、科目数を表す整数 N と合格点を表す整数 T が、スペース区切りで与えられる。
  • 1 + i 行目(1 \leq i \leq N)には、i 番目の科目の現在の得点 A_i と、その科目の得点を 1 点上げるのに必要な勉強時間 C_i が、スペース区切りで与えられる。

出力

すべての科目の得点を合格点 T 以上にするために必要な合計勉強時間の最小値を 1 行で出力してください。


入力例 1

3 60
50 3
70 2
40 5

出力例 1

130

入力例 2

5 80
75 2
100 6
60 4
50 1
80 3

出力例 2

120

入力例 3

10 100
100 8
90 5
150 3
30 2
100 10
99 100
0 1
9999 4
50 3
85 7

出力例 3

645

Score : 233 pts

Problem Statement

Takahashi has to take exams in N subjects. For each subject i (1 \leq i \leq N), he can currently score A_i points with his present ability. Takahashi needs to score at least the passing mark T in every subject.

By studying additionally, Takahashi can raise his score in each subject. To raise the score in subject i by 1 point, C_i hours of study are required. That is, to raise the score in subject i by k points, k \times C_i hours of study are required. Note that he cannot lower his score.

For subjects where his current score is already T or higher, no additional study is needed, and the study time for that subject is 0.

Find the minimum total study time required for Takahashi to bring his score in every subject to T or higher.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq T \leq 10^4
  • 0 \leq A_i \leq 10^4
  • 1 \leq C_i \leq 10^4
  • All input values are integers.

Input

N T
A_1 C_1
A_2 C_2
\vdots
A_N C_N
  • The first line contains an integer N representing the number of subjects and an integer T representing the passing mark, separated by a space.
  • The (1 + i)-th line (1 \leq i \leq N) contains the current score A_i of the i-th subject and the study time C_i required to raise that subject's score by 1 point, separated by a space.

Output

Print in one line the minimum total study time required to bring the scores of all subjects to at least the passing mark T.


Sample Input 1

3 60
50 3
70 2
40 5

Sample Output 1

130

Sample Input 2

5 80
75 2
100 6
60 4
50 1
80 3

Sample Output 2

120

Sample Input 3

10 100
100 8
90 5
150 3
30 2
100 10
99 100
0 1
9999 4
50 3
85 7

Sample Output 3

645
C - Choosing Souvenirs

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は旅行のお土産を買うために、お土産屋さんにやってきました。店には N 個のお菓子が並んでおり、それぞれに値段と美味しさが設定されています。

お菓子には 1 から N までの番号が付けられており、お菓子 i の値段は P_i 円、美味しさは S_i です。

高橋君は、友人の青木君へのお土産として、以下の条件をすべて満たすお菓子を 1 つ選びたいと思っています。

  • 値段が L 円以上 R 円以下である。
  • 美味しさが T 以上である。

条件を満たすお菓子が 1 つ以上存在する場合、その中から以下の優先順位に従ってちょうど 1 つのお菓子を選びます。

  • 値段が最も安い( P_i が最も小さい)ものを優先する。
  • 値段が同じものが複数ある場合、美味しさが最も高い( S_i が最も大きい)ものを優先する。
  • 値段も美味しさも同じものが複数ある場合、番号が最も小さいものを優先する。

お菓子の番号は 1 から N まで互いに異なるため、この優先順位により選ばれるお菓子は一意に定まります。選ばれたお菓子の番号を出力してください。

条件を満たすお菓子が 1 つも存在しない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L \leq R \leq 10^9
  • 1 \leq T \leq 10^9
  • 1 \leq P_i \leq 10^91 \leq i \leq N
  • 1 \leq S_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数である。

入力

N L R T
P_1 S_1
P_2 S_2
\vdots
P_N S_N
  • 1 行目には、お菓子の個数 N 、値段の下限 L 、値段の上限 R 、美味しさの下限 T が空白区切りで与えられる。
  • 続く N 行のうち i 行目( 1 \leq i \leq N )には、お菓子 i の値段 P_i と美味しさ S_i が空白区切りで与えられる。

出力

条件を満たすお菓子が存在する場合、上記の優先順位に従って選んだお菓子の番号を出力してください。存在しない場合は -1 を出力してください。


入力例 1

5 100 500 3
150 5
200 8
50 10
150 4
600 9

出力例 1

1

入力例 2

4 300 500 7
100 10
400 3
600 8
350 5

出力例 2

-1

入力例 3

10 200 800 5
500 3
1000 10
250 9
300 6
150 8
800 5
250 9
400 7
900 12
600 4

出力例 3

3

Score : 300 pts

Problem Statement

Takahashi has come to a souvenir shop to buy souvenirs from his trip. The shop has N sweets lined up, each with a set price and deliciousness.

The sweets are numbered from 1 to N. Sweet i has a price of P_i yen and a deliciousness of S_i.

Takahashi wants to choose exactly 1 sweet as a souvenir for his friend Aoki, satisfying all of the following conditions:

  • The price is at least L yen and at most R yen.
  • The deliciousness is at least T.

If there exists at least one sweet satisfying the conditions, he will choose exactly 1 sweet from among them according to the following priorities:

  • Prefer the one with the lowest price (smallest P_i).
  • If there are multiple sweets with the same price, prefer the one with the highest deliciousness (largest S_i).
  • If there are multiple sweets with the same price and the same deliciousness, prefer the one with the smallest number.

Since the sweet numbers from 1 to N are all distinct, the sweet chosen by these priorities is uniquely determined. Output the number of the chosen sweet.

If no sweet satisfies the conditions, output -1.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L \leq R \leq 10^9
  • 1 \leq T \leq 10^9
  • 1 \leq P_i \leq 10^91 \leq i \leq N
  • 1 \leq S_i \leq 10^91 \leq i \leq N
  • All inputs are integers.

Input

N L R T
P_1 S_1
P_2 S_2
\vdots
P_N S_N
  • The first line contains the number of sweets N, the lower bound of price L, the upper bound of price R, and the lower bound of deliciousness T, separated by spaces.
  • In the following N lines, the i-th line (1 \leq i \leq N) contains the price P_i and deliciousness S_i of sweet i, separated by spaces.

Output

If there exists a sweet satisfying the conditions, output the number of the sweet chosen according to the above priorities. If no such sweet exists, output -1.


Sample Input 1

5 100 500 3
150 5
200 8
50 10
150 4
600 9

Sample Output 1

1

Sample Input 2

4 300 500 7
100 10
400 3
600 8
350 5

Sample Output 2

-1

Sample Input 3

10 200 800 5
500 3
1000 10
250 9
300 6
150 8
800 5
250 9
400 7
900 12
600 4

Sample Output 3

3
D - The Path of Carrying Luggage

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君は配達員として、一列に並んだ N 個の倉庫を順番に訪れて荷物を集めています。倉庫は西から東へ順に 1, 2, \ldots, N と番号が付けられており、倉庫 i には重さ A_i の荷物が置かれています。

高橋君はある倉庫 s1 \leq s \leq N)からスタートし、荷物を持っていない状態で東へ向かって倉庫を順に訪れます。各倉庫を訪れるたびに、その倉庫にある荷物を拾って持ち荷に加えます。拾った荷物はすべて持ち続けます。

具体的には、各倉庫で以下の手順に従います:

  1. その倉庫にある荷物を拾い、持ち荷に加える。
  2. 持ち荷の合計重量(今拾った分を含む)を確認する。
  3. 合計重量が K より大きい(すなわち、K を超えている)場合、高橋君はそれ以上進めず、その倉庫で止まる。
  4. 合計重量が K 以下で、かつ現在の倉庫が倉庫 N でない場合、次の倉庫(東隣の倉庫)へ進み、手順 1 に戻る。
  5. 合計重量が K 以下で、かつ現在の倉庫が倉庫 N の場合、それ以上東に倉庫がないため、その倉庫で止まる。

すなわち、倉庫 s からスタートした場合、高橋君は倉庫 s, s+1, s+2, \ldots を順に訪れ、累積の荷物の重量 A_s + A_{s+1} + \cdots + A_t が初めて K より大きくなった倉庫 t で止まります。K を超えることなく倉庫 N まで到達した場合は倉庫 N で止まります。

高橋君が止まった倉庫の番号を f(s) と定義します。

高橋君は Q 個の質問に答えたいと思っています。j 番目の質問では、スタート地点の範囲を表す整数 L_j, R_j が与えられます。f(L_j) + f(L_j + 1) + \cdots + f(R_j) の値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{15}
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q)
  • 入力はすべて整数である。

入力

N K Q
A_1 A_2 \ldots A_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q
  • 1 行目には、倉庫の数 N 、荷物の合計重量の上限 K 、質問の数 Q が、スペース区切りで与えられる。
  • 2 行目には、各倉庫の荷物の重さ A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 3 行目から Q 行にわたって、各質問のスタート地点の範囲 L_j, R_j がスペース区切りで与えられる。

出力

Q 行出力してください。 j 行目には、 j 番目の質問に対する答え、すなわち f(L_j) + f(L_j + 1) + \cdots + f(R_j) の値を出力してください。


入力例 1

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

出力例 1

23
13
15

入力例 2

4 5 2
3 3 3 3
1 4
2 3

出力例 2

13
7

入力例 3

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

出力例 3

78
39
4
56

入力例 4

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

出力例 4

164
62
35
90
67

入力例 5

1 1000000000000000 1
1
1 1

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi is a delivery worker who visits N warehouses arranged in a row, collecting packages in order. The warehouses are numbered 1, 2, \ldots, N from west to east, and warehouse i contains a package of weight A_i.

Takahashi starts at some warehouse s (1 \leq s \leq N) carrying no packages, and visits warehouses sequentially heading east. Each time he visits a warehouse, he picks up the package there and adds it to his load. He keeps all packages he has picked up.

Specifically, at each warehouse, he follows these steps:

  1. Pick up the package at that warehouse and add it to his load.
  2. Check the total weight of his load (including the package just picked up).
  3. If the total weight is greater than K (i.e., exceeds K), Takahashi cannot proceed further and stops at that warehouse.
  4. If the total weight is at most K and the current warehouse is not warehouse N, he moves to the next warehouse (the neighboring warehouse to the east) and returns to step 1.
  5. If the total weight is at most K and the current warehouse is warehouse N, there are no more warehouses to the east, so he stops at that warehouse.

In other words, when starting from warehouse s, Takahashi visits warehouses s, s+1, s+2, \ldots in order, and stops at warehouse t where the cumulative package weight A_s + A_{s+1} + \cdots + A_t first exceeds K. If he reaches warehouse N without exceeding K, he stops at warehouse N.

Define f(s) as the number of the warehouse where Takahashi stops.

Takahashi wants to answer Q questions. In the j-th question, integers L_j, R_j representing a range of starting points are given. Find the value of f(L_j) + f(L_j + 1) + \cdots + f(R_j).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{15}
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q)
  • All input values are integers.

Input

N K Q
A_1 A_2 \ldots A_N
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q
  • The first line contains the number of warehouses N, the upper limit on total weight K, and the number of questions Q, separated by spaces.
  • The second line contains the weights of packages at each warehouse A_1, A_2, \ldots, A_N, separated by spaces.
  • The following Q lines each contain the range of starting points L_j, R_j for each question, separated by spaces.

Output

Print Q lines. On the j-th line, print the answer to the j-th question, namely the value of f(L_j) + f(L_j + 1) + \cdots + f(R_j).


Sample Input 1

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

Sample Output 1

23
13
15

Sample Input 2

4 5 2
3 3 3 3
1 4
2 3

Sample Output 2

13
7

Sample Input 3

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

Sample Output 3

78
39
4
56

Sample Input 4

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

Sample Output 4

164
62
35
90
67

Sample Input 5

1 1000000000000000 1
1
1 1

Sample Output 5

1
E - Optimal Route for a Sightseeing Tour

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433

問題文

高橋君は N 個の観光スポットがある街を旅行しています。観光スポットには 1 から N までの番号が付けられており、観光スポット間を結ぶ M 本の双方向の道路があります。

各観光スポット i には「満足度」と呼ばれる正の整数値 P_i が設定されています。高橋君はホテルのある観光スポット S からスタートし、目的地である観光スポット T へ向かいます。

道路 j は観光スポット U_jV_j を双方向に結んでおり、どちらの向きに通過しても交通費 W_j がかかります。

高橋君は観光スポット S を出発し、道路を順にたどって観光スポット T に到着します。このとき、同じ道路や同じ観光スポットを何度通ってもかまいません。

高橋君の「利得」を次のように定義します。

  • 出発地 S および到着地 T を含め、経路上で通過したすべての観光スポットの集合を考える。この集合に含まれる各観光スポットの満足度を 1 回ずつ合計した値から、経路上で通過した道路の交通費の合計値を引いた値を利得とする。
  • 同じ観光スポットを複数回通過した場合でも、その観光スポットの満足度は 1 回分のみ加算される。
  • 同じ道路を複数回通過した場合は、通過した回数分だけ交通費が加算される。道路の通過方向によらず、同じ道路を通過するたびに 1 回分の交通費がかかる。

高橋君が観光スポット S から観光スポット T まで移動するとき、利得の最大値を求めてください。

なお、同じ道路を無駄に通過するほど交通費が増える一方で満足度は増えないため、利得の最大値は必ず有限に定まります。利得の最大値が負になる場合もあることに注意してください。S から T へ到達可能であることは保証されます。

制約

  • 2 \leq N \leq 12
  • 1 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq P_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq S \leq N
  • 1 \leq T \leq N
  • S \neq T
  • 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
  • 1 \leq W_j \leq 10^4 (1 \leq j \leq M)
  • 同じ観光スポットの組を結ぶ道路は高々 1 本である。
  • 観光スポット S から観光スポット T へ到達可能である。
  • 入力はすべて整数で与えられる。

入力

N M
P_1 P_2 \ldots P_N
S T
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
  • 1 行目には、観光スポットの数を表す N と、道路の数を表す M が、スペース区切りで与えられる。
  • 2 行目には、各観光スポットの満足度を表す P_1, P_2, \ldots, P_N が、スペース区切りで与えられる。
  • 3 行目には、スタート地点を表す S と、ゴール地点を表す T が、スペース区切りで与えられる。
  • 4 行目から M 行にわたって、道路の情報が与えられる。
  • 3 + j 行目では、 j 番目の道路が結ぶ観光スポット U_j , V_j と、その交通費 W_j が、スペース区切りで与えられる。

出力

利得の最大値を 1 行で出力せよ。なお、利得の最大値が負になる場合は負の値を出力すること。


入力例 1

3 3
10 20 30
1 3
1 2 5
2 3 5
1 3 25

出力例 1

50

入力例 2

5 7
10 20 30 40 50
1 5
1 2 3
1 4 10
2 3 4
2 4 7
2 5 3
3 5 8
4 5 6

出力例 2

126

入力例 3

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

出力例 3

294

Score : 433 pts

Problem Statement

Takahashi is traveling in a city with N sightseeing spots. The sightseeing spots are numbered from 1 to N, and there are M bidirectional roads connecting them.

Each sightseeing spot i has a positive integer value P_i called "satisfaction." Takahashi starts from sightseeing spot S, where his hotel is located, and heads to his destination, sightseeing spot T.

Road j bidirectionally connects sightseeing spots U_j and V_j, and costs a transportation fee of W_j regardless of the direction of travel.

Takahashi departs from sightseeing spot S and follows roads sequentially to arrive at sightseeing spot T. He may pass through the same road or the same sightseeing spot any number of times.

Takahashi's "profit" is defined as follows:

  • Consider the set of all sightseeing spots visited along the route, including the starting point S and the destination T. The profit is the sum of the satisfaction values of each sightseeing spot in this set (counted once each) minus the total transportation fees of all roads traversed along the route.
  • Even if the same sightseeing spot is visited multiple times, its satisfaction value is added only once.
  • If the same road is traversed multiple times, the transportation fee is added for each traversal. Regardless of the direction of travel, each traversal of a road incurs one transportation fee.

Find the maximum profit when Takahashi travels from sightseeing spot S to sightseeing spot T.

Note that traversing roads unnecessarily only increases transportation fees without increasing satisfaction, so the maximum profit is always finite. Be aware that the maximum profit may be negative. It is guaranteed that T is reachable from S.

Constraints

  • 2 \leq N \leq 12
  • 1 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq P_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq S \leq N
  • 1 \leq T \leq N
  • S \neq T
  • 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
  • 1 \leq W_j \leq 10^4 (1 \leq j \leq M)
  • There is at most one road connecting any pair of sightseeing spots.
  • Sightseeing spot T is reachable from sightseeing spot S.
  • All input values are integers.

Input

N M
P_1 P_2 \ldots P_N
S T
U_1 V_1 W_1
U_2 V_2 W_2
\vdots
U_M V_M W_M
  • The first line contains N, the number of sightseeing spots, and M, the number of roads, separated by a space.
  • The second line contains the satisfaction values P_1, P_2, \ldots, P_N of each sightseeing spot, separated by spaces.
  • The third line contains the starting point S and the goal point T, separated by a space.
  • The following M lines provide road information.
  • The (3 + j)-th line contains the sightseeing spots U_j, V_j connected by the j-th road and its transportation fee W_j, separated by spaces.

Output

Output the maximum profit in a single line. If the maximum profit is negative, output a negative value.


Sample Input 1

3 3
10 20 30
1 3
1 2 5
2 3 5
1 3 25

Sample Output 1

50

Sample Input 2

5 7
10 20 30 40 50
1 5
1 2 3
1 4 10
2 3 4
2 4 7
2 5 3
3 5 8
4 5 6

Sample Output 2

126

Sample Input 3

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

Sample Output 3

294