Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
732 B
0
Indexable
Never
long solve(vector<int> &pnl, int k){
    int n = pnl.size();
    long ans = pnl[0];
    long curr = 0;
    int start = 0;
    for(int i=0;i<n;i++){
        if(curr+pnl[i]>0){
            curr+=pnl[i];
            if(i-start+1<=k){
                ans = max(ans, curr);
            }else{
                curr-=pnl[start];
                ans = max(ans,curr);
            }
        }else{
            if(pnl[i]>ans){
                ans = pnl[i];
            }
            curr = 0;
            start = i+1;
        }
    }
    return ans;
}
int main()
{
    int n,k;
    cin>>n>>k;
    vector<int> pnl(n);
    for(int i=0;i<n;i++){
        cin>>pnl[i];
    }
    cout<<solve(pnl,k);
    return 0;
}