-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathLuckBalance.java
More file actions
29 lines (27 loc) · 757 Bytes
/
LuckBalance.java
File metadata and controls
29 lines (27 loc) · 757 Bytes
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
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
List<Integer> ar = new ArrayList<Integer>();
int ans = 0;
for(int i=0; i<n; i++) {
int val = in.nextInt();
int imp = in.nextInt();
if(imp == 0)
ans += val;
else
ar.add(val);
}
Collections.sort(ar, Collections.reverseOrder());
for(int i=0; i<ar.size(); i++) {
if(i < k)
ans += ar.get(i);
else
ans -= ar.get(i);
}
System.out.print(ans);
}
}