forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuckBalance.java
More file actions
48 lines (45 loc) · 1.01 KB
/
LuckBalance.java
File metadata and controls
48 lines (45 loc) · 1.01 KB
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
package Greedy;
import java.util.*;
import java.io.*;
/*
* @author -- rajatgoyal715
*/
public class LuckBalance {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n];
int b[]=new int[n];
int t[]=new int[n];
int sum=0;
int total=0,n2=0;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
t[i]=sc.nextInt();
if(t[i]==1){
n2++;
b[i]=a[i];
}
else{
total++;
sum+=a[i];
b[i]=-1;
}
}
Arrays.sort(b);
if(n2>=k){
for(int i=total;i<n-k;i++){
sum-=b[i];
}
for(int i=n-k;i<n;i++){
sum+=b[i];
}
}
else{
for(int i=total;i<n;i++)
sum+=b[i];
}
System.out.println(sum);
}
}