House Robber
The security firm Calder Risk models a burglary along one side of a street to show a client where the exposure is. Every house holds a known amount of cash, and each has an alarm wired to its immediate neighbours, so raiding two houses that stand next to each other trips it. The two ends of the row are not neighbours — this is a street, not a ring. The client wants the worst case: the most that could be taken in one night.
Task: Print the largest total that can be taken without raiding two neighbouring houses.
Input
A single line holding between 1 and 100000 integers separated by single spaces — the cash in each house, in street order. Every value is zero or more.
Output
One line holding a single integer: the largest total obtainable from a set of houses in which no two stand next to each other. The chosen houses need not alternate strictly — any set with no two neighbours is allowed.
Example:
Input:
2 7 9 3 1
Output:
12
(rob houses worth 2, 9, 1 = 12)
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in