01背包

 

 

 

 

 

 例1:

 

 

 

 

 

 

 

 

 

import java.util.Scanner;
class jianbaozang{
    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int c=sc.nextInt();
        int []a=new int[1001];
        int []b=new int[1001];
        for (int i=1;i<=n ;i++ )
        {
            a[i]=sc.nextInt();
            b[i]=sc.nextInt();
        }
        int [][]dp=new int[1001][1001];
        dp[0][0]=0;
        for (int i=1;i<=n ;i++ )
        {
            for (int j=1;j<=c;j++ )
            {
                if (j<a[i])
                {
                    dp[i][j]=dp[i-1][j];
                }else{
                    dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-a[i]]+b[i]);
                }
            }
        }
        System.out.println(dp[n][c]);
    }
}

 

posted @ 2020-04-28 10:31  柠檬不酸i  阅读(116)  评论(0编辑  收藏  举报