Sasha is taking part in a programming competition. In one of the problems she should check if some rooted trees are isomorphic or not. She has never seen this problem before, but, being an experienced participant, she guessed that she should match trees to some sequences and then compare these sequences instead of trees. Sasha wants to match each tree with a sequence a_0, a_1, ..., a_{h}, where h is the height of the tree, and a_{i} equals to the number of vertices that are at distance of i edges from root. 

Unfortunately, this time Sasha's intuition was wrong, and there could be several trees matching the same sequence. To show it, you need to write a program that, given the sequence a_{i}, builds two non-isomorphic rooted trees that match that sequence, or determines that there is only one such tree.

Two rooted trees are isomorphic, if you can reenumerate the vertices of the first one in such a way, that the index of the root becomes equal the index of the root of the second tree, and these two trees become equal.

The height of a rooted tree is the maximum number of edges on a path from the root to any other vertex.


-----Input-----

The first line contains a single integer h (2 ≤ h ≤ 10^5) — the height of the tree.

The second line contains h + 1 integers — the sequence a_0, a_1, ..., a_{h} (1 ≤ a_{i} ≤ 2·10^5). The sum of all a_{i} does not exceed 2·10^5. It is guaranteed that there is at least one tree matching this sequence.


-----Output-----

If there is only one tree matching this sequence, print "perfect".

Otherwise print "ambiguous" in the first line. In the second and in the third line print descriptions of two trees in the following format: in one line print $\sum_{i = 0}^{h} a_{i}$ integers, the k-th of them should be the parent of vertex k or be equal to zero, if the k-th vertex is the root.

These treese should be non-isomorphic and should match the given sequence.


-----Examples-----
Input
2
1 1 1

Output
perfect

Input
2
1 2 2

Output
ambiguous
0 1 1 3 3
0 1 1 3 2



-----Note-----

The only tree in the first example and the two printed trees from the second example are shown on the picture:

$88$

Source: codeforces | Difficulty: MEDIUM
Tags: Tree algorithms

Constructive algorithms


Solutions:
import sys
f = sys.stdin
n = f.readline()
n = int(n)
cl = f.readline().split()
cl = [int(x) for x in cl]
c_index = 0
p1 = ['0']
p2 = ['0']
ambiguous = False
cur_index = 1
for (i, c) in enumerate(cl):
	if i == 0:
		continue
	if i > 0 and cl[i - 1] > 1 and (c > 1):
		ambiguous = True
		p1 += [str(cur_index)] * c
		p2 += [str(cur_index - 1)] + [str(cur_index)] * (c - 1)
	else:
		p1 += [str(cur_index)] * c
		p2 += [str(cur_index)] * c
	cur_index += c
if ambiguous:
	print('ambiguous')
	print(' '.join(p1))
	print(' '.join(p2))
else:
	print('perfect')


n = int(input())
a = [int(i) for i in input().split()]
t1 = []
alert = 0
record = 0
for i in range(n + 1):
	if a[i] != 1:
		if alert == 1:
			t1.append(0)
			record = i
			break
		else:
			alert = 1
	else:
		alert = 0
if t1 == []:
	print('perfect')
	exit(0)
print('ambiguous')
index = 1
for x in a[1:]:
	for i in range(x):
		t1.append(index)
	index += x
t2 = t1[:]
change = sum(a[:record])
i = t1.index(change)
t2[i] -= 1
print(*t1)
print(*t2)


def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def gen(r):
	p = q = 1
	t = [0]
	for i in range(h):
		pp = p
		p = len(t) + 1
		if r and i and (min(a[i], a[i - 1]) > 1):
			t += [q]
			t += [pp] * (a[i] - 1)
		else:
			t += [pp] * a[i]
		q = len(t)
	return t
h = ii()
a = li()[1:]
(t1, t2) = (gen(0), gen(1))
if t1 == t2:
	print('perfect')
else:
	print('ambiguous')
	print(*t1)
	print(*t2)


h = int(input())
a = list(map(int, input().split()))
for i in range(h):
	if a[i] != 1 and a[i + 1] != 1:
		I = i
		f = False
		break
else:
	f = True
if f:
	print('perfect')
else:
	print('ambiguous')
	(ans1, ans2) = ([0], [0])
	last_el = 1
	last_i = 0
	f = False
	for (i, el) in enumerate(a[1:]):
		for k in range(el):
			ans1.append(last_el)
			if k != el - 1:
				ans2.append(last_el)
		if i == I:
			ans2.append(last_el + 1)
			f = True
		else:
			ans2.append(last_el)
		last_i += a[i]
		last_el = last_i + 1
	print(' '.join(map(str, ans1)))
	print(' '.join(map(str, ans2)))


h = int(input())
a = list(map(int, input().split()))
perfect = True
for i in range(1, len(a)):
	if a[i] != 1 and a[i - 1] != 1:
		perfect = False
print('perfect' if perfect else 'ambiguous')
ret1 = [0] * sum(a)
ret2 = [0] * sum(a)
node = 0
p = 0
if perfect == False:
	for i in range(len(a)):
		for j in range(a[i]):
			ret1[node] = p
			node += 1
		p = node
	p1 = 0
	p2 = 0
	node = 0
	for i in range(len(a)):
		for j in range(a[i]):
			if j & 1:
				ret2[node] = p1
			else:
				ret2[node] = p2
			node += 1
		p1 = node
		p2 = node
		if a[i] != 1:
			p1 = node
			p2 = node - 1
	print(' '.join((str(x) for x in ret1)))
	print(' '.join((str(x) for x in ret2)))


h = int(input())
a = list(map(int, input().split()))
perfect = True
for i in range(h):
	if a[i] > 1 and a[i + 1] > 1:
		perfect = False
		break
if perfect:
	print('perfect')
else:
	print('ambiguous')
	tree1 = list(range(h + 1))
	tree2 = list(tree1)
	for i in range(1, h + 1):
		tree1.extend([i] * (a[i] - 1))
	for i in range(1, h + 1):
		p = i
		if a[i] > 1 and a[i - 1] > 1:
			p = len(tree2)
		tree2.extend([p] * (a[i] - 1))
	print(' '.join(map(str, tree1)))
	print(' '.join(map(str, tree2)))


h = int(input())
A = list(map(int, input().split()))
f = False
for (i, a) in enumerate(A):
	if f and a != 1:
		f = None
		break
	else:
		f = a != 1
if f is not None:
	print('perfect')
else:
	T = []
	for (j, a) in enumerate(A):
		if j == i:
			x = len(T)
		T += [len(T)] * a
	print('ambiguous')
	print(' '.join(map(str, T)))
	T[x + 1] = x - 1
	print(' '.join(map(str, T)))


h = int(input())
a = list(map(int, input().split()))
(p, c, f) = (0, 0, False)
(s1, s2) = ([], [])
for i in a:
	for j in range(i):
		s1.append(c)
		if j == i - 1 and (not f) and (p >= 2) and (i >= 2):
			f = True
			s2.append(c - 1)
		else:
			s2.append(c)
	p = i
	c += i
print('perfect' if not f else 'ambiguous')
if f:
	print(' '.join(map(str, s1)))
	print(' '.join(map(str, s2)))


h = int(input())
a = list(map(int, input().split()))
f = False
for i in range(h):
	if a[i] > 1 and a[i + 1] > 1:
		f = True
if not f:
	print('perfect')
else:
	cur = 0
	cur2 = 0
	x = []
	y = []
	bruh = False
	for i in range(h + 1):
		if i and a[i] > 1 and (a[i - 1] > 1) and (bruh == False):
			x.append(cur2)
			for j in range(a[i] - 1):
				x.append(cur)
			bruh = True
		else:
			for j in range(a[i]):
				x.append(cur)
		for j in range(a[i]):
			y.append(cur)
		cur = len(x)
		cur2 = len(x) - 1
	print('ambiguous')
	for i in x:
		print(i, end=' ')
	print('')
	for i in y:
		print(i, end=' ')


I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
ans = 'perfect'
f = 0
temp = -1
node = 1
for i in range(n):
	if min(l[i], l[i + 1]) > 1:
		ans = 'ambiguous'
		temp = i + 1
		f = 1
		break
if not f:
	print(ans)
else:
	a = '0'
	b = '0'
	for i in range(1, n + 1):
		a += (' ' + str(node)) * l[i]
		if i != temp:
			b += (' ' + str(node)) * l[i]
		else:
			b += ' ' + str(node - 1) + (' ' + str(node)) * (l[i] - 1)
		node += l[i]
	print(ans)
	print(a)
	print(b)


n = int(input())
a = [int(i) for i in input().split()]
g = [0] * (n + 1)
for i in range(1, n + 1):
	g[i] = g[i - 1] + a[i - 1]
ans = [0] + [g[i - 1] + 1 for i in range(1, n + 1)]
idx = 0
for i in range(1, n + 1):
	if a[i] > 1 and a[i - 1] > 1:
		idx = i
if idx == 0:
	print('perfect')
else:
	print('ambiguous')
	k = ''
	for i in range(n + 1):
		for j in range(a[i]):
			k += ' ' + str(ans[i])
	print(k[1:])
	k = ''
	for i in range(n + 1):
		for j in range(idx == i, a[i]):
			k += ' ' + str(ans[i])
		if idx == i:
			k += ' ' + str(ans[i] + 1)
	print(k[1:])


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	return [[default for j in range(m)] for i in range(n)]

def arr3D(n, m, r, default=0):
	return [[[default for k in range(r)] for j in range(m)] for i in range(n)]

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

class DSU:

	def __init__(self, length):
		self.length = length
		self.parent = [-1] * self.length

	def getParent(self, node, start):
		if node >= self.length:
			return False
		if self.parent[node] < 0:
			if start != node:
				self.parent[start] = node
			return node
		return self.getParent(self.parent[node], start)

	def union(self, node1, node2):
		parent1 = self.getParent(node1, node1)
		parent2 = self.getParent(node2, node2)
		if parent1 == parent2:
			return False
		elif self.parent[parent1] <= self.parent[parent2]:
			self.parent[parent1] += self.parent[parent2]
			self.parent[parent2] = parent1
		else:
			self.parent[parent2] += self.parent[parent1]
			self.parent[parent1] = parent2
		return True

	def getCount(self, node):
		return -self.parent[self.getParent(node, node)]

def exact(num):
	if abs(num - round(num)) <= 10 ** (-9):
		return round(num)
	return num

def solve(n, lst):
	get = False
	ind = -1
	for i in range(n):
		if lst[i] > 1 and lst[i + 1] > 1:
			get = True
			ind = i
			break
	if get:
		print('ambiguous')
		pre = [lst[0]]
		for i in lst[1:]:
			pre.append(pre[-1] + i)
		ans1 = [0]
		for i in range(lst[1]):
			ans1.append(1)
		prei = 0
		for i in lst[2:]:
			ans1 += [pre[prei] + 1] * i
			prei += 1
		print(' '.join(list(map(str, ans1))))
		val = pre[ind] + 1
		if val in ans1:
			for i in range(len(ans1)):
				if ans1[i] == val:
					ans1[i - 1] += 1
		else:
			ans1[-1] += 1
		return ' '.join(list(map(str, ans1)))
	else:
		return 'perfect'
n = int(input())
lst = list(map(int, input().split()))
print(solve(n, lst))


def solve():
	h = get(int)
	A = [1] + get([int])
	t1 = []
	t1p = 0
	t2 = []
	t2p = 0
	perfect = True
	for (a, b) in zip(A, A[1:]):
		t1.extend([t1p] * b)
		if a > 1 and b > 1:
			perfect = False
			t2.extend([t2p] * (b - 1) + [t2p + 1])
		else:
			t2.extend([t2p] * b)
		t1p += a
		t2p += a
	if perfect:
		return 'perfect'
	return '{}\n{}\n{}'.format('ambiguous', ' '.join(map(str, t1)), ' '.join(map(str, t2)))
_testcases = '\n2\n1 1 1\n\nperfect\n\n\n\n2\n1 2 2\n\nambiguous\n0 1 1 3 3\n0 1 1 3 2\n\n\n\n3\n1 2 1 2\n\nperfect\n\n\n\n3\n1 1 2 2\n\nambiguous\n'.strip()
from bisect import bisect_left, bisect_right
from collections import defaultdict
from functools import lru_cache
from heapq import heapify, heappop, heappush
from operator import itemgetter, attrgetter
import bisect
import collections
import functools
import heapq
import itertools
import math
import operator
import re
import string
import sys
inf = float('inf')
cache = lru_cache(None)
sys.setrecursionlimit(10000)

def tree():
	return collections.defaultdict(tree)

def equal(x, y, epsilon=1e-06):
	if -epsilon <= x - y <= epsilon:
		return True
	if -epsilon <= x <= epsilon or -epsilon <= y <= epsilon:
		return False
	return -epsilon <= (x - y) / x <= epsilon or -epsilon <= (x - y) / y <= epsilon

def get(_type):
	if type(_type) == list:
		if len(_type) == 1:
			_type = _type[0]
			return list(map(_type, input().strip().split()))
		else:
			return [_type[i](inp) for (i, inp) in enumerate(input().strip().split())]
	else:
		return _type(input().strip())
printRecursionTree = timeit = lambda x: x
(_p, print) = (print, lambda *a, **b: None)
_p(solve())


import sys
sys.setrecursionlimit(1000000)
read = sys.stdin.readline
h = int(read())
ds = list(map(int, read().split()))
amb = False
for i in range(h):
	if ds[i] > 1 and ds[i + 1] > 1:
		amb = True
		break
if amb:
	print('ambiguous')
	tree1 = []
	tree2 = []
	(f, l) = (0, 0)
	for d in ds:
		tree1 += [f] * d
		tree2 += [f] * (d // 2) + [l] * (d - d // 2)
		l += d
		f = l - d + 1
	print(' '.join(map(str, tree1)), ' '.join(map(str, tree2)), sep='\n')
else:
	print('perfect')


n = int(input())
a = list(map(int, input().split()))
p = []
q = []
r = 0
ls = -1
for i in a:
	for j in range(i):
		p.append(r)
		if j:
			q.append(r - (ls > 1))
		else:
			q.append(r)
	r += i
	ls = i
if p == q:
	print('perfect')
else:
	print('ambiguous')
	print(*p)
	print(*q)


def divisors(M):
	d = []
	i = 1
	while M >= i ** 2:
		if M % i == 0:
			d.append(i)
			if i ** 2 != M:
				d.append(M // i)
		i = i + 1
	return d

def popcount(x):
	x = x - (x >> 1 & 1431655765)
	x = (x & 858993459) + (x >> 2 & 858993459)
	x = x + (x >> 4) & 252645135
	x = x + (x >> 8)
	x = x + (x >> 16)
	return x & 127

def eratosthenes(n):
	res = [0 for i in range(n + 1)]
	prime = set([])
	for i in range(2, n + 1):
		if not res[i]:
			prime.add(i)
			for j in range(1, n // i + 1):
				res[i * j] = 1
	return prime

def factorization(n):
	res = []
	for p in prime:
		if n % p == 0:
			while n % p == 0:
				n //= p
			res.append(p)
	if n != 1:
		res.append(n)
	return res

def euler_phi(n):
	res = n
	for x in range(2, n + 1):
		if x ** 2 > n:
			break
		if n % x == 0:
			res = res // x * (x - 1)
			while n % x == 0:
				n //= x
	if n != 1:
		res = res // n * (n - 1)
	return res

def ind(b, n):
	res = 0
	while n % b == 0:
		res += 1
		n //= b
	return res

def isPrimeMR(n):
	d = n - 1
	d = d // (d & -d)
	L = [2, 3, 5, 7, 11, 13, 17]
	for a in L:
		t = d
		y = pow(a, t, n)
		if y == 1:
			continue
		while y != n - 1:
			y = y * y % n
			if y == 1 or t == n - 1:
				return 0
			t <<= 1
	return 1

def findFactorRho(n):
	from math import gcd
	m = 1 << n.bit_length() // 8
	for c in range(1, 99):
		f = lambda x: (x * x + c) % n
		(y, r, q, g) = (2, 1, 1, 1)
		while g == 1:
			x = y
			for i in range(r):
				y = f(y)
			k = 0
			while k < r and g == 1:
				ys = y
				for i in range(min(m, r - k)):
					y = f(y)
					q = q * abs(x - y) % n
				g = gcd(q, n)
				k += m
			r <<= 1
		if g == n:
			g = 1
			while g == 1:
				ys = f(ys)
				g = gcd(abs(x - ys), n)
		if g < n:
			if isPrimeMR(g):
				return g
			elif isPrimeMR(n // g):
				return n // g
			return findFactorRho(g)

def primeFactor(n):
	i = 2
	ret = {}
	rhoFlg = 0
	while i * i <= n:
		k = 0
		while n % i == 0:
			n //= i
			k += 1
		if k:
			ret[i] = k
		i += 1 + i % 2
		if i == 101 and n >= 2 ** 20:
			while n > 1:
				if isPrimeMR(n):
					(ret[n], n) = (1, 1)
				else:
					rhoFlg = 1
					j = findFactorRho(n)
					k = 0
					while n % j == 0:
						n //= j
						k += 1
					ret[j] = k
	if n > 1:
		ret[n] = 1
	if rhoFlg:
		ret = {x: ret[x] for x in sorted(ret)}
	return ret

def divisors(n):
	res = [1]
	prime = primeFactor(n)
	for p in prime:
		newres = []
		for d in res:
			for j in range(prime[p] + 1):
				newres.append(d * p ** j)
		res = newres
	res.sort()
	return res

def xorfactorial(num):
	if num == 0:
		return 0
	elif num == 1:
		return 1
	elif num == 2:
		return 3
	elif num == 3:
		return 0
	else:
		x = baseorder(num)
		return 2 ** x * ((num - 2 ** x + 1) % 2) + function(num - 2 ** x)

def xorconv(n, X, Y):
	if n == 0:
		res = [X[0] * Y[0] % mod]
		return res
	x = [X[i] + X[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]
	y = [Y[i] + Y[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]
	z = [X[i] - X[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]
	w = [Y[i] - Y[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]
	res1 = xorconv(n - 1, x, y)
	res2 = xorconv(n - 1, z, w)
	former = [(res1[i] + res2[i]) * inv for i in range(2 ** (n - 1))]
	latter = [(res1[i] - res2[i]) * inv for i in range(2 ** (n - 1))]
	former = list(map(lambda x: x % mod, former))
	latter = list(map(lambda x: x % mod, latter))
	return former + latter

def merge_sort(A, B):
	(pos_A, pos_B) = (0, 0)
	(n, m) = (len(A), len(B))
	res = []
	while pos_A < n and pos_B < m:
		(a, b) = (A[pos_A], B[pos_B])
		if a < b:
			res.append(a)
			pos_A += 1
		else:
			res.append(b)
			pos_B += 1
	res += A[pos_A:]
	res += B[pos_B:]
	return res

class UnionFindVerSize:

	def __init__(self, N):
		self._parent = [n for n in range(0, N)]
		self._size = [1] * N
		self.group = N

	def find_root(self, x):
		if self._parent[x] == x:
			return x
		self._parent[x] = self.find_root(self._parent[x])
		stack = [x]
		while self._parent[stack[-1]] != stack[-1]:
			stack.append(self._parent[stack[-1]])
		for v in stack:
			self._parent[v] = stack[-1]
		return self._parent[x]

	def unite(self, x, y):
		gx = self.find_root(x)
		gy = self.find_root(y)
		if gx == gy:
			return
		self.group -= 1
		if self._size[gx] < self._size[gy]:
			self._parent[gx] = gy
			self._size[gy] += self._size[gx]
		else:
			self._parent[gy] = gx
			self._size[gx] += self._size[gy]

	def get_size(self, x):
		return self._size[self.find_root(x)]

	def is_same_group(self, x, y):
		return self.find_root(x) == self.find_root(y)

class WeightedUnionFind:

	def __init__(self, N):
		self.parent = [i for i in range(N)]
		self.size = [1 for i in range(N)]
		self.val = [0 for i in range(N)]
		self.flag = True
		self.edge = [[] for i in range(N)]

	def dfs(self, v, pv):
		stack = [(v, pv)]
		new_parent = self.parent[pv]
		while stack:
			(v, pv) = stack.pop()
			self.parent[v] = new_parent
			for (nv, w) in self.edge[v]:
				if nv != pv:
					self.val[nv] = self.val[v] + w
					stack.append((nv, v))

	def unite(self, x, y, w):
		if not self.flag:
			return
		if self.parent[x] == self.parent[y]:
			self.flag = self.val[x] - self.val[y] == w
			return
		if self.size[self.parent[x]] > self.size[self.parent[y]]:
			self.edge[x].append((y, -w))
			self.edge[y].append((x, w))
			self.size[x] += self.size[y]
			self.val[y] = self.val[x] - w
			self.dfs(y, x)
		else:
			self.edge[x].append((y, -w))
			self.edge[y].append((x, w))
			self.size[y] += self.size[x]
			self.val[x] = self.val[y] + w
			self.dfs(x, y)

class Dijkstra:

	class Edge:

		def __init__(self, _to, _cost):
			self.to = _to
			self.cost = _cost

	def __init__(self, V):
		self.G = [[] for i in range(V)]
		self._E = 0
		self._V = V

	@property
	def E(self):
		return self._E

	@property
	def V(self):
		return self._V

	def add_edge(self, _from, _to, _cost):
		self.G[_from].append(self.Edge(_to, _cost))
		self._E += 1

	def shortest_path(self, s):
		import heapq
		que = []
		d = [10 ** 15] * self.V
		d[s] = 0
		heapq.heappush(que, (0, s))
		while len(que) != 0:
			(cost, v) = heapq.heappop(que)
			if d[v] < cost:
				continue
			for i in range(len(self.G[v])):
				e = self.G[v][i]
				if d[e.to] > d[v] + e.cost:
					d[e.to] = d[v] + e.cost
					heapq.heappush(que, (d[e.to], e.to))
		return d

def Z_algorithm(s):
	N = len(s)
	Z_alg = [0] * N
	Z_alg[0] = N
	i = 1
	j = 0
	while i < N:
		while i + j < N and s[j] == s[i + j]:
			j += 1
		Z_alg[i] = j
		if j == 0:
			i += 1
			continue
		k = 1
		while i + k < N and k + Z_alg[k] < j:
			Z_alg[i + k] = Z_alg[k]
			k += 1
		i += k
		j -= k
	return Z_alg

class BIT:

	def __init__(self, n, mod=0):
		self.BIT = [0] * (n + 1)
		self.num = n
		self.mod = mod

	def query(self, idx):
		res_sum = 0
		mod = self.mod
		while idx > 0:
			res_sum += self.BIT[idx]
			if mod:
				res_sum %= mod
			idx -= idx & -idx
		return res_sum

	def update(self, idx, x):
		mod = self.mod
		while idx <= self.num:
			self.BIT[idx] += x
			if mod:
				self.BIT[idx] %= mod
			idx += idx & -idx
		return

class dancinglink:

	def __init__(self, n, debug=False):
		self.n = n
		self.debug = debug
		self._left = [i - 1 for i in range(n)]
		self._right = [i + 1 for i in range(n)]
		self.exist = [True for i in range(n)]

	def pop(self, k):
		if self.debug:
			assert self.exist[k]
		L = self._left[k]
		R = self._right[k]
		if L != -1:
			if R != self.n:
				(self._right[L], self._left[R]) = (R, L)
			else:
				self._right[L] = self.n
		elif R != self.n:
			self._left[R] = -1
		self.exist[k] = False

	def left(self, idx, k=1):
		if self.debug:
			assert self.exist[idx]
		res = idx
		while k:
			res = self._left[res]
			if res == -1:
				break
			k -= 1
		return res

	def right(self, idx, k=1):
		if self.debug:
			assert self.exist[idx]
		res = idx
		while k:
			res = self._right[res]
			if res == self.n:
				break
			k -= 1
		return res

class SparseTable:

	def __init__(self, A, merge_func, ide_ele):
		N = len(A)
		n = N.bit_length()
		self.table = [[ide_ele for i in range(n)] for i in range(N)]
		self.merge_func = merge_func
		for i in range(N):
			self.table[i][0] = A[i]
		for j in range(1, n):
			for i in range(0, N - 2 ** j + 1):
				f = self.table[i][j - 1]
				s = self.table[i + 2 ** (j - 1)][j - 1]
				self.table[i][j] = self.merge_func(f, s)

	def query(self, s, t):
		b = t - s + 1
		m = b.bit_length() - 1
		return self.merge_func(self.table[s][m], self.table[t - 2 ** m + 1][m])

class BinaryTrie:

	class node:

		def __init__(self, val):
			self.left = None
			self.right = None
			self.max = val

	def __init__(self):
		self.root = self.node(-10 ** 15)

	def append(self, key, val):
		pos = self.root
		for i in range(29, -1, -1):
			pos.max = max(pos.max, val)
			if key >> i & 1:
				if pos.right is None:
					pos.right = self.node(val)
					pos = pos.right
				else:
					pos = pos.right
			elif pos.left is None:
				pos.left = self.node(val)
				pos = pos.left
			else:
				pos = pos.left
		pos.max = max(pos.max, val)

	def search(self, M, xor):
		res = -10 ** 15
		pos = self.root
		for i in range(29, -1, -1):
			if pos is None:
				break
			if M >> i & 1:
				if xor >> i & 1:
					if pos.right:
						res = max(res, pos.right.max)
					pos = pos.left
				else:
					if pos.left:
						res = max(res, pos.left.max)
					pos = pos.right
			elif xor >> i & 1:
				pos = pos.right
			else:
				pos = pos.left
		if pos:
			res = max(res, pos.max)
		return res

def solveequation(edge, ans, n, m):
	x = [0] * m
	used = [False] * n
	for v in range(n):
		if used[v]:
			continue
		y = dfs(v)
		if y != 0:
			return False
	return x

	def dfs(v):
		used[v] = True
		r = ans[v]
		for (to, dire, id) in edge[v]:
			if used[to]:
				continue
			y = dfs(to)
			if dire == -1:
				x[id] = y
			else:
				x[id] = -y
			r += y
		return r

class SegmentTree:

	def __init__(self, init_val, segfunc, ide_ele):
		n = len(init_val)
		self.segfunc = segfunc
		self.ide_ele = ide_ele
		self.num = 1 << (n - 1).bit_length()
		self.tree = [ide_ele] * 2 * self.num
		self.size = n
		for i in range(n):
			self.tree[self.num + i] = init_val[i]
		for i in range(self.num - 1, 0, -1):
			self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

	def update(self, k, x):
		k += self.num
		self.tree[k] = x
		while k > 1:
			self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
			k >>= 1

	def query(self, l, r):
		if r == self.size:
			r = self.num
		res = self.ide_ele
		l += self.num
		r += self.num
		while l < r:
			if l & 1:
				res = self.segfunc(res, self.tree[l])
				l += 1
			if r & 1:
				res = self.segfunc(res, self.tree[r - 1])
			l >>= 1
			r >>= 1
		return res

	def bisect_l(self, l, r, x):
		l += self.num
		r += self.num
		Lmin = -1
		Rmin = -1
		while l < r:
			if l & 1:
				if self.tree[l] <= x and Lmin == -1:
					Lmin = l
				l += 1
			if r & 1:
				if self.tree[r - 1] <= x:
					Rmin = r - 1
			l >>= 1
			r >>= 1
		if Lmin != -1:
			pos = Lmin
			while pos < self.num:
				if self.tree[2 * pos] <= x:
					pos = 2 * pos
				else:
					pos = 2 * pos + 1
			return pos - self.num
		elif Rmin != -1:
			pos = Rmin
			while pos < self.num:
				if self.tree[2 * pos] <= x:
					pos = 2 * pos
				else:
					pos = 2 * pos + 1
			return pos - self.num
		else:
			return -1
import sys, random, bisect
from collections import deque, defaultdict
from heapq import heapify, heappop, heappush
from itertools import permutations
from math import gcd, log
input = lambda : sys.stdin.readline().rstrip()
mi = lambda : map(int, input().split())
li = lambda : list(mi())
h = int(input())
A = li()
n = sum(A)
for i in range(h):
	if A[i] > 1 and A[i + 1] > 1:
		print('ambiguous')
		p1 = [-1 for i in range(n)]
		pre = 0
		next = 1
		for j in range(1, h + 1):
			if j != i + 1:
				next_pre = next
				for k in range(next, next + A[j]):
					p1[next] = pre
					next += 1
				pre = next_pre
			else:
				next_pre = next
				p1[next] = pre + 1
				next += 1
				for k in range(A[j] - 1):
					p1[next] = pre
					next += 1
				pre = next_pre
		p2 = [-1 for i in range(n)]
		pre = 0
		next = 1
		for j in range(1, h + 1):
			next_pre = next
			for k in range(A[j]):
				p2[next] = pre
				next += 1
			pre = next_pre
		for j in range(n):
			p1[j] += 1
			p2[j] += 1
		print(*p1)
		print(*p2)
		break
else:
	print('perfect')


def printarr(arr):
	n = len(arr)
	for i in range(n):
		print(arr[i], end=' ')
	print('')
h = int(input())
level = [0] + [int(i) for i in input().split()]
flag = 1
for i in range(1, h + 1):
	if level[i] > 1 and level[i + 1] > 1:
		flag = 0
		split = i + 1
if flag:
	print('perfect')
else:
	print('ambiguous')
	sums = [0]
	curr = 0
	for i in range(1, h + 2):
		curr += level[i]
		sums.append(curr)
	a = []
	b = []
	for i in range(1, h + 2):
		if i != split:
			a.extend([sums[i - 1]] * level[i])
			b.extend([sums[i - 1]] * level[i])
		else:
			a.extend([sums[i - 1]] * level[i])
			b.append(sums[i - 1] - 1)
			b.extend([sums[i - 1]] * (level[i] - 1))
	printarr(a)
	printarr(b)


h = int(input())
a = list(map(int, input().split()))
(w, q) = ([], [])
p = r = 0
for i in a:
	for j in range(i):
		w.append(r)
		q.append(r - (j and p > 1))
	p = i
	r += i
if w == q:
	print('perfect')
else:
	print('ambiguous')
	print(*w)
	print(*q)


n = int(input())
n += 1
l = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
	if l[i] > 1 and l[i + 1] > 1:
		ans = i + 1
		break
else:
	print('perfect')
	exit(0)
print('ambiguous')
prev = 0
now = 0
for i in range(n):
	for j in range(l[i]):
		print(prev, end=' ')
		now += 1
	prev = now
print()
prev = 0
now = 0
for i in range(n):
	for j in range(l[i]):
		if ans == i:
			print(prev - 1, end=' ')
			ans = -1
		else:
			print(prev, end=' ')
		now += 1
	prev = now


n = int(input())
ar = list(map(int, input().split()))
b = False
c = False
for x in ar:
	if x > 1:
		if b == True:
			c = True
			break
		b = True
	else:
		b = False
if not c:
	print('perfect')
else:
	print('ambiguous')
	ar1 = []
	ar2 = []
	c = 0
	b = 0
	d = 0
	for x in range(n + 1):
		if ar[x] > 1:
			if b and (not d):
				for el in range(ar[x] - 1):
					ar1.append(c)
					ar2.append(c - 1)
				ar1.append(c)
				ar2.append(c)
				d = 1
			else:
				for el in range(ar[x]):
					ar1.append(c)
					ar2.append(c)
			b = True
		else:
			b = False
			for el in range(ar[x]):
				ar1.append(c)
				ar2.append(c)
		c += ar[x]
	print(*ar1)
	print(*ar2)


h = int(input())
a = list(map(int, input().split()))
k = 0
for i in range(h):
	if a[i] > 1 and a[i + 1] > 1:
		k = i + 1
		break
else:
	print('perfect')
	exit()
print('ambiguous')
h1 = 1
p = 1
print(0, end=' ')
while h1 <= h:
	for i in range(a[h1]):
		print(p, end=' ')
	p += a[h1 - 1]
	h1 += 1
print('')
h1 = 1
p = 1
print(0, end=' ')
while h1 <= h:
	if h1 == k:
		for i in range(a[h1] - 1):
			print(p, end=' ')
		print(p + 1, end=' ')
		p += a[h1 - 1]
		h1 += 1
		continue
	for i in range(a[h1]):
		print(p, end=' ')
	p += a[h1 - 1]
	h1 += 1


def print_iso(nnodes, counter):
	print('ambiguous')
	final_tree1 = []
	final_tree2 = []
	c = 0
	parent = 0
	for n in nnodes:
		if c != counter:
			for i in range(0, n):
				final_tree1.append(parent)
				final_tree2.append(parent)
		else:
			final_tree1.append(parent - 1)
			final_tree2.append(parent)
			for i in range(0, n - 1):
				final_tree1.append(parent)
				final_tree2.append(parent)
		parent += n
		c += 1
	final_tree1 = [str(i) for i in final_tree1]
	final_tree2 = [str(i) for i in final_tree2]
	print(' '.join(final_tree1))
	print(' '.join(final_tree2))

def main():
	height = int(input())
	nnodes = input()
	nnodes = nnodes.split(' ')
	nnodes = [int(i) for i in nnodes]
	perfect = True
	prev = 1
	counter = 0
	for n in nnodes:
		if n > 1 and prev > 1:
			print_iso(nnodes, counter)
			perfect = False
			break
		prev = n
		counter += 1
	if perfect:
		print('perfect')
main()


(h, ip, nf) = (int(input()), True, 1)
an = list(map(int, input().split()))
for i in range(h):
	if an[i] > 1 and an[i + 1] > 1:
		ip = False
		break
if ip:
	print('perfect')
else:
	print('ambiguous')
	t1 = list(range(h + 1))
	t2 = list(range(h + 1))
	for i in range(1, h + 1):
		t1.extend([i] * (an[i] - 1))
		nf = len(t2) if an[i] > 1 and an[i - 1] > 1 else i
		t2.extend([nf] * (an[i] - 1))
	print(' '.join((str(e) for e in t1)))
	print(' '.join((str(e) for e in t2)))


import sys
lines = []
for line in sys.stdin:
	lines.append(line.rstrip('\r\n\t '))
h = int(lines[0])
ids = lines[1].split(' ')
leafs1 = '0'
leafs2 = '0'
parents = []
last_id = 1
is_perfect = True
leafs = int(ids[1])
last_leafs = leafs
for j in range(0, leafs):
	last_id += 1
	leafs1 += ' 1'
	leafs2 += ' 1'
	parents.append(last_id)
for i in range(3, h + 2):
	leafs = int(ids[i - 1])
	s0 = ' ' + str(parents[0])
	if last_leafs > 1 and leafs > 1:
		is_perfect = False
		last_id += 1
		s1 = ' ' + str(parents[1])
		parents = []
		leafs1 += s0
		leafs2 += s1
		parents.append(last_id)
		for j in range(1, leafs):
			last_id += 1
			leafs1 += s0
			leafs2 += s0
			parents.append(last_id)
	else:
		parents = []
		for j in range(0, leafs):
			last_id += 1
			leafs1 += s0
			leafs2 += s0
			parents.append(last_id)
	last_leafs = leafs
if is_perfect:
	print('perfect')
else:
	print('ambiguous')
	print(leafs1)
	print(leafs2)


import sys
h = int(input()) + 1
a = []
for v in input().split(' '):
	a.append(int(v))
for k in range(1, h):
	if a[k - 1] > 1 and a[k] > 1:
		print('ambiguous')
		for i in range(0, h):
			print(i, end=' ')
		for i in range(1, h):
			for j in range(0, a[i] - 1):
				print(i, end=' ')
		print()
		for i in range(0, h):
			print(i, end=' ')
		print(k - 1, end=' ')
		a[k - 1] -= 1
		print(h + 1, end=' ')
		a[k] -= 1
		for i in range(1, h):
			for j in range(0, a[i] - 1):
				print(i, end=' ')
		print()
		sys.exit(0)
print('perfect')


n = int(input())
arr = list(map(int, input().strip().split(' ')))
ans = 0
j = arr[1]
for i in range(2, n + 1):
	if j > 1 and arr[i] > 1:
		ans = 1
		break
	j = arr[i]
if ans == 0:
	print('perfect')
else:
	print('ambiguous')
	print(0, end=' ')
	for i in range(arr[1]):
		print(1, end=' ')
	prev = 1 + arr[1]
	for i in range(2, n + 1):
		for j in range(arr[i]):
			print(prev, end=' ')
		prev += arr[i]
	print()
	print(0, end=' ')
	for i in range(arr[1]):
		print(1, end=' ')
	k = arr[1]
	prev = 1 + arr[1]
	flag = 0
	for i in range(2, n + 1):
		if k > 1 and arr[i] > 1 and (flag == 0):
			print(prev - 1, end=' ')
			flag = 1
			for j in range(arr[i] - 1):
				print(prev, end=' ')
		else:
			for j in range(arr[i]):
				print(prev, end=' ')
		prev += arr[i]
		k = arr[i]
	print()


import sys
from collections import Counter, deque

def lInt(d=None):
	return map(int, input().split(d))
(n, *_) = lInt()
a = list(lInt())
a.append(0)
j = 0
tot = sum(a)
try:
	for (i, va) in enumerate(a):
		if va > 1 and a[i - 1] > 1:
			print('ambiguous')
			j = i
			raise
except:
	p = 0
	ans = []
	for i in range(0, n + 1):
		va = a[i]
		for u in range(va):
			ans.append(p)
		p += va
	for u in ans:
		sys.stdout.write(str(u) + ' ')
	print('')
	p = 0
	ans = []
	for i in range(0, n + 1):
		va = a[i]
		if i == j:
			for u in range(va // 2):
				ans.append(p - 1)
			for u in range(va // 2, va):
				ans.append(p)
		else:
			for u in range(va):
				ans.append(p)
		p += va
	for u in ans:
		sys.stdout.write(str(u) + ' ')
	print('')
else:
	print('perfect')


h = int(input())
l = list(map(int, input().split()))
x = False
pos = 0
s = 1
for i in range(h):
	if l[i] != 1 and l[i + 1] != 1:
		pos = i + 1
		x = True
		print('ambiguous')
		break
if not x:
	print('perfect')
else:
	ch = '0'
	ch1 = '0'
	for i in range(1, len(l)):
		ch += (' ' + str(s)) * l[i]
		if i != pos:
			ch1 += (' ' + str(s)) * l[i]
		else:
			ch1 += ' ' + str(s - 1) + (' ' + str(s)) * (l[i] - 1)
		s += l[i]
	print(ch)
	print(ch1)


h = int(input())
d = list(map(int, input().split()))
f = True
ix = -1
c = 0
for i in range(len(d) - 1):
	if d[i] > 1 and d[i + 1] > 1:
		f = False
		ix = i
		break
	c += d[i]
if f:
	print('perfect')
else:
	print('ambiguous')
	b = 0
	s = 0
	for i in range(ix):
		for j in range(d[i]):
			print(b, end=' ')
		s += d[i]
		b = s
	for i in range(d[ix]):
		print(b, end=' ')
	s += d[ix]
	b = s
	for i in range(d[ix + 1]):
		print(b, end=' ')
	s += d[ix + 1]
	b = s
	for i in range(ix + 2, len(d)):
		for j in range(d[i]):
			print(b, end=' ')
		s += d[i]
		b = s
	print()
	b = 0
	s = 0
	for i in range(ix):
		for j in range(d[i]):
			print(b, end=' ')
		s += d[i]
		b = s
	for i in range(d[ix]):
		print(b, end=' ')
	s += d[ix]
	b = s
	for i in range(d[ix + 1] - 1):
		print(b, end=' ')
	print(b - 1, end=' ')
	s += d[ix + 1]
	b = s
	for i in range(ix + 2, len(d)):
		for j in range(d[i]):
			print(b, end=' ')
		s += d[i]
		b = s


n = int(input())
c = list(map(int, input().split()))
f = True
for i in range(n):
	if c[i + 1] > 1 and c[i] > 1:
		f = False
if f:
	print('perfect')
else:
	last = 0
	print('ambiguous')
	g = 0
	for i in range(n + 1):
		for j in range(c[i]):
			print(g, end=' ')
		g += c[i]
		if i > 0 and last == 0:
			if c[i] > 1 and c[i - 1] > 1:
				last = i
	print()
	g = 0
	for i in range(n + 1):
		if i != last:
			for j in range(c[i]):
				print(g, end=' ')
		else:
			for j in range(c[i] - 1):
				print(g, end=' ')
			print(g - 1, end=' ')
		g += c[i]


n = int(input())
h = [int(i) for i in input().split()]
flag = 0
for i in range(n):
	if h[i] >= 2 and h[i + 1] >= 2:
		flag = i
if flag:
	a = []
	c = 0
	for i in range(n + 1):
		for j in range(h[i]):
			a.append(c)
		c += h[i]
	b = []
	c = 0
	for i in range(n + 1):
		for j in range(h[i]):
			if i == flag + 1 and j == 0:
				b.append(c - 1)
			else:
				b.append(c)
		c += h[i]
	print('ambiguous')
	print(' '.join([str(i) for i in a]))
	print(' '.join([str(i) for i in b]))
else:
	print('perfect')


n = int(input().strip())
a = [int(x) for x in input().split()]
m = 0
for i in range(1, n):
	if a[i] > 1 and a[i + 1] > 1:
		print('ambiguous')
		m = i
		break
if m == 0:
	print('perfect')
else:
	count = 0
	for i in range(n + 1):
		curr = count
		for j in range(a[i]):
			print(curr, end=' ')
			count += 1
	print()
	count = 0
	for i in range(n + 1):
		curr = count
		for j in range(a[i]):
			if i == m + 1 and j == 0:
				print(curr - 1, end=' ')
			else:
				print(curr, end=' ')
			count += 1
	print()


n = int(input())
a = list(map(int, input().split()))
for i in range(2, n + 1):
	if a[i] != 1 and a[i - 1] != 1:
		print('ambiguous')
		break
else:
	print('perfect')
	exit()
s1 = ''
s2 = ''
cnt = 0
for i in range(n + 1):
	s1 += a[i] * (str(cnt) + ' ')
	if i > 1 and a[i] != 1 and (a[i - 1] != 1):
		s2 += str(cnt - 1) + ' '
		s2 += (a[i] - 1) * (str(cnt) + ' ')
	else:
		s2 += a[i] * (str(cnt) + ' ')
	cnt += a[i]
print(s1)
print(s2)


h = int(input())
a = list(map(int, input().split()))
ok = True
for i in range(h):
	if a[i] >= 2 and a[i + 1] >= 2:
		ok = False
		idx = i + 1
if ok:
	print('perfect')
else:
	print('ambiguous')
	ans = []
	p = 0
	for x in a:
		ans.extend([p] * x)
		p = len(ans)
	print(' '.join(map(str, ans)))
	ans[sum(a[:idx])] -= 1
	print(' '.join(map(str, ans)))


n = int(input())
p = list(map(int, input().split()))

def check():
	global z
	for i in range(n):
		if p[i] != 1 and p[i + 1] != 1:
			z = i
			return True
	return False
z = 0
if check():
	print('ambiguous')
	x = 1
	s = ['0']
	zx = 0
	for i in range(1, n + 1):
		a = x
		for j in range(p[i]):
			s.append(str(a))
			x += 1
		if z == i and (not zx):
			z = len(s) + 1
			zx = x - 1
	print(' '.join(s))
	s[z] = str(zx)
	print(' '.join(s))
else:
	print('perfect')


n = int(input())
h = list(map(int, input().split()))
t1 = [0]
t2 = [0]
ans = 'perfect'
for i in range(1, len(h)):
	l = len(t1)
	if h[i - 1] > 1 and h[i] > 1:
		l = len(t1)
		pl = len(t1) - 1
		for j in range(h[i]):
			t1.append(l)
		for j in range(h[i]):
			if j % 2:
				t2.append(l)
			else:
				t2.append(pl)
		ans = 'ambiguous'
	else:
		for j in range(h[i]):
			t1.append(l)
			t2.append(l)
if ans == 'perfect':
	print(ans)
else:
	print(ans)
	print(' '.join([str(i) for i in t1]))
	print(' '.join([str(i) for i in t2]))


n = int(input())
a = list(map(int, input().split()))
ans = 0
f = 0
for i in range(1, n + 1):
	if a[i] >= 2 and a[i - 1] >= 2:
		f = i
		ans = 1
if ans == 0:
	print('perfect')
else:
	print('ambiguous')
	s1 = '0'
	s2 = '0'
	p = 0
	ans = 1
	for i in range(1, len(a)):
		s1 += (' ' + str(ans)) * a[i]
		ans += a[i]
	print(s1)
	ans = 1
	p = 0
	for i in range(1, len(a)):
		if i == f:
			s2 += ' ' + str(ans - 1)
			a[i] -= 1
			p = 1
		s2 += (' ' + str(ans)) * a[i]
		if p == 1:
			ans += 1
			p = 0
		ans += a[i]
	print(s2)


import sys
input = sys.stdin.readline
h = int(input())
a = list(map(int, input().split()))
n = sum(a)
par = [-1] * n
cnt = 1
(s, t) = (-1, -1)
for i in range(1, h + 1):
	for v in range(cnt, cnt + a[i]):
		par[v] = cnt - 1
	if a[i - 1] >= 2 and a[i] >= 2:
		s = cnt
		t = cnt - a[i - 1]
	cnt += a[i]
if s == -1 and t == -1:
	print('perfect')
else:
	print('ambiguous')
	print(*list(map(lambda x: x + 1, par)))
	par[s] = t
	print(*list(map(lambda x: x + 1, par)))


h = int(input())
nodes = [int(x) for x in input().split()]
first = []
second = []
mbo = True
stillgood = True
pn = 0
for nc in nodes:
	if mbo and nc == 1:
		first.append(pn)
		second.append(pn)
		pn += 1
		mbo = False
	elif mbo and nc != 1:
		stillgood = False
		for i in range(nc - 1):
			first.append(pn)
			second.append(pn)
		first.append(pn)
		second.append(pn - 1)
		pn += nc
		mbo = True
	elif not mbo and nc == 1:
		first.append(pn)
		second.append(pn)
		pn += 1
		mbo = False
	elif not mbo and nc != 1:
		for i in range(nc):
			first.append(pn)
			second.append(pn)
		pn += nc
		mbo = True
if stillgood:
	print('perfect')
else:
	print('ambiguous')
	print(' '.join((str(x) for x in first)))
	print(' '.join((str(x) for x in second)))


n = int(input())
(*a,) = map(int, input().split())
(b, c) = ([0], [0])
cur = 1
for i in range(1, n + 1):
	for j in range(a[i]):
		b.append(cur)
	cur += a[i]
cur = 1
for i in range(1, n + 1):
	if a[i] > 1 and a[i - 1] > 1:
		c.append(cur - 1)
		for j in range(1, a[i]):
			c.append(cur)
	else:
		for j in range(a[i]):
			c.append(cur)
	cur += a[i]
if b == c:
	exit(print('perfect'))
print('ambiguous')
print(*b)
print(*c)


n = int(input())
a = list(map(int, input().split()))
pre = a[0]
index = -1
for i in range(1, n + 1):
	if pre >= 2 and a[i] >= 2:
		index = i
		break
	pre = a[i]
else:
	print('perfect')
	exit()
print('ambiguous')
N = sum(a)
R = [0]
for i in range(1, n + 1):
	k = len(R)
	for j in range(a[i]):
		R.append(k)
L = [0]
flag = False
for i in range(1, n + 1):
	k = len(L)
	m = 0
	if i == index:
		L.append(k - a[i - 1] + 1)
		m = 1
	for j in range(a[i] - m):
		L.append(k)
print(*R)
print(*L)


n = int(input())
fl = 1
a = [int(x) for x in input().split()]
for i in range(1, n + 1):
	if a[i] > 1 and a[i - 1] > 1:
		fl = 0
		break
if fl == 1:
	print('perfect')
else:
	print('ambiguous')
	print('0', end=' ')
	cnt = 1
	x = 1
	fl = 1
	for i in range(1, n + 1):
		for j in range(a[i]):
			print(cnt, end=' ')
			x += 1
		cnt = x
	print()
	print('0', end=' ')
	cnt = 1
	x = 1
	fl = 1
	for i in range(1, n + 1):
		if a[i] > 1 and a[i - 1] > 1 and (fl == 1):
			fl = 0
			for j in range(a[i] - 1):
				print(cnt, end=' ')
				x += 1
			print(cnt - 1, end=' ')
			x += 1
		else:
			for j in range(a[i]):
				print(cnt, end=' ')
				x += 1
		cnt = x


h = int(input())
widths = [int(x) for x in input().split()]
index = -1
isAmbiguous = False
for (i, a, b) in zip(range(h), widths, widths[1:]):
	if a > 1 and b > 1:
		isAmbiguous = True
		index = i + 1
		break
else:
	print('perfect')
	exit(0)
parents = [0 for i in range(sum(widths))]
parents2 = [0 for i in range(sum(widths))]
pos = 0
parent = 0
for (j, w) in enumerate(widths):
	for i in range(w):
		parents[pos + i] = parent
		parents2[pos + i] = parent
	if j == index:
		parents2[pos + i] -= 1
	pos += w
	parent += w
print('ambiguous')
print(*parents)
print(*parents2)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def solve(case):
	n = int(inp())
	a = lis()
	cnt = False
	for i in range(1, n + 1):
		if a[i] > 1 and a[i - 1] > 1:
			cnt = True
	if not cnt:
		print('perfect')
		return
	print('ambiguous')
	ans = [0]
	par = 1
	for i in range(1, n + 1):
		for j in range(a[i]):
			ans.append(par)
		par += a[i - 1]
	ans2 = [0]
	par = 1
	for i in range(1, n + 1):
		for j in range(a[i] - 1):
			ans2.append(par)
		if a[i - 1] > 1:
			ans2.append(par + 1)
		else:
			ans2.append(par)
		par += a[i - 1]
	print(' '.join((str(ans[i]) for i in range(len(ans)))))
	print(' '.join((str(ans2[i]) for i in range(len(ans2)))))
testcase(1)


h = int(input())
a = list(map(int, input().split()))
s = 0
flag = 0
for i in range(0, h):
	if a[i] > 1 and a[i + 1] > 1:
		flag = 1
if flag == 0:
	print('perfect')
	exit(0)
si = sum(a)
d = []
led = 0
for i in range(h + 1):
	if led > si:
		break
	for j in range(a[i]):
		d.append(s)
		led += 1
	s += a[i]
j = 0
for i in range(1, h + 1):
	if a[i] > 1 and a[i - 1] > 1:
		j = i
		break
sumu = sum(a[:j])
print('ambiguous')
print(*d[:si])
d[sumu + 1] -= 1
print(*d[:si])


n = int(input())
arr = list(map(int, input().split()))
(b, c) = ([0], [0])
curr = 1
for i in range(1, n + 1):
	b.extend([curr] * arr[i])
	curr += arr[i]
curr = 1
for i in range(1, n + 1):
	if arr[i] > 1 and arr[i - 1] > 1:
		c.append(curr - 1)
		c.extend([curr] * (arr[i] - 1))
	else:
		c.extend([curr] * arr[i])
	curr += arr[i]
if b == c:
	print('perfect')
else:
	print('ambiguous')
	print(*b)
	print(*c)


h = int(input())
nums = [int(x) for x in input().split()]
if not any([nums[i] > 1 and nums[i + 1] > 1 for i in range(h)]):
	print('perfect')
else:
	print('ambiguous')
	cur = 1
	flag = False
	print(0, end=' ')
	for l in range(1, h + 1):
		[print(cur, end=' ') for i in range(nums[l])]
		cur += nums[l]
	print()
	cur = 1
	flag = False
	print(0, end=' ')
	for l in range(1, h + 1):
		if not flag and nums[l] > 1 and (nums[l - 1] > 1):
			flag = True
			print(cur - 1, end=' ')
			[print(cur, end=' ') for i in range(nums[l] - 1)]
		else:
			[print(cur, end=' ') for i in range(nums[l])]
		cur += nums[l]


def do_amb(v, n, sum):
	print('ambiguous')
	diff = []
	for i in v:
		temp = len(diff)
		diff.extend((temp for j in range(i)))
	print(' '.join((str(i) for i in diff)))
	diff[sum + 1] = sum - 1
	print(' '.join((str(i) for i in diff)))
n = int(input())
v = [int(i) for i in input().split()]
ans = []
ind = -1
sum = 0
for i in v:
	if i != 1 and v[ind] != 1:
		do_amb(v, ind, sum)
		exit()
	sum += i
	ind += 1
print('perfect')


import sys
inp = sys.stdin.readline
input = lambda : inp().strip()
flush = sys.stdout.flush

def iin():
	return int(input())

def lin():
	return list(map(int, input().split()))

def main():
	h = iin()
	a = lin()
	ans = 0
	for i in range(h):
		if a[i] > 1 and a[i + 1] > 1:
			ans = 1
			break
	if ans == 0:
		print('perfect')
	else:
		print('ambiguous')
		sol1 = [0]
		sol2 = [0]
		pv = 1
		ch = 1
		sm = a[0]
		for i in range(1, h + 1):
			for j in range(a[i]):
				ch += 1
				sol1.append(pv)
			pv = sm + 1
			sm += a[i]
		ch = 1
		did = 0
		pv = 1
		sm = a[0]
		for i in range(1, h + 1):
			for j in range(a[i]):
				ch += 1
				sol2.append(pv)
			if did == 0:
				if a[i - 1] > 1 and a[i] > 1:
					sol2[-1] += 1
					did = 1
			pv = sm + 1
			sm += a[i]
		print(*sol1)
		print(*sol2)
main()


n = int(input())
l = list(map(int, input().split()))
f = 0
for i in range(n):
	if l[i] > 1 and l[i + 1] > 1:
		ind = i
		f += 1
		break
if f == 0:
	print('perfect')
else:
	print('ambiguous')
	(l1, l2) = ([], [])
	cnt = 0
	for i in range(len(l)):
		for j in range(l[i]):
			l1.append(cnt)
		cnt += l[i]
	print(*l1)
	cnt1 = 0
	for i in range(len(l)):
		if i == ind + 1:
			k = cnt1 - 1
			for j in range(l[i] - 1):
				l2.append(cnt1)
			l2.append(k)
			cnt1 += l[i]
		else:
			for j in range(l[i]):
				l2.append(cnt1)
			cnt1 += l[i]
	print(*l2)


h = int(input())
L = [int(x) for x in input().split()]

def f():
	for i in range(1, len(L)):
		if L[i] > 1 and L[i - 1] > 1:
			return [False, i]
	return [True]
bb = f()
if bb[0]:
	print('perfect')
else:
	y = 0
	r = []
	rr = []
	b = True
	for (k, x) in enumerate(L):
		for i in range(x):
			r.append(y)
			rr.append(y)
		y = len(r)
		if b and k == bb[1]:
			rr[-1] -= 1
			b = False
	print('ambiguous')
	print(*r)
	print(*rr)


def read():
	return tuple((int(x) for x in input().split()))

def main():
	(h,) = read()
	a = read()
	tree = []
	fi = 0
	flag = False
	for (i, x) in enumerate(a):
		if fi == 0:
			if not flag and x > 1:
				flag = True
			elif flag and x > 1:
				fi = len(tree)
			else:
				flag = False
		tree.extend([len(tree)] * x)
	if fi == 0:
		print('perfect')
		return
	else:
		print('ambiguous')
	print(' '.join((str(x) for x in tree)))
	tree[fi] = fi - 1
	print(' '.join((str(x) for x in tree)))
main()


h = int(input())
l = [int(i) for i in input().split()]
n = 0
v = []
pc = []
k = l[0]
x = 0
z = 0
y = 0
b = [0]
b1 = ''
for i in range(h + 1):
	k = l[i]
	while k > 0:
		v.append(x)
		k -= 1
		z += 1
	pc.append(z)
	x += 1
for i in range(1, h + 1):
	y += l[i - 1]
	if l[i] > 1 and l[i - 1] > 1:
		n = 1
		print('ambiguous')
		for i in range(1, z):
			b.append(pc[v[i] - 1])
		for i in range(len(b)):
			b1 += str(b[i]) + ' '
		print(b1)
		b[y + 1] -= 1
		b1 = ''
		for i in range(len(b)):
			b1 += str(b[i]) + ' '
		print(b1)
		break
if n == 0:
	print('perfect')


n = int(input())
a = list(map(int, input().split()))
ambi = 0
for i in range(1, len(a)):
	if a[i] > 1 and a[i - 1] > 1:
		ambi = 1
ans1 = ''
ans2 = ''
if ambi == 0:
	print('perfect')
else:
	print('ambiguous')
	ans1 += '0 '
	k = 0
	s = sum(a)
	j = 1
	h = 1
	c = 0
	st = 2
	ans2 += '0' + ' '
	k1 = 0
	j1 = 1
	h1 = 1
	c1 = 0
	st1 = 2
	prev_st1 = 1
	for i in range(1, s):
		ans1 += str(j) + ' '
		ans2 += str(j1) + ' '
		k += 1
		c += 1
		if c >= a[h]:
			k = 0
			h += 1
			c = 0
			j = st
			st = i + 2
		k1 += 1
		if k1 == 1:
			k1 = 0
			j1 += 1
		c1 += 1
		if c1 >= a[h1 - 1]:
			j1 = prev_st1
			k1 = 0
		if c1 >= a[h1]:
			k1 = 0
			h1 += 1
			c1 = 0
			j1 = st1
			prev_st1 = st1
			st1 = i + 2
	print(ans1)
	print(ans2)


n = int(input())
h = [int(x) for x in input().split()]
perfect = True
for i in range(n):
	if h[i] > 1 and h[i + 1] > 1:
		perfect = False
if perfect:
	print('perfect')
else:
	print('ambiguous')
	parent = 0
	vertex = 1
	for i in range(n + 1):
		for j in range(h[i]):
			print(parent, end=' ')
		parent = vertex
		vertex += h[i]
	print()
	parent = 0
	vertex = 1
	for i in range(n + 1):
		for j in range(h[i]):
			print(parent, end=' ')
		parent = vertex
		vertex += h[i]
		if i < n and h[i] > 1 and (h[i + 1] > 1):
			print(parent + 1, end=' ')
			h[i + 1] -= 1
			vertex += 1
	print()


def main():
	from sys import stdin, stdout
	from collections import defaultdict
	h = int(input())
	if h <= 1:
		stdout.write('perfect\n')
		return
	a = list(map(int, stdin.readline().strip().split(' ')))
	ambiguous = False
	for i in range(0, h + 1):
		if a[i] > 1 and a[i - 1] > 1:
			ambiguous = True
			break
	if not ambiguous:
		stdout.write('perfect\n')
		return
	stdout.write('ambiguous\n')
	t = defaultdict(list)
	prev = 1
	cur = 2
	r = [0]
	for i in range(1, h + 1):
		for j in range(a[i]):
			t[prev].append(cur)
			cur += 1
		for l in t[prev]:
			r.append(prev)
		prev = cur - 1
	stdout.write(' '.join(list(map(str, r))) + '\n')
	t = defaultdict(list)
	prev = 1
	cur = 2
	r = [0]
	o = True
	for i in range(1, h + 1):
		for j in range(a[i]):
			t[prev].append(cur)
			cur += 1
		for l in t[prev]:
			if a[i] > 1 and a[i - 1] > 1 and o:
				r.append(prev - 1)
				o = False
			else:
				r.append(prev)
		prev = cur - 1
	stdout.write(' '.join(list(map(str, r))) + '\n')
main()


h = int(input())
a = list(map(int, input().split()))
prev = 0
ambiguous = False
ind = 0
for i in a:
	if i > 1 and prev > 1:
		ambiguous = True
		break
	prev = i
	ind += 1
if ambiguous:
	print('ambiguous')
	res = []
	lenSeq = 0
	for i in a:
		res += [lenSeq] * i
		lenSeq += i
	print(*res)
	lenSeq = 0
	for i in a[:ind]:
		lenSeq += i
	res[lenSeq + 1] -= 1
	print(*res)
else:
	print('perfect')


h = int(input())
arr = list(map(int, input().split()))
s = 0
flag = True
for i in range(0, h):
	if arr[i] > 1 and arr[i + 1] > 1:
		flag = False
if flag:
	print('perfect')
else:
	total = sum(arr)
	ch = []
	temp = 0
	for i in range(h + 1):
		if temp > total:
			break
		for j in range(arr[i]):
			ch.append(s)
			temp += 1
		s += arr[i]
	j = 0
	for i in range(1, h + 1):
		if arr[i] > 1 and arr[i - 1] > 1:
			j = i
			break
	tot2 = sum(arr[:j])
	print('ambiguous')
	ans1 = list(ch[:total])
	print(*ans1)
	ch[tot2 + 1] -= 1
	ans2 = list(ch[:total])
	print(*ans2)


INF = 10 ** 10

def main():
	print = out.append
	h = get_int()
	li = get_list()
	possible = False
	for i in range(h):
		if li[i] > 1 and li[i + 1] > 1:
			possible = True
			break
	if not possible:
		print('perfect')
		return
	res1 = []
	res2 = []
	for (index, i) in enumerate(li):
		ele = len(res1)
		for _ in range(i):
			res1.append(ele)
			res2.append(ele)
		if index > 0 and li[index - 1] > 1 and (i > 1):
			res2[-1] -= 1
	print('ambiguous')
	print(' '.join(map(str, res1)))
	print(' '.join(map(str, res2)))
import sys
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
out = []
get_int = lambda : int(input())
get_list = lambda : list(map(int, input().split()))
main()
print(*out, sep='\n')


from sys import stdin as cin
from sys import stdout as cout

def main():
	h = int(cin.readline())
	a = list(map(int, cin.readline().split()))
	f = 1
	for i in range(h):
		if a[i] > 1 and a[i + 1] > 1:
			f = 0
			break
	if f:
		print('perfect')
		return
	print('ambiguous')
	s = sum(a)
	b = [0] * s
	c = [0] * s
	w = 1
	r = 1
	f = 1
	for i in range(1, len(a)):
		for j in range(a[i]):
			b[w] = r
			c[w] = r
			if f and a[i - 1] > 1 and (a[i] > 1):
				f = 0
				c[w] -= 1
			w += 1
		r = w
	print(*b)
	print(*c)
main()


def tree(h, a, special=False):
	ind = 0
	parent = [-1] * sum(a)
	bottom = []
	for i in range(a[-1]):
		bottom.append(ind)
		ind += 1
	prev = bottom
	for y in range(h - 1, -1, -1):
		root = ind
		ind += 1
		if special and a[y] > 1 and (a[y + 1] > 1):
			for node in prev[1:]:
				parent[node] = root
			parent[prev[0]] = root + 1
			special = False
		else:
			for node in prev:
				parent[node] = root
		nextlayer = [root]
		for i in range(a[y] - 1):
			nextlayer.append(ind)
			ind += 1
		prev = nextlayer
	return (parent, special)
h = int(input())
a = [int(x) for x in input().split()]
(p1, special) = tree(h, a)
(p2, special) = tree(h, a, special=True)
if not special:
	print('ambiguous')
	print(*[x + 1 for x in p1])
	print(*[x + 1 for x in p2])
else:
	print('perfect')


import sys
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def print_array(a):
	print(' '.join(map(str, a)))

def main():
	n = int(input())
	e = get_array()
	flag = False
	bp = 0
	for i in range(n):
		if e[i] > 1 and e[i + 1] > 1:
			flag = True
			bp = i + 1
			break
	print('ambiguous' if flag else 'perfect')
	if flag:
		(a, b) = ([], [])
		i = 0
		val = 0
		while i < n + 1:
			for _ in range(e[i]):
				a.append(val)
				b.append(val)
			if i == bp:
				b[-1] -= 1
			val += e[i]
			i += 1
		print_array(a)
		print_array(b)
main()