Invest

No attempts yetTime limit2sMemory limit512 MB

Problem

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:

  • The investor commits to an investment plan over mm months.
  • The investor commits to a sum ss that is invested each month.
  • Each month the investor buys one product for the amount ss.
  • The investor has to buy the same product in the first 15 months of the plan, or in all months if the plan is shorter than 15 months.
  • Two consecutive changes of the product bought must be separated by at least 14 months in which the product stays the same. For example, if the investor bought gold in month 15 and switched to oil in month 16, he has to buy oil in months 17, 18, ..., 30. In month 31 he may change to another product.
  • At the end of the investment period, everything bought is sold at the price of that moment.

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:

  • a recommended number mm of months to invest;
  • a table whose rows give the prices of the five products in the corresponding month, with a final row holding the buy-back price at which Hyperinvest 2888 buys the products back.

Every price is a one-digit figure between $1 and $9, so $2520 always buys a whole number of units: buying product jj in month ii gives 2520/pi,j2520 / p_{i,j} units, and those units are sold at the buy-back price of product jj 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.

Input

The first line contains the number mm of months to invest, where 1<m<10001 < m < 1000.

Each of the following mm 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.

Output

Print one line with the returns of the best investment strategy: the total amount received when every unit bought during the mm months is sold at the buy-back price of its product, with $2520 invested each month.