Problem 785
Sum of Elements at Even Indices
Problem
Add up the elements that sit at even positions of the array, counting positions from 0: A[0] + A[2] + A[4] + .... The elements at odd positions are ignored, whatever their values. Loop over the indices with a step of 2, or check whether each index is divisible by 2. Use a 64-bit sum.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].
Output
Print the sum of the elements at indices 0, 2, 4, ....
Constraints
1 <= N <= 100000; -10^9 <= A[i] <= 10^9
Example input
5 5 1 7 2 9
Example output
21
Explanation
In 5 1 7 2 9, the elements at indices 0, 2 and 4 are 5, 7 and 9, which add up to 21.