Wrong answer showing up in ENDCORR

Need help for https://www.codechef.com/IARCSJUD/problems/ENDCORR

The result: https://drive.google.com/file/d/1JSUJQKEo0XyHdQ2pe6oc1IZGdkiaLi4m/view?usp=sharing

Here is my code:

#include <iostream>
using namespace std;
int main() {
int N=0,M=0,max=0,maxpos=0;
cin>>N>>M;
int A[M+N];
for(int i=0;i<M+N;i++)
{
cin>>A[i];
}
for(int i=0;i<M+N;i++)
{
if(A[i]==-1)
{
for(int j=0;j<i;j++)
{
if(A[j]>max)
{
max=A[j];
maxpos=j;
}
}
cout<<max<<endl;
max=0;
for(int k=maxpos;k<M+N-1;k++)
{
A[k]=A[k+1];
}
}
}
return 0;
}

Where is my mistake? I had run the program using the sample input as given in the problem page manually and the answer matched with the sample output. Where am I going wrong? Please help.