Notation: S(i) where S(i) is the sum of the largest set up to the i position
Recurrence Relation: S(i) =max{S(i-2) + A[i], S(i-1)}
Optimality: Given the set of numbers {1,3,8,16,2, 3, 10, 40, 22, 35, 64} the above algorithm would find {1,3,9,19, 19, 22, 29, 62, 62, 97, 126} providing a set of {3, 16, 3, 40, 64} = 126.
If we assume the maximum number of options (even or odd) is the best answer we would get {1, 8, 2, 10, 22, 64} = 115 or {3, 16, 3, 40, 22, 35} = 119 both of which is suboptimal to the proposed algorithm.
Algorithm:
A[n] = {1,3,8,16,2, 3, 10, 40, 22, 35, 64}
S[n]
s[0] = a[0]
if(a[1] > S[0]) {
S[1] = a[1]
} else {
S[1] = s[0]
}
for(int i = 2; i < n; i++) {
if(S[i - 1] > S[i-2] + A[i]) {
s[i] = s[i-1]
} else {
S[i] = S[i-2] + A[i]
}