#P8244. Bajs' Ham Distribution
Bajs' Ham Distribution
Bajs' Ham Distribution
At the annual pig slaughter event, butcher Bajs is about to distribute a portion of his award-winning ham among (n) participants. Initially, the (i)-th person has already eaten (a_i) kilograms of ham. Bajs plans to distribute his ham according to the ratio (b_1:b_2:\cdots:b_n); that is, if (S=\sum_{j=1}^{n}b_j), then the (i)-th person will receive (\frac{b_i}{S}) of the total ham weight (X) that Bajs takes out. After distribution, everyone consumes the extra ham and a ranking is generated based on the total amount of ham consumed (in kilograms), in descending order. In case of ties, the participant with the smaller index comes first. Bajs, who prides himself on his honesty and has obsessive–compulsive tendencies, desires the final ranking order to be exactly (1,2,\ldots,n).
Find the minimum non‐negative total ham weight (X) that Bajs must take out to achieve the ranking order (1,2,\ldots,n). If it is impossible to do so, output (-1). Note that if some participants end up with equal total amounts, the tie is broken by their indices (in ascending order), which still may satisfy the required ranking order.
inputFormat
The first line contains an integer (n) (with (2\le n\le 10^5)).\nThe second line contains (n) space-separated integers (a_1, a_2, \ldots, a_n), where (a_i) is the kilograms of ham already consumed by the (i)-th person ((0\le a_i\le 10^9)).\nThe third line contains (n) space-separated integers (b_1, b_2, \ldots, b_n) representing the distribution ratio ((1\le b_i\le 10^9)).
outputFormat
If it is possible to achieve the desired ranking order (1,2,\ldots,n), print the minimum total ham weight (X) (possibly a real number, printed with at most 9 decimal places) that Bajs must take out. Otherwise, print (-1).
sample
3
3 2 1
1 2 3
0