Cutting in Line
InterviewTime limit1sMemory limit1024 MB
Given each student's satisfaction, find the maximum total when each student either cuts to the front or joins the back of the line.
- Level
Medium4 of 10
- Topics
- Prefix sum, Greedy, Array
- Solved
- No attempts yet
Problem
Today the lunch menu at Chamseulgi Cafeteria is chicken mayo rice bowl. Perhaps because of that, far more students than usual are lining up. Students have ticket numbers from to . They line up in ticket order. The student with ticket joins the line first, and their satisfaction is . Starting from ticket , each student chooses one of the following two actions.
- Stand at the front: The student stands at the front of the line, and student has satisfaction .
- Stand at the back: The student stands at the back of the line, and student has satisfaction .
The way students line up can also change the satisfaction of students already in line.
- If student is in front of student , student has been cut in front of, so student 's satisfaction changes to .
- Note that it can change to even if the existing satisfaction was .
- If no student is in front of student , student has not been cut in front of, so student 's satisfaction does not change.
Input
The first line contains the number of students .
The second line contains positive integers , , , , , separated by spaces.
Output
Print the maximum possible total satisfaction on the first line.