# bzoj 1673: [Usaco2005 Dec]Scales 天平（DFS）

## 1673: [Usaco2005 Dec]Scales 天平

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 695   Solved: 253
[ Submit][ Status][ Discuss]

## Description

Farmer John has a balance for weighing the cows. He also has a set of N (1 <= N <= 1000) weights with known masses (all of which fit in 31 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 his face whenever they can.) The balance has a maximum mass rating and will break if FJ uses more than a certain total mass C (1 <= C < 2^30) on one side. The weights have the curious property that when 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 that he can use his weights to measure exactly. Since the total mass must be no larger than C, he might not be 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, will determine the maximum legal mass that he can weigh exactly.

约翰有一架用来称牛的体重的天平．与之配套的是N(1≤N≤1000)个已知质量的砝码（所 有砝码质量的数值都在31位二进制内）．每次称牛时，他都把某头奶牛安置在天平的某一边， 然后往天平另一边加砝码，直到天平平衡，于是此时砝码的总质量就是牛的质量（约翰不能把砝码放到奶牛的那边，因为奶牛不喜欢称体重，每当约翰把砝码放到她的蹄子底下，她就会尝试把砝码踢到约翰脸上）．天平能承受的物体的质量不是无限的，当天平某一边物体的质量大于C(1≤C<230)时，天平就会被损坏．    砝码按照它们质量的大小被排成一行．并且，这一行中从第3个砝码开始，每个砝码的质量至少等于前面两个砝码（也就是质量比它小的砝码中质量最大的两个）的质量的和．    约翰想知道，用他所拥有的这些砝码以及这架天平，能称出的质量最大是多少．由于天平的最大承重能力为C．他不能把所有砝码都放到天平上．
现在约翰告诉你每个砝码的质量，以及天平能承受的最大质量．你的任务是选出一些砝码，

## Input

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

* Lines 2..N+1: Each line contains a single positive integer that is the mass of one weight. The masses are guaranteed to be in non-decreasing order.

第1行：两个用空格隔开的正整数N和C.

第2到N+1行：每一行仅包含一个正整数，即某个砝码的质量．保证这些砝码的质量是一个不下降序列

## Output

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

一个正整数，表示用所给的砝码能称出的不压坏天平的最大质量．

3 15
1
10
20

## Sample Output

11

#include<stdio.h>
#include<algorithm>
using namespace std;
#define LL long long
LL n, m, ans, a[1005], sum[1005];
void Sech(LL x, LL now)
{
LL i;
if(now+sum[x]<=ans)
return;
for(i=x;i<=n;i++)
{
if(now+a[i]<=m)
{
ans = max(ans, now+a[i]);
if(i+1<=n)
Sech(i+1, now+a[i]);
}
}
}
int main(void)
{
LL i;
scanf("%lld%lld", &n, &m);
for(i=1;i<=n;i++)
scanf("%lld", &a[i]);
for(i=1;i<=n/2;i++)
swap(a[i], a[n-i+1]);
for(i=n;i>=1;i--)
sum[i] = sum[i+1]+a[i];
ans = 0;
Sech(1, 0);
printf("%lld\n", ans);
return 0;
}

10-22 167
10-11 447
11-16 399
08-25 23
09-10 27
09-21 34
05-22 84
06-15 1万+