#P11205. Maximizing the MEX of Petal Piles
Maximizing the MEX of Petal Piles
Maximizing the MEX of Petal Piles
You are given n piles of petals. The i-th pile contains ai petals. You are allowed to perform at most one operation as follows:
- You may choose a subset of piles (possibly empty). For each chosen pile, remove any positive number of petals, but you are not allowed to remove all the petals from that pile (i.e. at least one petal must remain in every chosen pile).
- After that, take all the removed petals and combine them to form a new pile.
You do not have to perform the operation.
After the operation (if any), you end up with several piles. Let the MEX be defined as the smallest positive integer that does not equal the number of petals in any pile. Your goal is to maximize this MEX value.
Note: Petals removed from a pile must be a positive integer less than the original number of petals in that pile.
You need to choose whether to perform the operation and if so, which piles to choose and how many petals to remove from each chosen pile, so that the final multiset of pile sizes yields the largest possible MEX.
Observation: If you do not perform any operation, the set of piles remains {a₁, a₂, ..., aₙ} and the MEX is the smallest positive integer not in that set. On the other hand, if you perform an operation, you have some flexibility. In any chosen pile with initial count ai (with ai > 1), you can reduce it to any value in the range [1, ai − 1]. Also, the new pile will have a value equal to the sum of the removed petals, and it can be any integer in the interval [number of chosen piles, Σ(ai − 1) over chosen piles].
A strategy to maximize the MEX is as follows:
- Let mex_no_op be the MEX of the original set {a₁, a₂, ..., aₙ}.
- For piles with more than 1 petal (ai > 1), if we choose them for the operation, we can assign any value from 1 to ai − 1 to each. So by choosing all such piles, we can guarantee that the union of the new values (if we set them appropriately) and the new pile (which can take a value from the range [number of chosen piles, Σ(ai − 1)]) will cover all positive integers up to at least Σ(ai − 1) + 1. Let op_candidate = (Σ(ai − 1) for all i with ai > 1) + 1.
- The answer is the maximum possible MEX, which is max(mex_no_op, op_candidate).
Print the maximum MEX that can be achieved.
inputFormat
The first line contains a single integer n — the number of piles.
The second line contains n space-separated integers a1, a2, ..., an — the number of petals in each pile.
outputFormat
Output a single integer — the maximum possible MEX you can achieve after performing at most one operation.
sample
3
1 3 3
5