class Solution {
fun maximumUnits(boxTypes: Array<IntArray>, truckSize: Int): Int {
// sort by unit in descending order
boxTypes.sortBy { -it[1] }
var mTruckSize = 0
var answer = 0
for (boxType in boxTypes) {
mTruckSize += boxType[0]
if (mTruckSize > truckSize) {
// accumulate left units of boxes
mTruckSize -= boxType[0]
answer += (truckSize - mTruckSize) * boxType[1]
break
}
// accumulate units of boxes
answer += (boxType[0] * boxType[1])
}
return answer
}
}