module main; // https://yukicoder.me/submissions/74134 より import std; import std.ascii : isDigit; void main() { // 入力 int N = readln.chomp.to!int; auto C = readln.split.join; // 答えの計算 int[] num; int plus = 0, minus = 0; foreach (c; C) { if (isDigit(c)) num ~= c - '0'; else if (c == '+') plus++; else minus++; } // 最大値 num.sort; int[] num2 = num.dup; long max = 0; foreach (i; 0 .. minus) { max -= num2[0]; num2.popFront; } foreach (i; 0 .. plus) { max += num2[0]; num2.popFront; } long add = 0; foreach_reverse (n; num2) { add *= 10; add += n; } max += add; // 最小値 long min = 0; if (minus == 0) { // - がない場合 num2 = num.dup.reverse; auto pow10 = 1L.recurrence!((a,n) => a[n-1] * 10).take(16).array; foreach (i; 0 .. num2.length) min += num2[i] * pow10[i / (plus + 1)]; } else { // - がある場合 num2 = num.dup; foreach (i; 0 .. plus + 1) { min += num2[0]; num2.popFront; } foreach (i; 0 .. minus - 1) { min -= num2[0]; num2.popFront; } add = 0; foreach_reverse (i; 0 .. num2.length) { add *= 10; add += num2[i]; } min -= add; } // 答えの出力 writefln("%d %d", max, min); }