Lemon Picking
InterviewTime limit2sMemory limit1024 MB
Seongwoo picks lemons from one tree and walks home, losing one lemon per move. Find the maximum number of lemons he can carry home.
- Level
Easy2 of 10
- Topics
- Implementation, Math
- Solved
- No attempts yet
Problem
Seongwoo has to pick lemons in a forest. The forest can be drawn as a single number line, and lemon trees grow at evenly spaced positions from to . Seongwoo is currently at , and his house is at . The tree at has lemons. Until he gets home, Seongwoo chooses one of the following actions each time.
- Pick: pick all lemons growing on the tree. He can do this action at most once.
- Move: if he is at , he moves to .
However, the bag he carries lemons in has a hole. Each time he moves (action 2), if the bag holds lemons, one lemon falls out, and it cannot be picked up again. Find the maximum number of lemons Seongwoo can take home.
Input
The first line contains the number of trees .
The second line contains the number of lemons on the tree at , in order.
Output
Print the maximum number of lemons Seongwoo can take home.