[bzoj1673] [Usaco2005 Dec] Scales Balance __bzoj

Source: Internet
Author: User
Tags time limit
1673: [Usaco2005 dec]scales balanceTime Limit:5 Sec Memory limit:64 MB
[Submit] [Status] [Discuss] Description

Farmer John has a balance for weighing the cows. He also has a set of the N (1 <= n <= 1000) weights with known masses (all of which fit in-bits) for use on one side of the balance. He places a cow in one side of the balance and then adds to the other weights the side until. (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 have the curious property This is lined up from smallest to biggest, each weight (from the third one on) has At least as much mass as the previous two combined. FJ wants to determine the maximum mass and can use his weights to measure exactly. Since The total mass must is no larger than C, he might is 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 scale used to weigh cattle. Matching is N (1≤n≤1000) a known quality weight (all weight weights are in the 31-bit binary). Every time he called a cow, he put a cow on one side of the scale, then add weights to the other side of the scale until the balance is balanced, so the overall 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 weight under her hoof, She would try to kick the weights onto John's face. Scales can withstand the quality of the object is not infinite, the day flat on one side of the mass of the object is greater than C (1≤c<230), the balance will be damaged.      weights are lined up in line with their mass size. And, in this line starting with the 3rd weight, each weight is at least equal to the quality of the first two weights (that is, the mass of the top two of the weight that is smaller than it is).      John wanted to know how much of the mass he could weigh with the weights and the scales he possessed. Because the maximum bearing 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 some weights to make their mass and the largest of all combinations under the premise of not crushing a bad 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 are guaranteed is in non-decreasing order. Line 1th: two positive integers n and C separated by spaces.

2nd to N+1 line: Each row contains only a positive integer, that is, the quality of a weight. Ensure that the quality of these weights is a no descending sequence Output

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

A positive integer that represents the maximum mass of a pressure-negative balance that can be weighed with the given weight. Sample Input 3 15//three items, your "bag" volume of 15, the following gives three numbers, starting from the third number, it is greater than the previous two digits of the sum, this condition is too important
1
10
20

INPUT DETAILS:

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

Sample Output one HINT

John has 3 weights, the quality of which is 1,10,20 units. His scales can only withstand objects of a mass of 15 units. With a weight of 1 and 10 of two weights can be called the quality of 11 cattle. These 3 weights can be composed of other quality is not smaller than 11 is a bad balance of the Source of the search on the line

#include <bits/stdc++.h>
using namespace std;
int N,c,a[1005],ans;long long sum[1005];
void Dfs (int x, int s) {
	if (Sum[x] + s <= ans) return;
	Ans = max (s, ans);
	for (int i = x; I >= 1; i--)
		if (S + a[i] <= c)
			Dfs (i-1, S + a[i]);
int main () {
	scanf ("%d%d", &n, &c);
	for (int i = 1; I <= n; i++) scanf ("%d", &a[i));
	while (A[n] > C) n--;
	for (int i = 1; I <= n; i++) sum[i] = Sum[i-1] + a[i];
	DFS (n, 0); printf ("%d\n", ans);
	return 0;
}
* *
3
1
/


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.