Official

A - Odd Index Sum Editorial by en_translator


If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).


In this problem, you are asked to do the following:

  1. Receive \(N,A_1,A_2,\dots,A_N\) from Standard Input.
  2. Find \(A_1+A_3+\dots\), and print the answer to Standard Output.

1. can be done by first receiving \(N\), and then receiving \(A\) using a for loop.
2. can also be done with a for loop. Note that if your array is 0-based, the answer is the sum of the even-indexed elements.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> a(n);
  for(auto &nx : a){cin >> nx;}
  int res=0;
  for(int i=0;i<n;i+=2){res+=a[i];}
  cout << res << "\n";
  return 0;
}

Original proposer: physics0523