#include #include #include using namespace std; int main() { int n; int dig[10]; int plus=0, minus=0; for (int i = 0; i < 10; i++) dig[i] = 0; cin >> n; for (int i = 0; i < n; i++) { char c; cin >> c; if (c == '+') { plus++; continue; } if (c == '-') { minus++; continue; } int k = c - 48; dig[k]++; } int dig_temp[10]; int plus_temp = plus, minus_temp = minus; for (int i = 0; i < 10; i++) dig_temp[i] = dig[i]; //----get max---- int max = 0; int k = 0; while (minus) { if (dig[k]) { max -= k; dig[k]--; minus--; } else { k++; } } while (plus) { if (dig[k]) { max += k; dig[k]--; plus--; } else { k++; } } int sub_max = 0; k = 9; while (k >= 0) { if (dig[k]) { sub_max = sub_max *10 + k; dig[k]--; } else { k--; } } max += sub_max; //---get min--- plus = plus_temp; minus = minus_temp; for (int i = 0; i < 10; i++) dig[i] = dig_temp[i]; int min = 0; if (plus == 0) { plus++; minus--; } k = 0; while (plus) { if (dig[k]) { min += k; dig[k]--; plus--; } else { k++; } } while (minus) { if (dig[k]) { min -= k; dig[k]--; minus--; } else { k++; } } int sub_min = 0; k = 9; while (k >= 0) { if (dig[k]) { sub_min = sub_min * 10 + k; dig[k]--; } else { k--; } } min = min - sub_min; // --- end---- cout << max << " " << min << endl; return 0; }