Skip to content

Instantly share code, notes, and snippets.

@JonathanLalou
Created June 27, 2021 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanLalou/5ec4bcc749109342edcdeaf370d50926 to your computer and use it in GitHub Desktop.
Save JonathanLalou/5ec4bcc749109342edcdeaf370d50926 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import static java.util.stream.Collectors.toList;
public class Solution {
public static void main(String[] args) throws IOException {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int n = Integer.parseInt(firstMultipleInput[0]);
int k = Integer.parseInt(firstMultipleInput[1]);
String[] szPrices = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
final List<Integer> prices = Arrays.stream(szPrices).map(it -> Integer.parseInt(it)).collect(toList());
prices.sort(Comparator.reverseOrder());
int minimalCost = 0;
int round = 0;
int i = 0;
while (i < n) {
// for (int i = 0; i<n; i++){
for (int j = 0; j < k; j++) {
minimalCost += (round + 1) * prices.get(i);
i++;
if (i >= n) {
System.out.println(minimalCost);
return;
}
}
round++;
}
System.out.println(minimalCost);
}
}
@JonathanLalou
Copy link
Author

15' (10' to understand the problem, <5' coding :-P )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment