#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; vector k; vector val; char buf[3]; vector V[1 << 15]; //values int pl; int mi; long long int dp[1 << 15][16][16]; long long int value[1 << 15]; long long int value2[1 << 15]; int main() { int n; scanf("%d", &n); int summ = 0; for (int i = 0;i < n;i++) { char s; scanf("%s", buf); s = buf[0]; if (s == '+') { k.push_back(1); pl++; } if (s == '-') { k.push_back(-1); mi++; } if (isdigit(s)) { val.push_back(s - '0'); summ += s - '0'; } } sort(val.begin(), val.end()); reverse(val.begin(), val.end()); for (int i = 1;i < (1 << val.size());i++) { long long int vv = 0; for (int j = 0;j < val.size();j++) { if ((i >> j) & 1) { vv *= 10LL; vv += val[j]; } } value[i] = vv; } for (int i = 1;i < (1 << val.size());i++) { long long int vv = 0; for (int j = val.size()-1;j >=0;j--) { if ((i >> j) & 1) { vv *= 10LL; vv += val[j]; } } value2[i] = vv; } for (int ii = 0;ii < (1 << val.size());ii++) { for (int i = 1;i < (1 << val.size());i++) { if ((ii&i)) { } else { V[ii].push_back(i); } } } for (int i = 0;i < (1 << 15);i++) { for (int j = 0;j < 16;j++) { for (int k = 0;k < 16;k++) { dp[i][j][k] = INT_MIN; } } } for (int i = 1;i < (1 << val.size());i++) { dp[i][0][0] = value[i]; } for (int i = 0;i < (1 << val.size())-1;i++) { for (int j = 0;j <=pl;j++) { for (int k = 0;k <= mi;k++) { if (dp[i][j][k] == INT_MIN) { continue; } if (j == pl && k == mi) { continue; } for (int kk = 0;kk < V[i].size();kk++) { //plus if (j != pl) { dp[i | V[i][kk]][j + 1][k] = max(dp[i | V[i][kk]][j + 1][k], dp[i][j][k] + value[V[i][kk]]); } if (k != mi) { dp[i | V[i][kk]][j][k+1] = max(dp[i | V[i][kk]][j][k+1], dp[i][j][k] - value2[V[i][kk]]); } } } } } long long int ans = dp[(1 << val.size()) - 1][pl][mi]; for (int i = 0;i < (1 << 15);i++) { for (int j = 0;j < 16;j++) { for (int k = 0;k < 16;k++) { dp[i][j][k] = INT_MAX; } } } for (int i = 1;i < (1 << val.size());i++) { dp[i][0][0] = value2[i]; } for (int i = 0;i < (1 << val.size()) - 1;i++) { for (int j = 0;j <= pl;j++) { for (int k = 0;k <= mi;k++) { if (dp[i][j][k] == INT_MAX) { continue; } if (j == pl && k == mi) { continue; } for (int kk = 0;kk < V[i].size();kk++) { //plus if (j != pl) { dp[i | V[i][kk]][j + 1][k] = min(dp[i | V[i][kk]][j + 1][k], dp[i][j][k] + value2[V[i][kk]]); } if (k != mi) { dp[i | V[i][kk]][j][k + 1] = min(dp[i | V[i][kk]][j][k + 1], dp[i][j][k] - value[V[i][kk]]); } } } } } long long int ans2 = dp[(1 << val.size()) - 1][pl][mi]; printf("%lld %lld\n", ans, ans2); return 0; }