PriorityQueue | Sort | Kotlin
Kotlin
# Kotlin
```kotlin [PriorityQueue]
class Solution {
    fun miceAndCheese(reward1: IntArray, reward2: IntArray, k: Int): Int {
        val pq = PriorityQueue<Int>( 
            compareByDescending<Int> { reward1[it] - reward2[it] }.thenBy { reward2[it] }
        )
        var res = 0
        val s = mutableSetOf<Int>()

        for(idx in reward1.indices){
            pq.add(idx)
        }

        repeat(k){
            val idx = pq.poll()!!
            s.add(idx)
            res += reward1[idx]
        }

        for(idx in reward2.indices){
            if(!s.contains(idx)) res += reward2[idx]
        }

        return res
    }
}
```

```kotlin [Sort]
class Solution {
    fun miceAndCheese(reward1: IntArray, reward2: IntArray, k: Int): Int {
        val indices = reward1.indices.sortedByDescending { reward1[it] - reward2[it] }
        
        return indices.take(k).sumOf { reward1[it] } + 
               indices.drop(k).sumOf { reward2[it] }
    }
}
```
['Sorting', 'Heap (Priority Queue)', 'Kotlin']