Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[int(i) for i in input().split()]
def Check(K):
  stack=[]
  n=A[0]
  for i in range(1,N):
    if A[i-1]<A[i]:
      n=A[i]
    else:
      if K==1:
        return False
      while n>A[i]:
        if stack:
          if stack[-1][0]==n:
            stack.pop()
        n-=1
      k=n
      while k>=1:
        if not stack:
          stack.append([k,1])
          break
        if stack[-1][0]==k:
          if stack[-1][1]==K-1:
            if k!=1:
              stack.pop()
              k-=1
            else:
              return False
          else:
            stack[-1][1]+=1
            break
        else:
          stack.append([k,1])
          break
  return True
l,r=1,N
while l<r:
  m=(l+r)//2
  if Check(m):
    r=m
  else:
    l=m+1
print(l)
          
          
          
      

Fixed submission:
N=int(input())
A=[int(i) for i in input().split()]
def Check(K):
  stack=[]
  n=A[0]
  for i in range(1,N):
    if A[i-1]<A[i]:
      n=A[i]
    else:
      if K==1:
        return False
      n=A[i]
      while True:
        l=0
        if stack:
          l=stack[-1][0]
        if l>n:
          stack.pop()
        else:
          break       
      k=n
      while k>=1:
        if not stack:
          stack.append([k,1])
          break
        if stack[-1][0]==k:
          if stack[-1][1]==K-1:
            if k!=1:
              stack.pop()
              k-=1
            else:
              return False
          else:
            stack[-1][1]+=1
            break
        else:
          stack.append([k,1])
          break
  return True
l,r=1,N
while l<r:
  m=(l+r)//2
  if Check(m):
    r=m
  else:
    l=m+1
print(l)

Buggy python submission (verdict: Runtime Error):
N=int(input())
A=[int(i) for i in input().split()]
def Check(K):
  stack=[]
  n=A[0]
  for i in range(1,N):
    if A[i-1]<A[i]:
      n=A[i]
    else:
      while n>A[i]:
        if stack:
          if stack[-1][0]==n:
            stack.pop()
        n-=1
      r=1
      while True:
        if not stack:
          if K==1:
            return False
          stack.append([n+1-r,1])
          break
        elif r==n:
          if stack[0][1]==K-1:
            return False
          else:
            stack[0][0]+=1
            break
        elif stack[(-1)*r][0]<n+1-r:
          if K==1:
            return False
          stack.append([n+1-r,1])
          break
        elif stack[(-1)*r][1]<K-1:
          stack[(-1)*r][1]+=1
          break
        elif stack[(-1)*r][1]==K-1:
          stack.pop()
          r+=1
  return True
l,r=1,N
while l<r:
  m=(l+r)//2
  if Check(m):
    r=m
  else:
    l=m+1
print(l)
          
          
          
      

Fixed submission:
N=int(input())
A=[int(i) for i in input().split()]
def Check(K):
  stack=[]
  n=A[0]
  for i in range(1,N):
    if A[i-1]<A[i]:
      n=A[i]
    else:
      if K==1:
        return False
      n=A[i]
      while True:
        l=0
        if stack:
          l=stack[-1][0]
        if l>n:
          stack.pop()
        else:
          break       
      k=n
      while k>=1:
        if not stack:
          stack.append([k,1])
          break
        if stack[-1][0]==k:
          if stack[-1][1]==K-1:
            if k!=1:
              stack.pop()
              k-=1
            else:
              return False
          else:
            stack[-1][1]+=1
            break
        else:
          stack.append([k,1])
          break
  return True
l,r=1,N
while l<r:
  m=(l+r)//2
  if Check(m):
    r=m
  else:
    l=m+1
print(l)