Skip to content

Instantly share code, notes, and snippets.

@JonathanLalou
Created June 27, 2021 21:12
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/6b7b06b9b1cdb01b01cbcdefa289449b to your computer and use it in GitHub Desktop.
Save JonathanLalou/6b7b06b9b1cdb01b01cbcdefa289449b to your computer and use it in GitHub Desktop.
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();
}
}
@JonathanLalou
Copy link
Author

20'

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