#include #include #include #include int max(std::list v, int plus, int minus){ v.sort(); int ans = 0; while(v.size() != plus + minus){ ans = ans * 10 + v.back(); v.pop_back(); } while(plus){ ans += v.back(); v.pop_back(); plus--; } v.push_back(ans); for (size_t i = 0; i < minus; i++) { int num = v.back(); v.pop_back(); int num2 = v.back(); v.pop_back(); v.push_back(num - num2); v.sort(); } return v.front(); } int min(std::list v, int plus, int minus){ v.sort(); if(minus == 0){ int s = v.size() / (plus + 1); if(v.size() % 2 == 1){ int num = 0; for (size_t i = 0; i < s; i++) { num = num * 10 + v.front(); v.pop_front(); } v.push_back(num); } for (size_t i = 0; i < plus + 1; i++) { int num = 0; for (size_t j = 0; j < s; j++) { num = num * 10 + v.front(); v.pop_front(); } v.push_back(num); v.sort(); } while(plus){ int num = v.front(); v.pop_front(); int num2 = v.front(); v.pop_front(); v.push_back(num + num2); plus--; } return v.front(); } int ans = 0; while(v.size() != minus + plus){ ans = ans * 10 + v.back(); v.pop_back(); } if(ans != 0)v.push_back(ans); v.sort(); while(plus){ int num = v.back(); v.pop_back(); num += v.back(); v.pop_back(); v.push_back(num); plus--; } while(v.size() != 1){ int mi = v.front();v.pop_front(); int ma = v.front();v.pop_front(); v.push_back(mi - ma); v.sort(); } return v.front(); } int main(){ int n; std::cin >> n; std::list v; int plus = 0, minus = 0; for(int i = 0; i < n; i++) { char c; std::cin >> c; if(c == '+') plus++; else if(c == '-') minus++; else v.push_back(c - '0'); } std::list v2 = v; int m = max(v, plus, minus); int mi = min(v2, plus, minus); std::cout << m << " " << mi << std::endl; return 0; }