This page is still under construction.

Parts of this page are still being built. What you see may change.

Sum Set

Time limit1sMemory limit128 MB

Summary
Given a set of distinct integers, find the largest element d that equals the sum of three other distinct elements.
Level

Medium6 of 10

Topics
Hash map, Brute force, Sorting
Solved
No attempts yet

Statement

You are given a set SS of integers. Among four distinct elements aa, bb, cc, dd of SS, find the largest dd such that a+b+c=da + b + c = d.

Input

The input consists of several test cases.

The first line of each test case contains the size nn (1≤n≤10001 \le n \le 1000) of the set SS. Each of the next nn lines contains one element of SS, an integer between −536870912-536870912 and 536870911536870911. The elements of a set are all distinct.

The last line of the input contains 00, marking the end of input.

Output

For each test case, print the largest dd that satisfies the condition on its own line. If no such dd exists, print no solution.

Examples4

  1. Example 1

    Input
    5
    2
    3
    5
    7
    12
    5
    2
    16
    64
    256
    1024
    0
    
    Expected output
    12
    no solution
    
  2. Example 2

    Input
    4
    1
    2
    3
    6
    0
    
    Expected output
    6
    
  3. Example 3

    Input
    5
    2
    3
    5
    7
    12
    0
    
    Expected output
    12
    
  4. Example 4

    Input
    4
    1
    2
    3
    6
    3
    1
    2
    3
    4
    1
    2
    5
    8
    0
    
    Expected output
    6
    no solution
    8