a + b = d - c;
枚举a + b 加入哈希表,再枚举 d - c 在哈希表中查找,并且四个数字不能重复
/***********************************************
* Author: fisty
* Created Time: 2015/3/17 22:36:27
* File Name : uva10125.cpp
*********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define MAX_N 1100
int n;
int a[MAX_N];
int cnt = 0;
struct Point{
int sum;
int x;
int y;
}p;
const int maxsize = MAX_N * MAX_N;
int data_cnt;
Point data[maxsize];
int head[maxsize];
int next[maxsize];
int hash(int x){
int t = (x << 1) + (x >> 1);
return (t & 0x7f7f7f7f) % maxsize;
}
void insert(Point x){
data[data_cnt] = x;
int key = hash(x.sum);
next[data_cnt] = head[key];
head[key] = data_cnt;
data_cnt++;
}
int find(int x, int y, int sum){
int s = head[hash(sum)];
for(int i = s;i != -1; i = next[i]){
if(sum == data[i].sum){
int flag = 1;
if(x == data[i].x || y == data[i].y)
flag = 0;
if(flag) return 1;
}
}
return 0;
}
int main() {
//freopen("in.cpp", "r", stdin);
cin.tie(0);
ios::sync_with_stdio(false);
while(cin >> n){
if(!n) break;
Memset(head, -1);
Memset(next, -1);
cnt = 0;
FOR(i, 0, n){
cin >> a[i];
}
for(int i = 0;i < n; i++){
for(int j = i+1;j < n; j++){
p.sum = a[i] + a[j]; //将A+B 插入
p.x = i;
p.y = j;
insert(p);
}
}
int ok = 0;
int _max = -INF;
for(int i = 0;i < n; i++){
for(int j = i+1;j < n; j++){
int d = a[i] - a[j];
if(find(i, j, d) && a[i] > _max){
ok = 1;
_max = a[i];
}
d = a[j] - a[i];
if(find(j, i, d) && a[j] > _max){
ok = 1;
_max = a[j];
}
}
}
if(ok)
cout << _max <<endl;
else
cout << "no solution" << endl;
}
return 0;
}