a = [1, 2, 1] last = [2, 1] for i in range(2, 23): if a[i] > len(last): last.append(0) for j in range(a[i]): last[j] += 1 a += last[:a[i]] print(a)