friends
Adisor realized that part of his troubles were due to the fact that he didn't choose his friends right. Therefore, he has made an important decision: he is going to give up on some of them.
Here's how he went about it: first he numbered all his n friends from 1 to n. Then he attached to each friend i, an integer Ci, called "quality", which in Adisor's mind represented the algebraic sum of the qualities and defects of that person. Afterwards, he drew a circle and on its side he entered, in order, values C1, C2,…Cn, so that Ci neighbors Ci-1 and Ci+1, for any i between 2 and n-1. Also, C1 neighbors Cn and C2, and Cn neighbors Cn-1 and C1.
Finally, he set the criteria as per which he would choose that would remain
his friends from now on. His purpose is to obtain a maximum value S,
by summing up the individual qualities of the persons he selects. Positioning
on the friend circle wasn't done randomly: Adisor seated nearby friends with
similar behavior, so that he would never choose two people located on consecutive
positions in the circle.
Help Adisor select his future friends, so that the above restrictions are respected.
Task
Write a program that determines positive integer S, representing the largest value that can be obtained by summing up the values of the "quality" of all persons that will continue to remain friends with Adisor.
Input Data
File friends.in contains on the first line a positive integer n representing the number of friends Adisor has. Starting with the second line we find n positive integers C1, C2, … Cn, one per line, representing in order the values of the "qualities" of each person.
Output Data
File friends.out will contain a single line, which will display positive integer S, with the meaning above.
Restrictions
Example
friends.in | friends.out | Explanation |
5 7 3 -2 1 6 |
9 |
In the first example, Adisor selects the second friend with "quality" 3 and the fit, with "quality" 6. |
prof. Constantin Galatan
C.N. "Liviu Rebreanu" Bistriţa
tucu_galatan@yahoo.com