#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define endl '\n'
#define fer(i, m, n) for (int i = m; i < n; ++i)
const int MOD = 1e9 + 7;
const int N = 2e5 + 2;
struct HuffNode
{
int w;
HuffNode *left;
HuffNode *right;
HuffNode(int w) : w(w), left(nullptr), right(nullptr) {}
};
struct cmp
{
bool operator()(HuffNode *a, HuffNode *b)
{
return a->w > b->w;
}
};
HuffNode *BuildTree(vector<int> w)
{
if (w.empty())
return nullptr;
priority_queue<HuffNode *, vector<HuffNode *>, cmp> pq;
fer(i, 0, w.size())
{
HuffNode *node = new HuffNode(w[i]);
pq.push(node);
}
while (pq.size() > 1)
{
HuffNode *left = pq.top();
pq.pop();
HuffNode *right = pq.top();
pq.pop();
HuffNode *par = new HuffNode(left->w + right->w);
par->left = left, par->right = right;
pq.push(par);
}
return pq.top();
}
void encode(HuffNode *&T, unordered_map<int, string> &codes, string code)
{
if (!T)
return;
if (!T->left && !T->right)
codes[T->w] = code;
else
{
encode(T->left, codes, code + "0");
encode(T->right, codes, code + "1");
}
}
string decode(HuffNode *root, string code)
{
string ans = "";
HuffNode *cur = root;
for (char c : code)
{
if (c == '0')
cur = cur->left;
else
cur = cur->right;
if (!cur->left && !cur->right)
{
ans += to_string(cur->w) + ' ';
cur = root;
}
}
return ans;
}
void preorder(HuffNode *T)
{
if (!T)
cout << -1 << ' ';
else
{
cout << T->w << ' ';
if (T->left)
preorder(T->left);
else
cout << -1 << ' ';
if (T->right)
preorder(T->right);
else
cout << -1 << ' ';
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> weight(n);
fer(i, 0, n) cin >> weight[i];
HuffNode *root = BuildTree(weight);
unordered_map<int, string> codes;
encode(root, codes, "");
for (auto it : codes)
cout << it.first << ' ' << it.second << endl;
string code;
cin >> code;
cout << decode(root, code) << endl;
return 0;
}