A supply convoy's manifest lists the weight of each crate in the order the crates are loaded onto the truck. As part of a balance-inspection protocol, an auditor examines every contiguous stretch of crates whose length is odd (a single crate, three crates in a row, five in a row, and so on) and adds up the weights within that stretch. Two stretches of the same length starting at different positions are both examined separately, even if their weights happen to be identical. The auditor wants the grand total obtained by summing the stretch-total of every such odd-length stretch that appears anywhere in the manifest.
Print a single integer: the sum, over every contiguous odd-length stretch of crates, of the total weight of that stretch.
Example 1
Input
5 1 4 2 5 3
Expected
58
Explanation
The odd-length stretches are the five single crates 1, 4, 2, 5, 3 (summing to 15), the three length-3 stretches [1,4,2]=7, [4,2,5]=11, [2,5,3]=10 (summing to 28), and the one length-5 stretch [1,4,2,5,3]=15. Adding 15+28+15 gives 58.
Example 2
Input
2 1 2
Expected
3
Explanation
With only 2 crates, the only odd-length stretches are the two single crates 1 and 2 (the one length-2 stretch is skipped because 2 is even). Their weights sum to 1+2=3.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →