Run error in code

#include
using namespace std;

define ll long long

int main()
{
ll n,k;
ll dp[2000005],mx[2000005],a[2000005];
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
dp[0]=a[0];
mx[0]=a[0];
for(int i=1;i<n;i++)
{
if(i<k)
{
dp[i]=a[i];
}
else
{
dp[i] = mx[i-k] + a[i];
}
mx[i] = max(dp[i],mx[i-1]);
}

cout<<max(0LL,mx[n-1])<<"\n";
return 0;

}

Why is this code throwing error ? Is it conceptually wrong ?

@O17CPBTC0021 hey ,yes your logic is not upto mark ,you simply need to find a max from every k elements and add it to the sum. This can be accomplished using the following very simple code.

Ok ; I get it my concept was wrong but why did code cause segmetation fault ?

@O17CPBTC0021 hey segmentation fault occurs when you try to acess the element which is not in memory so apne array ke us index ko acess kia hoga jo valid nhi tha pls check that.

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.