//Kéo xuống dưới copy test :Đ
//Đề Bài: Tìm số lớn nhất tạo ra bởi việc ghép 2 số có tổng các chữ số giống nhau
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define all(x) x.begin(), x.end()
using namespace std;
const int maxn = 1e5 + 1;
ll A[maxn];
vector<ll> D[46];
pair<ll, ll> scs[maxn];
int sumDigit(int n) {
int s = 0;
while (n != 0) {
s += n % 10;
n /= 10;
}
return s;
}
int cntDigit(int n) {
int cnt = 0;
while (n != 0) {
cnt++;
n /= 10;
}
return cnt;
}
ll lt[12];
void tenEn() {
lt[0] = 1;
for (int i = 1; i <= 10; ++i) {
lt[i] = lt[i - 1] * 10;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("file.inp","r",stdin);
// freopen("file.out","w",stdout);
tenEn();
int n;
ll res = 0;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> A[i];
}
sort(A + 1, A + n + 1);
for (int i = 1; i <= n; ++i) {
D[sumDigit(A[i])].push_back(A[i]);
}
for (int i = 0; i <= 45; ++i) {
if (D[i].size() < 2) continue;
ll a = (D[i][D[i].size() - 1] * lt[scs[D[i].size() - 2].first] + D[i][D[i].size() - 2]);
ll b = (D[i][D[i].size() - 2] * lt[scs[D[i].size() - 1].first] + D[i][D[i].size() - 1]);
res = max(res, max(a, b));
}
cout << res;
}
Ly9Lw6lvIHh14buRbmcgZMaw4bubaSBjb3B5IHRlc3QgOsSQCi8vxJDhu4EgQsOgaTogVMOsbSBz4buRIGzhu5tuIG5o4bqldCB04bqhbyByYSBi4bufaSB2aeG7h2MgZ2jDqXAgMiBz4buRIGPDsyB04buVbmcgY8OhYyBjaOG7ryBz4buRIGdp4buRbmcgbmhhdQojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgojZGVmaW5lIHVsbCB1bnNpZ25lZCBsb25nIGxvbmcKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBhbGwoeCkgeC5iZWdpbigpLC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