You have an initial power P, an initial score of 0, and a bag of tokens where tokens[i] is the value of the i-th token.
Your goal is to maximize your total score by potentially playing each token in one of two ways:
- If your current power is at least
tokens[i], you may play thei-thtoken face up, losingtokens[i]power and gaining 1 score. - If your current score is at least 1, you may play the
i-thtoken face down, gainingtokens[i]power and losing 1 score. Each token can be played at most once and in any order. You do not have to play all the tokens. Return the largest possible score you can achieve after playing any number of tokens.
- An array of integers
tokens. - An integer
power.
Input: tokens = [100, 200, 300, 400], power = 200
Output: 2