#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; using mint = atcoder::modint998244353; vector pw10(10); pair build_min_max(vector v) { int m = v.size(); assert(m % 2 == 0 && m <= 10); ll x = 0, y = 0; rep(i, 0, m / 2) x = x * 10 + v[i]; rep(i, m / 2, m) y += v[i] * pw10[i - m / 2]; return {x, y}; } ll build_abs_min(vector v) { sort(v.begin(), v.end()); int m = v.size(); assert(m % 2 == 0); ll res = 1e18; rep(i, 0, m - 1) { auto w = v; w.erase(w.begin() + i); w.erase(w.begin() + i); auto [x, y] = build_min_max(w); x += pw10[m / 2 - 1] * v[i + 1]; y += pw10[m / 2 - 1] * v[i]; chmin(res, abs(x - y)); } return res; } void solve() { pw10[0] = 1; rep(i, 1, 10) pw10[i] = pw10[i - 1] * 10; int n; cin >> n; vector c(10); rep(i, 0, n) { int t; cin >> t; c[t]++; } if (n & 1) { mint x = 0, y = 0; int szx = (n + 1) / 2, szy = n / 2; for (int i = 1; i <= 9; i++) { while (szx > 0 && c[i] > 0) { szx--; c[i]--; x = x * 10 + i; } } for (int i = 9; i >= 1; i--) { while (szy > 0 && c[i] > 0) { szy--; c[i]--; y = y * 10 + i; } } cout << (x - y).val() << '\n'; } else { vector odd; rep(i, 0, 10) if (c[i] & 1) odd.push_back(i); ll res = 1e18; chmin(res, build_abs_min(odd)); rep(i, 1, 10) if (c[i] > 0 && c[i] % 2 == 0) { odd.push_back(i); odd.push_back(i); chmin(res, build_abs_min(odd)); odd.pop_back(); odd.pop_back(); } cout << res << '\n'; } } int main() { int t = 1; // cin >> t; while (t--) solve(); }