The Little Girl who Picks Mushrooms
64-bit integer IO format: %lld Java class name: Main
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.
Input
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.
Output
For each test case, output the maximum possible amount of mushrooms Alice can bring home, modulo 20121014 (this is NOT a prime).
Sample Input
1 9 4 512 512 512 512 5 100 200 300 400 500 5 208 308 508 708 1108
Sample Output
1024 1024 0 792
Note
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:
- Giving Sunny, Lunar and Star: (208+308+508)=1024
- Stolen by Marisa: ((708+1108)-1024)=792
Source
Author
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 int d[6],n,ans,sum; 18 bool flag; 19 int main(){ 20 while(~scanf("%d",&n)){ 21 sum = ans = 0; 22 for(int i = 1; i <= n; i++){ 23 scanf("%d",d+i); 24 sum += d[i]; 25 } 26 if(n <= 3) {puts("1024");continue;} 27 if(n == 5){ 28 for(int i = 1; i <= n; i++){ 29 for(int j = i+1; j <= n; j++){ 30 for(int k = j+1; k <= n; k++){ 31 int tmp = d[i]+d[j]+d[k]; 32 if(tmp%1024 == 0){ 33 tmp = sum - tmp; 34 while(tmp > 1024) tmp -= 1024; 35 ans = max(ans,tmp); 36 } 37 } 38 } 39 } 40 printf("%d ",ans); 41 continue; 42 } 43 flag = false; 44 for(int i = 1; i <= n; i++){ 45 for(int j = i+1; j <= n; j++){ 46 for(int k = j+1; k <= n; k++){ 47 int tmp = d[i]+d[j]+d[k]; 48 if(tmp%1024 == 0){ 49 flag = true; 50 break; 51 } 52 } 53 } 54 } 55 if(flag){puts("1024");continue;} 56 for(int i = 1; i <= n; i++){ 57 for(int j = i+1; j <= n; j++){ 58 int tmp = d[i]+d[j]; 59 while(tmp > 1024) tmp -= 1024; 60 ans = max(ans,tmp); 61 } 62 } 63 printf("%d ",ans); 64 } 65 return 0; 66 }