Problem · Dynamic Programming

Maximum Chocolates from Jars (L.C. 198 :)

EasyCiscoNEW GRADOA
See Cisco hiring insights

There is a set of N jars containing chocolates. Some of them may be empty. Determine the maximum number of chocolates Andrew can pick from the jars given that he cannot pick from jars next to each other.

Write an algorithm to find the maximum number of chocolates that can be picked from the jars in such a way that the chocolates are not picked from jars next to each other.

Input

The first line of input consists of an integer- numJars, representing the number of jars (N). The next line consists of N space-separated integers representing the number of chocolates in each jar.

Output

Print the maximum number of chocolates that can be picked from the jars in such a way that the chocolates are not picked from jars next to each other.

Constraints

1 ≤ N ≤ 1000

💐 spike carries! ༊·°🌺

Examples
01 · Example 1
jars = [5, 30, 99, 60, 5, 10]
return = 114
Andrew picks from the 1st (5), 3rd (99) and 6th (10) jars. So, the output is 114. Explanation added on 03-13-2025 :)
Constraints
1 ≤ N ≤ 1000
More Cisco problems
drafts saved locally
public int maximumChocolates(int[] jars) {
  // write your code here
}
jars[5, 30, 99, 60, 5, 10]
expected114
sign in to submit