diff --git a/.vscode/settings.json b/.vscode/settings.json index d10cb1f..1532b0a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,4 +2,6 @@ "python.linting.pylintEnabled": false, "python.linting.mypyEnabled": true, "python.linting.enabled": true, + "python.formatting.provider": "autopep8", + "python.pythonPath": "C:\\Users\\mamodzilla\\AppData\\Local\\Programs\\Python\\Python39\\python.exe", } \ No newline at end of file diff --git a/yandex_contest/module2/task1.py b/yandex_contest/module2/task1.py new file mode 100644 index 0000000..627c4b3 --- /dev/null +++ b/yandex_contest/module2/task1.py @@ -0,0 +1,16 @@ +a = int(input()) +stroka = input() +new_stroka = stroka.split(" ") +massiv = [] +for i in range(0, len(stroka)): + massiv.append(int(new_stroka[i])) +n = len(stroka) +num = 0 +for i in range(0, n-1): + for j in range(0, n-i-1): + if massiv[j] > massiv[j+1]: + massiv[j], massiv[j+1] = massiv[j+1], massiv[j] + num += 1 + print(" ".join(map(str, massiv))) +if num == 0: + print(0) diff --git a/yandex_contest/module2/task2.py b/yandex_contest/module2/task2.py new file mode 100644 index 0000000..3dfb2ed --- /dev/null +++ b/yandex_contest/module2/task2.py @@ -0,0 +1,23 @@ +a = int(input()) +nabor = [] +for i in range(a): + nabor.append(list(map(int, input().split()))) + +for i in range(a-1): + for j in range(a - i - 1): + if nabor[j][1] < nabor[j+1][1]: + nabor[j], nabor[j+1] = nabor[j+1], nabor[j] +for i in range(a-1): + for j in range(a - i - 1): + if nabor[j][1] == nabor[j+1][1]: + if nabor[j][0] > nabor[j+1][0]: + nabor[j], nabor[j+1] = nabor[j+1], nabor[j] + + +for i in range(a): + stroka = "" + stroka += str(nabor[i][0]) + for j in range(1, len(nabor[i])): + stroka += " " + stroka += str(nabor[i][j]) + print(stroka) diff --git a/yandex_contest/module2/task3.py b/yandex_contest/module2/task3.py new file mode 100644 index 0000000..0ee39e9 --- /dev/null +++ b/yandex_contest/module2/task3.py @@ -0,0 +1,31 @@ +def merge(l_list, r_list, str_idx, end_idx): + s_list = [] + l_idx = 0 + r_idx = 0 + for i in range(len(l_list)+len(r_list)): + if l_idx < len(l_list) and r_idx < len(r_list): + if l_list[l_idx] <= r_list[r_idx]: + s_list.append(l_list[l_idx]) + l_idx += 1 + else: + s_list.append(r_list[r_idx]) + r_idx += 1 + s_list += l_list[l_idx:] + s_list += r_list[r_idx:] + print(str(str_idx)+" "+str(end_idx)+" "+str(s_list[0])+" "+str(s_list[-1])) + return s_list + + +def merge_sort(uns_list, str_idx, end_idx): + if len(uns_list) == 1: + return uns_list + mid = len(uns_list) // 2 + l_list = merge_sort(uns_list[:mid], str_idx, str_idx + mid-1) + r_list = merge_sort(uns_list[mid:], str_idx + mid, end_idx) + return merge(l_list, r_list, str_idx, end_idx) + + +n = int(input()) +stroka = list(map(int, input().split())) +stroka = merge_sort(stroka, 1, len(stroka)) +print(" ".join(map(str, stroka))) \ No newline at end of file diff --git a/yandex_contest/module2/task4.py b/yandex_contest/module2/task4.py new file mode 100644 index 0000000..0d502a4 --- /dev/null +++ b/yandex_contest/module2/task4.py @@ -0,0 +1,37 @@ +def inv(massiv, c): + + if len(M) > 1: + mid = len(massiv)//2 + left = massiv[:mid] + right = massiv[mid:] + + c = inv(left, c) + c = inv(right, c) + + i,j,k = 0,0,0 + while i < len(left) and j < len(right): + if left[i] <= right[j]: + massiv[k] = left[i] + i += 1 + else: + massiv[k] = right[j] + j = j+1 + c += len(left)-i + k = k+1 + while i < len(left): + massiv[k] = left[i] + i = i+1 + k = k+1 + while j < len(right): + massiv[k] = right[j] + j = j+1 + k = k+1 + + return c + + +inversions = 0 +n = int(input()) +M = list(map(int, input().split()))[:n] +inversions = inv(M, inversions) +print(inversions) \ No newline at end of file diff --git a/yandex_contest/module2/task5.py b/yandex_contest/module2/task5.py new file mode 100644 index 0000000..880cd49 --- /dev/null +++ b/yandex_contest/module2/task5.py @@ -0,0 +1,33 @@ +def merge(l_list, r_list): + s_list = [] + l_idx = 0 + r_idx = 0 + for i in range(len(l_list)+len(r_list)): + if l_idx < len(l_list) and r_idx < len(r_list): + if l_list[l_idx] <= r_list[r_idx]: + s_list.append(l_list[l_idx]) + l_idx += 1 + else: + s_list.append(r_list[r_idx]) + r_idx += 1 + s_list += l_list[l_idx:] + s_list += r_list[r_idx:] + return s_list + + +def merge_sort(uns_list): + if len(uns_list) == 1: + return uns_list + mid = len(uns_list) // 2 + l_list = merge_sort(uns_list[:mid]) + r_list = merge_sort(uns_list[mid:]) + return merge(l_list, r_list) + + +n = int(input()) +stroka = list(map(int, input().split())) +stroka = merge_sort(stroka) +for i in range(1, len(stroka)): + if stroka[i]==stroka[i-1]: + n-=1 +print(n) \ No newline at end of file diff --git a/yandex_contest/module2/task6.py b/yandex_contest/module2/task6.py new file mode 100644 index 0000000..52be1c3 --- /dev/null +++ b/yandex_contest/module2/task6.py @@ -0,0 +1,12 @@ +n = int(input()) +c = list(map(int,input().split())) +k = int(input()) +p = list(map(int,input().split())) +s = [0]*max(p) +for i in p: + s[i-1] += 1 +for i in range(n): + if c[i] >= s[i]: + print("no") + else: + print("yes") diff --git a/yandex_contest/module2/task7.py b/yandex_contest/module2/task7.py new file mode 100644 index 0000000..3a723e8 --- /dev/null +++ b/yandex_contest/module2/task7.py @@ -0,0 +1,33 @@ +massiv = [] +length = int(input()) +for i in range(length): + massiv.append(input()) +print("Initial array:") +print(', '.join(massiv)) +c = len(massiv[0]) +ph=0 +r=10 +for i in range(c-1,-1,-1): + ph+=1 + print('**********') + print(f'Phase {ph}') + b = [[] for _ in range(r)] + for j in range(len(massiv)): + b[ord(massiv[j][i]) - ord('0')].append(massiv[j]) + for j in range(r): + if len(b[j])==0: + print(f'Bucket {j}: empty') + else: + print("Bucket "+str(j)+": ", end='') + for now in range(len(b[j])-1): + print(b[j][now],end=', ') + print(b[j][len(b[j])-1]) + p = 0 + for j in range(r): + for k in range(len(b[j])): + massiv[p] = b[j][k] + p += 1 + +print('**********') +print("Sorted array:") +print(', '.join(massiv)) \ No newline at end of file diff --git a/yandex_contest/module3/task1.py b/yandex_contest/module3/task1.py new file mode 100644 index 0000000..72d1af6 --- /dev/null +++ b/yandex_contest/module3/task1.py @@ -0,0 +1,7 @@ +s = input() +t = input() +res = [] +for i in range(len(t), len(s)+1): + if t == s[i-len(t):i]: + res.append(i-len(t)) +print(" ".join(map(str,res))) \ No newline at end of file diff --git a/yandex_contest/module3/task2.py b/yandex_contest/module3/task2.py new file mode 100644 index 0000000..ff78e65 --- /dev/null +++ b/yandex_contest/module3/task2.py @@ -0,0 +1,39 @@ +def sdvig(s, t, q): + if s==t: + return 0 + else: + t*=2 + p = 11 + m = 1 + sh = 0 + for i in s[::-1]: + sh += ord(i) * m + m *= p + sh %= q + m %= q + + m = 1 + hash = 0 + for i in t[:len(s)][::-1]: + hash += m * ord(i) + m *= p + hash %= q + m %= q + + ht = 1 + for i in range(len(s) - 1): + ht *= p + ht %= q + for i in range(1, len(t) - len(s) + 1): + hn = ((hash % q - ord(t[i - 1]) * ht % q) * p % q + ord(t[i + len(s) - 1])) % q + if hn == sh: + return i + hash = hn + + return -1 + +s = input() +t = input() +q = 2**31 - 1 +print(sdvig(s,t,q)) + diff --git a/yandex_contest/module3/task3.py b/yandex_contest/module3/task3.py new file mode 100644 index 0000000..0238019 --- /dev/null +++ b/yandex_contest/module3/task3.py @@ -0,0 +1,27 @@ +def prefix(s): + dlina = len(s) + k = [0] * dlina + k[0] = 0 + for i in range(dlina - 1): + j = k[i] + while (j > 0) and (s[i + 1] != s[j]): + j = k[j - 1] + if (s[i + 1] == s[j]): + k[i+1] = j + 1 + else: + k[i + 1] = 0 + return k + + +inp = str(input()) + +dlina = len(inp) +pref = prefix(inp) + +for i in range(dlina - 1, dlina): + res = dlina - pref[i] + +if (dlina % res == 0): + print(dlina // res) +else: + print(1) diff --git a/yandex_contest/module3/task4.py b/yandex_contest/module3/task4.py new file mode 100644 index 0000000..967cdd9 --- /dev/null +++ b/yandex_contest/module3/task4.py @@ -0,0 +1,20 @@ +def prefix(s): + dlina = len(s) + k = [0] * dlina + k[0] = 0 + for i in range(dlina - 1): + j = k[i] + while (j > 0) and (s[i + 1] != s[j]): + j = k[j - 1] + if (s[i + 1] == s[j]): + k[i+1] = j + 1 + else: + k[i + 1] = 0 + return k + + +inp = str(input()) + +dlina = len(inp) +pref = prefix(inp) +print(dlina - pref[-1]) \ No newline at end of file diff --git a/yandex_contest/module4/task1.py b/yandex_contest/module4/task1.py new file mode 100644 index 0000000..1d285b1 --- /dev/null +++ b/yandex_contest/module4/task1.py @@ -0,0 +1,14 @@ +def play_station_portable(s): + k = 0 + massiv = [] + for i in s: + if (i == '('): + massiv.append(i) + elif (massiv != []) and (massiv[-1] == '('): + massiv.pop() + else: + k += 1 + return k+len(massiv) + +s = str(input()) +print(play_station_portable(s)) \ No newline at end of file diff --git a/yandex_contest/module4/task2.py b/yandex_contest/module4/task2.py new file mode 100644 index 0000000..79b8262 --- /dev/null +++ b/yandex_contest/module4/task2.py @@ -0,0 +1,21 @@ +n=int(input()) +s=list(map(int, input (). split())) +massiv=[] +bool_var= True +number=1 +for i in s: + for _ in range (len(massiv)): + if massiv and number == massiv[-1]: + massiv.pop() + number+=1 + if i!=number: + if massiv and i>massiv[-1]: + bool_var= False + break + massiv.append(i) + else: + number+=1 +if bool_var: + print ("YES") +else: + print ("NO") \ No newline at end of file diff --git a/yandex_contest/module4/task3.py b/yandex_contest/module4/task3.py new file mode 100644 index 0000000..a8a7bda --- /dev/null +++ b/yandex_contest/module4/task3.py @@ -0,0 +1,14 @@ +n=int(input()) +numbers=list(map(int,input().split())) +massiv=[] +idx=[0]*n +d={i:numbers[i] for i in range(n)} +for i in range(n-1,-1,-1): + while massiv and massiv[-1][1]>=d[i]: + massiv.pop() + if not massiv: + idx[i]=-1 + else: + idx[i]=massiv[-1][0] + massiv.append([i,d[i]]) +print(*idx) \ No newline at end of file diff --git a/yandex_contest/module4/task4.py b/yandex_contest/module4/task4.py new file mode 100644 index 0000000..f912513 --- /dev/null +++ b/yandex_contest/module4/task4.py @@ -0,0 +1,16 @@ +from collections import deque +n,k= map(int,input().split()) +numbers=list(map(int,input().split())) +dq=deque() +for i in range(k): + while dq and numbers[i] < numbers[dq[-1]]: + dq.pop() + dq.append(i) +print(numbers[dq[0]]) +for i in range(k,n): + while dq and dq[0] <=i-k: + dq.popleft() + while dq and numbers[i] <= numbers[dq[-1]]: + dq.pop() + dq.append(i) + print(numbers[dq[0]]) \ No newline at end of file diff --git a/yandex_contest/module5/task1.py b/yandex_contest/module5/task1.py new file mode 100644 index 0000000..78deafe --- /dev/null +++ b/yandex_contest/module5/task1.py @@ -0,0 +1,50 @@ +class Node: + __size = 0 + def __init__(self, data): + self.data = data + self.left = None + self.right = None + Node.__size += 1 + + def ff(self): + if self.left: + self.left.ff() + if not self.left and not self.right: + print(self.data) + if self.right: + self.right.ff() + + def add(self, value): + if value == self.data: + return + if value < self.data: + if self.left: + self.left.add(value) + else: + self.left = Node(value) + else: + if self.right: + self.right.add(value) + else: + self.right = Node(value) + + def ff(self): + if self.left: + self.left.ff() + if (self.left and self.right is None) or (self.left is None and self.right): + print(self.data) + if self.right: + self.right.ff() + +def btree(elements): + s = Node(elements[0]) + for i in range(1, len(elements)): + s.add(elements[i]) + return s + +numbers = [int(item) for item in input().split()] +numbers.pop() + +derevo = btree(numbers) + +derevo.ff() \ No newline at end of file diff --git a/yandex_contest/module5/task2.py b/yandex_contest/module5/task2.py new file mode 100644 index 0000000..179cd2f --- /dev/null +++ b/yandex_contest/module5/task2.py @@ -0,0 +1,46 @@ +class Node: + def __init__(self, data): + self.data = data + self.left = None + self.right = None + + def add(self, data): + if data == self.data: + return + if data < self.data: + if self.left: + self.left.add(data) + else: + self.left = Node(data) + else: + if self.right: + self.right.add(data) + else: + self.right = Node(data) + +def h(tree): + if tree is None: + return 0 + return max(h(tree.left), h(tree.right))+1 + +def ptree(elements): + root = Node(elements[0]) + for i in range(1, len(elements)): + root.add(elements[i]) + return root + +def balans(tree): + if not tree or ((h(tree.left) == h(tree.right) or h(tree.left)+1 == h(tree.right) or h(tree.left) == h(tree.right)+1) and balans(tree.right) and balans(tree.left)): + return True + return False + + +n = [int(i) for i in input().split()] +n.pop() +tree = ptree(n) + +if balans(tree): + print("YES") +else: + print("NO") + diff --git a/yandex_contest/module5/task3.py b/yandex_contest/module5/task3.py new file mode 100644 index 0000000..37cad3b --- /dev/null +++ b/yandex_contest/module5/task3.py @@ -0,0 +1,37 @@ +from math import gcd +def bld(v, l, r, segment_tree, nums): + if r - l == 1: + segment_tree[v] = nums[l] + return + m = (r+l)//2 + bld(2*v+1, l, m, segment_tree, nums) + bld(2 * v + 2, m, r, segment_tree, nums) + segment_tree[v] = gcd(segment_tree[2*v +1], segment_tree[2*v + 2]) + + +def nod(v, l, r, segment_tree, ql, qr): + if ql <= l and qr >= r: + return segment_tree[v] + if ql >= r or qr<=l: + return 0 + m = (r+l)//2 + st_l = int(nod(2*v +1, l, m, segment_tree, ql, qr)) + st_r = int(nod(2*v + 2, m, r, segment_tree, ql, qr)) + return gcd(st_l, st_r) + + + + +n = int(input()) +numbers = list(map(int, input().split()))[:n] +segment_tree = [0]*4*n +bld(0, 0, n, segment_tree, numbers) +q = int(input()) +massiv = [] +while q != 0: + l, r = map(int, input().split()) + massiv.append(int(nod(0, 0, n, segment_tree, l-1, r))) + q -= 1 +print(*massiv) + + diff --git a/yandex_contest/module5/task4.py b/yandex_contest/module5/task4.py new file mode 100644 index 0000000..6dae6fb --- /dev/null +++ b/yandex_contest/module5/task4.py @@ -0,0 +1,50 @@ +def bld(v,l,r,segment_tree,nums): + if r - l == 1: + segment_tree[v]= nums[l] + return + m=(r+l)//2 + bld(2*v+1,l,m,segment_tree,nums) + bld(2*v+2,m,r,segment_tree,nums) + segment_tree[v] = (segment_tree[2*v+1]+segment_tree[2*v+2]) + +def get_sum(v,l,r,segment_tree,ql,qr): + if ql<=l and qr>=r: + return segment_tree[v] + if ql >= r or qr<=l: + return 0 + m = (r+l)//2 + st_l = get_sum (2*v+1,l,m,segment_tree,ql,qr) + st_r = get_sum (2*v+2,m,r,segment_tree,ql,qr) + return st_l + st_r + +def find_k(v, l, r, segment_tree, k): + if k > segment_tree[v]: + return -1 + if r - l == 1: + return r + m = (r+l)//2 + if segment_tree[2*v+1] >= k: + return find_k(2*v+1, l, m, segment_tree, k) + else: + return find_k(2*v+2, m, r, segment_tree, k - segment_tree[2*v+1]) + + +n=int(input()) +numbers= [1 if int(i)==0 else 0 for i in input().split()] +segment_tree = [0]*4*n +bld(0,0,n,segment_tree,numbers) +q = int(input()) +massiv = [] +while q !=0: + l,r,k = map(int, input().split()) + sum = get_sum(0, 0, n, segment_tree, l-1, r) + if sum >= k and l == 1: + massiv.append(find_k(0, 0, n, segment_tree, k)) + elif sum >= k and l > 1: + massiv.append(find_k(0, 0, n, segment_tree, k +(get_sum(0, 0, n, segment_tree, 0, l-1)))) + else: + massiv.append(-1) + q-=1 +print(*massiv) + + diff --git a/yandex_contest/module5/task5.py b/yandex_contest/module5/task5.py new file mode 100644 index 0000000..60541a5 --- /dev/null +++ b/yandex_contest/module5/task5.py @@ -0,0 +1,50 @@ +from math import gcd +def bld(v, l, r, segment_tree, nums): + if r - l == 1: + segment_tree[v] = nums[l] + return + m = (r+l)//2 + bld(2*v+1, l, m, segment_tree, nums) + bld(2*v + 2, m, r, segment_tree, nums) + segment_tree[v] = gcd(segment_tree[2*v +1], segment_tree[2*v + 2]) + +def update(v, l, r, segment_tree, indx, value): + if r - l == 1: + segment_tree[v] = value + return + m = (l+r)//2 + if indx < m: + update(2*v + 1, l, m, segment_tree, indx, value) + else: + update(2*v + 2,m, r, segment_tree,indx, value) + segment_tree[v] = gcd(segment_tree[2*v +1], segment_tree[2*v + 2]) + + +def nod(v, l, r, segment_tree, ql, qr): + if ql <= l and qr >= r: + return segment_tree[v] + if ql >= r or qr<=l: + return 0 + m = (r+l)//2 + st_l = nod(2*v +1, l, m, segment_tree, ql, qr) + st_r = nod(2*v + 2, m, r, segment_tree, ql, qr) + return gcd(st_l, st_r) + + + +n = int(input()) +numbers = list(map(int, input().split()))[:n] +segment_tree = [0]*4*n +bld(0, 0, n, segment_tree, numbers) +q = int(input()) +massiv = [] +while q != 0: + type_q, l, r = map(str, input().split()) + if type_q == 's': + massiv.append(nod(0, 0, n, segment_tree, int(l)-1, int(r))) + else: + update(0, 0, n, segment_tree, int(l)-1, int(r)) + q -= 1 +print(*massiv) + + diff --git a/yandex_contest/module6/task1.py b/yandex_contest/module6/task1.py new file mode 100644 index 0000000..3cd821e --- /dev/null +++ b/yandex_contest/module6/task1.py @@ -0,0 +1,67 @@ +def shift_up(idx, heap): + while heap[idx] > heap[(idx-1) // 2] and (idx-1) // 2 >= 0: + heap[idx], heap[(idx-1) // 2] = heap[(idx-1) // 2], heap[idx] + idx = (idx-1) // 2 + return idx + 1 + +def shift_down(idx, heap): + while 2 * idx + 1 < len(heap): + left_index = 2 * idx + 1 + right_index = 2 * idx + 2 + child_index = left_index + + if right_index < len(heap) and heap[left_index] < heap[right_index]: + child_index = right_index + + if heap[child_index] <= heap[idx]: + break + + heap[idx], heap[child_index] = heap[child_index], heap[idx] + idx = child_index + return idx + 1 + + +def get_max(heap): + return heap[0] + + +def add(i, heap): + heap.append(i) + return shift_up(len(heap)-1, heap) + + +def extract(heap): + heap[0], heap[len(heap)-1] = heap[len(heap)-1], heap[0] + idx = heap.pop() + if heap: + return shift_down(0, heap), idx + else: + return 0, idx + + +heap = [] +result = [] +num = input().split() +t1, t2 = int(num[0]), int(num[1]) + +for _ in range(t2): + data = input().split() + if int(data[0]) == 1: + if heap == []: + result.append(-1) + else: + result.append(extract(heap)) + + elif int(data[0]) == 2: + if len(heap) == t1: + result.append(-1) + else: + result.append(add(int(data[-1]), heap)) + +for item in result: + if type(item) == tuple: + print(*item) + else: + print(item) + +print(*heap) \ No newline at end of file diff --git a/yandex_contest/module6/task2.py b/yandex_contest/module6/task2.py new file mode 100644 index 0000000..af5b05c --- /dev/null +++ b/yandex_contest/module6/task2.py @@ -0,0 +1,74 @@ +def shift_up(idx, heap): + while heap[idx] > heap[(idx-1)//2] and (idx-1)//2 >= 0: + heap[idx], heap[(idx-1)//2] = heap[(idx-1)//2], heap[idx] + idx = (idx-1)//2 + return idx+1 + +def shift_down(idx, heap): + while 2*idx+1 < len(heap): + left_index = 2*idx+1 + right_index = 2*idx+2 + child_index = left_index + if right_index < len(heap) and heap[left_index] < heap[right_index]: + child_index = right_index + if heap[child_index] <= heap[idx]: + break + heap[idx], heap[child_index] = heap[child_index], heap[idx] + idx = child_index + return idx+1 + +def add(item, heap): + heap.append(item) + return shift_up(len(heap)-1, heap) + +def direct_extract(idx, heap): + val = heap[len(heap)-1] + heap[idx-1], heap[len(heap)-1] = heap[len(heap)-1], heap[idx-1] + ind = heap.pop() + if val < ind: + shift_down(idx-1, heap) + elif val > ind: + shift_up(idx-1, heap) + return ind + +def extract(heap): + heap[0], heap[len(heap)-1] = heap[len(heap)-1], heap[0] + ind = heap.pop() + if heap: + return shift_down(0, heap), ind + else: + return 0, ind + +def get_max(heap): + return heap[0] + + +heap = [] +result = [] +input_ = input().split() +n, m = int(input_[0]), int(input_[1]) +for i in range(m): + data = input().split() + if int(data[0]) == 1: + if not heap: + result.append(-1) + else: + result.append(extract(heap)) + elif int(data[0]) == 2: + if len(heap) == n: + result.append(-1) + else: + result.append(add(int(data[-1]), heap)) + elif int(data[0]) == 3: + if len(heap) >= int(data[-1]) and int(data[-1]) > 0: + result.append(direct_extract(int(data[-1]), heap)) + else: + result.append(-1) + +for i in result: + if type(i) == tuple: + print(*i) + else: + print(i) +print(*heap) + diff --git a/yandex_contest/module6/task3.py b/yandex_contest/module6/task3.py new file mode 100644 index 0000000..bfb1a50 --- /dev/null +++ b/yandex_contest/module6/task3.py @@ -0,0 +1,48 @@ +from collections import * + +class Heap: + def __init__(self, arr): + self.arr = arr + + def delete(self): + self.arr[0], self.arr[self.length-1] = self.arr[self.length-1], self.arr[0] + max = self.arr.pop() + self.shift_down(0) + return max + + def display(self): + print(*self.arr) + + def shift_down(self, idx): + while 2 * idx + 1 < self.length: + left_index = 2 * idx + 1 + right_index = 2 * idx + 2 + child_index = left_index + if right_index < self.length and self.arr[left_index] < self.arr[right_index]: + child_index = right_index + if self.arr[child_index] <= self.arr[idx]: + break + self.arr[idx], self.arr[child_index] = self.arr[child_index], self.arr[idx] + idx = child_index + + @property + def length(self): + return len(self.arr) + + +def bld(arr): + heap = arr[:] + first_heap = Heap(heap) + for item in range(len(heap)-1, -1, -1): + first_heap.shift_down(item) + return first_heap + + +n = int(input()) +numbers = list(map(int, input().split())) +heap = bld(numbers) +res = deque() +while heap.length: + heap.display() + res.appendleft(heap.delete()) +print(*res) \ No newline at end of file