All problems

Problem 572

Largest Perimeter Triangle

Easy · 10 pts · Array, Greedy, Sorting

Problem

Choose three of the given lengths that form a triangle with non-zero area, and make its perimeter as large as possible. Three lengths a <= b <= c form such a triangle exactly when a + b > c. Print the largest perimeter, or 0 if no three lengths work. Sort the lengths in decreasing order and check each run of three neighbours; the first run that works is the answer.

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print the largest perimeter, or 0.

Constraints

3 <= N <= 100000; 1 <= A[i] <= 1000000

Example input

3
2 1 2

Example output

5

Explanation

From 2 1 2, the lengths 1, 2 and 2 satisfy 1 + 2 > 2, so they form a triangle with perimeter 5. That is the only choice, so the answer is 5.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Height Checker Next: Pairs of Songs With Total Durations Divisible by 60