-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.java
45 lines (37 loc) · 1.28 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
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);
}
}