Created
June 27, 2021 21:48
-
-
Save JonathanLalou/5ec4bcc749109342edcdeaf370d50926 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
15' (10' to understand the problem, <5' coding :-P )