[1048] Luo Gu herbs

Title Description

Chen Chen is a gifted child, his dream is to become the world's greatest physician. To this end, he wanted to worship in the vicinity of the most prestigious physician as a teacher. Physician in order to determine his qualifications, gave him a problem. Physicians took him to a cave full of herbs she says to him:. "Children, this cave has some different herbs, picking each plant requires some time, each plant has its own value and I will give you some time, during this time, you can pick some herbs maximum if you are a smart kid, you should be taken to make the total value of herbs. "

If you are chen, you can accomplish this task?

Input Format

First line 2 2 integer T (. 1 \ Le T \ Le 1000) T ( . 1 T . 1 0 0 0 ) and M (. 1 \ Le M \ Le 100) M ( . 1 M . 1 0 0 ) , separated by a space, T T represents a total time of herbs, can be used M number M represents the cave of herbs.
The next M M comprises two lines each . 1 . 1 to 100 . 1 0 0 (including . 1 1 and 100 . 1 0 integer of 0) respectively represent the value of a strain picked herbs time this strain and herbs.

Output Format

1 an integer representing the maximum total value within a specified period of time can be taken to the herbs.

Sample input and output

Input # 1
70 3
71 100
69 1
1 2
Output # 1
3

Description / Tips

For 30% of the data, M \ Le 10 M . 1 0;

For all the data, M \ Le 100 M . 1 0 0.

NOIP2005 popularity of Group III title

 

Solution: only to find I actually did not do this question. (Question mark black face?)

        Tonight's punch topic (Review Review title backpack hehe ......) Do not forget to enumerate backwards on it.

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1002;
int m,n,f[N];
int a[N],b[N];
int main(){
    freopen("1048.in","r",stdin);
    freopen("1048.out","w",stdout);
    scanf("%d %d",&m,&n);
    for(int i=1;i<=n;i++)
        scanf("%d %d",&a[i],&b[i]);
    for(int i=1;i<=n;i++)
        for(int j=m;j>=a[i];j--)
            f[j]=max(f[j],f[j-a[i]]+b[i]);
    printf("%d",f[m]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11715305.html