#P8103. Merge to Minimize Cow Stalls
Merge to Minimize Cow Stalls
Merge to Minimize Cow Stalls
Farmer John's farm originally had n cow stalls. The i-th stall contains ai cows. You are allowed to perform a series of merge operations. In each merge operation, you choose two distinct stalls i and j such that ai \ge aj. Then, you remove exactly aj cows from stall i and add them into stall j (i.e. update: ai = ai - aj and aj = 2aj). If after the merge ai = 0, stall i is considered merged and will not participate in further operations.
Notice that if you merge two stalls with equal numbers of cows, one stall will vanish (since ai - aj = 0 when ai = aj). Such merges decrease the total number of stalls currently holding cows. Merging stalls with unequal counts, while allowed, does not reduce the number of stalls.
Your task is to determine the minimum possible number of non‐merged stalls (i.e. stalls with cows) that can remain after performing an optimal sequence of merge operations. You may perform the operations in any order, and you have a time limit of 1 second.
inputFormat
The first line contains a single integer n (1 ≤ n ≤ 105), the number of cow stalls.
The second line contains n space‐separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai denotes the number of cows in the i-th stall.
outputFormat
Output a single integer – the minimum number of stalls that can remain (i.e. stalls with a positive number of cows) after performing an optimal sequence of merge operations.
sample
2
5 5
1