#P1853. 投资的最大效益

投资的最大效益

Description

For example, consider the following two types of bonds:

  1. Investment amount 40004000, annual interest 400400.
  2. Investment amount 30003000, annual interest 250250.

Initially, with a total of 1000010000 in assets, he can invest in two units of Bond 1 and earn 800800 in interest in one year. Alternatively, investing in one unit of Bond 1 and two units of Bond 2 yields 900900 in interest in one year; after two years, the total interest is 18001800, bringing total assets to 1180011800. Then, by selling one unit of Bond 2 and switching to Bond 1, the annual interest can reach 10501050. After the third year, total assets reach 1285012850, at which point he can purchase three units of Bond 1, yielding an annual interest of 12001200. After the fourth year, total assets reach 1405014050.

Given several types of bonds and the initial total assets, help Mr. John compute the maximum possible total assets after nn years of investment.

Input Format

The first line contains three positive integers s,n,ds, n, d, representing the initial total assets, the number of years, and the number of bond types, respectively.

The next dd lines each describe one type of bond. Each line contains two positive integers a,ba, b, representing the investment amount and the annual interest of the bond, respectively.

Output Format

Output a single integer, the maximum total assets after nn years.

10000 4 2
4000 400
3000 250
14050

Hint

For 100%100\% of the testdata, 1s1061 \le s \le {10}^6, 2n402 \le n \le 40, 1d101 \le d \le 10, 1a1041 \le a \le {10}^4, aa is a multiple of 10001000, and bb does not exceed 10%10\% of aa.

Translated by ChatGPT 5