Language: C++

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#define REP(i,n) for(int(i)=0;(i)<(int)(n);(i)++)
#define REPN(i,n) for(int(i)=1;(i)<(int)(n);(i)++)
#define _abs(x) ((x)>0?(x):-(x))
#define ll  long long 
using namespace std;    
int main()
{ 
    int n,ans,sum,cnt=0,s=0,p1[10000],p2[10000];
    cin>>n;
    int h[n],a[n];
    REP(i,n)
    {
        cin>>h[i];
        a[i]=h[i];
    }
    REP(i,n)
    {
        if(h[i]==0)
        {
                s++;
                p1[i]=i;
        }
    }
    REP(i,n)
    {
        if(h[i]!=0)
        {
                sum=h[i];
                p2[i]=i;
        }
    }
    REP(i,n)
    {
        if(h[i]<sum&&h[i]>0)
        {
                sum=h[i];
                p2[i]=i;
        }
    }
    if(s==1)
    {
        if(p2[0]<p1[0])
        {
                REP(i,p1[0])
                {
                                a[i]=h[i]-sum;
                }
        }
        else
        {
                for(int i=p1[0]+1;i<n;i++)
                {
                                a[i]=h[i]-sum;
                }
        }
    }
    else if(s==0)
    {
        REP(i,n)
        {
                a[i]=h[i]-sum;
        }
    }
    else
    {
        REP(i,s-1)
        {
                if(p2[0]<p1[i+1]&&p2[0]>p1[i])
                {
                                for(int i=p1[0];i<p1[s];i++) 
                                {
                                                                a[i]=h[i]-sum;
                                }
                }
        }
    }
    s=0;
    ans=0;
    sum=0;
    cnt=0;
    memset(p1,0,sizeof(p1));
    memset(p2,0,sizeof(p2));
    while(1!=2)
{
    REP(i,n)
    {
        if(a[i]==0)
        {
                s++;
                p1[i]=i;
        }
    }
    REP(i,n)
    {
        if(a[i]!=0)
        {
                sum=a[i];
                p2[i]=i;
        }
    }
    REP(i,n)
    {
        if(a[i]<sum&&h[i]>0)
        {
                sum=a[i];
                p2[i]=i;
        }
    }
    if(s==1)
    {
        if(p2[0]<p1[0])
        {
                REP(i,p1[0])
                {
                                a[i]=a[i]-sum;
                }
        }
        else
        {
                for(int i=p1[0]+1;i<n;i++)
                {
                                a[i]=a[i]-sum;
                }
        }
    }
    else if(s==0)
    {
        REP(i,n)
        {
                a[i]=a[i]-sum;
        }
    }
    else
    {
        REP(i,s-1)
        {
                if(p2[0]<p1[i+1]&&p2[0]>p1[i])
                {
                                for(int i=p1[0];i<p1[s];i++) 
                                {
                                                                a[i]=a[i]-sum;
                                }
                }
        }
    }
    cnt++;
}
    cout<<cnt<<endl; 
    //system("pause");
    return 0;
} 

Compiler output:
a.cc: In function 'int main()':
a.cc:103:5: error: 'memset' was not declared in this scope
  103 |     memset(p1,0,sizeof(p1));
      |     ^~~~~~
a.cc:23:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
   22 | #include <iterator>
  +++ |+#include <cstring>
   23 | #define REP(i,n) for(int(i)=0;(i)<(int)(n);(i)++)