#K5426. Maximum Sum of Non-Adjacent Numbers
Maximum Sum of Non-Adjacent Numbers
Maximum Sum of Non-Adjacent Numbers
You are given a list of integers. Your task is to compute the maximum sum you can achieve by summing a subset of these integers such that no two chosen numbers are adjacent in the original list. In other words, if you choose an element at position i, you cannot choose the elements at positions i-1 and i+1.
If all elements are negative, then the maximum sum is defined to be 0. The list may also be empty; in that case, the output should be 0.
Note: The input format is as follows: the first integer n represents the number of elements in the list, followed by n integers separated by spaces.
inputFormat
The first line contains an integer n
— the number of elements in the list. The second line contains n
space-separated integers.
outputFormat
Output a single integer — the maximum sum achievable by selecting non-adjacent numbers from the list.
## sample5
3 2 5 10 7
15