spoj street parade runtime error

problem link : https://www.spoj.com/problems/STPAR

i do not know why i M GETTING runtime error in my problem can you please tell me the problem

#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>

using namespace std;

int main()
{
   int i,j,n;
   stack<int> s;

   while(1)
  {
      cin>>n;
     // queue<int> q;

     if(n == 0)
        break;

     int a[n];

      for(i=0;i<n;i++)
        cin>>a[i];

      j = 1;

      for(int k=0;k<n;k++)
      {
          if(a[k] != j)
         {
            if(s.size()!=0)
            {
                if(s.top() == j)
                {
                    while(s.top() == j && s.size()!=0)
                    {
                        //cout<<"med"<<j<<" "<<a[k]<<"\n";
                        //q.push(j);
                        s.pop();
                        j++;
                    }
                    s.push(a[k]);
                }
                else
                {
                    //cout<<a[k]<<"\n";
                    s.push(a[k]);
                }
             }
             else
             {
                //cout<<a[k]<<"\n";
                s.push(a[k]);
             }
         }
         else
         {
            //cout<<"#"<<j<<"\n";
            //q.push(a[k]);
            j++;
         }
     }

    while(true)
    {
       if(s.empty()==true)
			break;
		if(j!=s.top())
		{
			/*printsp_d(temp);
			println_d(stk.top());*/
			//f=0;
			break;
		}
		else
		{
			/*puts("Stack");
			println_d(stk.top());*/
			j++;
			s.pop();
		}
    }

    //cout<<"222222";

    if(s.size() == 0)
        cout<<"yes\n";
    else
        cout<<"no\n";

    while(!s.empty())
        s.pop();

   }
   return 0;
}
//