bzoj:1673: [Usaco2005 dec]scales balance

Source: Internet
Author: User

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 that, given a list of weights and the maximum mass the BalanCe can take, would determine the maximum legal mass that he can weigh exactly.

John has a balance to weigh the cows. It is supplied with N (1≤n≤1000) weights of known weights (all weight mass values are in 31-bit binary). Each time the cow, he put a cow on one side of the balance, and then add weights to the other side of the balance, until the balance, then the total quality of the weight is the quality of the cow (John can not put the weight on the cow's side, because the cows do not like to weigh, whenever John put the weight on her hoof, She would try to kick the weight to John's face.    Balance can withstand the quality of the object is not infinite, the same day when the mass of one side of the object is greater than C (1≤c<230), the balance will be damaged. Weights are lined up according to their mass size.    And, starting from the 3rd weight in this line, the mass of each weight is at least equal to the quality of the first two weights (i.e., two of the highest mass in the weight of its smaller weights). John wanted to know how much mass he could weigh with the weights he had and the balance. Since the maximum load capacity of the balance is C.    He can't put all the weights on the scales. Now John tells you the quality of each weight and the maximum quality the balance can withstand. Your task is to select weights that make them the largest of all combinations, without compromising the balance. Input

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

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

Line 1th: two positive integers n and c separated by a space.

2nd to n+1: Each line contains only a positive integer, which is the mass of a weight. Ensure that the mass of these weights is a non-descending sequence

Output

* Line 1: A single integer, which is the largest mass, can be accurately and safely measured.

A positive integer representing the maximum mass of the non-pressurized balance that can be weighed with the given weight.

Sample Input3 15//Three items, your "bag" volume is 15, the following gives three numbers, starting from the third number, it is greater than the sum of the preceding two numbers, this condition is too important
1
10
20

INPUT DETAILS:

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

Sample Output11 HINT

John has 3 weights, the quality of which is 1,10,20 units respectively. His balance can only withstand objects of a mass of 15 units. Two weights with a mass of 1 and 10 can be used to weigh a cow with a mass of 11. The other qualities that these 3 weights can make are either smaller than 11, or they can crush the balance.

Must be spit groove, the legend of all weights in the quality of the values are in the 31-bit binary, the legend from the third number, it is greater than the sum of the previous two numbers, then the data if you want to construct, n up to 30. The rest of a deep search is good, as long as the deep search does not write slag, minutes 0MS ...

#include <cstdio>#include<algorithm>#definell Long Longusing namespacestd;ll n,a[ +],c,ans=0, q[ +];voidDFS (ll p,ll sum) {if(Sum+q[p]<=ans)return; if(ans<sum) ans=sum;  for(LL i=p;i;i--){        if(sum+a[i]<=c) DFS (i-1, sum+A[i]); }}intMain () {scanf ("%lld%lld",&n,&c);  for(LL i=1; i<=n;i++) {scanf ("%lld",&A[i]); if(a[i]>c) n=i-1;Elseq[i]=q[i-1]+A[i]; } DFS (n,0); printf ("%lld\n", ans);}

bzoj:1673: [Usaco2005 dec]scales balance

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.