This program basically checks the two ends of a given interger sequence, adds the greatest of the two to R and changes the sign of the end we didn't choose. Repeats the process until there's only one number left (which is not add to R). The first line of the input specifies the quantity of intergers in the sequence and the others left is the sequence itself.
For example, if we input "5 5 4 3 2 1", we should get "14", because only the "1" doesn't get add to R.
For some reason when I input "5 -5 -4 -3 -2 -1" I'm getting an output of "10" instead of "-10".
#include <iostream>
using namespace std;
int main(void) {
int N, *L, R = 0, i = 0, d = 0;
cin >> N;
L = new int[N];
for (; i < N; ++i) cin >> L[i];
i = 0;
d = N - 1;
while (d != i) {
if (L[i] > L[d]){
R += L[i];
L[d] *= -1;
++i;
}
else {
R += L[d];
L[i] *= -1;
--d;
}
}
cout << R << endl;
return 0;
}`