poj3624 Charm Bracelet

Description

Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a 'desirability' factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).

Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.

Input

赤裸裸的01背包问题,唯一的难处就是怎样用一维数组来进行存储,因为从题目中可看出二维的显然存不下。

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    int f[13000],j,i,w,d,n,m;
    memset(f,0,sizeof(f));
    scanf("%d%d",&n,&m);
    for(i=0;i<n;i++)
    {
      scanf("%d%d",&w,&d);
      for(j=m;j>=0;j--)
      {
        if(w<=j)f[j]=max(f[j],f[j-w]+d);               
      }              
    }
    printf("%d\n",f[m]);
    return 0;
}

你可能感兴趣的:(F#,J#)