2600. K Items With the Maximum Sum

There is a bag that consists of items, each item has a number 10, or -1 written on it.

You are given four non-negative integers numOnesnumZerosnumNegOnes, and k.

The bag initially contains:

We want to pick exactly k items among the available items. Return the maximum possible sum of numbers written on the items.

Example 1:

Input: numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
Output: 2
Explanation: We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2.
It can be proven that 2 is the maximum possible sum.

Example 2:

Input: numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
Output: 3
Explanation: We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3.
It can be proven that 3 is the maximum possible sum.

def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:
	if (numOnes + numZeros + numNegOnes) < k:
		return False
	count = 0
	while k > 0:
		if numOnes != 0 and k != 0:
			k -= 1
			count += 1
			numOnes -= 1
		elif numZeros != 0 and k != 0:
			k -= 1
			numZeros -= 1
		elif numNegOnes != 0 and k != 0:
			k -= 1
			count -= 1
			numNegOnes
	return count