File tree Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io ._
2
+ import java .math ._
3
+ import java .security ._
4
+ import java .text ._
5
+ import java .util ._
6
+ import java .util .concurrent ._
7
+ import java .util .function ._
8
+ import java .util .regex ._
9
+ import java .util .stream ._
10
+
11
+ object Solution {
12
+
13
+ object MinOrder extends Ordering [Int ] {
14
+ def compare (x: Int , y: Int ) = y compare x
15
+ }
16
+
17
+ def luckBalance (k : Int , contests : Array [Array [Int ]]): Int = {
18
+
19
+ // Store important contests in a min-heap to ensure o(1) access
20
+ val minHeap = scala.collection.mutable.PriorityQueue .empty(MinOrder )
21
+
22
+ var sol = 0
23
+
24
+ // O(n) time
25
+ contests.foreach { el =>
26
+ val luck = el(0 )
27
+ val imp = el(1 )
28
+
29
+ if (imp == 1 ) {
30
+ minHeap += luck
31
+ }
32
+
33
+ sol += luck
34
+ }
35
+
36
+ // Now just dicrease each min twice (We added mins before)
37
+ for (_ <- 1 to minHeap.size - k) {
38
+ sol -= 2 * minHeap.dequeue()
39
+ }
40
+
41
+ sol
42
+
43
+ }
44
+
45
+ def main (args : Array [String ]) {
46
+ val stdin = scala.io.StdIn
47
+
48
+ val printWriter = new PrintWriter (new OutputStreamWriter (System .out))
49
+
50
+ val nk = stdin.readLine.split(" " )
51
+
52
+ val n = nk(0 ).trim.toInt
53
+
54
+ val k = nk(1 ).trim.toInt
55
+
56
+ val contests = Array .ofDim[Int ](n, 2 )
57
+
58
+ for (i <- 0 until n) {
59
+ contests(i) = stdin.readLine.split(" " ).map(_.trim.toInt)
60
+ }
61
+
62
+ val result = luckBalance(k, contests)
63
+
64
+ printWriter.println(result)
65
+
66
+ printWriter.close()
67
+ }
68
+ }
You can’t perform that action at this time.
0 commit comments