Choose nonadjacent rewards
Return the largest sum of nonadjacent nonnegative values in a line. Choosing no values is allowed.
older, previous, value = 2, 7, 9
print(max(previous, older+value))Reason about the state
For each prefix compare skipping the current item with taking it after the best prefix two positions earlier. Two rolling states suffice. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the largest sum of nonadjacent nonnegative values in a line. Choosing no values is allowed.
The explanation opens here once every check passes.