The financial company Hyperinvest 2888 lets its clients invest in five products: oil, shares, steel, silver and gold. Every product is traded on a monthly basis, and the investor has to buy one of them each month. The company imposes these rules:
So one possible strategy for a 36-month plan is gold for the first 18 months, silver for the next 15 months and gold again for the remaining three months.
Dagobert Dollarsen decides to check whether this investment pays off for him. He goes to a fortune teller working under the name Futurevision 3000, and he trusts her because she correctly predicted the birth dates and the genders of his children whenever his wife was pregnant. She cannot tell him the exact outcome, but she recommends investing $2520 each month and hands him two things:
Every price is a one-digit figure between $1 and $9, so $2520 always buys a whole number of units: buying product j in month i gives 2520/pi,j units, and those units are sold at the buy-back price of product j when the plan ends.
Write Dollarsen's program. Assuming the predicted prices really come up, it has to determine the returns of the best strategy that respects the rules of Hyperinvest 2888.
The first line contains the number m of months to invest, where 1<m<1000.
Each of the following m lines contains the price of each product at the beginning of the respective month, separated by spaces. The last line contains the price of each product at the end of the investment period, separated by spaces. Every price is an integer between 1 and 9, and the products always appear in the order oil, shares, steel, silver, gold.
Print one line with the returns of the best investment strategy: the total amount received when every unit bought during the m months is sold at the buy-back price of its product, with $2520 invested each month.