It's yet another festival season in Gensokyo. Little girl Alice planned to pick mushrooms in five mountains. She brought five bags with her and used different bags to collect mushrooms from different mountains. Each bag has a capacity of 2012 grams. Alice has finished picking mushrooms in 0 ≤ n ≤ 5 mountains. In the the i-th mountain, she picked 0 ≤ wi ≤ 2012 grams of mushrooms. Now she is moving forward to the remained mountains. After finishing picking mushrooms in all the five mountains, she want to bring as much mushrooms as possible home to cook a delicious soup.
Alice lives in the forest of magic. At the entry of the forest of magic, lives 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 get 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 belive that 1 kilograms is equal to 1024 grams.
There are about 8192 test cases. Process to the end of file.
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 ≤ wi ≤ 2012, the amount of mushrooms picked in each mountain.
For each test case, output the maximum possible amount of mushrooms Alice can bring home, modulo 20121014 (this is NOT a prime).
1 9 4 512 512 512 512 5 100 200 300 400 500 5 208 308 508 708 1108
1024 1024 0 792
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 and Star. Marisa won't steal any mushrooms from her as she has exactly 1 kilograms 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:
//题目说明:小女孩有5个篮子,要去5座山采蘑菇,每个篮子只装一个山的蘑菇
//有三个小精灵 要其中个篮子,而且必须组成1024的倍数 否则就没收蘑菇
// 小女孩家附近有个女巫,只要小女孩蘑菇多于1024 就偷她,每次偷1024克
// 思路 n<=3 时 就是 1024
// n>3时枚举情况 下面的代码以前写 思路是枚举给小精灵的3个篮子
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; int main() { //前3种情况组1024倍数,后2个组最大可保留数的最大值
C(5)3=10 就是5个选3个组成1024倍数 int h[10][5]={ 0,1,2,3,4, 0,1,3,2,4, 0,1,4,2,3, 0,2,3,1,4, 0,2,4,1,3, 0,3,4,1,2, 1,2,3,0,4, 1,2,4,0,3, 1,3,4,0,2, 2,3,4,0,1 }; int n,Max; int a[5]; int i,j,temp=0; bool flag; while(scanf("%d",&n)!=EOF) { memset(a,-1,sizeof(a)); Max=0; for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<10;i++) { temp=0;flag=0; for(j=0;j<3;j++) if(a[h[i][j]]==-1) { flag=1; break; } else { temp+=a[h[i][j]]; }
j=3; if(flag)//说明选取成功 { temp=0; for(;j<5;j++) if(a[h[i][j]]==-1) { Max=1024; break; } else { temp+=a[h[i][j]]; } if(Max==1024) break; while(temp>1024) temp-=1024; Max=max(Max,temp); } else { if(temp%1024==0)//说明选取成功 { temp=0; for(;j<5;j++) if(a[h[i][j]]==-1) { Max=1024; break; } else { temp+=a[h[i][j]]; } if(Max==1024) break; while(temp>1024) temp-=1024; Max=max(Max,temp); } } } printf("%d\n",Max); } return 0; }