// yukicoder: No.297 カードの数式 // 2019.7.7 bal4u #include #include #define gc() getchar() int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } char p, m, f[10]; int N; int main() { int i; long long a, mi, ma; char pp, mm, g[10]; N = in(); for (i = 0; i < N; i++) { int a = gc(); gc(); if (a == '+') p++; else if (a == '-') m++; else f[a & 0xf]++; } // 最大値 ma = 0, pp = p, mm = m; memcpy(g, f, sizeof(g)); for (i = 0; i < 10 && (mm | pp); i++) if (g[i]) { if (mm) { if (mm >= g[i]) { mm -= g[i], ma -= i*g[i], g[i] = 0; continue; } ma -= i*mm, g[i] -= mm, mm = 0; } if (pp) { if (pp >= g[i]) pp -= g[i], ma += i*g[i], g[i] = 0; else ma += i*pp, g[i] -= pp, pp = 0; } } a = 0; for (i = 9; i >= 0; i--) { while (g[i]) a = a * 10 + i, g[i]--; } ma += a; // 最小値 mi = 0; if (m) { m--, a = -1; for (i = 0; i < 10; i++) if (f[i]) { if (a < 0) { a = i; if (--f[i] == 0) continue; } if (p) { if (p >= f[i]) { p -= f[i], mi += i*f[i], f[i] = 0; continue; } mi += i*p, f[i] -= p, p = 0; } if (m) { if (m >= f[i]) m -= f[i], mi -= i*f[i], f[i] = 0; else mi -= i*m, f[i] -= m, m = 0; } } mi += a, a = 0; for (i = 9; i >= 0; i--) { while (f[i]) a = a * 10 + i, f[i]--; } mi -= a; } else { int k = ++p, s = 0; long long b = 1; for (i = 9; i >= 0; i--) { while (f[i]) { s += i, f[i]--; if (--k == 0) mi += b*s, k = p, s = 0, b *= 10; } } } printf("%lld %lld\n", ma, mi); return 0; }