-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.java
63 lines (52 loc) · 1.87 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Problem: https://www.hackerrank.com/challenges/luck-balance
//Java 8
/*
Initial Thoughts:
We should automatically lose all unimportant contest
to increase our rating, and then we should lose the
k highest luck contests. The sum of all these contests
will give us the maximum luck we can achieve before
the competition
To do this, we would need to sort the important contest
by their luck descending and add the first k and subtract
the rest
Time Complexity: O(n log(n)) //We have to sort the important contest then do n iterations
Space Complexity: O(1) //Considering the inputs as given
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
///////Variables//////
int n = input.nextInt();
int k = input.nextInt();
int maxLuck = 0;
ArrayList<Integer> importantContests = new ArrayList<>();
//////////////////////
//Build list of important contests
for(int i = 0; i < n; i++){
int luck = input.nextInt();
int important = input.nextInt();
if(important != 1)
{
maxLuck += luck;
}
else
importantContests.add(luck);
}
//Sort the important contests in descending order
Collections.sort(importantContests, Collections.reverseOrder());
//Lose the k largest contests and win the rest
for(int i = 0; i < importantContests.size(); i++){
if(i < k)
maxLuck += importantContests.get(i);
else
maxLuck -= importantContests.get(i);
}
System.out.println(maxLuck);
}
}