# Program to find maximum score from removing stones in Python

PythonServer Side ProgrammingProgramming

Suppose we have three values a, b and c. We are playing a solitaire game with three piles of stones whose sizes are a, b, and c respectively. Each turn the player selects two different non-empty piles, take one stone from each, and add 1 point to his score. The game ends when there are fewer than two non-empty piles. So we have to find the maximum score you can get.

So, if the input is like a = 4, b = 4, c = 6, then the output will be 7 because the initial state is (4, 4, 6), then we can follow these steps −

• Select from 1st and 2nd piles so current state is (3, 3, 6)

• Select from 1st and 3rd piles so current state is (2, 3, 5)

• Select from 1st and 3rd piles so current state is (1, 3, 4)

• Select from 1st and 3rd piles so current state is (0, 3, 3)

• Select from 2nd and 3rd piles so current state is (0, 2, 2)

• Select from 2nd and 3rd piles so current state is (0, 1, 1)

• Select from 2nd and 3rd piles so current state is (0, 0, 0)

And finally there are fewer than two non-empty piles, so the game ends.

To solve this, we will follow these steps −

• minimum := minimum of a, b and c

• maximum := maximum of a, b and c

• left := a + b + c - maximum - minimum

• if maximum-left <= minimum, then

• return minimum + left - quotient of (1 + minimum - (maximum-left))/2

• return minimum + (minimum of (maximum-minimum) and left)

## Example

Let us see the following implementation to get better understanding −

def solve(a, b, c):
minimum = min(a,b,c)
maximum = max(a,b,c)
left = a+b+c-maximum-minimum
if maximum-left<=minimum:
return minimum + left-(1+minimum-(maximum-left))//2
return minimum + min(maximum-minimum,left)

a = 4
b = 4
c = 6
print(solve(a, b, c))

## Input

4, 4, 6


## Output

7
Published on 06-Oct-2021 08:22:38