Simple Prefix sums solution O(n) Time


#1
int Solution::solve(vector<int> &A, int B) {
    for(int i=1;i<(int)A.size();i++){
        A[i]+=A[i-1];
    }
    int n = A.size();
    int ans = INT_MIN;
    for(int i=0;i<=B;i++){
        ans = max(ans, (i>0?A[i-1]:0) + A[n-1]-A[n-1-B+i]);
    }
    return ans;
}