import java.util.Scanner;
class HuffmanNode {
int freq;
HuffmanNode left;
HuffmanNode right;
}
public class Main {
public static void heapify(int A[], int i, int size) {
int left = Left(i);
int right = Right(i);
int max = i;
if (left < size && A[left] > A[max])
max = left;
if (right < size && A[right] > A[max])
max = right;
if (max != i) {
int temp = A[i];
A[i] = A[max];
A[max] = temp;
heapify(A, max, size);
}
}
public static void buildHeap(int A[], int size) {
for (int i = size / 2 - 1; i >= 0; i--) {
heapify(A, i, size);
}
}
public static void heapSort(int A[], int size) {
buildHeap(A, size);
for (int treeSize = size - 1; treeSize >= 0; treeSize--) {
int temp = A[0];
A[0] = A[treeSize];
A[treeSize] = temp;
heapify(A, 0, treeSize);
}
}
public static int Left(int i) {
return i * 2 + 1;
}
public static int Right(int i) {
return i * 2 + 2;
}
public static int Parent(int i) {
return (int) (i / 2);
}
public static int HeapMax(int[] A) {
return A[0];
}
public static int HeapExtractMax(int[] A) {
if (A.length < 0) {
return 0;
}
int max = A[0];
A[0] = A[A.length - 1];
return max;
}
public static void HeapIncreaseKey(int[] A, int i, HuffmanNode key) {
if (key.freq <= A[i]) {
return;
}
A[i] = key.freq;
while (i > 0 && A[Parent(i)] < A[i]) {
int temp = A[i];
A[i] = A[Parent(i)];
A[Parent(i)] = temp;
i = Parent(i);
}
}
public static void MaxHeapInsert(int[] A, HuffmanNode key) {
int AHeapSize = A.length - 1;
HeapIncreaseKey(A, AHeapSize, key);
}
public static int[] LossArr(int[] A) {
int[] temp = new int[A.length - 1];
for (int i = 0; i < A.length - 1; i++) {
temp[i] = A[i];
}
return temp;
}
public static int[] AddArr(int[] A) {
int[] temp = new int[A.length + 1];
for (int i = 0; i < A.length + 1; i++) {
if (i < A.length) {
temp[i] = A[i];
} else {
}
}
return temp;
}
public static HuffmanNode Huffman(int[] C) {
int[] Q = C;
heapSort(Q, Q.length);
HuffmanNode root = new HuffmanNode();
HuffmanNode rootTemp1 = new HuffmanNode();
HuffmanNode rootTemp2 = new HuffmanNode();
HuffmanNode rootTemp3 = new HuffmanNode();
int mrtPoint = 0, rtPoint1 = 0, rtPoint2 = 0, rtPoint3 = 0;
for (int i = 0; i < C.length - 1; i++) {
HuffmanNode x = new HuffmanNode();
if (Q[0] != root.freq && Q[0] != rootTemp1.freq && Q[0] != rootTemp2.freq && Q[0] != rootTemp3.freq) {
x.freq = HeapExtractMax(Q);
} else if (Q[0] == root.freq && mrtPoint == 1) {
x = root;
HeapExtractMax(Q);
// mrtPoint=0;
// root=null;
} else if (Q[0] == rootTemp1.freq && rtPoint1 == 1) {
x = rootTemp1;
HeapExtractMax(Q);
rtPoint1 = 0;
} else if (Q[0] == rootTemp2.freq && rtPoint2 == 1) {
x = rootTemp2;
HeapExtractMax(Q);
rtPoint2 = 0;
} else if (Q[0] == rootTemp3.freq && rtPoint3 == 1) {
x = rootTemp3;
HeapExtractMax(Q);
rtPoint3 = 0;
}
int[] temp = LossArr(Q);
Q = temp;
heapSort(Q, Q.length);
HuffmanNode y = new HuffmanNode();
if (Q[0] != root.freq && Q[0] != rootTemp1.freq && Q[0] != rootTemp2.freq && Q[0] != rootTemp3.freq
|| Q[0] == x.freq) {
y.freq = HeapExtractMax(Q);
} else if (Q[0] == root.freq && mrtPoint == 1) {
y = root;
HeapExtractMax(Q);
mrtPoint = 0;
} else if (Q[0] == rootTemp1.freq && rtPoint1 == 1) {
y = rootTemp1;
HeapExtractMax(Q);
rtPoint1 = 0;
} else if (Q[0] == rootTemp2.freq && rtPoint2 == 1) {
y = rootTemp2;
HeapExtractMax(Q);
rtPoint2 = 0;
} else if (Q[0] == rootTemp3.freq && rtPoint3 == 1) {
y = rootTemp3;
HeapExtractMax(Q);
rtPoint3 = 0;
}
temp = LossArr(Q);
Q = temp;
heapSort(Q, Q.length);
HuffmanNode z = new HuffmanNode();
z.left = x;
z.right = y;
z.freq = x.freq + y.freq;
if (rtPoint1 == 0 && mrtPoint == 1) {
rootTemp1 = root;
rtPoint1 = 1;
} else if (rtPoint1 == 1 && rtPoint2 == 0) {
rootTemp2 = root;
rtPoint2 = 1;
} else {
rootTemp3 = root;
rtPoint3 = 1;
}
root = z;
mrtPoint = 1;
temp = AddArr(Q);
Q = temp;
MaxHeapInsert(Q, z);
heapSort(Q, Q.length);
}
return root;
}
public static void SaveCode
(HuffmanNode root,
String s
) { if (root.left == null && root.right == null) {
root.data = s;
return;
}
SaveCode(root.left, s + "0");
SaveCode(root.right, s + "1");
}
public static void FindHuffman(HuffmanNode root, int find) {
if (root.left == null && root.right == null) {
if (root.freq == find) {
System.
out.
println(root.
data); return;
}
return;
}
FindHuffman(root.left, find);
FindHuffman(root.right, find);
}
public static void main
(String[] args
) {
Scanner sc
= new Scanner
(System.
in); int size;
size = sc.nextInt();
if (size <= 0) {
return;
}
int arr[] = new int[size];
int[] temp = new int[size];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
temp[i] = arr[i];
}
HuffmanNode root = Huffman(temp);
SaveCode(root, "");
for (int i = 0; i < arr.length; i++) {
FindHuffman(root, arr[i]);
}
}
}