Hdu 2546 meal card (DP)

Source: Internet
Author: User

I had this question a long time ago and couldn't sleep at night. When I saw nyist playing a DP game, I came in and made a soy sauce.

The best touch of this question is to record the maximum value and change it to 0. Then it is a normal backpack.

 

#include<stdio.h>#include<string.h>#define N 1005int Max(int x,int y){    if(x>y)        return x;    else        return y;}int main(){    int n;    int a[N],mark[N];    while(scanf("%d",&n),n)    {        int max,flag;        int i,j;        flag=-1;        max=0;        int sum=0;        for(i=0;i<n;i++)        {            scanf("%d",&a[i]);            sum+=a[i];            if(a[i]>max)            {                max=a[i];                flag=i;            }        }        int v;        scanf("%d",&v);        if(v<5)        {            printf("%d\n",v);            continue;        }        if(v-sum>=5)        {            printf("%d\n",v-sum);            continue;        }        a[flag]=0;        v-=5;        memset(mark,0,sizeof(mark));        for(i=0;i<n;i++)        {            for(j=v;j>=a[i];j--)                mark[j]=Max(mark[j],mark[j-a[i]]+a[i]);        }        v=v+5-mark[v]-max;        printf("%d\n",v);    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.