#C13706. House Robber Problem
House Robber Problem
House Robber Problem
You are given an array of non-negative integers where each integer represents the amount of money in a house. You need to determine the maximum amount of money you can rob tonight without alerting the police. The constraint is that if you rob house i, you cannot rob house i+1 because it will trigger the alarm system. In other words, if you choose to rob a house, you must skip its adjacent house.
This can be formulated using the recurrence relation:
\(dp[i] = \max(dp[i-1],\; dp[i-2] + nums[i])\)
where \(dp[i]\) represents the maximum money robbed up to house i.
Your task is to implement an algorithm which reads the input from stdin and outputs the result to stdout.
inputFormat
The input consists of two lines.
- The first line contains a single integer \(n\) indicating the number of houses.
- The second line contains \(n\) space-separated non-negative integers representing the amount of money in each house.
If \(n = 0\), it means there are no houses.
outputFormat
Output a single integer representing the maximum amount of money you can rob without alerting the police.
## sample0
0