Ham and the man of the year
Time limit1sMemory limit128 MB
Find the smallest ham total up to 10^7 whose ratio shares added to eaten amounts rank people 1 to N from most to least.
Problem
Melita has just come back from the yearly pig feast. In Croatia this is a normal event. The best part was how much food there was. Spicy sausages, ham, black pudding, teewurst, top quality bacon and čvarci, all with warm white bread and butter. After those appetizers came a deep pot of sarma (Melita ate about twenty of them) and a large platter of roast pork so soft it almost melted in the mouth. They washed it all down with the best dry white wine, which only made them hungrier.
The butcher Bajs saved his award winning ham for the very end. people came to the feast, numbered from to . Person has eaten kilograms of meat so far. Bajs will hand out his ham in exactly the ratio , but he has not decided how many kilograms he will hand out.
When the feast ends, the man of the year is announced. The ranking follows the total weight of meat each person ate, and Bajs changes that ranking just by choosing how much ham to hand out. People have offered him bribes many times and he refused every time, saying he is an honest man who would not hurt a fly.
Bajs cares about order. Lining people up from the one who ate the most kilograms down to the one who ate the least, he wants the numbers to come out exactly as . Equal weights are fine. Pick a total amount of ham that gets him what he wants.
Input
The first line contains the integer , the number of candidates for the man of the year ().
Each of the next lines contains two integers and (). At least one is not 0.
Output
If no total amount of ham from 0 to kilograms, both ends included, produces the wanted order, print -1 on a single line.
Otherwise print the smallest such total, in kilograms, as an irreducible fraction on a single line. Here is at least 1, and an integer answer is written with equal to 1. For a smallest total of , print 21/2.
Hint
Take the first example. Handing out kilograms in the ratio gives , and kilograms. Adding what each person already ate gives totals of , and kilograms, which is a valid order. Any smaller amount breaks the order, so the smallest total is and the answer is 21/2.