01 Backpack-poj3628__ Dynamic Planning

Source: Internet
Author: User

Typical 01 backpack.

Q: Tell you the height of all cows and the height of the shed, ask the minimum difference how much.

Just start to read the wrong title, WA a lot of times, I think is the height of cattle can not exceed B, can be the original title is said to exceed the minimum height, sad reminders.

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
Long long high[25];
Long long f[1000050];
int main ()
{
    int n;
    Long long H;
    Long long sum;
    while (cin>>n>>h)
    {

        sum=0;
        for (int i=1; i<=n; i++)
        {
            cin>>high[i];
            Sum+=high[i];
        }
        memset (F,0,sizeof (f));
        for (int i=1; i<=n; i++) for
            (Long long j=sum; j>=high[i]; j--)

                F[j]=max (f[j],f[j-high[i]]+high[i));
        for (int i=1; i<=sum; ++i)
            if (f[i]>=h)
            {
                cout<<f[i]-h<<endl;
                break;
            }
    }
    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.