Herb Trees
InterviewTime limit1sMemory limit1024 MB
Pick trees to keep so that no two kept trees are adjacent, maximizing the sum of their berry counts.
- Level
Medium4 of 10
- Topics
- Dynamic programming
- Solved
- No attempts yet
Problem
Farmer Ugnė planted herb trees in her garden. She planted them in a straight line and numbered them in order from to .
Unfortunately, as soon as the first berry buds appeared, Ugnė realized that the trees had been planted too densely. If she does nothing, neighboring trees will overshadow one another and she will be left with no berries this year.
To prevent this, Ugnė plans to transplant some of the trees; a transplanted tree bears no fruit this year. If Ugnė wants to keep a tree , she must transplant both of its neighbors, trees and .
Which trees should Ugnė transplant so that the remaining harvest is as large as possible?
Input
The first line contains the number of trees .
The second line contains integers separated by spaces, where is the number of berry buds on tree .
Output
Output a single integer on one line: the maximum number of berries Ugnė can hope for if she transplants the trees optimally.