Algorithm

Every corner of the N-dimensional (1 < N < 15) unit cube has weight (some positive integer less than 256). We will call two corners neighbouring, if they have common edge. Potency of the corner is the sum of weights of all neighbouring corners. Weights of all the corners are given. You are to determine two neighbouring corners that have the maximum sum of potencies and to output this sum. Input The input will consist of several input blocks. Each input block begins with the integer N, the dimension of the cube. Then there are weights of the corners, one per line in the natural order: • the first line contains the weight of the corner (0,...0,0,0), • the second one - the weight of (0,...,0,0,1), • then there is the weight of (0,...,0,1,0), then (0,...,0,1,1), then (0,...,1,0,0), • the penultimate line contains the weight of the corner (1,...,1,1,0), • the last one - (1,...,1,1,1). The input is terminated by ¡EOF¿. Output For each input block the output line should contain one number, the maximum potencies sum.

Sample Input 3 82 73 8 49 120 44 242 58 2 1 1 1 1

Sample Output 619 4

Code Examples

#1 Code Example with C Programming

```Code - C Programming```

`````` #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
using namespace std;

int main() {
int d, v;
while(scanf("%d",&d) != EOF){
vector<int> weights;
vector<int> potency;
for(int i=0;i i+It 1 i+It d;i++){
scanf("%d",&v);
weights.push_back(v);
}
int best = 0;
for(int i=0;i i+It1  i+It d;i++){
int cur = 0;
for(int j=0;j  i+It d;j++){
cur += weights[i^(1  i+It j)];
}
potency.push_back(cur);
}
for(int i=0;i i+It 1  i+It d;i++)
for(int j=0;j i+It d;j++)
best = max(best,potency[i] + potency[i^(1 i+It  j)]);
cout << best << endl;
}
}``````
Copy The Code &

Input

cmd
3
82
73
8
49
120
44
242
58
2
1
1
1

Output

cmd
619
4