Please check my code

#include<bits/stdc++.h>
using namespace std;
int firstMax=INT_MIN,secondMax = INT_MIN;
int fMaxIdx,sMaxIdx;
void UpdateVal(int st,int ed,int a[]){

for (int i = st; i <=ed ; i ++) 
{ 
    if (a[i] > firstMax) 
    { 
        secondMax = firstMax; 
        firstMax = a[i]; 
        fMaxIdx=i;
    } 
    else if (a[i] > secondMax && a[i] != firstMax) {
        secondMax = a[i]; 
        sMaxIdx=i;
    }
}

}
int main(){
int n,k;
cin>>n>>k;
int a[n+1];
for(int i=1;i<=n;i++){
cin>>a[i];
}
int dp[n+1]={0};
UpdateVal(1,k,a);
for(int i=1;i<=k;i++){
dp[i]=a[i];
}
for(int i=k+1;i<=n;i++){
if(i%k==0){
firstMax=INT_MIN,secondMax = INT_MIN;
UpdateVal(i,i+k,a);
}
dp[i]=(abs(i-fMaxIdx)!=k)?a[i]+firstMax:(abs(i-sMaxIdx)!=k)?a[i]+secondMax:a[i];
}
int ans=INT_MIN;
for(int i=1;i<=n;i++){
ans=max(ans,dp[i]);
}
cout<<ans;
}

@Nikhil-AngRaal-1800886836633817 please provide your code in coding blocks IDE.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.