P1853 Get the most out of your investment

topic background

Mr. John received a large inheritance, which he did not need for the time being, and decided to invest for greater benefits. The bank staff offered him a variety of bonds, each offering a stable annual interest after a fixed investment. Of course, the investment amount of each bond is different. Generally speaking, the larger the investment, the greater the income. Moreover, every year, according to the increase of the total amount of funds, you can replace the bonds with higher income.

Topic description

For example: There are two different bonds as follows: ① The investment amount is $4000 and the annual interest is $400; ② the investment amount is $3000 and the annual interest is $250. Initially, with total assets of $10,000, you can invest in two bonds ① bonds, and get $800 in interest for one year; and invest in one bond ① and two bonds ②, you can get $900 in interest for one year, and after two years, you can get $900 in interest. Get $1800 in interest; and all assets reach $11800, then you will sell a bond ②, exchange for bonds ①, the annual interest can reach $1050; after the third year, the total assets reach $12850, you can buy three bonds①, Annual interest can reach $1200, and after the fourth year, total assets can reach $14050.

Now given several kinds of bonds and initial total assets, help Mr. John to calculate the maximum value of total assets after n years of investment.

Input and output format

Input format:

 

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

Next d lines, each line represents a bond, two positive integers a, b represent the bond investment amount and annual interest, respectively.

 

Output format:

 

Just an integer representing the maximum total assets after n years.

 

Input and output example

Input Example #1:  Copy
10000 4 2
4000 400
3000 250
Output Sample #1:  Copy
14050

illustrate

s≤10^6, n≤40, d≤10, a≤10^4, and a is a multiple of 1000, and b does not exceed 10% of a.

 

 

// . . Investing is not about subtracting money, it's like lending money to someone else, but the money is still 
 yours.//And the interest on the investment is given in the year it was invested

// So this is a complete knapsack problem
 // Each bond can be chosen an infinite number of times, as long as there is enough money
 // dp[s] represents the maximum benefit we can get by spending s money
 // So we arrive at The money at the end of the year is s+dp[s]
 // In other words, our assets are constantly changing, and the upper bound of the dp cycle is also changing.//But
 from the data range of the title, it can be seen that the final money will definitely not exceed
 1e7 // So just open an array of 1e7 directly

// Of course the title says that a is a multiple of 1000, you can compress the data 1000 times to do it
 // But the data is water 

#include <iostream> 
#include <cstdio> 
#include <algorithm>
 using  namespace std;

const int N=1e7+5;
const int M=50;

int s,n,d;
int a[M],b[M];
int dp[N];

inline int read()
{
    char c=getchar();int num=0;
    for(;!isdigit(c);c=getchar());
    for(;isdigit(c);c=getchar())
        num = num * 10 + c - ' 0 ' ;
    return num;
}

intmain ()
{
    s=read(),n=read(),d=read();
    for(int i=1;i<=d;++i)
        a[i]=read(),b[i]=read();
    for(int A=1;A<=n;++A)
    {
        for(int i=1;i<=d;++i)
        {
            for(int j=a[i];j<=s;++j)
                dp[j]=max(dp[j],dp[j-a[i]]+b[i]);
        }
        s+=dp[s];
    }
    printf("%d",s);
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325050467&siteId=291194637