A - ABC400 Party

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

ABC400 を記念した式典において、400 人の高橋君を AB 列の長方形状に隙間なく並べようとしています。

正整数 A が与えられるので、このような並べ方ができるような正整数 B の値を出力してください。ただし、そのような正整数 B が存在しない場合には -1 を出力してください。

制約

  • A1 以上 400 以下の整数

入力

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

A

出力

問題文の指示に従って B の値あるいは -1 を出力せよ。


入力例 1

10

出力例 1

40

400 人の高橋君を 1040 列の長方形状に並べることができます。


入力例 2

11

出力例 2

-1

入力例 3

400

出力例 3

1

Score : 100 points

Problem Statement

In the ceremony commemorating ABC400, we want to arrange 400 people in a rectangular formation of A rows and B columns without any gaps.

You are given a positive integer A. Print the value of a positive integer B for which such an arrangement is possible. If there is no such positive integer B, print -1.

Constraints

  • A is an integer between 1 and 400, inclusive.

Input

The input is given from Standard Input in the following format:

A

Output

Print the value of B or -1 as specified by the problem statement.


Sample Input 1

10

Sample Output 1

40

We can arrange 400 people in 10 rows and 40 columns.


Sample Input 2

11

Sample Output 2

-1

Sample Input 3

400

Sample Output 3

1
B - Sum of Geometric Series

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

正整数 N, M が与えられます。

X = \displaystyle\sum_{i = 0}^{M} N^i とします。X \leq 10^9 のときは X の値を、X > 10^9 のときは文字列 inf を出力してください。

制約

  • 1 \leq N \leq 10^9
  • 1 \leq M \leq 100
  • 入力される値はすべて整数

入力

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

N M

出力

問題文の指示に従って X の値あるいは inf を出力せよ。


入力例 1

7 3

出力例 1

400

X = 1 + 7 + 49 + 343 = 400 です。400 \leq 10^9 であるため 400 を出力します。


入力例 2

1000000 2

出力例 2

inf

X = 1000001000001 > 10^9 であるため、inf を出力します。


入力例 3

999999999 1

出力例 3

1000000000

入力例 4

998244353 99

出力例 4

inf

Score : 200 points

Problem Statement

You are given two positive integers N and M.

Let X = \displaystyle\sum_{i = 0}^{M} N^i. If X \leq 10^9, print the value of X. If X > 10^9, print inf.

Constraints

  • 1 \leq N \leq 10^9
  • 1 \leq M \leq 100
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M

Output

Print the value of X or inf as specified by the problem statement.


Sample Input 1

7 3

Sample Output 1

400

X = 1 + 7 + 49 + 343 = 400. Since 400 \leq 10^9, print 400.


Sample Input 2

1000000 2

Sample Output 2

inf

X = 1000001000001 > 10^9, so print inf.


Sample Input 3

999999999 1

Sample Output 3

1000000000

Sample Input 4

998244353 99

Sample Output 4

inf
C - 2^a b^2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

正の整数 X は、次の条件をみたすときかつその時に限り、良い整数と呼ばれます。

  • 正の整数の組 (a,b) を用いて、X=2^a\times b^2 と書ける。

例えば、400400=2^2\times 10^2 と書けるため、良い整数です。

正の整数 N が与えられるので、1 以上 N 以下の良い整数の個数を求めてください。

制約

  • 1 \leq N \leq 10^{18}
  • N は整数

入力

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

N

出力

1 以上 N 以下の良い整数の個数を出力せよ。


入力例 1

20

出力例 1

5

1 以上 20 以下の良い整数は 2,4,8,16,185 つです。
よって、5 を出力します。


入力例 2

400

出力例 2

24

入力例 3

1234567890

出力例 3

42413

入力が 32bit 整数型に収まるとは限らないことに注意してください。

Score : 350 points

Problem Statement

A positive integer X is called a good integer if and only if it satisfies the following condition:

  • There exists a pair of positive integers (a,b) such that X = 2^a \times b^2.

For example, 400 is a good integer because 400 = 2^2 \times 10^2.

Given a positive integer N, find the number of good integers between 1 and N, inclusive.

Constraints

  • 1 \leq N \leq 10^{18}
  • N is an integer.

Input

The input is given from Standard Input in the following format:

N

Output

Print the number of good integers between 1 and N, inclusive.


Sample Input 1

20

Sample Output 1

5

There are five good integers between 1 and 20: 2, 4, 8, 16, and 18.
Thus, print 5.


Sample Input 2

400

Sample Output 2

24

Sample Input 3

1234567890

Sample Output 3

42413

Note that the input might not fit in a 32-bit integer type.

D - Takahashi the Wall Breaker

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は魚屋にウナギを買いに行こうとしています。

高橋君の住む町は縦 H マス横 W マスからなるグリッド状の区画に分かれており、 各区画は道か壁かのいずれかです。
以下、上から i 番目 (1\leq i \leq H) かつ左から j 番目 (1\leq j\leq W) の区画を区画 (i,j) で表します。
各区画の情報は H 個の長さ W の文字列 S_1,S_2,\ldots,S_H で与えられ、 具体的には S_ij 文字目 (1\leq i \leq H,1\leq j\leq W). のとき区画 (i,j) が道であることを、 S_ij 文字目が # のとき区画 (i,j) が壁であることを表します。

高橋君は次の 2 種類の行動を好きな順番で繰り返し行うことができます。

  • 上下左右に隣接する、町の中の区画であって、道であるようなものに移動する。
  • 上下左右の方向を一つ決め、その方向に前蹴りを行う。
    高橋君が前蹴りを行うと、現在いる区画からその方向に 1 つ前の区画および 2 つ前の区画について、その区画が壁ならば道に変えることができる。
    ここで、1 つ前の区画または 2 つ前の区画が町の外である場合でも前蹴りを行うことができるが、町の外が変化することはない。

高橋君は最初、区画 (A,B) におり、区画 (C,D) にある魚屋まで移動したいです。
ここで、高橋君の最初にいる区画および魚屋のある区画は道であることが保証されます。
高橋君が魚屋にたどり着くために必要な 前蹴りの回数 の最小値を求めてください。

制約

  • 1\leq H\leq 1000
  • 1\leq W\leq 1000
  • S_i., # のみからなる長さ W の文字列
  • 1\leq A,C\leq H
  • 1\leq B,D\leq W
  • (A,B)\neq (C,D)
  • H,W,A,B,C,D は整数
  • 高橋君の最初にいる区画および魚屋のある区画は道である。

入力

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

H W
S_1
S_2
\vdots
S_H
A B C D

出力

高橋君が魚屋にたどり着くために必要な 前蹴りの回数 の最小値を出力せよ。


入力例 1

10 10
..........
#########.
#.......#.
#..####.#.
##....#.#.
#####.#.#.
.##.#.#.#.
###.#.#.#.
###.#.#.#.
#.....#...
1 1 7 1

出力例 1

1

高橋君は最初、区画 (1,1) にいます。
道である区画への移動を繰り返して区画 (7,4) まで移動することができます。
区画 (7,4) において左方向に対して前蹴りを行うと区画 (7,3) と区画 (7,2) が壁から道に変わります。
その後、道である区画(道に変わった区画を含む)への移動を繰り返して、区画 (7,1) にある魚屋に移動することができます。

このとき、前蹴りを行った回数は 1 回であり、前蹴りを行わずに魚屋へ移動することは不可能なため、1 を出力します。


入力例 2

2 2
.#
#.
1 1 2 2

出力例 2

1

高橋君は最初、区画 (1,1) にいます。
右方向に対して前蹴りを行うと、区画 (1,2) を壁から道に変えることができます。
区画 (1,1) から右方向に 2 つ前のマスは町の外であるため、変化しません。
その後、高橋君は区画 (1,2) へ移動し、区画 (2,2) にある魚屋へ移動することができます。

このとき、前蹴りを行った回数は 1 回であり、前蹴りを行わずに魚屋へ移動することは不可能なため、1 を出力します。


入力例 3

1 3
.#.
1 1 1 3

出力例 3

1

前蹴りを行う際、それにより道に変えられ得る区画の中に魚屋のある区画が含まれていても問題ありません。 具体的には魚屋のある区画はもともと道であるため変化せず、特に前蹴りによって魚屋が壊れることはありません。


入力例 4

20 20
####################
##...##....###...###
#.....#.....#.....##
#..#..#..#..#..#..##
#..#..#....##..#####
#.....#.....#..#####
#.....#..#..#..#..##
#..#..#.....#.....##
#..#..#....###...###
####################
####################
##..#..##...###...##
##..#..#.....#.....#
##..#..#..#..#..#..#
##..#..#..#..#..#..#
##.....#..#..#..#..#
###....#..#..#..#..#
#####..#.....#.....#
#####..##...###...##
####################
3 3 18 18

出力例 4

3

Score : 400 points

Problem Statement

Takahashi is about to go buy eel at a fish shop.

The town where he lives is divided into a grid of H rows and W columns. Each cell is either a road or a wall.
Let us denote the cell at the i-th row from the top (1\leq i \leq H) and the j-th column from the left (1\leq j \leq W) as cell (i,j).
Information about each cell is given by H strings S_1,S_2,\ldots,S_H, each of length W. Specifically, if the j-th character of S_i (1\leq i \leq H,1\leq j\leq W) is ., cell (i,j) is a road; if it is #, cell (i,j) is a wall.

He can repeatedly perform the following two types of actions in any order:

  • Move to an adjacent cell (up, down, left, or right) that is within the town and is a road.
  • Choose one of the four directions (up, down, left, or right) and perform a front kick in that direction.
    When he performs a front kick, for each of the cells at most 2 steps away in that direction from the cell he is currently in, if that cell is a wall, it becomes a road.
    If some of the cells at most 2 steps away are outside the town, a front kick can still be performed, but anything outside the town does not change.

He starts in cell (A,B), and he wants to move to the fish shop in cell (C,D).
It is guaranteed that both the cell where he starts and the cell with the fish shop are roads.
Find the minimum number of front kicks he needs in order to reach the fish shop.

Constraints

  • 1\leq H\leq 1000
  • 1\leq W\leq 1000
  • Each S_i is a string of length W consisting of . and #.
  • 1\leq A,C\leq H
  • 1\leq B,D\leq W
  • (A,B)\neq (C,D)
  • H, W, A, B, C, and D are integers.
  • The cell where Takahashi starts and the cell with the fish shop are roads.

Input

The input is given from Standard Input in the following format:

H W
S_1
S_2
\vdots
S_H
A B C D

Output

Print the minimum number of front kicks needed for Takahashi to reach the fish shop.


Sample Input 1

10 10
..........
#########.
#.......#.
#..####.#.
##....#.#.
#####.#.#.
.##.#.#.#.
###.#.#.#.
###.#.#.#.
#.....#...
1 1 7 1

Sample Output 1

1

Takahashi starts in cell (1,1).
By repeatedly moving to adjacent road cells, he can reach cell (7,4).
If he performs a front kick to the left from cell (7,4), cells (7,3) and (7,2) turn from walls to roads.
Then, by continuing to move through road cells (including those that have become roads), he can reach the fish shop in cell (7,1).

In this case, the number of front kicks performed is 1, and it is impossible to reach the fish shop without performing any front kicks, so print 1.


Sample Input 2

2 2
.#
#.
1 1 2 2

Sample Output 2

1

Takahashi starts in cell (1,1).
When he performs a front kick to the right, cell (1,2) turns from a wall to a road.
The cell two steps to the right of (1,1) is outside the town, so it does not change.
Then, he can move to cell (1,2) and then to the fish shop in cell (2,2).

In this case, the number of front kicks performed is 1, and it is impossible to reach the fish shop without performing any front kicks, so print 1.


Sample Input 3

1 3
.#.
1 1 1 3

Sample Output 3

1

When performing a front kick, it is fine if the fish shop’s cell is within the cells that could be turned into a road. Specifically, the fish shop’s cell is a road from the beginning, so it remains unchanged; particularly, the shop is not destroyed by the front kick.


Sample Input 4

20 20
####################
##...##....###...###
#.....#.....#.....##
#..#..#..#..#..#..##
#..#..#....##..#####
#.....#.....#..#####
#.....#..#..#..#..##
#..#..#.....#.....##
#..#..#....###...###
####################
####################
##..#..##...###...##
##..#..#.....#.....#
##..#..#..#..#..#..#
##..#..#..#..#..#..#
##.....#..#..#..#..#
###....#..#..#..#..#
#####..#.....#.....#
#####..##...###...##
####################
3 3 18 18

Sample Output 4

3
E - Ringo's Favorite Numbers 3

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

正整数 N に対し、N400 number であることは以下の 2 つの条件をともに満たすことであると定義します。

  • N の素因数はちょうど 2 種類である。
  • N の各素因数 p について、Np で割り切れる回数は偶数回である。より厳密には、N の各素因数 p について p^kN の約数であるような最大の非負整数 k は偶数である。

Q 個のクエリが与えられるので、それぞれについて答えてください。各クエリでは、整数 A が与えられるので、A 以下の最大の 400 number の値を求めてください。ただし、本問題の制約下では A 以下の 400 number は常に存在します。

制約

  • 1 \leq Q \leq 2 \times 10^5
  • 各クエリについて、36 \leq A \leq 10^{12}
  • 入力される値はすべて整数

入力

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

ここで、\text{query}_ii 番目のクエリであり、以下の形式で与えられる。

A

出力

Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。


入力例 1

5
404
36
60
1000000000000
123456789

出力例 1

400
36
36
1000000000000
123454321

1 番目のクエリについて説明します。

400 の素因数は 2, 5 のちょうど 2 種類であり、4002 で割り切れる回数は 4 回、5 で割り切れる回数は 2 回であるため 400 は 400 number です。401, 402, 403, 404 は 400 number ではないため答えは 400 となります。

Score : 425 points

Problem Statement

A positive integer N is a 400 number if and only if it satisfies both of the following two conditions:

  • N has exactly 2 distinct prime factors.
  • For each prime factor p of N, p divides N an even number of times. More formally, the maximum non-negative integer k such that p^k divides N is even.

Process Q queries. Each query gives you an integer A, so find the largest 400 number not exceeding A. Under the constraints of this problem, a 400 number not exceeding A always exists.

Constraints

  • 1 \leq Q \leq 2 \times 10^5
  • For each query, 36 \leq A \leq 10^{12}.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Here, \text{query}_i is the i-th query, given in the following format:

A

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

5
404
36
60
1000000000000
123456789

Sample Output 1

400
36
36
1000000000000
123454321

Let us explain the first query.

There are exactly 2 prime factors of 400: 2 and 5. Also, 2 divides 400 four times and 5 divides it twice, so 400 is a 400 number. None of 401, 402, 403, and 404 is a 400 number, so the answer is 400.

F - Happy Birthday! 3

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

半径で切って N 等分された円形のケーキがあります。

各ピースには時計回りに 1, 2, \ldots, N の番号が付けられています。また、1 \leq i \leq N なる整数 i についてピース i をピース N + i とも呼ぶこととします。

はじめ、すべてのピースの色は色 0 です。

あなたは、以下の操作を好きな回数行うことができます。

  • 1 \leq a, b, c \leq N なる整数 a, b, c を選ぶ。0 \leq i < b なる各整数 i に対してピース a + i の色を色 c に変更する。この操作にはコストが b + X_c かかる。

1 \leq i \leq N なるすべての整数 i に対してピース i の色を色 C_i にするために必要なコストの合計の最小値を求めてください。

制約

  • 1 \leq N \leq 400
  • 1 \leq C_i \leq N
  • 1 \leq X_i \leq 10^9
  • 入力される値はすべて整数

入力

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

N
C_1 C_2 \ldots C_N
X_1 X_2 \ldots X_N

出力

答えを出力せよ。


入力例 1

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

出力例 1

20

ピース i の色が色 A_i であるとします。はじめ、(A_1, A_2, A_3, A_4, A_5, A_6) = (0, 0, 0, 0, 0, 0) です。

(a, b, c) = (2, 1, 4) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (0, 4, 0, 0, 0, 0) となります。

(a, b, c) = (3, 3, 2) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (0, 4, 2, 2, 2, 0) となります。

(a, b, c) = (1, 1, 1) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 2, 2, 0) となります。

(a, b, c) = (4, 1, 1) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 1, 2, 0) となります。

(a, b, c) = (6, 1, 5) として操作を行うと (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 1, 2, 5) となります。

このとき、コストの合計は 5 + 5 + 2 + 2 + 6 = 20 となります。


入力例 2

5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000

出力例 2

5000000005

入力例 3

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

出力例 3

23

Score : 550 points

Problem Statement

There is a circular cake that has been cut into N equal slices by its radii.

Each piece is labeled with an integer from 1 to N in clockwise order, and for each integer i with 1 \leq i \leq N, the piece i is also referred to as piece N + i.

Initially, every piece’s color is color 0.

You can perform the following operation any number of times:

  • Choose integers a, b, and c such that 1 \leq a, b, c \leq N. For each integer i with 0 \leq i < b, change the color of piece a + i to color c. The cost of this operation is b + X_c.

You want each piece i (for 1 \leq i \leq N) to have color C_i. Find the minimum total cost of operations needed to achieve this.

Constraints

  • 1 \leq N \leq 400
  • 1 \leq C_i \leq N
  • 1 \leq X_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
C_1 C_2 \ldots C_N
X_1 X_2 \ldots X_N

Output

Print the answer.


Sample Input 1

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

Sample Output 1

20

Let A_i denote the color of piece i. Initially, (A_1, A_2, A_3, A_4, A_5, A_6) = (0, 0, 0, 0, 0, 0).

Performing an operation with (a, b, c) = (2, 1, 4) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (0, 4, 0, 0, 0, 0).

Performing an operation with (a, b, c) = (3, 3, 2) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (0, 4, 2, 2, 2, 0).

Performing an operation with (a, b, c) = (1, 1, 1) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (1, 4, 2, 2, 2, 0).

Performing an operation with (a, b, c) = (4, 1, 1) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (1, 4, 2, 1, 2, 0).

Performing an operation with (a, b, c) = (6, 1, 5) changes (A_1, A_2, A_3, A_4, A_5, A_6) to (1, 4, 2, 1, 2, 5).

In this case, the total cost is 5 + 5 + 2 + 2 + 6 = 20.


Sample Input 2

5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 2

5000000005

Sample Input 3

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

Sample Output 3

23
G - Patisserie ABC 3

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 625

問題文

ABC 洋菓子店で働くパティシエである高橋君は、AtCoder Beginner Contest 400 を記念したケーキのセットを販売することにしました。

ABC 洋菓子店にはケーキ 1, ケーキ 2, \ldots, ケーキ NN 種類のケーキが販売されています。
各ケーキは綺麗さ、おいしさ、人気度の 3 つの非負整数値を持ち、 具体的にはケーキ i の綺麗さ、おいしさ、人気度はそれぞれ X_i,Y_i,Z_i です。

高橋君はケーキを被りのない K 個のペアにして売り出すことを考えました。
形式的に説明すると、2K 個の 相異なる 1 以上 N 以下の整数 a_1,b_1,a_2,b_2,\ldots,a_K,b_K を選んでケーキ a_i とケーキ b_i をペアにすることにしました。
ケーキ a_i とケーキ b_i をペアにした時のペアの価格は \max(X_{a_i}+X_{b_i}, Y_{a_i}+Y_{b_i}, Z_{a_i}+Z_{b_i}) とします。
ただし、\max(P,Q,R)P,Q,R のうち最大の値を表します。

K 個のペアの価格の総和としてあり得る最大の値を求めてください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\leq T\leq 1000
  • 2\leq N \leq 10^5
  • 各入力ファイルについて、すべてのテストケースの N の総和は 10^5 以下である。
  • 1\leq K \leq \lfloor \frac{N}{2}\rfloor (実数 x に対し、\lfloor x\rfloorx 以下の最大の整数を表す。)
  • 0\leq X_i,Y_i,Z_i \leq 10^9
  • 入力はすべて整数

入力

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

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

\mathrm{case}_ii 番目のテストケースを表す。 各テストケースは以下の形式で与えられる。

N K
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_N Y_N Z_N

出力

T 行出力せよ。i 行目 (1\leq i\leq N) には i 番目のテストケースに対する答えを出力せよ。


入力例 1

1
3 1
6 3 8
3 5 0
2 7 3

出力例 1

12

3 つのケーキから 1 つのペアを作ります。

ケーキ 1 とケーキ 2 をペアにした時の価格は \max(6+3,3+5,8+0)=9 です。
ケーキ 1 とケーキ 3 をペアにした時の価格は \max(6+2,3+7,8+3)=11 です。
ケーキ 2 とケーキ 3 をペアにした時の価格は \max(3+2,5+7,0+3)=12 です。

よってケーキ 2 とケーキ 3 をペアにした時の価格が最大であり、12 を出力します。


入力例 2

2
5 2
1 2 3
1 2 3
1 2 3
1 2 3
100 100 200
6 2
21 74 25
44 71 80
46 28 96
1 74 24
81 83 16
55 31 1

出力例 2

209
333

それぞれのケーキは高々 1 つのペアにしか含むことができないことに注意してください。
また、異なる種類のケーキであっても、綺麗さ、おいしさ、人気度がすべて等しい場合もあることに注意してください。

1 つめのテストケースについては、ケーキ 1 とケーキ 2 をペアにしたときの価格は 6、ケーキ 3 とケーキ 5 をペアにしたときの価格は 203 であり、これら 2 つのペアを選んだとき価格の合計は 209 となりこのときが最大です。

2 つめのテストケースについては、ケーキ 2 とケーキ 3 をペアにしたときの価格は 176、ケーキ 4 とケーキ 5 をペアにしたときの価格は 157 であり、これら 2 つのペアを選んだとき価格の合計は 333 となりこのときが最大です。

Score : 625 points

Problem Statement

Takahashi, a patissier working at the ABC pastry shop, decided to sell assorted cakes to commemorate AtCoder Beginner Contest 400.

The shop sells N kinds of cakes: cake 1, cake 2, \ldots, cake N.
Each cake has three non-negative integer values: beauty, tastiness, and popularity. Specifically, cake i has beauty X_i, tastiness Y_i, and popularity Z_i.

He considers pairing up these cakes into K pairs without overlaps.
Formally, he will choose 2K distinct integers a_1,b_1,a_2,b_2,\ldots,a_K,b_K between 1 and N (inclusive), and pair cake a_i with cake b_i.
The price of a pair formed by cakes a_i and b_i is \max(X_{a_i} + X_{b_i},\, Y_{a_i} + Y_{b_i},\, Z_{a_i} + Z_{b_i}).
Here, \max(P,Q,R) denotes the greatest value among P,Q,R.

Find the maximum possible total price of the K pairs.

You are given T test cases; solve each of them.

Constraints

  • 1\leq T\leq 1000
  • 2\leq N \leq 10^5
  • The sum of N over all test cases in each input file is at most 10^5.
  • 1\leq K \leq \lfloor \frac{N}{2}\rfloor (For a real number x, \lfloor x\rfloor denotes the greatest integer not exceeding x.)
  • 0\leq X_i,Y_i,Z_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

\mathrm{case}_i represents the i-th test case. Each test case is given in the following format:

N K
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_N Y_N Z_N

Output

Print T lines. The i-th line (1\leq i\leq T) should contain the answer to the i-th test case.


Sample Input 1

1
3 1
6 3 8
3 5 0
2 7 3

Sample Output 1

12

We form one pair out of three cakes.

If we pair cake 1 with cake 2, the price is \max(6+3,\,3+5,\,8+0) = 9.
If we pair cake 1 with cake 3, the price is \max(6+2,\,3+7,\,8+3) = 11.
If we pair cake 2 with cake 3, the price is \max(3+2,\,5+7,\,0+3) = 12.

Hence, pairing cake 2 with cake 3 gives the highest price, which is 12.


Sample Input 2

2
5 2
1 2 3
1 2 3
1 2 3
1 2 3
100 100 200
6 2
21 74 25
44 71 80
46 28 96
1 74 24
81 83 16
55 31 1

Sample Output 2

209
333

Note that each cake can appear in at most one pair.
Also note that there can be different cakes with identical values of beauty, tastiness, and popularity.

For the first test case, pairing cake 1 with cake 2 gives a price of 6, pairing cake 3 with cake 5 gives a price of 203, and choosing these two pairs yields a total price of 209, which is the maximum.

For the second test case, pairing cake 2 with cake 3 gives a price of 176, pairing cake 4 with cake 5 gives a price of 157, and choosing these two pairs yields a total price of 333, which is the maximum.