Getting wrong answer on STPAR

Question link


`

#This is my solution
while True:
n=int(input())
if n==0:
break
a=list(map(int,input().split()))
z=sorted(a)
m=0
b,c=[],[]
for i in range(0,n):
if a[i]==z[m]:
b.append(a[i])
m+=1
elif z[m] in c:
b.append(c.pop(c.index(z[m])))
c.append(a[i])
m+=1
else:
c.append(a[i])
b.extend(c[::-1])
if b==sorted(a):
print(‘yes’)
else:
print(‘no’)