One day, meow haha village, the pillars in their own room, face Magic mirror
"Mirror, Magic Mirror, what is the most powerful martial arts in the world?" ”
Instantly, a thunder fall, the column of the front of the Lord unexpectedly surfaced an ancient black books, and the name of the book is "Salted Fish magic"
!
Open a look, the first page of the book is impressively written: "Practice to large into a variable body of salted fish King, control the Thunder!"
“
See this sentence, the prince immediately decided to cultivate salted fish martial!
Since it is the martial, it is not so good practice, so the column ye decided to well plan his own process of cultivation!
The column has n
N
The mood value, and the salty fish magic has M
M
For each of these moves, the boss must consume TI
T
I
Point mood value in order to master, while the column will get VI
V
I
The cultivation value
So what is the maximum amount of cultivation that the Lord can get?
Input
First row two integers n
N
M
M
, indicating the number of mood points and the moves of the salted fish.
Next m
M
Rows, 2 per line
2
An integer, Ti,vi
T
I
V
I
, respectively, to learn the consumption of the moves and the cultivated values that can be obtained.
Data assurance:
1≤n,m≤5000
1
N
M
5000
1≤ti,vi≤10000
1
T
I
V
I
10000
Output
Output only one row, indicating the maximum value that the master can obtain
Sample Input and output
5 4
2 5
2 3
3 9
4 4
Sample Input Sample Output
14
Water problem, one of the most classic dynamic planning problems, and 01 backpack exactly the same.
#include <stdio.h>#define MAXSIZE 10005#define MAX (A, b) a > B? a:bintdp[5001][5001];//dp[i][j] The number of moves is I the maximum cultivation value when the mood points are JintMainintargcConst Char* argv[]) {intI, J;intN, M;intT[maxsize], v[maxsize];scanf("%d%d", &n, &m); for(inti =1; I <= M; i++) {scanf("%d%d", T + i, V + i); } for(i =0; I <= M; i++) {dp[i][0] =0; } for(i =0; I <= N; i++) {dp[0][i] =0; } for(i =1; I <= M; i++) { for(j =0; J <= N; J + +) {if(J < T[i]) {Dp[i][j] = dp[i-1][J]; }Else{Dp[i][j] = MAX (Dp[i-1][J], Dp[i-1][j-t[i]] + v[i]); } } }printf("%d\n", Dp[m][n]);return 0;}
As for this OJ, I want to vomit groove, what broken OJ, I wrote C code accidentally submitted to C + +, this past I can also accept, who know, I again with C submit, unexpectedly compile error ... The first time you meet this dilemma, but you can be sure that the code is right!
cdoj-2016-a-and the Martial fish