A - Plant Growth Record

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君は N 本の植物を育てています。

現在、i 番目の植物の高さは A_i ミリメートルです。それぞれの植物は1日あたり一定の長さだけ伸び、i 番目の植物は1日あたり B_i ミリメートル成長します。

現在から D 日後における、N 本の植物の高さの合計をミリメートル単位で求めてください。

なお、答えは 2 \times 10^{16} を超えない範囲に収まることが保証されます。

制約

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

入力

N D
A_1 B_1
A_2 B_2
\vdots
A_N B_N

1 行目には、植物の本数 N と経過日数 D がスペース区切りで与えられる。

i + 1 行目 (1 \leq i \leq N) には、i 番目の植物の現在の高さ A_i と1日あたりの成長量 B_i がスペース区切りで与えられる。

出力

現在から D 日後の N 本の植物の高さの合計値をミリメートル単位で 1 行に出力してください。


入力例 1

3 5
10 2
20 3
15 1

出力例 1

75

入力例 2

2 1
100 100
1 1

出力例 2

202

入力例 3

8 30
5 2
12 4
7 1
20 3
100 10
50 5
33 7
18 2

出力例 3

1265

入力例 4

20 365
1000 12
850 7
920 9
1100 15
760 6
1340 11
980 8
1230 10
670 5
890 7
1450 13
710 4
1600 14
540 3
1180 9
1300 12
1020 6
970 8
880 5
1500 16

出力例 4

86590

入力例 5

1 1000000
1000000000 100000

出力例 5

101000000000

Score : 200 pts

Problem Statement

Takahashi is growing N plants.

Currently, the height of the i-th plant is A_i millimeters. Each plant grows by a fixed length per day, and the i-th plant grows by B_i millimeters per day.

Find the total height of all N plants, in millimeters, after D days from now.

It is guaranteed that the answer does not exceed 2 \times 10^{16}.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq D \leq 10^6
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq B_i \leq 10^5 (1 \leq i \leq N)
  • All inputs are integers

Input

N D
A_1 B_1
A_2 B_2
\vdots
A_N B_N

The first line contains the number of plants N and the number of elapsed days D, separated by a space.

The (i + 1)-th line (1 \leq i \leq N) contains the current height A_i of the i-th plant and its daily growth amount B_i, separated by a space.

Output

Print on a single line the total height of all N plants after D days from now, in millimeters.


Sample Input 1

3 5
10 2
20 3
15 1

Sample Output 1

75

Sample Input 2

2 1
100 100
1 1

Sample Output 2

202

Sample Input 3

8 30
5 2
12 4
7 1
20 3
100 10
50 5
33 7
18 2

Sample Output 3

1265

Sample Input 4

20 365
1000 12
850 7
920 9
1100 15
760 6
1340 11
980 8
1230 10
670 5
890 7
1450 13
710 4
1600 14
540 3
1180 9
1300 12
1020 6
970 8
880 5
1500 16

Sample Output 4

86590

Sample Input 5

1 1000000
1000000000 100000

Sample Output 5

101000000000
B - Choosing an Observation Deck

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333

問題文

高橋君は山岳地帯をハイキングしており、一直線上に等間隔に並んだ N 個の展望台から景色を眺めようとしています。展望台には左から順に 1, 2, \ldots, N の番号がついており、展望台 i と展望台 j の間の距離は |i - j| です。

各展望台 i の地点には高さ H_i の山がそびえています。高橋君がある展望台に立って左右を見渡すとき、他の展望台の地点にある山が視界を遮るため、見える空の広さが変わります。

具体的に、高橋君が展望台 p に立ったとき、見渡せる「空の広さ」を次のように定義します。高橋君の目線の高さは 0 とします。なお、展望台 p 自身の地点にある山は高橋君の視界を遮らないものとします。

  • 左方向の遮り度:展望台 p から左方向を見たとき、展望台 ll < p)の山が視界を遮る度合いを、距離に対する高さの比 \frac{H_l}{p - l} で表します。左方向の遮り度を

L_p = \max_{1 \le l < p} \frac{H_l}{p - l}

と定義します。ただし p = 1 のときは左に展望台が存在しないため、L_p = 0 とします。

  • 右方向の遮り度:同様に、右方向の遮り度を

R_p = \max_{p < r \le N} \frac{H_r}{r - p}

と定義します。ただし p = N のときは右に展望台が存在しないため、R_p = 0 とします。

  • 空の広さ:展望台 p における空の広さは

V_p = \frac{1}{1 + L_p + R_p}

です。

ところが、山岳地帯では工事や自然現象によって山の高さが時々変化します。

Q 個のイベントが順番に発生します。各イベントは以下の 2 種類のいずれかです。

  • タイプ 1:展望台 x の地点にある山の高さが h に変わる(すなわち H_x の値が h に更新される)。
  • タイプ 2:展望台 a から展望台 b まで(a \le b)の範囲にある展望台の中で、その時点での「空の広さ」V_p が最大となる展望台の番号を求める。最大となる展望台が複数ある場合は、最も番号の小さいものを答える。

なお、V_p の比較は厳密な値(有理数)に基づいて行われます。

各タイプ 2 のイベントについて、答えを求めてください。

制約

  • 1 \leq N \leq 200
  • 1 \leq Q \leq 200
  • 1 \leq H_i \leq 10^91 \leq i \leq N
  • タイプ 1 のイベントにおいて、1 \leq x \leq N かつ 1 \leq h \leq 10^9
  • タイプ 2 のイベントにおいて、1 \leq a \leq b \leq N
  • タイプ 2 のイベントは 1 つ以上存在する
  • 入力はすべて整数で与えられる

入力

N Q
H_1 H_2 \ldots H_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリは以下のいずれかの形式で与えられます。

1 x h
2 a b
  • 1 行目には、展望台の数 N とイベントの数 Q がスペース区切りで与えられる。
  • 2 行目には、各展望台の地点にある山の初期の高さ H_1, H_2, \ldots, H_N がスペース区切りで与えられる。
  • 3 行目から Q 行にわたって、各イベントの情報が 1 行ずつ与えられる。
  • タイプ 1 のイベントは 1 x h の形式で、展望台番号 x と新しい山の高さ h が与えられる。
  • タイプ 2 のイベントは 2 a b の形式で、範囲の左端 a と右端 b が与えられる。

出力

タイプ 2 のイベントごとに、「空の広さ」V_p が最大となる展望台の番号を 1 行に 1 つずつ出力せよ。


入力例 1

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

出力例 1

1
3
5

入力例 2

3 4
10 1 10
2 1 3
1 2 100
2 1 3
2 2 2

出力例 2

1
2
2

入力例 3

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

出力例 3

1
3
5
5
1
10

入力例 4

20 15
12 5 18 3 25 7 14 9 30 2 11 6 22 8 17 4 28 1 19 10
2 1 20
2 5 15
1 9 1
2 1 20
1 1 1000000000
2 1 5
2 18 20
1 17 1
2 10 20
2 1 20
1 5 1
1 15 500
2 1 20
2 7 13
2 19 20

出力例 4

1
9
1
1
20
20
1
1
13
20

入力例 5

1 1
1000000000
2 1 1

出力例 5

1

Score : 333 pts

Problem Statement

Takahashi is hiking in a mountainous area and wants to enjoy the scenery from N observation decks arranged in a straight line at equal intervals. The observation decks are numbered 1, 2, \ldots, N from left to right, and the distance between observation deck i and observation deck j is |i - j|.

At each observation deck i, there stands a mountain of height H_i. When Takahashi stands at an observation deck and looks around to the left and right, mountains at other observation deck locations obstruct his view, changing the amount of visible sky.

Specifically, when Takahashi stands at observation deck p, the "sky openness" is defined as follows. Takahashi's eye level is 0. Note that the mountain at observation deck p itself does not obstruct Takahashi's view.

  • Left obstruction degree: When looking left from observation deck p, the degree to which the mountain at observation deck l (l < p) obstructs the view is represented by the ratio of height to distance \frac{H_l}{p - l}. The left obstruction degree is defined as

L_p = \max_{1 \le l < p} \frac{H_l}{p - l}

However, when p = 1, there are no observation decks to the left, so L_p = 0.

  • Right obstruction degree: Similarly, the right obstruction degree is defined as

R_p = \max_{p < r \le N} \frac{H_r}{r - p}

However, when p = N, there are no observation decks to the right, so R_p = 0.

  • Sky openness: The sky openness at observation deck p is

V_p = \frac{1}{1 + L_p + R_p}

However, in the mountainous area, mountain heights occasionally change due to construction or natural phenomena.

Q events occur in order. Each event is one of the following two types:

  • Type 1: The height of the mountain at observation deck x changes to h (i.e., the value of H_x is updated to h).
  • Type 2: Among the observation decks in the range from observation deck a to observation deck b (a \le b), find the observation deck number with the maximum "sky openness" V_p at that point in time. If there are multiple observation decks with the maximum value, output the one with the smallest number.

Note that comparisons of V_p are performed based on exact values (rational numbers).

For each Type 2 event, find the answer.

Constraints

  • 1 \leq N \leq 200
  • 1 \leq Q \leq 200
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • For Type 1 events, 1 \leq x \leq N and 1 \leq h \leq 10^9
  • For Type 2 events, 1 \leq a \leq b \leq N
  • There is at least one Type 2 event
  • All input values are integers

Input

N Q
H_1 H_2 \ldots H_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in one of the following formats:

1 x h
2 a b
  • The first line contains the number of observation decks N and the number of events Q, separated by a space.
  • The second line contains the initial heights of the mountains at each observation deck H_1, H_2, \ldots, H_N, separated by spaces.
  • The following Q lines each contain information about one event.
  • A Type 1 event is given in the format 1 x h, where the observation deck number x and the new mountain height h are given.
  • A Type 2 event is given in the format 2 a b, where the left endpoint a and right endpoint b of the range are given.

Output

For each Type 2 event, output the observation deck number with the maximum "sky openness" V_p, one per line.


Sample Input 1

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

Sample Output 1

1
3
5

Sample Input 2

3 4
10 1 10
2 1 3
1 2 100
2 1 3
2 2 2

Sample Output 2

1
2
2

Sample Input 3

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

Sample Output 3

1
3
5
5
1
10

Sample Input 4

20 15
12 5 18 3 25 7 14 9 30 2 11 6 22 8 17 4 28 1 19 10
2 1 20
2 5 15
1 9 1
2 1 20
1 1 1000000000
2 1 5
2 18 20
1 17 1
2 10 20
2 1 20
1 5 1
1 15 500
2 1 20
2 7 13
2 19 20

Sample Output 4

1
9
1
1
20
20
1
1
13
20

Sample Input 5

1 1
1000000000
2 1 1

Sample Output 5

1
C - Laying Optical Fiber

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君は通信会社のエンジニアです。新しい光ファイバー回線を敷設するプロジェクトを任されました。

道路沿いに N 個の中継基地があり、各基地は一直線上に並んでいます。基地には道路の起点から近い順に 1, 2, \ldots, N と番号が付けられており、i 番目の基地は道路の起点から X_i メートルの位置にあります(X_1 < X_2 < \cdots < X_N)。各基地 i を稼働させるためには C_i 万円の費用がかかります。

高橋君のプロジェクトには M 万円の予算が割り当てられています。光ファイバー回線を敷設するために、高橋君は連続する基地の区間を1つ選ぶことができます。具体的には、1 \leq l \leq r \leq N を満たす整数の組 (l, r) を1つ選び、l 番目から r 番目までのすべての基地を稼働させます。このとき必要な費用は、選んだ区間に含まれる基地の稼働費用の合計 \displaystyle\sum_{i=l}^{r} C_i 万円であり、これが予算 M 万円以下でなければなりません。なお、基地間のケーブル敷設にかかる費用は考えなくてよいものとします。

この条件を満たす (l, r) を選んだとき、X_r - X_l メートルの光ファイバー回線を敷設できます(l = r の場合は 0 メートルです)。高橋君は、予算内で選べる区間がない場合や、あえて区間を選ばない場合には何も敷設しないこともでき、その場合の回線の長さは 0 メートルです。

高橋君が敷設できる光ファイバー回線の長さの最大値をメートル単位で求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^{14}
  • 1 \leq X_i \leq 10^9 (1 \leq i \leq N)
  • X_1 < X_2 < \cdots < X_N
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N M
X_1 C_1
X_2 C_2
\vdots
X_N C_N
  • 1 行目には、基地の数を表す整数 N と、予算(万円)を表す整数 M が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目には、各基地の情報が与えられる。
  • 1 + i 行目には、i 番目の基地の位置(メートル)を表す整数 X_i と、その基地を稼働させるために必要な費用(万円)を表す整数 C_i が、スペース区切りで与えられる。
  • 基地は位置の昇順に並んでおり、X_1 < X_2 < \cdots < X_N が保証される。

出力

高橋君が敷設できる光ファイバー回線の長さの最大値をメートル単位で 1 行に出力せよ。


入力例 1

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

出力例 1

6

入力例 2

4 5
10 3
20 3
30 3
40 3

出力例 2

0

入力例 3

8 100
5 10
15 20
30 15
50 25
80 30
120 10
200 40
300 50

出力例 3

180

Score : 366 pts

Problem Statement

Takahashi is an engineer at a telecommunications company. He has been assigned a project to lay new fiber optic cables.

There are N relay stations along a road, and each station is aligned in a straight line. The stations are numbered 1, 2, \ldots, N in order of increasing distance from the starting point of the road, and the i-th station is located X_i meters from the starting point (X_1 < X_2 < \cdots < X_N). Activating each station i costs C_i ten-thousand yen.

Takahashi's project has been allocated a budget of M ten-thousand yen. To lay the fiber optic cable, Takahashi can select one contiguous interval of stations. Specifically, he chooses a pair of integers (l, r) satisfying 1 \leq l \leq r \leq N, and activates all stations from the l-th to the r-th. The required cost is the total activation cost of all stations in the chosen interval, \displaystyle\sum_{i=l}^{r} C_i ten-thousand yen, which must not exceed the budget of M ten-thousand yen. Note that the cost of laying cables between stations can be ignored.

When a valid (l, r) is chosen under this condition, a fiber optic cable of X_r - X_l meters can be laid (if l = r, the length is 0 meters). If there is no interval that fits within the budget, or if Takahashi chooses not to select any interval, nothing is laid, and the cable length is 0 meters.

Find the maximum length of fiber optic cable that Takahashi can lay, in meters.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^{14}
  • 1 \leq X_i \leq 10^9 (1 \leq i \leq N)
  • X_1 < X_2 < \cdots < X_N
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers

Input

N M
X_1 C_1
X_2 C_2
\vdots
X_N C_N
  • The first line contains an integer N representing the number of stations and an integer M representing the budget (in ten-thousand yen), separated by a space.
  • From the 2nd line to the (N + 1)-th line, information about each station is given.
  • The (1 + i)-th line contains an integer X_i representing the position (in meters) of the i-th station and an integer C_i representing the cost (in ten-thousand yen) required to activate that station, separated by a space.
  • The stations are given in ascending order of position, and it is guaranteed that X_1 < X_2 < \cdots < X_N.

Output

Print the maximum length of fiber optic cable that Takahashi can lay, in meters, on a single line.


Sample Input 1

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

Sample Output 1

6

Sample Input 2

4 5
10 3
20 3
30 3
40 3

Sample Output 2

0

Sample Input 3

8 100
5 10
15 20
30 15
50 25
80 30
120 10
200 40
300 50

Sample Output 3

180
D - Escape from the Ice Rink

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は、縦 H マス、横 W マスの長方形のアイスリンクに閉じ込められてしまいました。リンク内には N 個の柱が立っています。 i 番目の柱は座標 (R_i, C_i) にあります。ここで、座標 (r, c) は上から r 行目、左から c 列目のマスを表します。

高橋君は現在、リンクの左上隅の座標 (1, 1) におり、右下隅の座標 (H, W) にあるゴールを目指しています。

アイスリンクの氷は非常に滑りやすく、一度滑り出すと途中で方向を変えたり止まったりすることができません。高橋君は1回の移動で、上・下・左・右の4方向のうちいずれか1つを選んで滑り始め、以下のいずれかの条件を満たすまでその方向に直進し続けます:

  • リンクの端に到達する: リンクの外に出ることはできないため、端のマスで停止します。
  • 柱の手前に到達する: 滑っている方向の次のマスに柱がある場合、柱のあるマスには入れないため、その手前のマスで停止します。

ここで、上方向とは行番号が減る方向、下方向とは行番号が増える方向、左方向とは列番号が減る方向、右方向とは列番号が増える方向を指します。

選んだ方向に1マスも進めない場合(すなわち、隣のマスが柱であるか、既にリンクの端にいてその方向にはマスが存在しない場合)は、高橋君は移動せず元のマスに留まります。この場合も1回の移動として数えます。

高橋君は同じマスを何度でも訪れることができます。

高橋君が座標 (1, 1) から出発し、座標 (H, W)停止するために必要な最小の移動回数を求めてください。座標 (H, W) で停止するとは、上記の移動ルールに従って滑った結果、座標 (H, W) で止まることを意味します。滑っている途中に座標 (H, W) を通過しただけでは、ゴールに到達したとはみなしません。

到達が不可能な場合は -1 を出力してください。

なお、座標 (1, 1) および座標 (H, W) には柱は立っていないことが保証されます。

制約

  • 2 \leq H \leq 100
  • 2 \leq W \leq 100
  • 0 \leq N \leq H \times W - 2
  • 1 \leq R_i \leq H
  • 1 \leq C_i \leq W
  • (R_i, C_i) \neq (1, 1)
  • (R_i, C_i) \neq (H, W)
  • i \neq j ならば (R_i, C_i) \neq (R_j, C_j)
  • 入力はすべて整数

入力

H W N
R_1 C_1
R_2 C_2
\vdots
R_N C_N
  • 1 行目には、アイスリンクの縦のマス数を表す H 、横のマス数を表す W 、柱の個数を表す N が、スペース区切りで与えられる。
  • 続く N 行には、各柱の座標が与えられる。 N = 0 の場合、この部分は存在しない。
  • 1 + i 行目(1 \leq i \leq N)では、 i 番目の柱の行番号 R_i と列番号 C_i が、スペース区切りで与えられる。

出力

高橋君が座標 (1, 1) から座標 (H, W) で停止するために必要な最小の移動回数を 1 行で出力せよ。到達が不可能な場合は -1 を出力せよ。


入力例 1

3 3 1
2 2

出力例 1

2

入力例 2

5 6 3
2 3
4 4
3 6

出力例 2

2

入力例 3

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

出力例 3

4

Score : 400 pts

Problem Statement

Takahashi is trapped in a rectangular ice rink with H rows and W columns. There are N pillars standing inside the rink. The i-th pillar is located at coordinates (R_i, C_i). Here, coordinates (r, c) denote the cell in the r-th row from the top and the c-th column from the left.

Takahashi is currently at the top-left corner at coordinates (1, 1) and aims to reach the goal at the bottom-right corner at coordinates (H, W).

The ice on the rink is extremely slippery, and once he starts sliding, he cannot change direction or stop midway. In one move, Takahashi chooses one of the four directions — up, down, left, or right — and begins sliding, continuing straight in that direction until one of the following conditions is met:

  • Reaching the edge of the rink: Since he cannot go outside the rink, he stops at the edge cell.
  • Reaching the cell just before a pillar: If the next cell in the sliding direction contains a pillar, he cannot enter the cell with the pillar, so he stops at the cell just before it.

Here, up means the direction of decreasing row number, down means increasing row number, left means decreasing column number, and right means increasing column number.

If he cannot advance even one cell in the chosen direction (i.e., the adjacent cell contains a pillar, or he is already at the edge of the rink and no cell exists in that direction), Takahashi does not move and stays at his current cell. This still counts as one move.

Takahashi may visit the same cell any number of times.

Find the minimum number of moves required for Takahashi to start from coordinates (1, 1) and stop at coordinates (H, W). Stopping at coordinates (H, W) means that, following the movement rules described above, he comes to a stop at coordinates (H, W). Simply passing through coordinates (H, W) while sliding does not count as reaching the goal.

If it is impossible to reach the goal, output -1.

It is guaranteed that there are no pillars at coordinates (1, 1) or coordinates (H, W).

Constraints

  • 2 \leq H \leq 100
  • 2 \leq W \leq 100
  • 0 \leq N \leq H \times W - 2
  • 1 \leq R_i \leq H
  • 1 \leq C_i \leq W
  • (R_i, C_i) \neq (1, 1)
  • (R_i, C_i) \neq (H, W)
  • If i \neq j, then (R_i, C_i) \neq (R_j, C_j)
  • All input values are integers

Input

H W N
R_1 C_1
R_2 C_2
\vdots
R_N C_N
  • The first line contains H, the number of rows of the ice rink, W, the number of columns, and N, the number of pillars, separated by spaces.
  • The following N lines contain the coordinates of each pillar. If N = 0, this part does not exist.
  • The (1 + i)-th line (1 \leq i \leq N) contains the row number R_i and column number C_i of the i-th pillar, separated by spaces.

Output

Print in one line the minimum number of moves required for Takahashi to stop at coordinates (H, W) starting from coordinates (1, 1). If it is impossible, print -1.


Sample Input 1

3 3 1
2 2

Sample Output 1

2

Sample Input 2

5 6 3
2 3
4 4
3 6

Sample Output 2

2

Sample Input 3

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

Sample Output 3

4
E - Jewel Necklace

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433

問題文

高橋君は宝石店で、N 個の宝石の中からちょうど K 個を選んでネックレスを作ることにしました。宝石には 1 から N までの番号が付けられており、それぞれ1個ずつあります。同じ宝石を複数回選ぶことはできません。

各宝石 i (1 \leq i \leq N) には「輝き」と呼ばれる整数値 P_i が定められています。また、宝石 i と宝石 j を隣り合わせに配置したときの「調和度」を表す整数値 C_{i,j} が定められています。調和度は対称であり、C_{i,j} = C_{j,i} を満たします。

ネックレスは環状であるため、選んだ K 個の宝石を円形に並べます。並べ方は自由に決めることができます。ネックレスの「美しさ」は、次の2つの値の合計として計算されます。

  • 選んだ K 個の宝石それぞれの輝きの合計
  • ネックレス上で隣り合うすべての宝石ペアの調和度の合計(環状に並べた K 個の宝石には、隣り合うペアがちょうど K 組あります)

例えば K = 3 で宝石 a, b, c をこの順に環状に並べた場合、美しさは P_a + P_b + P_c + C_{a,b} + C_{b,c} + C_{c,a} です。

高橋君は、ネックレスの美しさをできるだけ大きくしたいと考えています。N 個の宝石からちょうど K 個を選び、最適な環状順序で並べたときの、美しさの最大値を求めてください。

なお、C_{i,j} = C_{j,i} であるため、環状の並べ方において回転や反転で一致するものは美しさが等しくなります。したがって、最大値を求める上でこれらを区別する必要はありません。

制約

  • 3 \leq K \leq N \leq 16
  • -10^6 \leq P_i \leq 10^6 (1 \leq i \leq N)
  • -10^6 \leq C_{i,j} \leq 10^6 (1 \leq i, j \leq N)
  • C_{i,j} = C_{j,i} (1 \leq i, j \leq N)
  • C_{i,i} = 0 (1 \leq i \leq N)
  • 入力はすべて整数である。

入力

N K
P_1 P_2 \ldots P_N
C_{1,1} C_{1,2} \ldots C_{1,N}
C_{2,1} C_{2,2} \ldots C_{2,N}
:
C_{N,1} C_{N,2} \ldots C_{N,N}
  • 1 行目には、宝石の個数を表す整数 N と、選ぶ宝石の個数を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各宝石の輝き P_1, P_2, \ldots, P_N がスペース区切りで与えられる。
  • 3 行目から N + 2 行目までの N 行で、調和度を表す N \times N の行列が与えられる。
  • 2 + i 行目 (1 \leq i \leq N) には、 C_{i,1}, C_{i,2}, \ldots, C_{i,N} がスペース区切りで与えられる。

出力

ネックレスの美しさの最大値を 1 行で出力せよ。


入力例 1

4 3
10 20 30 40
0 5 3 1
5 0 8 2
3 8 0 6
1 2 6 0

出力例 1

106

入力例 2

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

出力例 2

-76

入力例 3

8 5
100 200 150 300 250 50 175 225
0 10 20 30 15 5 25 35
10 0 40 10 20 30 15 5
20 40 0 50 10 20 30 10
30 10 50 0 60 15 25 20
15 20 10 60 0 35 45 30
5 30 20 15 35 0 10 40
25 15 30 25 45 10 0 55
35 5 10 20 30 40 55 0

出力例 3

1325

入力例 4

12 8
500 -300 400 100 -200 600 350 -150 450 200 -100 300
0 50 -30 20 10 -40 60 15 -25 35 45 -10
50 0 40 -20 30 15 -35 25 50 -15 20 10
-30 40 0 55 -10 20 30 -45 15 25 -20 35
20 -20 55 0 45 -30 10 40 -15 50 30 -25
10 30 -10 45 0 25 -20 35 40 -30 15 50
-40 15 20 -30 25 0 50 -10 30 45 -35 20
60 -35 30 10 -20 50 0 40 -15 25 35 -30
15 25 -45 40 35 -10 40 0 50 -20 30 15
-25 50 15 -15 40 30 -15 50 0 45 -10 25
35 -15 25 50 -30 45 25 -20 45 0 40 -35
45 20 -20 30 15 -35 35 30 -10 40 0 50
-10 10 35 -25 50 20 -30 15 25 -35 50 0

出力例 4

3240

入力例 5

3 3
1000000 1000000 1000000
0 1000000 1000000
1000000 0 1000000
1000000 1000000 0

出力例 5

6000000

Score : 433 pts

Problem Statement

Takahashi decided to select exactly K jewels from N jewels at a jewelry store to make a necklace. The jewels are numbered from 1 to N, and there is exactly one of each. The same jewel cannot be selected more than once.

Each jewel i (1 \leq i \leq N) has an integer value called "brilliance" P_i. Additionally, there is an integer value C_{i,j} representing the "harmony" when jewel i and jewel j are placed adjacent to each other. The harmony is symmetric, satisfying C_{i,j} = C_{j,i}.

Since a necklace is circular, the selected K jewels are arranged in a ring. The arrangement can be chosen freely. The "beauty" of the necklace is calculated as the sum of the following two values:

  • The sum of the brilliance of each of the K selected jewels
  • The sum of the harmony of all adjacent pairs of jewels on the necklace (there are exactly K adjacent pairs among the K jewels arranged in a ring)

For example, if K = 3 and jewels a, b, c are arranged in this order in a ring, the beauty is P_a + P_b + P_c + C_{a,b} + C_{b,c} + C_{c,a}.

Takahashi wants to maximize the beauty of the necklace. Select exactly K jewels from the N jewels and arrange them in an optimal circular order. Find the maximum value of the beauty.

Note that since C_{i,j} = C_{j,i}, circular arrangements that coincide by rotation or reflection have the same beauty. Therefore, there is no need to distinguish between them when finding the maximum value.

Constraints

  • 3 \leq K \leq N \leq 16
  • -10^6 \leq P_i \leq 10^6 (1 \leq i \leq N)
  • -10^6 \leq C_{i,j} \leq 10^6 (1 \leq i, j \leq N)
  • C_{i,j} = C_{j,i} (1 \leq i, j \leq N)
  • C_{i,i} = 0 (1 \leq i \leq N)
  • All input values are integers.

Input

N K
P_1 P_2 \ldots P_N
C_{1,1} C_{1,2} \ldots C_{1,N}
C_{2,1} C_{2,2} \ldots C_{2,N}
:
C_{N,1} C_{N,2} \ldots C_{N,N}
  • The first line contains two integers separated by a space: N, the number of jewels, and K, the number of jewels to select.
  • The second line contains the brilliance values P_1, P_2, \ldots, P_N separated by spaces.
  • The following N lines (from line 3 to line N + 2) give the N \times N matrix representing the harmony values.
  • The (2 + i)-th line (1 \leq i \leq N) contains C_{i,1}, C_{i,2}, \ldots, C_{i,N} separated by spaces.

Output

Output the maximum value of the beauty of the necklace in a single line.


Sample Input 1

4 3
10 20 30 40
0 5 3 1
5 0 8 2
3 8 0 6
1 2 6 0

Sample Output 1

106

Sample Input 2

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

Sample Output 2

-76

Sample Input 3

8 5
100 200 150 300 250 50 175 225
0 10 20 30 15 5 25 35
10 0 40 10 20 30 15 5
20 40 0 50 10 20 30 10
30 10 50 0 60 15 25 20
15 20 10 60 0 35 45 30
5 30 20 15 35 0 10 40
25 15 30 25 45 10 0 55
35 5 10 20 30 40 55 0

Sample Output 3

1325

Sample Input 4

12 8
500 -300 400 100 -200 600 350 -150 450 200 -100 300
0 50 -30 20 10 -40 60 15 -25 35 45 -10
50 0 40 -20 30 15 -35 25 50 -15 20 10
-30 40 0 55 -10 20 30 -45 15 25 -20 35
20 -20 55 0 45 -30 10 40 -15 50 30 -25
10 30 -10 45 0 25 -20 35 40 -30 15 50
-40 15 20 -30 25 0 50 -10 30 45 -35 20
60 -35 30 10 -20 50 0 40 -15 25 35 -30
15 25 -45 40 35 -10 40 0 50 -20 30 15
-25 50 15 -15 40 30 -15 50 0 45 -10 25
35 -15 25 50 -30 45 25 -20 45 0 40 -35
45 20 -20 30 15 -35 35 30 -10 40 0 50
-10 10 35 -25 50 20 -30 15 25 -35 50 0

Sample Output 4

3240

Sample Input 5

3 3
1000000 1000000 1000000
0 1000000 1000000
1000000 0 1000000
1000000 1000000 0

Sample Output 5

6000000