Two cards are compared at a time with the smaller pocketed, so find the largest possible sum of pocketed numbers for the given row.
Easy1ArrayInterviewNo attempts yetTime limit1sMemory limit32 MBSeunghyun has n cards, each with a front and a back. The front of each card holds one integer between 1 and 2222, and no two cards hold the same number. The back of each card has a picture of an animal, and no two cards have the same picture.

These are cards that could appear. The left picture is the front and the right picture is the back.
Seunghyun lays the cards on the floor in a row with the backs facing up and numbers them 1 through n in order. Let ci be the number on the front of card i. Seunghyun repeats the following until exactly one card is left on the floor.
The animal pictures differ, so Seunghyun can tell the cards apart and can turn over exactly the two cards he wants every time. He likes big numbers, so he wants the final sum in his pocket to be as large as possible. Find that maximum sum.
The first line holds a natural number n, the number of cards. (1≤n≤2222)
The second line holds c1,c2,…,cn in order, separated by spaces. Each ci is an integer between 1 and 2222, and they are all different.
Print the largest possible sum on the first line.
In the first example only one play is possible. Seunghyun turns over the card with 3 and the card with 4, and after the card with 3 goes into his pocket only one card is left on the floor, so the answer is 3.
In the second example three plays are possible.
The answer is therefore 4.