-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChocolates.java
91 lines (81 loc) · 2.92 KB
/
Chocolates.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* IMPORTANT: Multiple classes and nested static classes are supported */
/*
* uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
//import for Scanner and other utility classes
import java.util.*;
*/
import java.util.*;
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
import java.util.Vector;
import java.util.stream.*;
class TestClass {
public static void main(String args[] ) throws Exception {
/* Sample code to perform I/O:
* Use either of these methods for input
//BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine(); // Reading input from STDIN
System.out.println("Hi, " + name + "."); // Writing output to STDOUT
//Scanner
Scanner s = new Scanner(System.in);
String name = s.nextLine(); // Reading input from STDIN
System.out.println("Hi, " + name + "."); // Writing output to STDOUT
*/
// Write your code here
Scanner s = new Scanner(System.in);
int T = s.nextInt();
for(int i = 0; i <T; i++){
int Q = s.nextInt();
int K = s.nextInt();
//Vector<Vector<Integer>> Qs = new Vector<Vector<Integer>>();
int total_kids = 0;
Vector<LinkedList<Integer>>Qs = new Vector<LinkedList<Integer>>();
PriorityQueue<Integer> pQueue =new PriorityQueue<Integer>();
for(int q =0; q<Q; q++){
LinkedList<Integer> chocolate_demand = new LinkedList<Integer>();
int no_kids = s.nextInt();
total_kids+=no_kids;
for(int kids = 0; kids<no_kids; kids++)
chocolate_demand.add(s.nextInt());
Qs.addElement(chocolate_demand);
}
//K pops from all Qs
int chocolate = 0;
if(K>=total_kids){
for(int q =0; q<Q; q++){
chocolate+=Qs.get(q).parallelStream().reduce(0, Integer::sum);
}
}
else {
for ( int iteration = 0; iteration < K; iteration++){
int min = Integer.MAX_VALUE, index = -1;
for(int q = 0; q <Q; q++){
if(Qs.get(q).size()>0) {
if(Qs.get(q).getFirst() <min){
index = q;
min = Qs.get(q).getFirst();
}
}
}
if(index !=-1)
chocolate+=Qs.get(index).removeFirst();
}
}
System.out.println(chocolate);
}
}
}
/*
1
3 5
3
1 2 4
2
3 6
1
5
->15
*/