2005NOIP 采药简单DP

#include < stdio.h >
#include
< string .h >

int  t, n;
int  time[ 101 ], v[ 101 ];
int  f[ 101 ][ 1001 ];


void  DP()
{
    
int i, j;
    
for(i=n-1;i>=0;i--)
        
for(j=0;j<=t;j++)
        
{
            
if(j>=time[i])f[i][j] = f[i+1][j] > f[i+1][j-time[i]]+v[i] ? f[i+1][j] : f[i+1][j-time[i]]+v[i];
            
else
                f[i][j]
=f[i+1][j];
        }

}


int  main()
{
    
while(scanf("%d%d",&t,&n)==2)
    
{
        memset(f,
0,sizeof(f));
        
int i;
        
for(i=0;i<n;i++)
            scanf(
"%d%d",&time[i],&v[i]);

        DP();
        printf(
"%d\n",f[0][t]);
    }


    
return 0;
}

你可能感兴趣的:(IP)