POJ 3264 Charm Bracelet

01背包,还好没忘记是怎么写的!

/*Accepted 240K 266MS C++ 563B 2012-04-22 09:13:42 */

#include<cstdio>

#include<cstdlib>

#include<cstring>

#define MAXN 3407

#define MAXM 12900

#define max( a, b) ( a > b ? a : b)

int w[MAXN], d[MAXN];

int f[MAXM];

int N, M;



void init()

{

    for( int i = 1; i <= N; i ++)

        scanf( "%d%d", &w[i], &d[i]);

    memset( f, 0, sizeof f);

}



void dp()

{

    for( int i = 1; i <= N; i ++)

        for( int j = M; j >= w[i]; j --)

        {

            f[j] = max( f[j], f[j - w[i]] + d[i]);

        }

}



int main()

{

    while( scanf( "%d%d", &N, &M) == 2)

    {

        init();

        dp();

        printf( "%d\n", f[M]);

    }

    return 0;

}

 

你可能感兴趣的:(char)