House Robber II

MediumDynamic Programming
Next
Dynamic ProgrammingArray

The same as House Robber, but the houses are arranged in a circle, so the first and last houses are also neighbours and cannot both be robbed. Find the maximum amount you can rob.

Input

Line 1: n. Line 2: the n amounts.

Output

Print the maximum amount of money.

Constraints

1 <= n <= 10^4 0 <= amount <= 10^4

Example 1
Input
3
2 3 2
Output
3

Houses 0 and 2 are adjacent in the circle, so the best is to rob only house 1 for 3.

Example 2
Input
4
1 2 3 1
Output
4

Rob houses 1 and 3 (2 + 1)... or houses 0 and 2 (1 + 3) for 4.

Hints

House Robber II - Medium Dynamic Programming Problem | Skill Shiksha