← 返回 cisco 的题目列表Maximum Non-Adjacent Chocolates
类型:qbank
Given chocolate counts in a line of jars, maximize the total picked without choosing adjacent jars.
Requirements
Input line 1 is numJars, the number of jars N.
Input line 2 contains N space-separated integers, the chocolate count in each jar.
Pick a subset of jars with no two picked jars next to each other.
Print the maximum total chocolates.
Constraint shown: 1 < N <= 1000.
Examples
Input:
6
5 30 99 60 5 10
Output:
114
Explanation: pick the 1st, 3rd, and 6th jars: 5 + 99 + 10 = 114.
Notes
This is the standard two-state dynamic programming recurrence for maximum sum of non-adjacent elements.