# CHEFALT Alternating Subsequences

**Problem: CHEFALT**

You are given an array of **N** non-negative integers: **A _{1}**,

**A**, …,

_{2}**A**. An

_{N}*alternating subsequence*is a subsequence in which the indices of any two consecutive elements differ by exactly two in the original array. That is, if

**A**,

_{i1}**A**, …,

_{i2}**A**is some subsequence, then for it to be an

_{ik}*alternating subsequence*, (i

_{2}– i

_{1}= 2), (i

_{3}– i

_{2}= 2), and so on should all hold true. Among all

*alternating subsequences*, find the one which has maximum sum of elements, and output that sum.

Solution:

```
#include <iostream>
using namespace std;
int main(int argc,char const *argv[])
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int sumE=0,sumO=0;
int n;
cin>>n;
for(int j=0;j<n;j++)
{
int temp;
cin>>temp;
if(j%2==0)
{
sumE+=temp;
}
else
{
sumO+=temp;
}
}
cout<<(sumE>sumO?sumE:sumO)<<endl;
}
return 0;
}
```

The content uploaded on this website is for reference purposes only. Please do it yourself first. |