Created
June 27, 2021 21:12
-
-
Save JonathanLalou/6b7b06b9b1cdb01b01cbcdefa289449b to your computer and use it in GitHub Desktop.
This file contains hidden or 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.*; | |
import java.math.*; | |
import java.security.*; | |
import java.text.*; | |
import java.util.*; | |
import java.util.concurrent.*; | |
import java.util.function.*; | |
import java.util.regex.*; | |
import java.util.stream.*; | |
import static java.util.stream.Collectors.joining; | |
import static java.util.stream.Collectors.toList; | |
class Result { | |
/* | |
* Complete the 'luckBalance' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts following parameters: | |
* 1. INTEGER k | |
* 2. 2D_INTEGER_ARRAY contests | |
*/ | |
public static int luckBalance(int k, List<List<Integer>> contests) { | |
final int nonImportantSum = contests.stream().filter(it -> it.get(1).equals(0)).map(it -> it.get(0)).mapToInt(Integer::intValue).sum(); | |
List<Integer> importantPairs = contests | |
.stream() | |
.filter(it -> it.get(1).equals(1)) | |
.map(it -> it.get(0)) | |
.collect(Collectors.toList()); | |
Collections.sort(importantPairs, Collections.reverseOrder()); | |
final int lostImportantSum = importantPairs.subList(0, Math.min(k, importantPairs.size())) | |
.stream() | |
.mapToInt(Integer::intValue) | |
.sum(); | |
final int wonImportantSum = importantPairs.subList(Math.min(k, importantPairs.size()), importantPairs.size()) | |
.stream() | |
.mapToInt(Integer::intValue) | |
.sum(); | |
return nonImportantSum + lostImportantSum - wonImportantSum; | |
} | |
} | |
public class Solution { | |
public static void main(String[] args) throws IOException { | |
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]); | |
List<List<Integer>> contests = new ArrayList<>(); | |
IntStream.range(0, n).forEach(i -> { | |
try { | |
contests.add( | |
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) | |
.map(Integer::parseInt) | |
.collect(toList()) | |
); | |
} catch (IOException ex) { | |
throw new RuntimeException(ex); | |
} | |
}); | |
int result = Result.luckBalance(k, contests); | |
bufferedWriter.write(String.valueOf(result)); | |
bufferedWriter.newLine(); | |
bufferedReader.close(); | |
bufferedWriter.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
20'