Description John never knew he had a grand-uncle, until he received the notary ' s letter. He learned that He late Grand-uncle had gathered a lot of money, somewhere in South-america, and that John is the only I nheritor.
John did not need this much money for the moment. But he realized that it would is a good idea to store this capital in a safe place, and has it grow until he decided to R Etire. The bank convinced him that a certain kind of bond is interesting for him.
This kind of bond have a fixed value , and gives a fixed amount of yearly interest, payed to the owner at the end of each year. The bond has a fixed term. Bonds is available in different sizes. The larger ones usually give a better interest. Soon John realized that the optimal set of bonds to buy is not trivial to figure out. Moreover, after a few years he capital would has grown, and the schedule had to be re-evaluated.
assume the F Ollowing bonds is available:
Value |
Annual Interest |
4000 3000 |
400 250 |
With a capital of E10 one could buy one bonds of $4, giving a yearly interest of $800. Buying bonds of $ $, and one of $4 is a better idea, as it gives a yearly interest of $900. After both years the capital have grown to $11, and it makes sense to sell a $ one and buy a $4 one, so the Ann UAL interest grows to $050. This is where the this story grows Unlikely:the bank does not charge for buying and selling bonds. Next year the total sum is 850, which allows for three times $4, giving a yearly interest of $200.
Here are your problem:given an amount to begin with, a number of years, and a set of bonds with their values and interests , find out how big the amount is grow in the given period, using the best schedule for buying and selling bonds.
Input The first line contains a single positive an integer N which is the number of the test cases. The test cases follow.
The first line of a test case contains-positive integers:the amount to start with (at most), and the Numbe R of years the capital is grow (at most 40).
The following line contains a single number:the number D (1 <= D <=) of available bonds.
The next D lines each contain the description of a bond. The description of a bond consists of the positive integers:the value of the bond, and the yearly interest for that bond. The value of a bond is always a multiple of $000. The interest of a bond is never more than 10% of its value.
Output for each test case, output–on a separate line–the in the end of the period, after an optimal schedule O F Buying and selling.
Sample Input
1
10000 4
2
4000
3000 250
Sample Output
14050
The main idea: give the initial capital and total time (years), then give the principal and net profit of n things. To find out the ultimate maximum principal and interest. Idea: Using the current year's principal to the object with dynamic planning, is equal to a few complete backpack combination. But because the money is very large, the cycle time, we need to compress the backpack. Since the title says that the principal is a multiple of 1000, you can divide the principal by 1000 as the "volume" of the backpack.
#include <iostream> #include <string> #include <cstring> #include <cstdio> #define MAX (x, y) ((x) > (y)?
(x): (y)) using namespace Std;
int dp[500000];
int main () {int n, m, weight[3405], value[3405], T, Day, TEM;
CIN >> T;
while (T-) {int sum;
CIN >> m >> Day >> N;
sum = m;
M/= 1000;
for (int i = 1; I <= n; i + +) {cin >> weight[i] >> value[i];
Weight[i]/= 1000;
} for (int k = 1;k <= day;k + +) {tem = sum/1000;
memset (DP, 0, sizeof (DP));
for (int i = 1; I <= n; i + +) {for (int j = 1; J <= tem; j + +) {
if (J >= Weight[i]) dp[j] = max (Dp[j], Dp[j-weight[i]] + value[i]);
}} sum + = Dp[tem]; } cout << Sum << Endl;
}
}