Problem 182
House Robber II
Medium · 20 pts · Array, Dynamic Programming
Problem
This is the House Robber problem with one change: the houses stand in a circle, so the first and the last house are neighbours. You still cannot rob two adjacent houses. Find the largest total amount you can rob. Since the first and last houses cannot both be robbed, solve the ordinary straight-line problem twice: once without the last house and once without the first house, and take the larger answer. With a single house, just rob it.
Input
First line: integer N. Second line: N space-separated integers.
Output
Print the maximum amount.
Constraints
1 <= N <= 100000; 0 <= A[i] <= 10000
Example input
3 2 3 2
Example output
3
Explanation
With houses 2 3 2 in a circle, the two houses holding 2 are neighbours of each other (first and last), so you cannot take both for 4. The best choice is the middle house alone, giving 3.