Problem Description
John never knew he had a grand-uncle, until he got the notary's letter. he learned that his late grand-uncle had gathered a lot of money, somewhere in South-America, and that John was the only inheritor.
John did not need that much money for the moment. but he realized that it wocould be a good idea to store this capital in a safe place, and have it grow until he decided to retire. the bank convinced him that a certain kind of bond was interesting for him.
This kind of bond has a fixed value, and gives a fixed amount of yearly interest, payed to the owner at the end of each year. the bond has no fixed term. bonds are available in different sizes. the larger ones usually give a better interest. soon John realized that the optimal set of bonds to buy was not trivial to figure out. moreover, after a few years his capital wowould have grown, and the schedule had to be re-evaluated.
Assume the following bonds are available:
Value Annual interest
4000 400
3000 250
With a capital of $10 000 one cocould buy two bonds of $4 000, giving a yearly interest of $800. buying two bonds of $3 000, and one of $4 000 is a better idea, as it gives a yearly interest of $900. after two years the capital has grown to $11 800, and it makes sense to launch a $3 000 one and buy a $4 000 one, so the annual interest grows to $1 050. this is where this story grows unlikely: the bank does not charge for buying and selling bonds. next year the total sum is $12 850, which allows for three times $4 000, giving a yearly interest of $1 200.
Here is 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 may grow in the given period, using the best schedule for buying and selling bonds.
Input
The first line contains a single positive integer N which is the number of test cases. The test cases follow.
The first line of a test case contains two positive integers: the amount to start with (at most $1 000 000 ), and the number of years the capital may grow (at most 40 ).
The following line contains a single number: the number d (1 <= d <= 10) of available bonds.
The next d lines each contain the description of a bond. the description of a bond consists of two positive integers: the value of the bond, and the yearly interest for that bond. the value of a bond is always a multiple of $1 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 capital at the end of the period, after an optimal schedule of buying and selling.
Sample Input
1
10000 4
2
4000 400
3000 250
Sample Output
14050
Question: give an initial fund with a number of years, then give the purchase price of each item and the annual benefit, and ask for the sum of the maximum profits that can be obtained after the given year.
Idea: because each type of item can be purchased multiple times, it can be regarded as a question of a full backpack, but note that because the principal may be large, therefore, we need to compress the size of the backpack. It is worth noting that the subject already says that the principal and the purchase price of the item are multiples of 1000, so we can divide all of them by 1000 for compression, and then it is a complete backpack template question.
# Include <stdio. h> # include <string. h ># include <algorithm> using namespace std; struct node {int v, w;} a [20]; int dp [100000]; int main () {int t, n, I, j, k, val, y; scanf ("% d", & t); while (t --) {scanf ("% d ", & val, & y); scanf ("% d", & n); for (I = 1; I <= n; I ++) {scanf ("% d", & a [I]. v, & a [I]. w); a [I]. v/= 1000; // perform compression} for (I = 1; I <= y; I ++) {int s = val/1000; // The annual principal is the sum of the previous year's principal and interest memset (dp, 0, sizeof (dp); // The annual interest must be retained for (j = 1; j <= n; j ++) // complete backpack {for (k = a [j]. v; k <= s; k ++) {dp [k] = max (dp [k], dp [k-a [j]. v] + a [j]. w) ;}}val + = dp [s]; // The maximum sum of profits and} printf ("% d \ n", val) each year;} return 0 ;}