Problem:
式
与えられた 4 つの 1 から 9 の整数を使って、答えが 10 になる式をつくります。
 4 つの整数
a, b, c, d
を入力したとき、下記の条件に従い、答えが 10 になる式を出力するプログラムを作成してください。また、答えが複数ある時は、最初に見つかった答えだけを出力するものとします。答えがない時は、0 と出力してください。
演算子として、加算 (+)、減算 (-)、乗算 (*) だけを使います。除算 (/) は使いません。使用できる演算子は３個です。
数を4つとも使わなければいけません。
4つの数の順番は自由に入れ換えてかまいません。
カッコを使ってもかまいません。使用できるカッコは３組（６個）以下です。
Input
複数のデータセットが与えられます。各データセットの形式は以下のとおり：
a
b
c
d
入力は４つの 0 で終了します。データセットの数は 40 を超えません。
Output
各データセットについて、与えられた 4 つの整数と上記の演算記号およびカッコを組み合わせて値が 10 となる式または 0 を１行に出力してください。式の文字列が 1024 文字を超えてはいけません。
Sample Input
8 7 9 9
4 4 4 4
5 5 7 5
0 0 0 0
Output for the Sample Input
((9 * (9 - 7)) - 8)
0
((7 * 5) - (5 * 5))

Submitted solutions:

code_snippet: #pragma region Macros

// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2")

#include <bits/extc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using namespace __gnu_pbds;

// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// using Bint = mp::cpp_int;
// using Bdouble = mp::number<mp::cpp_dec_float<128>>;

#define TO_STRING(var) # var
#define pb emplace_back
#define int ll
#define endl '\n'
#define sqrt __builtin_sqrtl

using ll = long long;
using ld = long double;
const ld PI = acos(-1);
const int INF = 1 << 30;
const ll INFL = 1LL << 61;
const int MOD = 998244353;
// const int MOD = 1000000007;

const ld EPS = 1e-10;
const bool equals(ld a, ld b) { return fabs((a) - (b)) < EPS; }

const vector<int> dx = {0, 1, 0, -1, 1, 1, -1, -1}; // → ↓ ← ↑ ↘ ↙ ↖ ↗
const vector<int> dy = {1, 0, -1, 0, 1, -1, -1, 1};

struct Edge {
    int from, to;
    int cost;
    Edge(int to, int cost) : from(-1), to(to), cost(cost) {}
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
    Edge &operator=(const int &x) {
        to = x;
        return *this;
    }
};

__attribute__((constructor))
void constructor() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(12);
}

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

int POW(int x, int n) {
    __int128_t ret = 1;
    // if (ret >= INFL) return INFL;
    if (n < 0) { cout << "error" << endl; return 0; }
    else if (x == 1 or n == 0) ret = 1;
    else if (x == -1 && n % 2 == 0) ret = 1; 
    else if (x == -1) ret = -1; 
    else if (n % 2 == 0) ret = POW(x * x, n / 2);
    else ret = x * POW(x, n - 1);

    if (ret > 8e18) ret = 0;
    return ret;
}
int floor(int x, int y) { return (x > 0 ? x / y : (x - y + 1) / y); }
int ceil(int x, int y) { return (x > 0 ? (x + y - 1) / y : x / y); }
ll per(int x, int y) {
    if (y == 0) {
        cout << "error" << endl;
        return INFL;
    }
    if (x >= 0 && y > 0) return x / y;
    if (x >= 0 && y < 0) return x / y - (x % y < 0);
    if (x < 0 && y < 0) return x / y + (x % y < 0);
    // if (x < 0 && y > 0) 
    return x / y - (x % y < 0);
}
ll mod(int x, int y) {
    if (y == 0) {
        cout << "error" << endl;
        return INFL;
    }
    if (x >= 0 && y > 0) return x % y;
    if (x >= 0 && y < 0) return x % y;
    if (x < 0 && y < 0) {
        __int128_t ret = x % y;
        ret += (__int128_t)abs(y) * INFL;
        ret %= abs(y);
        return ret;
    }
    // if (x < 0 && y > 0) {
        __int128_t ret = x % y;
        ret += (__int128_t)abs(y) * INFL;
        ret %= abs(y);
        return ret;
    // }
}
int modf(ld x) {
    if (equals(x, 0)) return 0;
	else if (x < 0) return ceill(x);
	else return floorl(x);
}

template <class T> bool chmax(T &a, const T& b) {
    if (a < b) { a = b; return true; }
    return false;
}
template <class T> bool chmin(T &a, const T& b) {
    if (a > b) { a = b; return true; }
    return false;
}

int countl_zero(int N) { return __builtin_clzll(N); }
int countl_one(int N) {
    int ret = 0; while (N % 2) { N /= 2; ret++; }
    return ret;
}
int countr_zero(int N) { return __builtin_ctzll(N); }
int countr_one(int N) {
    int ret = 0, k = 63 - __builtin_clzll(N);
    while (k != -1 && (N & (1LL << k))) { k--; ret++; }
    return ret;
}
int popcount(int N) { return __builtin_popcountll(N); }
int unpopcount(int N) { return 64 - __builtin_clzll(N) - __builtin_popcountll(N); }

int top_bit(int N) { return 63 - __builtin_clzll(N);} // 2^kの位
int bot_bit(int N) { return __builtin_ctz(N);} // 2^kの位
int MSB(int N) { return 1 << (63 - __builtin_clzll(N)); } // mask

int bit_width(int N) { return 64 - __builtin_clzll(N); } // 桁数
int ceil_log2(int N) { return 63 - __builtin_clzll(N); }
int bit_floor(int N) { return 1 << (63 - __builtin_clzll(N)); }
int floor_log2(int N) { return 64 - __builtin_clzll(N-1); }
int bit_ceil(int N) { return 1 << (64 - __builtin_clzll(N-1)) - (N==1); }

class UnionFind {
public:
	UnionFind() = default;
    UnionFind(int N) : par(N), sz(N, 1) {
        iota(par.begin(), par.end(), 0);
    }

	int root(int x) {
		if (par[x] == x) return x;
		return (par[x] = root(par[x]));
	}

	bool unite(int x, int y) {
		int rx = root(x);
		int ry = root(y);

        if (rx == ry) return false;
		if (sz[rx] < sz[ry]) swap(rx, ry);

		sz[rx] += sz[ry];
		par[ry] = rx;

        return true;
	}

	bool issame(int x, int y) { return (root(x) == root(y)); }
	int size(int x) { return sz[root(x)]; }

    vector<vector<int>> groups(int N) {
        vector<vector<int>> G(N);
        for (int x = 0; x < N; x++) {
            G[root(x)].push_back(x);
        }
		G.erase(
            remove_if(G.begin(), G.end(),
                [&](const vector<int>& V) { return V.empty(); }),
                    G.end());
        return G;
    }

private:
	vector<int> par;
	vector<int> sz;
};

template<int mod> class Modint{
public:
    int val = 0;
    Modint(int x = 0) { while (x < 0) x += mod; val = x % mod; }
    Modint(const Modint &r) { val = r.val; }

    Modint operator -() { return Modint(-val); } // 単項
    Modint operator +(const Modint &r) { return Modint(*this) += r; }
    Modint operator +(const int &q) { Modint r(q); return Modint(*this) += r; }
    Modint operator -(const Modint &r) { return Modint(*this) -= r; }
    Modint operator -(const int &q) { Modint r(q); return Modint(*this) -= r; }
    Modint operator *(const Modint &r) { return Modint(*this) *= r; }
    Modint operator *(const int &q) { Modint r(q); return Modint(*this) *= r; }
    Modint operator /(const Modint &r) { return Modint(*this) /= r; }
    Modint operator /(const int &q) { Modint r(q); return Modint(*this) /= r; }
    
    Modint& operator ++() { val++; if (val >= mod) val -= mod; return *this; } // 前置
    Modint operator ++(signed) { ++*this; return *this; } // 後置
    Modint& operator --() { val--; if (val < 0) val += mod; return *this; }
    Modint operator --(signed) { --*this; return *this; }
    Modint &operator +=(const Modint &r) { val += r.val; if (val >= mod) val -= mod; return *this; }
    Modint &operator +=(const int &q) { Modint r(q); val += r.val; if (val >= mod) val -= mod; return *this; }
    Modint &operator -=(const Modint &r) { if (val < r.val) val += mod; val -= r.val; return *this; }
    Modint &operator -=(const int &q) { Modint r(q);  if (val < r.val) val += mod; val -= r.val; return *this; }
    Modint &operator *=(const Modint &r) { val = val * r.val % mod; return *this; }
    Modint &operator *=(const int &q) { Modint r(q); val = val * r.val % mod; return *this; }
    Modint &operator /=(const Modint &r) {
        int a = r.val, b = mod, u = 1, v = 0;
        while (b) {int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v);}
        val = val * u % mod; if (val < 0) val += mod;
        return *this;
    }
    Modint &operator /=(const int &q) {
        Modint r(q); int a = r.val, b = mod, u = 1, v = 0;
        while (b) {int t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v);}
        val = val * u % mod; if (val < 0) val += mod;
        return *this;
    }

    bool operator ==(const Modint& r) { return this -> val == r.val; }
    bool operator <(const Modint& r) { return this -> val < r.val; }
    bool operator !=(const Modint& r) { return this -> val != r.val; }
};

using mint = Modint<MOD>;

istream &operator >>(istream &is, mint& x) {
    int t; is >> t;
    x = t;
    return (is);
}
ostream &operator <<(ostream &os, const mint& x) {
    return os << x.val;
}
mint modpow(const mint &x, int n) {
    if (n == 0) return 1;
    mint t = modpow(x, n / 2);
    t = t * t;
    if (n & 1) t = t * x;
    return t;
}

int modpow(__int128_t x, int n, int mod) {
    __int128_t ret = 1;
    while (n > 0) {
        if (n % 2 == 1) ret = ret * x % mod;
        x = x * x % mod;
        n /= 2;
    }
    return ret;
}

int modinv(__int128_t x, int mod) {
    if (x == 1) return 1;
    return mod - modinv(mod % x, mod) * (mod / x) % mod;
}

istream &operator >>(istream &is, __int128_t& x) {
    string S; is >> S;
    __int128_t ret = 0;
    int f = 1;
    if (S[0] == '-') f = -1; 
    for (int i = 0; i < S.length(); i++)
        if ('0' <= S[i] && S[i] <= '9')
            ret = 10 * ret + S[i] - '0';
    x = ret * f;
    return (is);
}
ostream &operator <<(ostream &os, __int128_t x) {
    ostream::sentry s(os);
    if (s) {
        __uint128_t tmp = x < 0 ? -x : x;
        char buffer[128];
        char *d = end(buffer);

        do {
            --d;
            *d = "0123456789"[tmp % 10];
            tmp /= 10;
        } while (tmp != 0);

        if (x < 0) {
            --d;
            *d = '-';
        }
        int len = end(buffer) - d;

        if (os.rdbuf()->sputn(d, len) != len) {
            os.setstate(ios_base::badbit);
        }
    }
    return os;
}

string to_string(char c) {
    string s = {c};
    return s;
}

vector<mint> _fac, _finv, _inv;
void COMinit(int N) {
    _fac.resize(N + 1);
    _finv.resize(N + 1);
    _inv.resize(N + 1);
    _fac[0] = _fac[1] = 1;
    _finv[0] = _finv[1] = 1;
    _inv[1] = 1;
    for (int i = 2; i <= N; i++) {
        _fac[i] = _fac[i-1] * mint(i);
        _inv[i] = -_inv[MOD % i] * mint(MOD / i);
        _finv[i] = _finv[i - 1] * _inv[i];
    }
}

mint COM(int N, int K) {
    if (N < K) return 0;
    if (N < 0 or K < 0) return 0;
    return _fac[N] * _finv[K] * _finv[N - K];
}

#pragma endregion

using T = int;
T calc_revpo(vector<string> &S) {
    int N = S.size();
    vector<T> st;
    for (int i = 0; i < N; i++) {
        if (S[i] == "+") {
            T b = st.back();
            st.pop_back();
            T a = st.back();
            st.pop_back();
            st.pb(a + b);
        } else if (S[i] == "-") {
            T b = st.back();
            st.pop_back();
            T a = st.back();
            st.pop_back();
            st.pb(a - b);
        } else if (S[i] == "*") {
            T b = st.back();
            st.pop_back();
            T a = st.back();
            st.pop_back();
            st.pb(a * b);
        } else st.pb(stoll(S[i]));
    }
    return st.back();
}

string decode_revpo(vector<string> &S) {
    vector<string> st;
    int N = S.size();
    for (int i = 0; i < N; i++) {
        if (S[i][0] >= '0' && S[i][0] <= '9') st.push_back(S[i]);
        else {
            string B = st.back();
            st.pop_back();
            string A = st.back();
            st.pop_back();

            // 演算子が「*」「/」で、演算子の前の式が
            // 「宙ぶらりんの+ -」を含むとき括弧をつける
            if (S[i] == "*") {
                int depth = 0;
                int sz = A.size();
                for (int j = 0; j < sz; j++) {
                    if (A[j] == '(') depth++;
                    if (A[j] == ')') depth--;

                    if (depth == 0 && (A[j] == '+' or A[j] == '-')) {
                        A = "(" + A + ")";
                        break;
                    }
                }
            }

            // 演算子が「-」「*」で、演算子の後の式が
            // 「宙ぶらりんの+ -」を含むとき括弧をつける
            if (S[i] == "-" or S[i] == "*") {
                int depth = 0;
                int sz = B.size();
                for (int j = 0; j < sz; j++) {
                    if (B[j] == '(') depth++;
                    if (B[j] == ')') depth--;

                    if (depth == 0 && (B[j] == '+' or B[j] == '-')) {
                        B = "(" + B + ")";
                        break;
                    }
                }
            }

            if (S[i] == "+") st.push_back(A + " + " + B);
            if (S[i] == "-") st.push_back(A + " - " + B);
            if (S[i] == "*") st.push_back(A + " * " + B);
        }
    }
    return st.back();
}

// 数字を1, 演算子を0とした01列Bが逆ポーランド記法としてありえるか
// 括弧列のような判定方法
bool isvalid(vector<int> &B) {
    int N = B.size();

    int depth = B[0];
    for (int i = 1; i < N; i++) {
        if (depth == 0) return false;
        if (B[i]) depth++;
        else depth--;
    }
    return true;
}

signed main() {
    while (true) {
        int N = 4, M = 10;
        vector<int> A(N);
        for (int i = 0; i < N; i++) cin >> A[i];
        sort(A.begin(), A.end());
        if (A[0] + A[1] + A[2] + A[3] == 0) break;

        vector<int> B(N + N - 1, 1); // 01列。1が数字、0が演算子
        for (int i = 0; i < N - 1; i++) B[i] = 0;

        int ans = 0;
        string S;
        do {
            if (isvalid(B)) { // 逆ポの記法としてありえるなら
                sort(A.begin(), A.end());
                do {
                    int p = POW(3LL, N - 1);
                    for (int j = 0; j < p; j++) {
                        vector<string> T;
                        int curA = 0, curC = 0;

                        vector<string> C;
                        int J = j;
                        while (J) {
                            int r = J % 3;
                            J /= 3;

                            if (r == 0) C.pb("+");
                            if (r == 1) C.pb("-");
                            if (r == 2) C.pb("*");
                        }
                        while (C.size() < N - 1) C.pb("+");

                        for (int k = 0; k < N + N - 1; k++) {
                            if (B[k]) T.pb(to_string(A[curA])), curA++;
                            else T.pb(C[curC]), curC++;
                        }
                        if (abs(calc_revpo(T) - M) < EPS) {
                            S = decode_revpo(T);
                            ans++;
                        }
                    }
                } while (next_permutation(A.begin(), A.end()));
            }
        } while (next_permutation(B.begin(), B.end()));
        if (ans == 0) cout << ans << endl;
        else cout << S << endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3432
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define ALL(x) (x).begin(), (x).end()  // sortなどの引数を省略したい
#ifdef _DEBUG
#define PRE_COMMAND             \
    std::cin.rdbuf(in.rdbuf()); \
    std::cout << fixed << setprecision(15);
#else
#define PRE_COMMAND cout << fixed << setprecision(15);
#endif
const double EPS = 1e-10, PI = acos(-1.0);
template <class T> auto MAX(T& seq) {
    return *max_element(seq.begin(), seq.end());
}
template <class T> auto MIN(T& seq) {
    return *min_element(seq.begin(), seq.end());
}
template <class T> auto SUM(T& seq) {
    T temp{0};
    auto& temp2 = temp[0];
    return accumulate(seq.begin(), seq.end(), temp2);
}
template <class T> void SORT(T& seq) { sort(seq.begin(), seq.end()); }
template <class T, class S> void SORT(T& seq, S& sort_order) {
    sort(seq.begin(), seq.end(), sort_order);
}
template <class T> void SORTR(vector<T>& seq) {
    sort(seq.begin(), seq.end(), greater<T>());
}
template <class T, class S, class R> long long pow(T n_0, S k_0, R mod_0) {
    long long n = n_0 % mod_0, k = k_0, mod = mod_0, now = 1;
    while (k) {
        if (k & 1) now = (now * n) % mod;
        n = (n * n) % mod;
        k >>= 1;
    }
    return now;
}
template <typename T> istream& operator>>(istream& is, vector<T>& vec) {
    for (T& x : vec) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    if (!v.size()) return os;
    typename vector<T>::const_iterator ii = v.begin();
    os << *ii++;
    for (; ii != v.end(); ++ii) os << " " << *ii;
    return os;
}

// カンマ区切りのデータをvector<int>に変換
vector<int> comma_sep(string inp) {
    inp += ',';
    vector<int> v;
    string s = "";
    for (char c : inp) {
        if (c == ',') {
            v.push_back(stoi(s));
            s = "";
        } else {
            s += c;
        }
    }
    return v;
}

template <class T>
vector<vector<T>> permutations_parts(vector<T> p, int x, int s, int n) {
    vector<vector<T>> ret;
    if (x == s) {
        for (int i = x; i < n; i++) ret.push_back({p[i]});
        return ret;
    }
    for (int i = x; i < n; i++) {
        vector<T> v = p;
        swap(v[x], v[i]);
        vector<vector<T>> v2 = permutations_parts(v, x + 1, s, n);
        T q = v[x];
        int size = (int)v2.size();
        for (int i = 0; i < size; i++) { v2[i].push_back(q); }
        copy(v2.begin(), v2.end(), back_inserter(ret));
    }
    return ret;
}
template <class T> vector<vector<T>> permutations(vector<T> v, int size = -1) {
    int n = (int)v.size();
    if (size == -1) size = n;
    if (size == n) {
        vector<vector<T>> ret;
        int ret_size = 1;
        for (int i = size; i > 1; i--) ret_size *= i;
        ret.reserve(ret_size);
        do { ret.push_back(v); } while (next_permutation(v.begin(), v.end()));
        return ret;
    }
    return permutations_parts(v, 0, size - 1, n);
}

template <class T> vector<vector<T>> permutations2(vector<T> v, int size = -1) {
    int n = (int)v.size();
    if (size == -1) size = n;
    if (size == n) {
        vector<vector<T>> ret;
        int ret_size = 1;
        for (int i = size; i > 1; i--) ret_size *= i;
        ret.reserve(ret_size);
        do { ret.push_back(v); } while (next_permutation(v.begin(), v.end()));
    }
    return permutations_parts(v, 0, size - 1, n);
}

int main() {
    PRE_COMMAND
    int a, b, c, d;
    string a_s, b_s, c_s, d_s, s;
    while (cin >> a_s >> b_s >> c_s >> d_s) {
        if (a_s == "0") break;
        s = "0";
        int ans, ans2;
        vector<string> v2 = {a_s, b_s, c_s, d_s};
        vector<vector<string>> v3 = permutations(v2);
        vector<vector<string>> v4 = permutations2(v2);
        SORT(v4);
        for (string c1 : {" + ", " - ", " * "}) {
            for (string c2 : {" + ", " - ", " * "}) {
                for (string c3 : {" + ", " - ", " * "}) {
                    for (auto v : v4) {
                        a_s = v[0], b_s = v[1], c_s = v[2], d_s = v[3];
                        a = stoi(a_s), b = stoi(b_s), c = stoi(c_s),
                        d = stoi(d_s);
                        // ((a+b)+c)+d
                        ans = 0;
                        if (c1 == " + ") ans = a + b;
                        if (c1 == " - ") ans = a - b;
                        if (c1 == " * ") ans = a * b;
                        if (c2 == " + ") ans = ans + c;
                        if (c2 == " - ") ans = ans - c;
                        if (c2 == " * ") ans = ans * c;
                        if (c3 == " + ") ans = ans + d;
                        if (c3 == " - ") ans = ans - d;
                        if (c3 == " * ") ans = ans * d;
                        if (ans == 10)
                            s = "(((" + a_s + c1 + b_s + ")" + c2 + c_s + ")" +
                                c3 + d_s + ")";
                        // (a+b)+(c+d)
                        ans = 0, ans2 = 0;
                        if (c1 == " + ") ans = a + b;
                        if (c1 == " - ") ans = a - b;
                        if (c1 == " * ") ans = a * b;
                        if (c3 == " + ") ans2 = c + d;
                        if (c3 == " - ") ans2 = c - d;
                        if (c3 == " * ") ans2 = c * d;
                        if (c2 == " + ") ans = ans + ans2;
                        if (c2 == " - ") ans = ans - ans2;
                        if (c2 == " * ") ans = ans * ans2;
                        if (ans == 10)
                            s = "((" + a_s + c1 + b_s + ")" + c2 + "(" + c_s +
                                c3 + d_s + "))";
                        // (a+(b+c))+d
                        ans = 0;
                        if (c2 == " + ") ans = b + c;
                        if (c2 == " - ") ans = b - c;
                        if (c2 == " * ") ans = b * c;
                        if (c1 == " + ") ans = a + ans;
                        if (c1 == " - ") ans = a - ans;
                        if (c1 == " * ") ans = a * ans;
                        if (c3 == " + ") ans = ans + d;
                        if (c3 == " - ") ans = ans - d;
                        if (c3 == " * ") ans = ans * d;
                        if (ans == 10)
                            s = "((" + a_s + c1 + "(" + b_s + c2 + c_s + "))" +
                                c3 + d_s + ")";
                        // a+((b+c)+d)
                        ans = 0;
                        if (c2 == " + ") ans = b + c;
                        if (c2 == " - ") ans = b - c;
                        if (c2 == " * ") ans = b * c;
                        if (c3 == " + ") ans = ans + d;
                        if (c3 == " - ") ans = ans - d;
                        if (c3 == " * ") ans = ans * d;
                        if (c1 == " + ") ans = a + ans;
                        if (c1 == " - ") ans = a - ans;
                        if (c1 == " * ") ans = a * ans;
                        if (ans == 10)
                            s = "(" + a_s + c1 + "((" + b_s + c2 + c_s + ")" +
                                c3 + d_s + "))";
                        // a+(b+(c+d))
                        ans = 0;
                        if (c3 == " + ") ans = c + d;
                        if (c3 == " - ") ans = c - d;
                        if (c3 == " * ") ans = c * d;
                        if (c2 == " + ") ans = b + ans;
                        if (c2 == " - ") ans = b - ans;
                        if (c2 == " * ") ans = b * ans;
                        if (c1 == " + ") ans = a + ans;
                        if (c1 == " - ") ans = a - ans;
                        if (c1 == " * ") ans = a * ans;
                        if (ans == 10)
                            s = "(" + a_s + c1 + "(" + b_s + c2 + "(" + c_s +
                                c3 + d_s + ")))";
                    }
                }
            }
        }
        cout << s << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3200
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define ALL(x) (x).begin(), (x).end()  // sortなどの引数を省略したい
#ifdef _DEBUG
#define PRE_COMMAND             \
    std::cin.rdbuf(in.rdbuf()); \
    std::cout << fixed << setprecision(15);
#else
#define PRE_COMMAND cout << fixed << setprecision(15);
#endif
const double EPS = 1e-10, PI = acos(-1.0);
template <class T> auto MAX(T& seq) {
    return *max_element(seq.begin(), seq.end());
}
template <class T> auto MIN(T& seq) {
    return *min_element(seq.begin(), seq.end());
}
template <class T> auto SUM(T& seq) {
    T temp{0};
    auto& temp2 = temp[0];
    return accumulate(seq.begin(), seq.end(), temp2);
}
template <class T> void SORT(T& seq) { sort(seq.begin(), seq.end()); }
template <class T, class S> void SORT(T& seq, S& sort_order) {
    sort(seq.begin(), seq.end(), sort_order);
}
template <class T> void SORTR(vector<T>& seq) {
    sort(seq.begin(), seq.end(), greater<T>());
}
template <class T, class S, class R> long long pow(T n_0, S k_0, R mod_0) {
    long long n = n_0 % mod_0, k = k_0, mod = mod_0, now = 1;
    while (k) {
        if (k & 1) now = (now * n) % mod;
        n = (n * n) % mod;
        k >>= 1;
    }
    return now;
}
template <typename T> istream& operator>>(istream& is, vector<T>& vec) {
    for (T& x : vec) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    if (!v.size()) return os;
    typename vector<T>::const_iterator ii = v.begin();
    os << *ii++;
    for (; ii != v.end(); ++ii) os << " " << *ii;
    return os;
}

// カンマ区切りのデータをvector<int>に変換
vector<int> comma_sep(string inp) {
    inp += ',';
    vector<int> v;
    string s = "";
    for (char c : inp) {
        if (c == ',') {
            v.push_back(stoi(s));
            s = "";
        } else {
            s += c;
        }
    }
    return v;
}

template <class T>
vector<vector<T>> permutations_parts(vector<T> p, int x, int s, int n) {
    vector<vector<T>> ret;
    if (x == s) {
        for (int i = x; i < n; i++) ret.push_back({p[i]});
        return ret;
    }
    for (int i = x; i < n; i++) {
        vector<T> v = p;
        swap(v[x], v[i]);
        vector<vector<T>> v2 = permutations_parts(v, x + 1, s, n);
        T q = v[x];
        int size = (int)v2.size();
        for (int i = 0; i < size; i++) { v2[i].push_back(q); }
        copy(v2.begin(), v2.end(), back_inserter(ret));
    }
    return ret;
}
template <class T> vector<vector<T>> permutations(vector<T> v, int size = -1) {
    int n = (int)v.size();
    if (size == -1) size = n;
    if (size == n) {
        vector<vector<T>> ret;
        int ret_size = 1;
        for (int i = size; i > 1; i--) ret_size *= i;
        ret.reserve(ret_size);
        do { ret.push_back(v); } while (next_permutation(v.begin(), v.end()));
        //return ret;
    }
    return permutations_parts(v, 0, size - 1, n);
}

int main() {
    PRE_COMMAND
    int a, b, c, d;
    string a_s, b_s, c_s, d_s, s;
    while (cin >> a_s >> b_s >> c_s >> d_s) {
        if (a_s == "0") break;
        s = "0";
        int ans, ans2;
        vector<string> v2 = {a_s, b_s, c_s, d_s};
        vector<vector<string>> v3 = permutations(v2);
        for (string c1 : {" + ", " - ", " * "}) {
            for (string c2 : {" + ", " - ", " * "}) {
                for (string c3 : {" + ", " - ", " * "}) {
                    for (auto v : v3) {
                        a_s = v[0], b_s = v[1], c_s = v[2], d_s = v[3];
                        a = stoi(a_s), b = stoi(b_s), c = stoi(c_s),
                        d = stoi(d_s);
                        // ((a+b)+c)+d
                        ans = 0;
                        if (c1 == " + ") ans = a + b;
                        if (c1 == " - ") ans = a - b;
                        if (c1 == " * ") ans = a * b;
                        if (c2 == " + ") ans = ans + c;
                        if (c2 == " - ") ans = ans - c;
                        if (c2 == " * ") ans = ans * c;
                        if (c3 == " + ") ans = ans + d;
                        if (c3 == " - ") ans = ans - d;
                        if (c3 == " * ") ans = ans * d;
                        if (ans == 10)
                            s = "(((" + a_s + c1 + b_s + ")" + c2 + c_s + ")" +
                                c3 + d_s + ")";
                        // (a+b)+(c+d)
                        ans = 0, ans2 = 0;
                        if (c1 == " + ") ans = a + b;
                        if (c1 == " - ") ans = a - b;
                        if (c1 == " * ") ans = a * b;
                        if (c3 == " + ") ans2 = c + d;
                        if (c3 == " - ") ans2 = c - d;
                        if (c3 == " * ") ans2 = c * d;
                        if (c2 == " + ") ans = ans + ans2;
                        if (c2 == " - ") ans = ans - ans2;
                        if (c2 == " * ") ans = ans * ans2;
                        if (ans == 10)
                            s = "((" + a_s + c1 + b_s + ")" + c2 + "(" + c_s +
                                c3 + d_s + "))";
                        // (a+(b+c))+d
                        ans = 0;
                        if (c2 == " + ") ans = b + c;
                        if (c2 == " - ") ans = b - c;
                        if (c2 == " * ") ans = b * c;
                        if (c1 == " + ") ans = a + ans;
                        if (c1 == " - ") ans = a - ans;
                        if (c1 == " * ") ans = a * ans;
                        if (c3 == " + ") ans = ans + d;
                        if (c3 == " - ") ans = ans - d;
                        if (c3 == " * ") ans = ans * d;
                        if (ans == 10)
                            s = "((" + a_s + c1 + "(" + b_s + c2 + c_s + "))" +
                                c3 + d_s + ")";
                        // a+((b+c)+d)
                        ans = 0;
                        if (c2 == " + ") ans = b + c;
                        if (c2 == " - ") ans = b - c;
                        if (c2 == " * ") ans = b * c;
                        if (c3 == " + ") ans = ans + d;
                        if (c3 == " - ") ans = ans - d;
                        if (c3 == " * ") ans = ans * d;
                        if (c1 == " + ") ans = a + ans;
                        if (c1 == " - ") ans = a - ans;
                        if (c1 == " * ") ans = a * ans;
                        if (ans == 10)
                            s = "(" + a_s + c1 + "((" + b_s + c2 + c_s + ")" +
                                c3 + d_s + "))";
                        // a+(b+(c+d))
                        ans = 0;
                        if (c3 == " + ") ans = c + d;
                        if (c3 == " - ") ans = c - d;
                        if (c3 == " * ") ans = c * d;
                        if (c2 == " + ") ans = b + ans;
                        if (c2 == " - ") ans = b - ans;
                        if (c2 == " * ") ans = b * ans;
                        if (c1 == " + ") ans = a + ans;
                        if (c1 == " - ") ans = a - ans;
                        if (c1 == " * ") ans = a * ans;
                        if (ans == 10)
                            s = "(" + a_s + c1 + "(" + b_s + c2 + "(" + c_s +
                                c3 + d_s + ")))";
                    }
                }
            }
        }
        cout << s << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3176
final_rank: 6

code_snippet: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    vector<char> v(4);
    string A = "+-*";
    while(true){
        for (auto &&i : v) scanf(" %c", &i);
        if(v[0] == '0') break;
        auto solve = [&](string &s) -> string {
            stack<pair<string, int>> S;
            for (auto &&i : s) {
                if(isdigit(i)) S.emplace(string(1, i), i-'0');
                else if(i == '+'){
                    if(S.size() < 2) return {};
                    auto a = S.top(); S.pop();
                    auto b = S.top(); S.pop();
                    S.emplace("(" + a.first + "+" + b.first + ")", a.second+b.second);
                }else if(i == '*'){
                    if(S.size() < 2) return {};
                    auto a = S.top(); S.pop();
                    auto b = S.top(); S.pop();
                    S.emplace("(" + a.first + "*" + b.first + ")", a.second*b.second);
                }else if(i == '-'){
                    if(S.size() < 2) return {};
                    auto a = S.top(); S.pop();
                    auto b = S.top(); S.pop();
                    S.emplace("(" + a.first + "-" + b.first + ")", a.second-b.second);
                }
            }
            if(S.top().second != 10) return {};
            else return S.top().first;
        };
        int ok = 0;
        for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) for (int k = 0; k < 3; ++k) {
             string s = string(1, v[0])+v[1]+v[2]+v[3]+A[i]+A[j]+A[k];
             sort(s.begin(),s.end());
             do {
                 auto ret = solve(s);
                 if(!ret.empty()){
                     cout << ret << "\n";
                     i = j = k = 3;
                     ok = 1;
                     break;
                 }
             }while(next_permutation(s.begin(),s.end()));
        }
        if(!ok) puts("0");
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3100
final_rank: 10

code_snippet: #include <bits/stdc++.h>
//typedef
//-------------------------#include <bits/stdc++.h>

const double pi = 3.141592653589793238462643383279;

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s)
{
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x)
{
  ostringstream sout;
  sout << x;
  return sout.str();
}
inline int readInt()
{
  int x;
  scanf("%d", &x);
  return x;
}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-8;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

class UnionFind
{
public:
  vector<int> par;
  vector<int> siz;

  UnionFind(int sz_) : par(sz_), siz(sz_, 1)
  {
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  void init(int sz_)
  {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }

  int root(int x)
  {
    while (par[x] != x)
    {
      x = par[x] = par[par[x]];
    }
    return x;
  }

  bool merge(int x, int y)
  {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y)
  {
    return root(x) == root(y);
  }

  int size(int x)
  {
    return siz[root(x)];
  }
};

ll modPow(ll x, ll n, ll mod = MOD)
{
  ll res = 1;
  while (n)
  {
    if (n & 1)
      res = (res * x) % mod;

    res %= mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

#define SIEVE_SIZE 5000000 + 10
bool sieve[SIEVE_SIZE];
void makeSieve()
{
  for (int i = 0; i < SIEVE_SIZE; ++i)
    sieve[i] = true;
  sieve[0] = sieve[1] = false;
  for (int i = 2; i * i < SIEVE_SIZE; ++i)
    if (sieve[i])
      for (int j = 2; i * j < SIEVE_SIZE; ++j)
        sieve[i * j] = false;
}

bool isprime(ll n)
{
  if (n == 0 || n == 1)
    return false;
  for (ll i = 2; i * i <= n; ++i)
    if (n % i == 0)
      return false;
  return true;
}

const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit()
{
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++)
  {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k)
{
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

long long extGCD(long long a, long long b, long long &x, long long &y)
{
  if (b == 0)
  {
    x = 1;
    y = 0;
    return a;
  }
  long long d = extGCD(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
// 負の数にも対応した mod (a = -11 とかでも OK)
inline long long mod(long long a, long long m)
{
  return (a % m + m) % m;
}

// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m)
{
  long long x, y;
  extGCD(a, m, x, y);
  return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}
ll GCD(ll a, ll b)
{

  if (b == 0)
    return a;
  return GCD(b, a % b);
}

typedef vector<ll> vec;
typedef vector<vec> mat;

mat mul(mat &A, mat &B)
{
  mat C(A.size(), vec((int)B[0].size()));
  for (int i = 0; i < A.size(); ++i)
  {
    for (int k = 0; k < B.size(); ++k)
    {
      for (int j = 0; j < B[0].size(); ++j)
      {
        C[i][j] = (C[i][j] + A[i][k] % MOD * B[k][j] % MOD) % MOD;
      }
    }
  }
  return C;
}
mat matPow(mat A, ll n)
{
  mat B(A.size(), vec((int)A.size()));

  for (int i = 0; i < A.size(); ++i)
  {
    B[i][i] = 1;
  }

  while (n > 0)
  {
    if (n & 1)
      B = mul(B, A);
    A = mul(A, A);
    n >>= 1;
  }
  return B;
}

const int INF = 1e9 + 7;
#define repi(i, m, n) for (int i = m; i < n; i++)
#define fi first
#define se second
class Arithmetic
{
public:
  string str;
  int n, flag = 1;
  void init(string s)
  {
    str = s;
    n = str.size();
  }
  int add(int a, int b) { return a + b; }
  int mul(int a, int b) { return a * b; }
  int div(int a, int b) { return a / b; }
  int sur(int a, int b) { return a % b; }

  P get_num(int p)
  {
    int ans = 0, q = -1, f = 0;
    if (str[p] == '(')
      return calc(p + 1, 0);
    if (str[p] == '-')
    {
      f = 1;
      p++;
    }
    if (str[p] == '(')
    {
      P pi = calc(p + 1, 0);
      return P(-pi.fi, pi.se);
    }
    repi(i, p, n)
    {
      if (!isdigit(str[i]))
        break;
      ans = ans * 10 + (str[i] - '0');
      q = i;
    }
    if (f)
      return P(-ans, q);
    return P(ans, q);
  }

  P calc(int pp = 0, int f = 0)
  {
    P pi = get_num(pp);
    int ans = pi.fi, p = pi.se + 1;
    repi(i, p, n)
    {
      if (f and str[i] == ')')
        return P(ans, i - 1);
      if (str[i] == ')')
        return P(ans, i);
      if (str[i] == '*' or str[i] == '/' or str[i] == '%')
      {
        pi = get_num(i + 1);
        if (str[i] == '*')
          ans = mul(ans, pi.fi);
        if (str[i] == '/')
          ans = div(ans, pi.fi);
        if (str[i] == '%')
          ans = sur(ans, pi.fi);
      }
      if (str[i] == '+' or str[i] == '-')
      {
        if (f)
          return P(ans, i - 1);
        if (str[i] == '+')
          pi = calc(i + 1, 1);
        if (str[i] == '-')
          pi = calc(i, 1);
        ans = add(ans, pi.fi);
      }
      i = pi.se;
    }
    return P(ans, n - 1);
  }
  int solve(int ret = -INF)
  { //0除算のとき返す値
    flag = 1;
    P ans = calc();
    if (!flag)
      return ret;
    return ans.fi;
  }
};

string ans = "";

void rec(int idx, string s, vector<string> &v, bool p)
{
  if (idx == 3)
  {
    s += v[idx];
    if (p)
    {
      s += ')';
    }
    Arithmetic solver;
    solver.init(s);
    if (solver.solve() == 10)
    {
      ans = s;
    }
    return;
  }
  if (!p)
  {
    s += v[idx];
    rec(idx + 1, s + '+', v, false);
    rec(idx + 1, s + '*', v, false);
    rec(idx + 1, s + '-', v, false);
    s.pop_back();
    s += '(';
    s += v[idx];
    rec(idx + 1, s + '+', v, true);
    rec(idx + 1, s + '*', v, true);
    rec(idx + 1, s + '-', v, true);
  }
  else
  {
    s += v[idx];
    rec(idx + 1, s + '+', v, true);
    rec(idx + 1, s + '*', v, true);
    rec(idx + 1, s + '-', v, true);

    s.pop_back();
    s += v[idx];
    s += ')';
    rec(idx + 1, s + '+', v, false);
    rec(idx + 1, s + '*', v, false);
    rec(idx + 1, s + '-', v, false);
  }
}
int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  vector<string> v(4);
  while (cin >> v[0] >> v[1] >> v[2] >> v[3])
  {
    if (v[0] == "0" && v[1] == "0" && v[2] == "0" && v[3] == "0")
      break;

    ans = "";
    sort(all(v));
    do
    {
      string t = "";
      rec(0, "", v, false);

    } while (next_permutation(all(v)));
    if (ans == "")
      cout << 0 << endl;
    else
    {
      string tmp = "";
      for (int i = 0; i < ans.size(); i++)
      {
        if (ans[i] == '*' || ans[i] == '+' || ans[i] == '-')
        {
          tmp += " ";
          tmp += ans[i];
          tmp += " ";
        }
        else
        {
          tmp += ans[i];
        }
      }
      cout << tmp << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3128
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

using db = double;
using ll = long long;
using vi = vector <int>;
#define op operator
#define pb push_back

string ans;
void dfs(vector <pair <int, string>> v) {
	if(v.size() == 1) {
		if(v[0].first == 10)
			ans = v[0].second;
		else
			return;
	}
	int n = v.size();
	for(int i = 0; i < n; i ++)
		for(int j = 0; j < n; j ++) if(i != j) {
			vector <pair <int, string>> nxt;
			for(int k = 0; k < n; k ++) if(i != k && j != k)
				nxt.pb({v[k]});
			nxt.pb({v[i].first + v[j].first, "(" + v[i].second + "+" + v[j].second + ")"});
			dfs(nxt); nxt.pop_back();
			nxt.pb({v[i].first - v[j].first, "(" + v[i].second + "-" + v[j].second + ")"});
			dfs(nxt); nxt.pop_back();
			nxt.pb({v[i].first * v[j].first, "(" + v[i].second + "*" + v[j].second + ")"});
			dfs(nxt); nxt.pop_back();
		}
}

int a[4];

int main() {
	ios :: sync_with_stdio(0);

	for(;;) {
		for(int i = 0; i < 4; i ++)
			cin >> a[i];
		if(*max_element(a, a + 4) == 0)
			break;
		vector <pair <int, string>> v;
		for(int i = 0; i < 4; i ++)
			v.pb({a[i], to_string(a[i])});
		ans = "";
		dfs(v);
		if(ans.size())
			cout << ans << '\n';
		else
			cout << "0\n";
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3276
final_rank: 9

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF (1e9+1)
//#define INF (1LL<<59)


int RPN(vector<char> v){
	stack<int> num;
	rep(i,v.size()){
		if(isdigit(v[i])){
			num.push(v[i]-'0');
		}else{
			if(num.size()<2) throw -1;
			int a,b;
			a = num.top(),num.pop();
			b = num.top();num.pop();
			if(v[i]=='+')num.push(a+b);
			if(v[i]=='-')num.push(a-b);
			if(v[i]=='*')num.push(a*b);
		}
	}
	if(num.size()!=1)throw -1;
	
	return num.top();
}

void printRPN(vector<char> v){
	stack<string> num;
	rep(i,v.size()){
		if(isdigit(v[i])){
			num.push(string(1,v[i]));
		}else{
			string a,b;
			a = num.top(),num.pop();
			b = num.top();num.pop();
			if(v[i]=='+')num.push("("+a+"+"+b+")");
			if(v[i]=='-')num.push("("+a+"-"+b+")");
			if(v[i]=='*')num.push("("+a+"*"+b+")");
		}
	}
	cout<<num.top()<<endl;
}

int main(){
	vector<char> v(4);
	while(cin>>v[0]>>v[1]>>v[2]>>v[3]){
		if(v[0]=='0'&&v[1]=='0'&&v[2]=='0'&&v[3]=='0')break;
		rep(plus,4){
			rep(minus,4){
				rep(mul,4){
					vector<char> tmp=v;
					if(plus+minus+mul!=3)continue;
					rep(i,plus)tmp.pb('+');
					rep(i,minus)tmp.pb('-');
					rep(i,mul)tmp.pb('*');
					
					sort(all(tmp));
					do{
						int res;
						try{
							res = RPN(tmp);
						}catch(int e){continue;}
						
						if(res==10){
							printRPN(tmp);
							goto next;
						}
					}while(next_permutation(all(tmp)));
				}
			}
		}
		cout<<0<<endl;
	next:;
	}
}
accuracy: 1
time_ms: 330
memory_kb: 3144
final_rank: 14

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

bool Found;
char ans[30];

int calc(char arg[30]){

	int tmp,op1,work,count,work2;
	char line[30],calc;
	stack<int> NUM,work_num;
	stack<char> OP,work_op;

	int loop;
	for(loop = 0; arg[loop] != '\0';loop++)line[loop] = arg[loop];

	line[loop++] = '=';
	line[loop] = '\0';


	bool numFLG,negFLG;

	tmp = 0;
	numFLG = false;
	negFLG = false;

	for(int k = 0; line[k] != '\0'; k++){
		if(line[k] >= '0' && line[k] <= '9'){
			numFLG = true;
			tmp = 10*tmp + (line[k] - '0');

		}else{

			if(numFLG == true){

				if(negFLG){
					tmp = -1*tmp;
					negFLG = false;
				}

				if(OP.empty() == false && (OP.top() == '*' || OP.top() == '/')){
					calc = OP.top();
					OP.pop();
					op1 = NUM.top();
					NUM.pop();

					if(calc == '*'){
						NUM.push(tmp*op1);
					}else{
						NUM.push(op1/tmp);
					}
				}else{
					NUM.push(tmp);
				}

				numFLG = false;
				tmp = 0;
			}

			if(line[k] == '+' ||line[k] == '*' || line[k] == '/' || line[k] == '(' || line[k] == '-'){
				OP.push(line[k]);
			}else if(line[k] == ')'){
				work = NUM.top();
				NUM.pop();
				work_num.push(work);

				count = 0;

				while(true){
					calc = OP.top();
					OP.pop();
					if(calc == '(')break;

					count++;

					work_op.push(calc);

					work = NUM.top();
					NUM.pop();
					work_num.push(work);
				}

				if(count == 0){
					work = work_num.top();
					work_num.pop();
				}else{

					work = work_num.top();
					work_num.pop();

					while(!work_num.empty()){
						calc = work_op.top();
						work_op.pop();

						if(calc == '+'){
							work += work_num.top();
						}else if(calc == '-'){
							work -= work_num.top();
						}else if(calc == '*'){
							work *= work_num.top();
						}else{
							work /= work_num.top();
						}
						work_num.pop();
					}
				}

				if(OP.empty() == false && OP.top() == '*'){
					OP.pop();
					work2 = NUM.top();
					NUM.pop();
					work *= work2;
				}else if(OP.empty() == false &&OP.top() == '/'){
					OP.pop();
					work2 = NUM.top();
					NUM.pop();
					work = work2/work;
				}

				NUM.push(work);

			}else{
				work = NUM.top();
				NUM.pop();
				work_num.push(work);

				while(!NUM.empty()){
					calc = OP.top();
					OP.pop();

					work_op.push(calc);

					work = NUM.top();
					NUM.pop();
					work_num.push(work);
				}

				work = work_num.top();
				work_num.pop();

				while(!work_num.empty()){
					calc = work_op.top();
					work_op.pop();

					if(calc == '+'){
						work += work_num.top();
					}else if(calc == '-'){
						work -= work_num.top();
					}else if(calc == '*'){
						work *= work_num.top();
					}else{
						work /= work_num.top();
					}
					work_num.pop();
				}

				return work;
			}
		}
	}
	return -1;	//must not reach here
}


void func(int a,int b, int c,int d){

	char eq1[30],eq2[30],eq3[30],eq4[30],eq5[30],eq6[30],eq7[30],eq8[30];
	eq1[0] = '(',eq1[1] = '0' + a,eq1[2] = '?',eq1[3] = '0' + b,eq1[4] = ')',eq1[5] = '?',eq1[6] = '(',eq1[7] = '0' + c,eq1[8] = '?',eq1[9] = '0' + d,eq1[10] = ')',eq1[11] = '\0';

	eq2[0] = '0' + a,eq2[1] = '?',eq2[2] = '(',eq2[3] = '0' + b,eq2[4] = '?',eq2[5] = '0' + c,eq2[6] = ')',eq2[7] = '?',eq2[8] = '0' + d,eq2[9] = '\0';

	eq3[0] = '0' + a,eq3[1] = '?',eq3[2] = '0' + b,eq3[3] = '?',eq3[4] = '(',eq3[5] = '0' + c,eq3[6] = '?',eq3[7] = '0' + d,eq3[8] = ')',eq3[9] = '\0';

	eq4[0] = '0' + a,eq4[1] = '?',eq4[2] = '(',eq4[3] = '0' + b,eq4[4] = '?',eq4[5] = '(',eq4[6] = '0' + c,eq4[7] = '?',eq4[8] = '0' + d,eq4[9] = ')',eq4[10] = ')',eq4[11] = '\0';

	eq5[0] = '(',eq5[1] = '0' + a,eq5[2] = '?',eq5[3] = '(',eq5[4] = '0' + b,eq5[5] = '?',eq5[6] = '0' + c,eq5[7] = ')',eq5[8] = ')',eq5[9] = '?',eq5[10] = '0' + d,eq5[11] = '\0';

	eq6[0] = '(',eq6[1] = '0' + a,eq6[2] = '?',eq6[3] = '0' + b,eq6[4] = ')',eq6[5] = '?',eq6[6] = '0' + c,eq6[7] = '?',eq6[8] = '0' + d,eq6[9] = '\0';

	eq7[0] = '(',eq7[1] = '(',eq7[2] = '0' + a,eq7[3] = '?',eq7[4] = '0' + b,eq7[5] = ')',eq7[6] = '?',eq7[7] = '0' + c,eq7[8] = ')',eq7[9] = '?',eq7[10] = '0' + d,eq7[11] = '\0';

	eq8[0] = '0' + a,eq8[1] = '?',eq8[2] = '(',eq8[3] = '(',eq8[4] = '0' + b,eq8[5] = '?',eq8[6] = '0' + c,eq8[7] = ')',eq8[8] = '?',eq8[9] = '0' + d,eq8[10] = ')',eq8[11] = '\0';


	for(int x = 0; x < 3; x++){
		for(int y = 0; y < 3; y++){
			for(int z = 0; z < 3; z++){
				switch(x){
				case 0:
					eq1[2] = '+',eq2[1] = '+',eq3[1] = '+',eq4[1] = '+',eq5[2] = '+',eq6[2] = '+',eq7[3] = '+',eq8[1] = '+';
					break;
				case 1:
					eq1[2] = '-',eq2[1] = '-',eq3[1] = '-',eq4[1] = '-',eq5[2] = '-',eq6[2] = '-',eq7[3] = '-',eq8[1] = '-';
					break;
				case 2:
					eq1[2] = '*',eq2[1] = '*',eq3[1] = '*',eq4[1] = '*',eq5[2] = '*',eq6[2] = '*',eq7[3] = '*',eq8[1] = '*';
					break;
				}
				switch(y){
				case 0:
					eq1[5] = '+',eq2[4] = '+',eq3[3] = '+',eq4[4] = '+',eq5[5] = '+',eq6[5] = '+',eq7[6] = '+',eq8[5] = '+';
					break;
				case 1:
					eq1[5] = '-',eq2[4] = '-',eq3[3] = '-',eq4[4] = '-',eq5[5] = '-',eq6[5] = '-',eq7[6] = '-',eq8[5] = '-';
					break;
				case 2:
					eq1[5] = '*',eq2[4] = '*',eq3[3] = '*',eq4[4] = '*',eq5[5] = '*',eq6[5] = '*',eq7[6] = '*',eq8[5] = '*';
					break;
				}
				switch(z){
				case 0:
					eq1[8] = '+',eq2[7] = '+',eq3[6] = '+',eq4[7] = '+',eq5[9] = '+',eq6[7] = '+',eq7[9] = '+',eq8[8] = '+';
					break;
				case 1:
					eq1[8] = '-',eq2[7] = '-',eq3[6] = '-',eq4[7] = '-',eq5[9] = '-',eq6[7] = '-',eq7[9] = '-',eq8[8] = '-';
					break;
				case 2:
					eq1[8] = '*',eq2[7] = '*',eq3[6] = '*',eq4[7] = '*',eq5[9] = '*',eq6[7] = '*',eq7[9] = '*',eq8[8] = '*';
					break;
				}
				if(calc(eq1) == 10){
					for(int i = 0; i < 30; i++)ans[i] = eq1[i];
					Found = true;
				}
				if(calc(eq2) == 10){
					for(int i = 0; i < 30; i++)ans[i] = eq2[i];
					Found = true;
				}
				if(calc(eq3) == 10){
					for(int i = 0; i < 30; i++)ans[i] = eq3[i];
					Found = true;
				}
				if(calc(eq4) == 10){
					for(int i = 0; i < 30; i++)ans[i] = eq4[i];
					Found = true;
				}
				if(calc(eq5) == 10){
					for(int i = 0; i < 30; i++)ans[i] = eq5[i];
					Found = true;
				}
				if(calc(eq6) == 10){
					for(int i = 0; i < 30; i++)ans[i] = eq6[i];
					Found = true;
				}
				if(calc(eq7) == 10){
					for(int i = 0; i < 30; i++)ans[i] = eq7[i];
					Found = true;
				}
				if(calc(eq8) == 10){
					for(int i = 0; i < 30; i++)ans[i] = eq8[i];
					Found = true;
				}
			}
		}
	}
}


int main(){

	int a,b,c,d,num[4];

	while(true){
		scanf("%d %d %d %d",&a,&b,&c,&d);
		if(a == 0 && b == 0 && c == 0 && d == 0)break;

		Found = false;

		num[0] = a,num[1] = b,num[2] = c,num[3] =d;

		for(int e = 0; e < 4; e++){
			for(int f = 0; f < 4; f++){
				for(int g = 0; g < 4; g++){
					for(int h = 0; h < 4; h++){
						if(e != f && e != g && e != h && f != g && f != h && g != h)func(num[e],num[f],num[g],num[h]);
						if(Found)break;
					}
					if(Found)break;
				}
				if(Found)break;
			}
			if(Found)break;
		}

		if(Found)printf("%s\n",ans);
		else{
			printf("0\n");
		}
	}



	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 2556
final_rank: 3

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
#include <random>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)
#define ALL(v) v.begin(), v.end()

// every calculation can be written
// ((a ? b) ? c) ? d  or  (a ? b) ? (c ? d)

int calc(int a, int b, char ope) {
    if (ope == '+') return a + b;
    if (ope == '-') return a - b;
    if (ope == '*') return a * b;
    return -1;
}

bool isten(string s) {
    int ans;
    if (s[1] == '(') {
        int a, b, c, d;
        a = s[2] - '0';
        b = s[6] - '0';
        c = s[11] - '0';
        d = s[16] - '0';
        ans = calc(calc(calc(a,b,s[4]), c, s[9]), d, s[14]);
    }else {
        int a, b, c, d;
        a = s[1] - '0';
        b = s[5] - '0';
        c = s[11] - '0';
        d = s[15] - '0';
        ans = calc(calc(a, b, s[3]), calc(c, d, s[13]), s[8]);
    }
    return ans == 10;
}

// every calculation can be written
// ((a ? b) ? c) ? d  or  (a ? b) ? (c ? d)


vector<char> operands = {'+', '-', '*'};
random_device rd;

char getRandomOperand() {
    return operands[rd() % 3];
}

void solve41(int a, int b, int c, int d) {
    vector<int> nums;
    nums.push_back(a), nums.push_back(b), nums.push_back(c), nums.push_back(d);
    sort(ALL(nums));
    do {
        REP(i,100) {
            string eq1, eq2;
            eq1 = "((" + to_string(nums[0]) + " " + getRandomOperand() + " " +
            to_string(nums[1]) + ") " + getRandomOperand() + " " + to_string(nums[2]) +
            ") " + getRandomOperand() + " " + to_string(nums[3]);
            eq2 = "(" + to_string(nums[0]) + " " + getRandomOperand() + " " +
            to_string(nums[1]) + ") " + getRandomOperand() + " (" + to_string(nums[2]) +
            " " + getRandomOperand() + " " + to_string(nums[3]) + ")";
            if (isten(eq1)) {
                cout << eq1 << endl;
                return;
            }else if (isten(eq2)) {
                cout << eq2 << endl;
                return;
            }
        }
    }while (next_permutation(nums.begin(), nums.end()));
    cout << 0 << endl;
}

int main() {
    int a, b, c, d;
    while(cin >> a >> b >> c >> d, a + b + c + d) {
        solve41(a,b,c,d);
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3124
final_rank: 4

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
#include <random>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)
#define ALL(v) v.begin(), v.end()

// every calculation can be written
// ((a ? b) ? c) ? d  or  (a ? b) ? (c ? d)

int calc(int a, int b, char ope) {
    if (ope == '+') return a + b;
    if (ope == '-') return a - b;
    if (ope == '*') return a * b;
    return -1;
}

bool isten(string s) {
    int ans;
    if (s[1] == '(') {
        int a, b, c, d;
        a = s[2] - '0';
        b = s[6] - '0';
        c = s[11] - '0';
        d = s[16] - '0';
        ans = calc(calc(calc(a,b,s[4]), c, s[9]), d, s[14]);
    }else {
        int a, b, c, d;
        a = s[1] - '0';
        b = s[5] - '0';
        c = s[11] - '0';
        d = s[15] - '0';
        ans = calc(calc(a, b, s[3]), calc(c, d, s[13]), s[8]);
    }
    return ans == 10;
}

// every calculation can be written
// ((a ? b) ? c) ? d  or  (a ? b) ? (c ? d)


vector<char> operands = {'+', '-', '*'};
random_device rd;

char getRandomOperand() {
    return operands[rd() % 3];
}

void solve41(int a, int b, int c, int d) {
    vector<int> nums;
    nums.push_back(a), nums.push_back(b), nums.push_back(c), nums.push_back(d);
    sort(ALL(nums));
    do {
        REP(i,500) {
            string eq1, eq2;
            eq1 = "((" + to_string(nums[0]) + " " + getRandomOperand() + " " +
            to_string(nums[1]) + ") " + getRandomOperand() + " " + to_string(nums[2]) +
            ") " + getRandomOperand() + " " + to_string(nums[3]);
            eq2 = "(" + to_string(nums[0]) + " " + getRandomOperand() + " " +
            to_string(nums[1]) + ") " + getRandomOperand() + " (" + to_string(nums[2]) +
            " " + getRandomOperand() + " " + to_string(nums[3]) + ")";
            if (isten(eq1)) {
                cout << eq1 << endl;
                return;
            }else if (isten(eq2)) {
                cout << eq2 << endl;
                return;
            }
        }
    }while (next_permutation(nums.begin(), nums.end()));
    cout << 0 << endl;
}

int main() {
    int a, b, c, d;
    while(cin >> a >> b >> c >> d, a + b + c + d) {
        solve41(a,b,c,d);
    }
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3144
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;

typedef pair<int,string> pis;
#define fi first
#define se second

vector<pis> solve(vector<int> nums) {
	vector<pis> res;
	if(nums.size() == 1) {
		string s;
		stringstream sstr;
		sstr << nums[0];
		sstr >> s;
		res.push_back(pis(nums[0],s));
		return res;
	}
	for(int i = 1; i < nums.size(); i++) {
		vector<int> a(nums.begin(), nums.begin()+i);
		vector<int> b(nums.begin()+i, nums.end());
		vector<pis> aa, bb;
		aa = solve(a);
		bb = solve(b);
		for(int i = 0; i < aa.size(); i++) {
			for(int j = 0; j < bb.size(); j++) {
				res.push_back(pis(aa[i].fi + bb[j].fi, "(" + aa[i].se + "+" + bb[j].se + ")"));
				res.push_back(pis(aa[i].fi - bb[j].fi, "(" + aa[i].se + "-" + bb[j].se + ")"));
				res.push_back(pis(aa[i].fi * bb[j].fi, "(" + aa[i].se + "*" + bb[j].se + ")"));
			}
		}
	}
	return res;
}

int main() {
	while(1) {
		vector<int> a;
		a.resize(4);
		bool brk = true;
		for(int i = 0; i < 4; i++) {
			cin >> a[i];
			if(a[i] != 0) brk = false;
		}
		if(brk) break;

		sort(a.begin(), a.end());
		string res = "0";
		do {
			vector<pis> tmp;
			tmp = solve(a);
			for(int i = 0; i < tmp.size(); i++) {
				if(tmp[i].fi == 10)
					res = tmp[i].se;
			}
		} while(next_permutation(a.begin(), a.end()));
		cout << res << endl;
	}
}
accuracy: 1
time_ms: 30
memory_kb: 1284
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init));

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

const string op[4] = {"+","-","*","/"};
const string pare[] = {"(.!.)!.!.",".!.!(.!.)",".!(.!.)!.","(.!.)!(.!.)","(.!.!.)!.",".!(.!.!.)","(.!(.!.))!.","((.!.)!.)!.",".!((.!.)!.)",".!(.!(.!.))",".!.!.!."};

double RPN(string str){
  string s,s2;
  double ans;
  stack<double> n;
  s = str;
  ans = 0;
  s += " " ;
  rep(i,s.size()){
    if(s[i] != ' '){
      s2 += s[i];
    }
    else{
      double a,b;
      if(s2 == "+"){
        b = n.top(),n.pop();
        a = n.top(),n.pop();
        ans = a + b;
        n.push(ans);
      }
      else if(s2 == "-"){
        b = n.top(),n.pop();
        a = n.top(),n.pop();
        ans = a - b;
        n.push(ans);
      }
      else if(s2 == "*"){
        b = n.top(),n.pop();
        a = n.top(),n.pop();
        ans = a * b;
        n.push(ans);
      }
      else if(s2 == "/"){
        b = n.top(),n.pop();
        a = n.top(),n.pop();
        ans = a / b;
        n.push(ans);
      }
      else{
        n.push(ston(s2,1.0));
      }
      s2.clear();
    }
  }
  return n.top();
}

vector<string> Split(char c,string s){
  vector<string> res(0);
  string str = "";
  rep(i,s.size()){
    if(s[i] == c){
      if(str.size()){
        res.push_back(str);
      }
      str.clear();
    }
    else{
      str += s[i];
    }
  }
  res.push_back(str);
  return res;
}
bool check(string s1,string s2){
  if(((s1 == "+" || s1 == "-" ) && s2 != "(")|| s1 == s2) return true;
  if((s1 == "*" || s1 == "/")&& (s2 == "+" || s2 == "-")) return false;
  if(s2 == "(") return false;
}
string ConvertRPN(string s){
  const string notnumber = "+-*/() ";
  stack<string> fom;
  auto str = Split(' ',s);
  string res = "";
  rep(i,str.size()){
    if(notnumber.find(str[i]) == string::npos){
      res += str[i];
      res += " ";
    }
    else if(str[i] == ")"){
      while(fom.top() != "("){
        if(fom.top() != " "){
          res += fom.top();
          res += " ";
        }
        fom.pop();
      }
      fom.pop();
    }
    else if(str[i] == "(" || fom.empty()){
      fom.push(str[i]);
    }
    else if(check(str[i],fom.top())){
      while(!fom.empty() && check(str[i],fom.top())){
        if(fom.top() != " "){
          res += fom.top();
          res += " ";
        }
        fom.pop();
      }
      fom.push(str[i]);
    }
    else{
      fom.push(str[i]);
    }
  }
  bool f = true;
  while(fom.size()){
    if(fom.top() != " "){
      res += fom.top();
      res += " ";
    }
    fom.pop();
  }
  res.resize(res.size()-1);
  return res;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  vary(ll,v,4,0);
  while(cin >> v[0] >> v[1] >> v[2] >> v[3] && v[0] && v[1] && v[2] && v[3]){
    sort(all(v));
    bool f = false;
    do{
      rep(i,3){
        rep(j,3){
          rep(k,3){
            string formula = op[i]+op[j] + op[k];
            rep(m,10){
              ll c = 0,d = 0;
              string res ="";
              rep(l,pare[m].size()){
                if(pare[m][l] == '.'){
                  res += to_string(v[c])+ " ";
                  ++c;
                }
                else if(pare[m][l] == '!'){
                  res += formula[d];
                  res += " ";
                  ++d;
                }
                else{
                  res += pare[m][l];
                  res += " ";
                }
              }
              res.resize(res.size()-1);
              string tmp = res;
              res = ConvertRPN(res);
              if(RPN(res) == 10){
                cout << tmp<<endl;
                f = true;
                i = j = k = 4;
                v.clear();
                break;
              }
            }
          }
        }
      }
    }while(next_permutation(all(v)));
    if(!f){
      cout << 0 << endl;
    }
    v.resize(4);
  }
  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3220
final_rank: 13

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

int calc(string s){
  assert(s.size()==7);
  stack<int> st;
  REP(i,s.size()){
    char c = s[i];
    if(c=='+'){
      if(st.size()<2) return -1;
      int a = st.top(); st.pop();
      a += st.top(); st.pop();
      st.push(a);
    }else if(c=='-'){
      if(st.size()<2) return -1;
      int a = st.top(); st.pop();
      a -= st.top(); st.pop();
      st.push(a);
    }else if(c=='*'){
      if(st.size()<2) return -1;
      int a = st.top(); st.pop();
      a *= st.top(); st.pop();
      st.push(a);
    }else{
      st.push((int)(c-'0'));
    }
  }
  assert(st.size()==1);
  return st.top();
}

void output(string s){
  stack<string> st;
  REP(i,s.size()){
    char c = s[i];
    if(c=='+' || c=='-' || c=='*'){
      if(st.size()<2) assert(false&&"NO STACK");
      string a = "(";
      a += st.top(); st.pop();
      a += c;
      a += st.top(); st.pop();
      a += ")";
      st.push(a);
    }else{
      st.push(string(1,c));
    }
  }
  assert(st.size()==1);
  cout<<st.top()<<endl;
}

int main(){
  int a[4];
  while(~scanf("%d%d%d%d",a,a+1,a+2,a+3)){
    if(a[0]==0 && a[1]==0 && a[2]==0 && a[3]==0)break;
    // ???????????????????¨????
    vi expr(7,0);
    FOR(i,4,7)expr[i]=1;
    vi numlst(4);
    REP(i,4)numlst[i]=i;
    bool flag = false;
    do{
      if(flag)break;
      if(expr[0]==1 || expr[1]==1) continue;
      if(expr[6]==0) continue;
      REP(exmsk,3*3*3){
        if(flag)break;
        do{
          if(flag)break;
          string s = "";
          int i1=0;
          int tmp = exmsk;
          REP(i,7){
            if(expr[i]==0){
              s += (char)(a[numlst[i1]]+'0');
              ++i1;
            }else{
              s += "+-*"[tmp%3];
              tmp /= 3;
            }
          }
          int res = calc(s);
          if(res==10){
            flag = true;
            output(s);
            break;
          }
        }while(next_permutation(ALL(numlst)));
      }
    }while(next_permutation(ALL(expr)));
    if(!flag)cout<<0<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1240
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<58;
const double eps=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

string int_to_string(int x){
    stringstream ss;
    ss << x;
    return ss.str();
}

vector<pair<int,string>> nv(const vector<pair<int,string>>& v,const vector<pair<int,string>>& tmp,const vector<int>& idxs,char op){
    auto x=v[idxs[0]],y=v[idxs[1]];
    pair<int,string> p;
    if(op=='+') p=make_pair(x.first+y.first,"("+x.second+" + "+y.second+")");
    else if(op=='-') p=make_pair(x.first-y.first,"("+x.second+" - "+y.second+")");
    else if(op=='*') p=make_pair(x.first*y.first,"("+x.second+" * "+y.second+")");
    auto res=tmp; res.emplace_back(p);
    return move(res);
}

string dfs(vector<pair<int,string>> v){
    if(v.size()==1){
        if(v[0].first==10) return v[0].second;
        else return "";
    }

    string res="";
    rep(b,0,1<<v.size()){
        if(__builtin_popcount(b)!=2) continue;
        vector<pair<int,string>> tmp;
        vector<int> idxs;
        rep(i,0,v.size()){
            if((1&(b>>i))==0) tmp.emplace_back(v[i]);
            else idxs.emplace_back(i);
        }

        res=max(res,dfs(nv(v,tmp,idxs,'+')));
        res=max(res,dfs(nv(v,tmp,idxs,'*')));
        res=max(res,dfs(nv(v,tmp,idxs,'-')));
        swap(idxs[0],idxs[1]);
        res=max(res,dfs(nv(v,tmp,idxs,'-')));
    }
    return res;
}

void solve(){
    int a,b,c,d;
    while(cin >> a >> b >> c >> d){
        if(a==0 and b==0 and c==0 and d==0) break;
        vector<pair<int,string>> v;
        v.emplace_back(make_pair(a,int_to_string(a)));
        v.emplace_back(make_pair(b,int_to_string(b)));
        v.emplace_back(make_pair(c,int_to_string(c)));
        v.emplace_back(make_pair(d,int_to_string(d)));
        string ans=dfs(v);

        if(ans.empty()) cout << 0 << endl;
        else cout << ans << endl;
    }
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	solve();
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3224
final_rank: 8