Add - Subtract
Practice
4.1 (390 votes)
Approved
Brute Force search
Easy
Greedy algorithms
Open
Sorting
Problem
38% Success 4954 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Chandan is back with his array to blow your mind. As usual Chandan has an array consisting of N integers .He allows you to perform 2 kinds of operation on his array.

  1. Type 1 : Increment any integer of the array by 1.

  2. Type 2 : Decrement any integer of the array by 1.

You can perform these operation as many times as you want on his array.

Each operation of Type 1 costs 3 while each operation of Type 2 costs 5.

Now Chandan wants to have K equal elements in his array.So he asks you to tell him the minimum cost required in obtaining K equal elements in his array.

Input:

The first line contains T indicating test cases.Second line contains 2 integers N indicating the number of elements in his array and K.

Third line contains N space separated integers denoting Chandan array.

Output:

The minimum cost required to get K equal elements.

Constraints :

1 ≤ T ≤ 100

1 ≤ KN ≤100

1 ≤ A[i] ≤100

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
238 votes
Tags:
ApprovedEasyGreedy AlgorithmsImplementationOpen
Points:20
63 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsC++Greedy Algorithms
Points:20
7 votes
Tags:
AlgorithmsGreedy AlgorithmsBasics of Greedy Algorithms