LogiQuest

226. House Robber (Circle)

MediumDynamic Programming

Same as House Robber, but the houses form a circle: the first and last houses are adjacent.

Input format

Line 1: n Line 2: n non-negative integers

Output format

The maximum amount.

Example 1

Input

3
2 3 2

Output

3

Example 2

Input

4
1 2 3 1

Output

4

Constraints

1 ≤ n ≤ 10⁵

Read input from standard input and print the answer to standard output. Your code is judged against 6 test cases, most of them hidden.

Loading the MyLoginn IDE…