uva

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.4 kB
16
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

int main() {
    map<int, pair<int, int>> mp;
    int n;
    while (cin >> n) {
        if (!n) break;
        int arr[1000], ans = -1;
        mp.clear();

        //a+b
        for (int i = 0; i < n; i++) {
            cin >> arr[i];
            for (int j = 0; j < i; j++) mp[arr[i] + arr[j]] = {arr[i], arr[j]};
        }
//here, only one pair for mp[a+b]

        //d-c
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (mp.count(arr[i] - arr[j])) {
                    pair<int, int> pr = mp[arr[i] - arr[j]];
                    if (pr.first != arr[i] && pr.first != arr[j] &&
                        pr.second != arr[i] && pr.second != arr[j] &&
                        ans < arr[i])
                        ans = arr[i];
                }
                if (mp.count(arr[j] - arr[i])) {
                    pair<int, int> pr = mp[arr[j] - arr[i]];
                    if (pr.first != arr[i] && pr.first != arr[j] &&
                        pr.second != arr[i] && pr.second != arr[j] &&
                        ans < arr[j])
                        ans = arr[j];
                }
            }
        }
        if (ans == -1)
            cout << "no solution\n";
        else
            cout << ans << '\n';
    }
    return 0;
}