This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
 
using namespace std;
 
#define nl '\n'
#define pb push_back 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using vi = V<int>;
using ll = long long;
using pl = pair<ll, ll>;
using vl = V<ll>;
 
int main() {
	cin.tie(0)->sync_with_stdio(0);
 
	string S; cin >> S;
	vi F(26); for(auto& x : S) F[x-'a']++;
	vi H = F;
	for(int c = 0; c < 26; c++) {
		if (F[c] & 1) {
			cout << -1 << nl;
			return 0;
		}
		H[c] /= 2;
	}
	vi stk;
	string ans;
	for(int i = 0; i < sz(S); i++) {
		int c = S[i] - 'a';
		if (F[c] > H[c]) ans += "(", stk.pb(c);
		else {
			ans += ")";
			if (stk.back() != c) {
				cout << -1 << nl;
				return 0;
			}
			stk.pop_back();
		}
		F[c]--;
	}
	if (sz(stk)) cout << -1 << nl;
	else cout << ans << nl;
    return 0;
}			
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |