#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 dp2[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'; } } if (pl == 1&&mi==0) { return 1; } 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; } const int k2 = 1 << val.size(); for (int ii = 0;ii < k2;ii++) { for (int i = 1;i < k2;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] = LLONG_MIN; dp2[i][j][k] = LLONG_MAX; } } } for (int i = 1;i < k2;i++) { dp[i][0][0] = value[i]; } for (int i = 1;i < (1 << val.size());i++) { dp2[i][0][0] = value2[i]; } for (int i = 0;i < k2 - 1;i++) { for (int j = 0;j <= pl;j++) { for (int k = 0;k <= mi;k++) { if (dp[i][j][k] == LLONG_MIN) { } else { 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]]); } } } if (dp2[i][j][k] == LLONG_MAX) { continue; } if (j == pl && k == mi) { continue; } int t = V[i].size(); for (int kk = 0;kk dp2[i][j][k] + value2[V[i][kk]]) { dp2[i | V[i][kk]][j + 1][k] = dp2[i][j][k] + value2[V[i][kk]]; } if (k != mi&&dp2[i | V[i][kk]][j][k + 1]>dp2[i][j][k] - value[V[i][kk]]) { dp2[i | V[i][kk]][j][k + 1] = dp2[i][j][k] - value[V[i][kk]]; } } } } } long long int ans = dp[(1 << val.size()) - 1][pl][mi]; long long int ans2 = dp2[(1 << val.size()) - 1][pl][mi]; printf("%lld %lld\n", ans, ans2); return 0; }