Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Flip Flop Sum

#include <bits/stdc++.h>
using namespace std;

int solve(vector<int> &a) {
    int n = a.size();

    int sum = accumulate(a.begin(), a.end(), 0);

    bool atleast_one_minus = false;
    for (int i = 0; i < n - 1; i++) {
        if (a[i] == -1 && a[i + 1] == -1) {
            return sum + 4;
        }
    }

    sort(a.begin(), a.end());
    if (a.front() == -1) {
        return sum;
    } else {
        return sum - 4;
    }
}

int main() {
    int t;
    cin >> t;

    for (int zz = 0; zz < t; zz++) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        cout << solve(a) << "\n";
    }
    return 0;
}