C++: using stack


#1
int Solution::solve(vector<int> &A, int B) {

int n=A.size();
if(n<B)
{
    return 0;
}
stack<int>st;
int sum=0;
for(int i=0;i<B;i++)
{
    sum+=A[i];
    st.push(A[i]);
}
int curr=sum;
for(int i=n-1;i>=n-B;i--)
{
    int t=st.top();
    st.pop();
    curr=curr-t;
    curr=curr+A[i];
    sum=max(sum,curr);
}
return sum;

}