Readable and easy to understand solution


#1
struct Subarray {
    int start = -1;
    int len = 0;
    long long sum = 0;
    
    void include(int val) {
        ++len;
        sum += val;
    }
};

// is a better result than b
bool better(Subarray a, Subarray b) {
    if (a.sum == b.sum)
        return a.len == b.len ? a.start < b.start : a.len > b.len;
        
    return a.sum > b.sum;
}

vector<int> Solution::maxset(vector<int> &A) {
    Subarray result;
    optional<Subarray> current;
    
    for (int i = 0; i < A.size(); ++i)
    {
        if (A[i] < 0)
        {
            // encountered a negative number, reset current
            current.reset();
        }
        else
        {
            if (current)
            {
                // include A[i] in the current subarray
                current->include(A[i]);
            }
            else
            {
                // start current subaray with A[i]
                current = {i, 1, A[i]};
            }
            
            if (better(*current, result))
            {
                // if current is better than existing result, update result
                result = *current;
            }
        }
    }
    
    return vector<int>(A.begin() + result.start, 
                       A.begin() + result.start + result.len);
}