poj--3172 Scales (DFS high Capacity backpack C + +)

Source: Internet
Author: User

Scales
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3148 Accepted: 851

Description

Farmer John had a balance for weighing the cows. He also have a set of n (1 <= n <=) weights with known masses (all of the which fit in bits) for use on one side of the balance. He places a cow on one side of the balance and then adds weights to the other side until they balance. (FJ cannot put weights on the same side of the balance as the cow, because cows tend to kick weights in he face whenever They can.) The balance has a maximum mass rating and would break if FJ uses more than a certain total mass C (1 <= C < 2^30) on One side.

The weights has the curious property as lined up from smallest to biggest and each weight (from the third one on) has At least as much mass as the previous of the combined.

FJ wants to determine the maximum mass so he can use his weights to measure exactly. Since The total mass must is no larger than C, he might not being able to put all the weights onto the scale.

Write A program this, given a list of weights and the maximum mass the balance can take, would determine the maximum legal Mass that he can weigh exactly.

Input

Line 1:two space-separated positive integers, N and C.

Lines 2..n+1:each line contains a single positive integer so is the mass of one weight. The masses is guaranteed to is in non-decreasing order.

Output

Line 1: A single integer So is the largest mass so can be accurately and safely measured.

Sample Input

3 1511020

Sample Output

11

Hint

Explanation of the sample:

FJ have 3 weights, with masses of 1, ten, and units. He can put at the most units in one side of his balance.

The 1 and weights is used to measure 11.   Any greater weight that can is formed from the weights would break the balance. This problem is mainly data huge, the array can not open the original 01 routines cannot be used! Huge pruning Dfs left to do a reference, a good after the title of flag;
#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h>using namespace Std;typedef Long Long ll;int a[1005];ll sum[1005];ll ans,m,n;void dfs (ll X, ll CNT) {    if (Cnt>ans)        ans=cnt;    if (x<1)        return;    for (int i =x;i>=1;i--)    {        if (Cnt+sum[i]<=ans)            continue;        if (cnt+a[i]>m)            continue;        DFS (I-1,cnt+a[i]);}    } int main () {    while (~scanf ("%lld%lld", &n,&m))    {        ans=0;        for (int i=1;i<=n;i++)        {            scanf ("%d", &a[i]);            Sum[i]=sum[i-1]+a[i];        }        DFS (n,0);        printf ("%lld", ans);}    }

  

poj--3172 Scales (DFS high Capacity backpack C + +)

Related Article

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.