electicode
HomeCoursesResourcesProblemsNational OlympiadContestsLeaderboard
...

Arrangement

Time Limit: 1000msMemory Limit: 256MB
View Submissions

Problem Description

Teacher Komila Sobirovna has nnn desks in the class, arranged in a row. When a student sits at desk iii, the teacher's pleasure changes by p[i]p[i]p[i].

There are exactly ⌈n2⌉\lceil \frac{n}{2} \rceil⌈2n​⌉ students in the class. Komila Sobirovna needs to seat all students at desks such that no two students sit at neighboring desks. Among all valid arrangements, find the one that maximizes the total pleasure.

Note: ⌈x⌉\lceil x \rceil⌈x⌉ denotes the smallest integer greater than or equal to xxx. For example, ⌈3.14⌉=4\lceil 3.14 \rceil = 4⌈3.14⌉=4 and ⌈5⌉=5\lceil 5 \rceil = 5⌈5⌉=5.

Input Format

The first line contains an integer nnn --- the number of desks.

The second line contains nnn integers p[1],p[2],…,p[n]p[1], p[2], \ldots, p[n]p[1],p[2],…,p[n] --- the impact of each desk on the teacher's pleasure.

Constraints:

1≤n≤1051 \leq n \leq 10^51≤n≤105

−109≤p[i]≤109-10^9 \leq p[i] \leq 10^9−109≤p[i]≤109, for each 1≤i≤n1 \leq i \leq n1≤i≤n

Output Format

On a single line, print the maximum pleasure the teacher can get.

Scoring

SubtaskConstraintPoints
111nnn is odd101010
222n≤5n \leq 5n

Notes

In the first sample, n=6n=6n=6 and ⌈62⌉=3\lceil \frac{6}{2} \rceil = 3⌈26​⌉=3 students. Komila Sobirovna seats them at desks 111, 44, and . Total pleasure: .

In the second sample, n=3n=3n=3 and ⌈32⌉=2\lceil \frac{3}{2} \rceil = 2⌈23​⌉=2 students. The only option is desks 111 and 33. Total pleasure: .

Examples

Example 1
Input
6
5 2 -1 3 -4 7
Output
15
Example 2
Input
3
-1 -2 -3
Output
-4

© 2026 Electicode. All rights reserved.

≤
5
101010
333n≤1000n \leq 1000n≤1000353535
444No additional constraints454545
4
666
p[1]+p[4]+p[6]=5+3+7=15p[1]+p[4]+p[6] = 5+3+7 = 15p[1]+p[4]+p[6]=5+3+7=15
3
p[1]+p[3]=(−1)+(−3)=−4p[1]+p[3] = (-1)+(-3) = -4p[1]+p[3]=(−1)+(−3)=−4