Could You Please review the Code Where I am getting Wrong

#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
int *arr=new int[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int *dp=new int[n];

int max_1=0;
int max_2=0;
int index=-1;
for(int i=0;i<k;i++)
{
    if(arr[i]>max_1)
    {
        max_2=max_1;
        max_1=arr[i];
        index=i;
    }

    else if(arr[i]>max_2)
    {
        max_2=arr[i];
    }
    dp[i]=arr[i];
}
if(max_1<0)
{
    max_1=0;
}

if(max_2<0)
{
    max_2=0;
}

int s=k;
int e=n;
int start_index;
int end=k;
while(s<e)
{
        start_index=end;
        end=end+k;
    for(int z=(start_index);z<end;z++)
    {

        if((abs(z-index)!=k) && (arr[z]>=0))
        {
           arr[z]+=max_1; 
        }

        else if((abs(z-index)==k) && (arr[z]>=0))
        {
            arr[z]+=max_2;
        }

        else if(arr[z]<0 && (abs(z-index)!=k))
        {
            arr[z]=max_1;
        }

        else
            arr[z]=max_1;

    }

    int temp_max_1=arr[start_index];
    int temp_max_2=arr[start_index];
    int temp_index=start_index;
    for(int l=(start_index+1);l<end;l++)
    {
        if(arr[l]>=temp_max_1)
        {
            temp_max_2=temp_max_1;
            temp_max_1=arr[l];
            temp_index=l;
        }
    }

    if(temp_max_1>0)
        {
            max_1=temp_max_1;
            index=temp_index;
        }

    if(temp_max_2)
        max_2=temp_max_2;
    
    s=end;
}

int o=n/k;
int p_start=o*k;
int maxo=arr[p_start];
for(;p_start<n;p_start++)
{
if(arr[p_start]>maxo)
{
maxo=arr[p_start];
}
}
cout<<maxo;

return 0;
}

Hey I have Checked your solution. You seem to have over complicated it. You simply need to find a max from every k elements and add it to the sum if that indexed max wasn’t added from the previous k elements otherwise add second max. This can be accomplished using the following very sinple code.

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<long> a(n);
    for(auto &i : a)
        cin >> i;
    long maxSum = 0;
    vector<long> dp = a;
    for(int i = k; i < n; i += k) {
        int max_i = i - k, smax_i = -1;
        for(int j = i - k + 1; j < i; j++) {
            if(dp[j] > dp[max_i]) {
                smax_i = max_i;
                max_i = j;
            }
            else if(smax_i == -1 or dp[j] > dp[smax_i]) {
                smax_i = j;
            }
        }
        for(int j = i; j < i + k and j < n; j++) {
			if(a[j] < 0) dp[j] = dp[max_i];
            else if(j - max_i != k) dp[j] = a[j] + dp[max_i];
            else dp[j] = a[j] + dp[smax_i];
            maxSum = max(maxSum, dp[j]);
        }
    }
    cout << maxSum;
}

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.