WA in Princess Farida problem

Even after passing all the test cases ,I am getting a WA in the problem and I am unable to figure out the error

  long long int a[1000000],dp[1000000];
  int main()
    {
    long long int t;
    cin>>t;
   long long int n,i;
   long long int j=1;
   while(t--)
    {
    cin>>n;
    for(i=0;i<n;i++)
      {
      cin>>a[i];
      }
    dp[0]=a[0];
    dp[1]=max(a[0],a[1]);
    for(i=2;i<n;i++)
      {
      dp[i]=max(dp[i-1],a[i]+dp[i-2]);
      }
    if(n==0)
    cout<<"Case "<<j<<": 0"<<"\n";
    else
    cout<<"Case "<<j<<": "<<dp[i-1]<<"\n";
    j++;
    }
   return 0;
  }

The problem is with memory management. The base case is executed always irrespective of the value of N which fails it in cases like

2

6

5 3 4 8 6 2

1

0

Your Output:

Case 1: 15

Case 2: 3

Just add another condition that resets all elements to a value that is smaller than the amount of money any monster could ever have (e.g -1)

You can use memset and set all the elements to a default negative infinity
{ add memset(a,-1,sizeof(-1)); in your while loop}

or

You can use a for loop.

PS: If it time outs try reducing your array sizes. They are much higher than the specified constraints.

//