Alice lives in the forest of magic. At the entry of the forest of magic, live three mischievous fairies, Sunny, Lunar and Star. On Alice's way back home, to enter the forest, she must give them exactly three bags of mushrooms whose total weight must be of integral kilograms. If she cannot do so, she must leave all the five bags and enter the forest with no mushrooms.
Somewhere in the forest of magic near Alice's house, lives a magician, Marisa. Marisa will steal 1 kilogram of mushrooms repeatedly from Alice until she has no more than 1 kilogram mushrooms in total. So when Alice gets home, what's the maximum possible amount of mushrooms Alice has? Remember that our common sense doesn't always hold in Gensokyo. People in Gensokyo believe that 1 kilogram is equal to 1024 grams.
The first line of each test case contains an integer 0 ≤ n ≤ 5, the number of mountains where Alice has picked mushrooms. The second line contains n integers 0 ≤ w i ≤ 2012, the amount of mushrooms picked in each mountain.
1 9 4 512 512 512 512 5 100 200 300 400 500 5 208 308 508 708 1108
1024 1024 0 792
Hint
In the second sample, if Alice doesn't pick any mushrooms from the 5-th mountain. She can give (512+512+0) =1024 grams of mushrooms to Sunny, Lunar andStar. Marisa won't steal any mushrooms from her as she has exactly 1 kilogram of mushrooms in total.In the third sample, there are no three bags whose total weight is of integral kilograms. So Alice must leave all the five bags and enter the forest with no mushrooms.In the last sample:1.Giving Sunny, Lunar and Star: (208+308+508)=10242.Stolen by Marisa: ((708+1108)-1024)=792
题目输入是这样说的:小女孩已经采了的山,意思是说另外还有5-n座山,可以去采,然后满足这些情况
n<=3的时候:另外的山肯定可以使最后回去的时候达到最大值1024
n==4的时候:枚举三个可以凑成1024的,否则,就拿另外一个山来补
n==5的时候:全部枚举
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int map[9];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(map,0,sizeof(map));
for(int i=1;i<=n;i++)
scanf("%d",&map[i]);
//暴力过(找出三个组合能是1024的整数倍的三个袋子)
int sum=-1;
if(n>=4)
{
if(n==5)
{
bool flag=false;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(j==i)
continue;
for(int z=1;z<=5;z++)
{
if(z==j||z==i)
continue;
int temp=map[i]+map[j]+map[z];
int ans=0;
if(temp%1024==0)
{
flag=true;
for(int x=1;x<=5;x++)
{
if(x!=i&&x!=j&&x!=z)
ans+=map[x];
}
while(ans>1024)
{
ans-=1024;
}
if(ans>sum)
sum=ans;
}
}
}
}
if(flag==false)
printf("0\n");
else
printf("%d\n",sum);
}
if(n==4)
{
int ans=0;
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
for(int z=1;z<=4;z++)
{
if(i!=j&&i!=z&&j!=z)
{
if((map[i]+map[j]+map[z])%1024==0)
ans=1024;
}
}
if(ans!=1024)
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(i!=j)
{
int temp=0;
for(int z=1;z<=4;z++)
{
if(z!=i&&z!=j)
temp+=map[z];
}
while(temp>1024)
temp-=1024;
if(temp>sum)
sum=temp;
}
}
}
printf("%d\n",sum);
}
else
printf("1024\n");
}
}
else
printf("1024\n");
}
return 0;
}