class Solution {
fun bagOfTokensScore(tokens: IntArray, power: Int): Int {
tokens.sort()
var m_power = power
var start = 0
var end = tokens.size - 1
var score = 0
var answer = 0
while (start <= end) {
if (m_power >= tokens[start]) {
m_power -= tokens[start]
score++
start++
} else {
m_power += tokens[end]
score--
end--
}
if (score < 0) break
answer = maxOf(answer, score)
}
return answer
}
}