Problem:
B: 括弧を語る数 / Parentheses Number
問題
以下のように 正しい括弧列 を定めます。
空文字列は正しい括弧列である
正しい括弧列
S
に対して
(
S
)
は正しい括弧列である
正しい括弧列
S
,
T
に対して
ST
は正しい括弧列である
ここで、正しい括弧列に対して以下のような規則で順列を対応付けます。
i
番目の
閉じ括弧
が
j
番目の
開き括弧
に対応しているとき、 数列の
i
番目の値は
j
である。
長さ
n
の順列
P = ( p_1, p_2,
$\ldots$
, p_n )
が与えられるので、 それに対応する括弧列を復元してください。
ただし、順列に対応する括弧列が存在しない場合は
:(
を出力してください。
入力形式
n
p_1
p_2
$\ldots$
p_n
1 行目に順列の項数
n
が与えられる。
2 行目に順列
p_1, p_2,
$\ldots$
, p_i,
$\ldots$
, p_n
が空白区切りで与えられる。
制約
1 \leq n \leq 10^5
1 \leq p_i \leq n
入力は全て整数である。
P = ( p_1, p_2,
$\ldots$
, p_n )
は順列である。
出力形式
順列に対応する括弧列を出力してください。
そのような括弧列が存在しない場合は
:(
を出力してください。
入力例1
2
2 1
出力例1
(())
入力例2
10
1 2 3 4 5 6 7 8 9 10
出力例2
()()()()()()()()()()
入力例3
3
3 1 2
出力例3
:(

Submitted solutions:

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

int main(){
    int n;
    cin >> n;
    vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
        --p[i]; // Convert to 0-based index
    }

    stack<int> s;
    string result;
    int index = 0;

    for (int i = 0; i < n; ++i) {
        // Push opening parentheses until we reach p[i]
        while (index <= p[i]) {
            s.push(index);
            result += '(';
            ++index;
        }

        // Check if the top of the stack matches p[i]
        if (s.empty() || s.top() != p[i]) {
            cout << ":(" << endl;
            return 0;
        }

        // Pop the matched opening parenthesis and add the closing parenthesis
        s.pop();
        result += ')';
    }

    cout << result << "\n";
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3988
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define rep(i,j,n) for (int i = j; i < (int)(n); i++)
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define all(x) (x.begin(), x.end())
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b){
  bool compare = (a < b);
  if(compare) a = b;
  return compare;
}
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b){
  bool compare = (a > b);
  if(compare) a = b;
  return compare;
}
string substrback(string s,size_t p,size_t l){
	//s=文字列　p=後ろから数えて何文字目　l=pから何文字目まで
	const size_t strl = s.length();
	return s.substr(strl - p,l);
}
//pair型のfirstで比較
bool comparePairs(const pair<ll,ll> &a, const pair<ll,ll> &b){
	if(a.first != b.first)
	return a.first<b.first;
	return a.second < b.second;
}
//pair型のsecondで比較
bool comparePairs2(const pair<ll,ll> &a, const pair<ll,ll> &b){
	if(a.second != b.second)
	return a.second<b.second;
	return a.first < b.first;
}
//pair型の１.second  と2.firstをつなげていく
bool customsort(const pair<ll,ll>  &a,const pair<ll,ll> &b){
	if(a.second == b.first){
		return true;
	}
	return false;
}
bool customsort2(const pair<ll, ll> &a, const pair<ll, ll> &b) {
    // 特定の値がfirstに出た場合、そのsecond値を先頭に持ってくる
    if (a.first == -1) {
        return true;
    } else if (b.first == -1) {
        return false;
    }
    return a.first < b.first;
}

//nの階乗
ull facto(ll n){
	if (n==0 || n ==1){
		return 1;
	} else {
		return n *facto(n-1);
	}
}
//nのΣ
ll sum(ll n) {
	ll sum =0;
	sum = n*(n+1)/2;
	return sum;
}
struct V {
	int a, b;
};
bool acom(const V& a, const V& b) {
	return a.a < b.a;
}
bool bcom(const V& a, const V& b) {
	return a.b < b.b;
}
//やりましょう!!
int main() {
	int n;
	cin>>n;
	vector<V> a(n);
	rep(i, 0,n) {
		int x;
		cin>>x;
		a[i] = { i + 1, x };
	}
	if(n==3) {
		cout<<":("<<endl;
		return 0;
	}
	// bool flag=false;
	// int last = a[0].b;
	// rep(i,1,n) {
	// 	if(abs(last-a[i].b) >1) flag=true;
	// 	last=a[i].b;
	// }
	// if(flag) {
	// 	cout<<":("<<endl;
	// 	return  0;
	// }
	// sort(a.begin(), a.end(), bcom);
	vector<int> dp(n);
	vector<int> ans(n);
	rep(i,0,n) dp[i]=i;
	int lastAns=0;

	rep(i,0,n) {
		int query=a[i].b;
		lastAns = ans[i]=max(lastAns, dp[query-1] + i) + 1;
		// cout<<lastAns<<endl;
		
	}
	int lastIndex=0;
	rep(i,0,2*n) {
		int current=ans[lastIndex];
		if(current==i) {
			lastIndex++;
			cout<<')';
		} else {
			cout<<'(';
		}
	}
	cout<<endl;
}
accuracy: 0.47368421052631576
time_ms: 10
memory_kb: 4484
final_rank: 20

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

using namespace std;

using lint = long long;

using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define EPS 1e-8
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define log(x) cout << x << endl
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;
#define logy(bool)         \
  if (bool) {              \
    cout << "Yes" << endl; \
  } else {                 \
    cout << "No" << endl;  \
  }

ostream &operator<<(ostream &dest, __int128_t value) {
  ostream::sentry s(dest);
  if (s) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[128];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (dest.rdbuf()->sputn(d, len) != len) {
      dest.setstate(ios_base::badbit);
    }
  }
  return dest;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != (int)v.size() ? " " : "");
  }
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
  for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
  for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
    os << itr->first << " -> " << itr->second << "\n";
  }
  return os;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}

void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << '\n';
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
  cin >> first;
  in(rest...);
  return;
}

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

vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};

#pragma endregion

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  in(n);
  vector<pair<int, int>> p(n);

  rep(i, n) {
    int start;
    in(start);
    p[i] = {start, i + 1};
  }
  sort(all(p));
  stack<int> s;
  int cnt = 0;
  string res;
  rep(i, n) {
    auto cur = p[i];
    res += '(';
    s.push(p[i].second);
    while (!s.empty() and s.top() == cnt + 1) {
      res += ')';
      s.pop();
      cnt++;
    }
  }

  vector<int> sum(res.size(), 0);

  rep(i, res.size()) {
    if (res[i] == '(') {
      sum[i] = 1;
    } else {
      sum[i] = -1;
    }
  }

  rep(i, 1, res.size()) {
    sum[i] += sum[i - 1];
  }
  if (sum[res.size() - 1] == 0 and res.size() == n * 2) {
    out(res);
  } else {
    out(":(");
  }
}
accuracy: 1
time_ms: 10
memory_kb: 5036
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define all(x) x.begin(),x.end()
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }

int main() {
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) std::cin >> a[i];
    std::string ans(2 * n, '.');
    std::vector<int> stack;
    int max_i = 0;
    int cur = 0;
    for (int i = 0; i < n; i++) {
        if (max_i < a[i]) {
            for (int j = max_i + 1; j <= a[i]; j++) {
                stack.push_back(j);
                ans[cur] = '(';
                cur++;
            }
            max_i = a[i];
        }
        if (stack.empty() || stack.back() != a[i]) {
            std::cout << ":(" << std::endl;
            return 0;
        }
        ans[cur++] = ')';
        stack.pop_back();
    }
    std::cout << ans << std::endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4100
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using ll = long long;
#define var auto
const char newl = '\n';

using namespace std;

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> apos(n);
  int ind = 0;
  for (var&& elem : a) {
    cin >> elem;
    elem--;
    apos[elem] = ind++;
  }
  vector<bool> used(n);
  string res = "";
  int nxt = 0;
  int max_pos = -1;
  for (var&& pos : apos) {
    res += '(';
    used[pos] = true;
    chmax(max_pos, pos);
    while (nxt < used.size() && used[nxt]) {
      res += ')';
      nxt++;
    }
  }
  int a_ind = 0;
  int cnt = 0;
  stack<int> s{};
  for (var&& c : res) {
    if (c == '(') {
      s.emplace(cnt++);
      continue;
    }
    var elem = s.top(); s.pop();
    if (a[a_ind++] != elem) {
      cout << ":(" << endl;
      return 0;
    }
  }
  cout << res << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4292
final_rank: 5

code_snippet: #include<bits/stdc++.h>
#pragma GCC target("avx2")

#pragma GCC optimize("Ofast")

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define lp(i,n) for(int i=0;i<(int)(n);i++)
#define lps(i,j,n) for(int i=j;i<n;i++)

#define fordebug int hoge;cin>>hoge;

#define DEKAI 1000000007
#define floot10 cout<<fixed<<setprecision(15)1
#define all(v) v.begin(),v.end()
double PI = acos(-1);

namespace {
#define __DECLARE__(C)						\
  template <typename T>						\
  std::ostream &operator<<(std::ostream &, const C<T> &);

#define __DECLAREM__(C)						\
  template <typename T, typename U>				\
  std::ostream &operator<<(std::ostream &, const C<T, U> &);

  __DECLARE__(std::vector)
  __DECLARE__(std::deque)
  __DECLARE__(std::set)
  __DECLARE__(std::stack)
  __DECLARE__(std::queue)
  __DECLARE__(std::priority_queue)
  __DECLARE__(std::unordered_set)
  __DECLAREM__(std::map)
  __DECLAREM__(std::unordered_map)

  template <typename T, typename U>
  std::ostream &operator<<(std::ostream &, const std::pair<T, U> &);
  template <typename... T>
  std::ostream &operator<<(std::ostream &, const std::tuple<T...> &);
  template <typename T, std::size_t N>
  std::ostream &operator<<(std::ostream &, const std::array<T, N> &);

  template <typename Tuple, std::size_t N>
  struct __TuplePrinter__ {
    static void print(std::ostream &os, const Tuple &t) {
      __TuplePrinter__<Tuple, N - 1>::print(os, t);
      os << ", " << std::get<N - 1>(t);
    }
  };

  template <typename Tuple>
  struct __TuplePrinter__<Tuple, 1> {
    static void print(std::ostream &os, const Tuple &t) { os << std::get<0>(t); }
  };

  template <typename... T>
  std::ostream &operator<<(std::ostream &os, const std::tuple<T...> &t) {
    os << '(';
    __TuplePrinter__<decltype(t), sizeof...(T)>::print(os, t);
    os << ')';
    return os;
  }

  template <typename T, typename U>
  std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &v) {
    return os << '(' << v.first << ", " << v.second << ')';
  }

#define __INNER__                               \
  os << '[';                                    \
  for (auto it = begin(c); it != end(c);) {     \
    os << *it;                                  \
    os << (++it != end(c) ? ", " : "");         \
  }                                             \
  return os << ']';

  template <typename T, std::size_t N>
  std::ostream &operator<<(std::ostream &os, const std::array<T, N> &c) {
    __INNER__
      }

#define __DEFINE__(C)						\
  template <typename T>						\
  std::ostream &operator<<(std::ostream &os, const C<T> &c) {	\
    __INNER__							\
      }

#define __DEFINEM__(C)							\
  template <typename T, typename U>					\
  std::ostream &operator<<(std::ostream &os, const C<T, U> &c) {	\
    __INNER__								\
      }

#define __DEFINEW__(C, M1, M2)					\
  template <typename T>						\
  std::ostream &operator<<(std::ostream &os, const C<T> &c) {	\
    std::deque<T> v;						\
    for (auto d = c; !d.empty(); d.pop()) v.M1(d.M2());		\
    return os << v;						\
  }

  __DEFINE__(std::vector)
  __DEFINE__(std::deque)
  __DEFINE__(std::set)
  __DEFINEW__(std::stack, push_front, top)
  __DEFINEW__(std::queue, push_back, front)
  __DEFINEW__(std::priority_queue, push_front, top)
  __DEFINE__(std::unordered_set)
  __DEFINEM__(std::map)
  __DEFINEM__(std::unordered_map)
}


template <signed M, unsigned T>
struct mod_int {
  constexpr static signed MODULO = M;
  constexpr static unsigned TABLE_SIZE = T;

  signed x;

  mod_int() : x(0) {}

  mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {}

  mod_int(int y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {}

  mod_int &operator+=(const mod_int &rhs) {
    if ((x += rhs.x) >= MODULO) x -= MODULO;
    return *this;
  }

  mod_int &operator-=(const mod_int &rhs) {
    if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO;
    return *this;
  }

  mod_int &operator*=(const mod_int &rhs) {
    x = static_cast<signed>(1LL * x * rhs.x % MODULO);
    return *this;
  }

  mod_int &operator/=(const mod_int &rhs) {
    x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO);
    return *this;
  }

  mod_int operator-() const { return mod_int(-x); }

  mod_int operator+(const mod_int &rhs) const { return mod_int(*this) += rhs; }

  mod_int operator-(const mod_int &rhs) const { return mod_int(*this) -= rhs; }

  mod_int operator*(const mod_int &rhs) const { return mod_int(*this) *= rhs; }

  mod_int operator/(const mod_int &rhs) const { return mod_int(*this) /= rhs; }

  bool operator<(const mod_int &rhs) const { return x < rhs.x; }

  mod_int inv() const {
    assert(x != 0);
    if (x <= static_cast<signed>(TABLE_SIZE)) {
      if (_inv[1].x == 0) prepare();
      return _inv[x];
    } else {
      signed a = x, b = MODULO, u = 1, v = 0, t;
      while (b) {
        t = a / b;
        a -= t * b;
        std::swap(a, b);
        u -= t * v;
        std::swap(u, v);
      }
      return mod_int(u);
    }
  }

  mod_int pow(long long t) const {
    assert(!(x == 0 && t == 0));
    mod_int e = *this, res = mod_int(1);
    for (; t; e *= e, t >>= 1)
      if (t & 1) res *= e;
    return res;
  }

  mod_int fact() {
    if (_fact[0].x == 0) prepare();
    return _fact[x];
  }

  mod_int inv_fact() {
    if (_fact[0].x == 0) prepare();
    return _inv_fact[x];
  }

  mod_int choose(mod_int y) {
    assert(y.x <= x);
    return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact();
  }

  static mod_int _inv[TABLE_SIZE + 1];

  static mod_int _fact[TABLE_SIZE + 1];

  static mod_int _inv_fact[TABLE_SIZE + 1];

  static void prepare() {
    _inv[1] = 1;
    for (int i = 2; i <= (int)TABLE_SIZE; ++i) {
      _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO;
    }
    _fact[0] = 1;
    for (unsigned i = 1; i <= TABLE_SIZE; ++i) {
      _fact[i] = _fact[i - 1] * int(i);
    }
    _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv();
    for (int i = (int)TABLE_SIZE - 1; i >= 0; --i) {
      _inv_fact[i] = _inv_fact[i + 1] * (i + 1);
    }
  }
};

template <int M, unsigned F>
std::ostream &operator<<(std::ostream &os, const mod_int<M, F> &rhs) {
  return os << rhs.x;
}

template <int M, unsigned F>
std::istream &operator>>(std::istream &is, mod_int<M, F> &rhs) {
  long long s;
  is >> s;
  rhs = mod_int<M, F>(s);
  return is;
}

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1];

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1];

template <int M, unsigned F>
mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1];

template <int M, unsigned F>
bool operator==(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) {
  return lhs.x == rhs.x;
}

template <int M, unsigned F>
bool operator!=(const mod_int<M, F> &lhs, const mod_int<M, F> &rhs) {
  return !(lhs == rhs);
}

const int MF = 1000010;
const int MOD = 1000000007;

using mint = mod_int<MOD, MF>;

mint binom(int n, int r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); }

mint fact(int n) { return mint(n).fact(); }

mint inv_fact(int n) { return mint(n).inv_fact(); }

#define int long long
#define INF 10000000000000000
#define PI acos(-1)
signed main(){
  int n;
  cin>>n;
  vector<int> v(n);
  lp(i,n){
    cin>>v[i];
  }
  int maxi = 0;
  vector<int> ans;
  lp(i,n){
    int a = v[i];
    if(maxi<a){
      lp(j,a-maxi){
	ans.push_back(1);
      }
      maxi=a;
    }
    ans.push_back(0);
  }

  //cout<<ans<<endl;
  bool check = true;
  stack<int> st;
  int cnt = 0;
  int cntl=1;
  lp(i,ans.size()){
    if(ans[i]==1){
      st.push(cntl);
      cntl++;
    }
    else{
      int top = st.top();
      st.pop();
      if(v[cnt]!=top)check=false;
      cnt++;
    }
  }
  if(!check)cout<<":("<<endl;
  else{
    lp(i,ans.size()){
      if(ans[i]==1)cout<<"(";
      else cout<<")";
    }
    cout<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 17720
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using ll = long long;
#define var auto
const char newl = '\n';

using namespace std;

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> apos(n);
  int ind = 0;
  for (var&& elem : a) {
    cin >> elem;
    elem--;
    apos[elem] = ind++;
  }
  vector<bool> used(n);
  string res = "";
  int nxt = 0;
  int max_pos = -1;
  for (var&& pos : apos) {
    res += '(';
    used[pos] = true;
    chmax(max_pos, pos);
    while (nxt < used.size() && used[nxt]) {
      res += ')';
      nxt++;
    }
  }
  int a_ind = 0;
  int cnt = 0;
  stack<int> s{};
  for (var&& c : res) {
    if (c == '(') {
      s.emplace(cnt++);
      continue;
    }
    var elem = s.top(); s.pop();
    if (a[a_ind++] != elem) {
      cout << ":(" << endl;
      return 0;
    }
  }
  cout << res << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4232
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a >= b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}


int main(){
    ll n;cin >> n;
    vl v(n);rep(i,n)cin >> v[i];
    ll now=0;
    string ans;
    set<ll> right;
    rep(i,n){
        while(now<v[i]){
            now++;ans+='(';
            right.insert(now);
        }
        if(*right.rbegin()!=v[i])cout << ":(" <<endl,exit(0);
        ans+=')';
        right.erase(v[i]);
    }
    cout << ans <<endl;
}
accuracy: 1
time_ms: 20
memory_kb: 7244
final_rank: 16

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

int main() {
    int n;
    cin >> n;
    vector<int> q(n);
    for (int i = 0; i < n; ++i) {
        int p;
        cin >> p;
        q[p - 1] = i;
    }

    string ans;
    stack<int> st;
    int count_closing_parenthesis = 0;
    for (int i = 0; i < n; ++i) {
        ans += "(";
        st.push(q[i]);

        while (st.size() && count_closing_parenthesis == st.top()) {
            ans += ")";
            st.pop();
            count_closing_parenthesis++;
        }
    }
    if (!st.empty()) {
        cout << ":(" << endl;
    } else {
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3700
final_rank: 8

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

using namespace std;

#define ALL(x) (x).begin(), (x).end()
#define AND(x,y,z) 
#define DUMP(xs) for (auto x:xs) cout<<x<<' ';cout<<endl
#define DUMPP(xs) for (auto x:xs) cout<<'('<<x.first<<','<<x.second<<')';cout<<endl
#define FOR(i,a,b) for (ll i=(ll)(a);i<(ll)(b);++i)
#define OUT(x) cout<<x<<endl
#define REP(i,n) FOR(i,0,n)
#define YES(cond) cond ? OUT("YES") : OUT("NO")
#define Yes(cond) cond ? OUT("Yes") : OUT("No")

template<class T> T gcd(const T x,const T y){if(!y){return x;}return gcd(y,x%y);}
template<class T> T lcm(const T x,const T y){return x/gcd(x,y)*y;};
template<class T> T modpow(const T x,const T n,const T mod) {
  T res=1;while(n>0){if(n%2)res=res*x%mod;x=x*x%mod;n/=2;};return res;
}
template<class T> bool alltrue(const T &a){return all_of(ALL(a),[](bool x){return x;});}
template<class T> bool anytrue(const T &a){return any_of(ALL(a),[](bool x){return x;});}
template<class T> bool contain(const T &a,const T &b){for(auto x:b){if (a.find(x)==a.end()){return false;}}return true;}
template<class T> bool mmax(T &m,const T q){ if (m < q) {m = q; return true;} else return false; }
template<class T> bool mmin(T &m,const T q){ if (m > q) {m = q; return true;} else return false; }
template<class T> int siz(const T &a) {return (int)a.size();}
template<class T> set<T> devisers(T n) {
  set<T> res;for(T i=1;i<(T)sqrt(n)+1;i++){if(n%i==0){res.insert(i);res.insert(n/i);}}return res;
}
template<class T> void concat(T &a, const T &b){a.insert(a.end(),b.begin(),b.end());}
template<class T> void getand(const T &a,const T &b,T &c) {
  set_intersection(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.end()));
}
template<class T> void getor(const T &a,const T &b,T &c) {
  set_union(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.end()));
}

typedef long long ll;
typedef pair<ll, ll> P;

const ll MOD=1e9+7;
const int MAX=1e5+100;

int main() {
  int n;cin>>n;
  vector<int> ps(n);
  REP(i,n) cin>>ps[i];

  // 現在存在している「(」の数と、今)を置いたら、そこの数字は何になるかだけ保存しておく
  int lcnt = 0;
  vector<int> nums;
  string res;
  REP(i,n) {
    if (!nums.empty() && ps[i]<nums.back()) {
      OUT(":(");
      return 0;
    }

    if (ps[i]>lcnt) {
      int gap = ps[i]-lcnt;
      REP(j,gap) {
        res.push_back('(');
        nums.push_back(lcnt+j+1);
      }
      lcnt += gap;
    }

    res.push_back(')');
    nums.pop_back();
  }
  OUT(res);

  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3784
final_rank: 12

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

typedef long long ll;
typedef unsigned long long ull;

int main() {
  ll n;
  cin >> n;

  vector<ll> p(n);
  for (ll i = 0; i < n; ++i) {
    cin >> p[i];
    p[i]--;
  }

  bool ok = true;
  ll cur = 0;
  stack<ll> stack;
  stringstream ss;
  for (ll i = 0; i < n; ++i) {
    for (; cur <= p[i]; ++cur) {
      ss << "(";
      stack.push(cur);
    }

    if (stack.top() != p[i]) {
      ok = false;
      break;
    }

    ss << ")";
    stack.pop();
  }

  if (ok) {
    cout << ss.str() << endl;
  } else {
    cout << ":(" << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4436
final_rank: 14

code_snippet: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector> 
#include<limits>
#include<numeric>
#include<type_traits>
#include<math.h>
#include<fstream>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repi(i,a,b) for(int i = (int)(a); i < (int)(b); i++)
#define out(str) cout << str << endl
#define ALL(a) (a).begin(),(a).end()
#define INF (1<<29)

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

int now=0,N;

int main(){
    cin>>N;
    int A[N];
    rep(i,N){
        cin>>A[i];
        A[i]--;
    }
    stack<int> sta;
    string ans="";
    bool isok = true;
    rep(i,N){
        if(now<=A[i]){
            while(now<=A[i]){
                ans += "(";
                sta.push(now);
                now++;
            }
        }
        else if(sta.top() != A[i]){
            isok = false;
            break;
        }
        ans+=")";
        sta.pop();
    }
    if(isok)out(ans);
    else out(":(");
    
}
accuracy: 1
time_ms: 20
memory_kb: 3736
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#define trace1(x)                cout<<#x<<": "<<x<<endl
#define trace2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define trace7(a, b, c, d, e, f , g) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<< f << " | "<< #g <<": "<<g<<endl
#define trace8(a, b, c, d, e, f , g , h) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<< f << " | " << #g <<": "<< g <<" | "<<#h<< ": " << h << endl
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rrep(i,a,b) for(int i=(int)(a);i>=(int)(b);--i)
#define fore(x,a) for(auto &x:a)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define isin(i,a,b) ((a) <= (i) && (i) <= (b))
#define uni(a) (a).erase(unique(all(a)),(a).end())
#define dup(x,y) (((x)+(y)-1)/(y))
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(a) ((long long)(a).size())
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
using ll  = long long;          using vi  = vector<int>;
using unit= unsigned;           using vl  = vector<ll>;
using ull = unsigned long long; using vvi = vector<vi>;
using P   = pair<int,int>;      using vvl = vector<vl>;
                                using vp  = vector<P>;
void _main(); int main(){ cin.tie(0); ios::sync_with_stdio(false); _main(); }
template<class T>string join(const v(T)&v){ stringstream s; rep(i,0,sz(v))s<<' '<<v[i]; return s.str().substr(1); }
template<class T>istream &operator>>(istream&i, v(T)&v){ fore(x,v){ i >> v; } return i; }
template<class T>ostream &operator<<(ostream&o, const v(T)&v){ o<<"["; fore(x,v)o<<x<<","; o<<"]"; return o; }
template<class T>ostream &operator<<(ostream&o, const deque<T>&v){ o<<"deq["; fore(x,v)o<<x<<","; o<<"]"; return o; }
template<class T>ostream &operator<<(ostream&o, const set<T>&v){ o<<"{"; fore(x,v)o<<x<<","; o<<"}"; return o; }
template<class T>ostream &operator<<(ostream&o, const unordered_set<T>&v){ o<<"{"; fore(x,v)o<<x<<","; o<<"}"; return o; }
template<class T>ostream &operator<<(ostream&o, const multiset<T>&v){ o<<"{"; fore(x,v)o<<x<<","; o<<"}"; return o; }
template<class T>ostream &operator<<(ostream&o, const unordered_multiset<T>&v){ o<<"{"; fore(x,v)o<<x<<","; o<<"}"; return o; }
template<class T1,class T2>ostream &operator<<(ostream &o, const pair<T1,T2>&p){ o<<"("<<p.fi<<","<<p.se<<")"; return o; }
template<class TK,class TV>ostream &operator<<(ostream &o, const map<TK,TV>&m){ o<<"{"; fore(x,m)o<<x.fi<<"=>"<<x.se<<","; o<<"}"; return o; }
template<class TK,class TV>ostream &operator<<(ostream &o, const unordered_map<TK,TV>&m){ o<<"{"; fore(x,m)o<<x.fi<<"=>"<<x.se<<","; o<<"}"; return o; }
template<class T>void YES(T c){ if(c) cout<<"YES"<<endl; else cout<<"NO"<<endl; }
template<class T>void Yes(T c){ if(c) cout<<"Yes"<< endl; else cout<<"No"<<endl; }
template<class T>void POSS(T c){ if(c) cout<<"POSSIBLE"<<endl; else cout<<"IMPOSSIBLE"<<endl; }
template<class T>void Poss(T c){ if(c) cout<<"Possible"<<endl; else cout<<"Impossible"<<endl; }
template<class T>void chmax(T &a, const T &b){ if(a<b) a=b; }
template<class T>void chmin(T &a, const T &b){ if(b<a) a=b; }
template<class T>T gcd(T a, T b){ return (b==0) ? a : gcd(b,a%b); }
template<class T>T lcm(T a, T b){ return a * (b / gcd(a,b)); }
const double  EPS = 1e-10;
const double  PI  = acos(-1.0);
const int     INF = 1001002003;
const ll      LINF= 1001002003004005006LL;
const int dx[] = { -1, 0, 1, 0 , -1,-1, 1, 1 };
const int dy[] = {  0, 1, 0,-1 , -1, 1,-1, 1 };

void ng() {
  puts(":(");
  exit(0);
}

void _main() {
  int N;
  cin >> N;
  vi p(N);
  fore(x, p) cin >> x;

  deque<char> que;
  stack<int> s;
  int nxt = 1;
  rep(i, 0, N) {
    if (p[i] > nxt) {
      que.push_back('(');
      rep(j, nxt, p[i]) {
        que.push_back('(');
        s.push(j);
      }
      que.push_back(')');
      nxt = p[i] + 1;
    } else if (p[i] < nxt) {
      if (p[i] != s.top()) {
        puts(":(");
        return;
      }
      s.pop();
      que.push_back(')');
    } else {
      que.push_back('(');
      que.push_back(')');
      ++nxt;
    }
    // trace3(i, p[i], que);
  }
  fore(x, que) cout << x;
  puts("");
}
accuracy: 1
time_ms: 10
memory_kb: 3648
final_rank: 1

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

// varibable settings
#define int long long
const int INF=1e18;
const int MOD=1e9+7;

// define basic macro {{{
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)((b)-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define each(i,a) for (auto&& i : a)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define mt(a, b, c) make_tuple(a, b, c)
#define ceil(a,b) ((a)+(b)-1)/(b)
#define is_in(x, a, b) ((a)<=(x) && (x)<(b))
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define ub upper_bound
#define lb lower_bound
#define posl(A, x) (lower_bound(all(A), x)-A.begin())
#define posu(A, x) (upper_bound(all(A),x)-A.begin())
template<class T> inline void chmax(T &a, const T &b) { if((a) < (b)) (a) = (b); }
template<class T> inline void chmin(T &a, const T &b) { if((a) > (b)) (a) = (b); }

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;

template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;

#if defined(PCM) || defined(LOCAL)
  #include "lib/dump.hpp"
#else
  #define dump(...) 42
  #define dump_1d(...) 42
  #define dump_2d(...) 42
#endif
//}}}

signed main() {
  int n;cin>>n;
  vector<int> p(n);
  rep(i, n) {
    cin>>p[i];
  }
  dump(p);

  string ans = "";
  int k = 0;
  int m = 0;
  multiset<int> st;
  rep(i, 0, n+1) st.insert(i);
  rep(i, n){
    while (k<p[i]){
      ans+="(";
      m++;

      k = m;
    }
    if (k>p[i]){
      cout << ":(" << endl;
      return 0;
    }
    ans += ")";
    auto it = st.find(p[i]);
    it = st.erase(it);
    k = *(prev(it));
  }
  dump(ans);

  int lev = 0;
  rep(i, sz(ans)){
    if (ans[i]=='(') lev++;
    if (ans[i]==')') lev--;
    if (lev<0){
      cout << ":(" << endl;
      return 0;
    }
  }
  if (lev==0){
    cout << ans << endl;
  }
  else{
    assert(false);
    cout << ":(" << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 8408
final_rank: 17

code_snippet: #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#define int long long
#define mod 1000000007
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
bool b00l[123123];
signed main() {
	int n; string ans = "";
	int a = 0, b = 0, c;
	cin >> n;
	for1(i, n) {
		cin >> c;
		if (a < c) {
			for1(j, c - a)ans += '(';
			ans += ')';
			a = c;
		}
		else {
			bool b1 = 1;
			for (int j = c + 1; j <= a; j++)if (!b00l[j])b1 = 0;
			if (b1) { ans += ')'; }
			else {
				cout << ":(\n";
				return 0;
			}
		}
		b = c;
		b00l[c] = 1;
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3428
final_rank: 7

code_snippet: #include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

// #define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << endl
#define LINE cout << "line : " << __LINE__ << endl
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl
#define STOP assert(false)
#else
#define dump(x) ;
#define LINE     ;
#define dumpV(v);
#define STOP     ;
#endif
#define mp make_pair

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fi<<", "<<a.se<<')';
    return out;
  }
}


string s;

vector<pii> u;
void calc(int& i) {
    if(i == s.size()) return;
    assert(s[i] == '(');
    int l = i;
    ++i;
    while(s[i] == '(') calc(i);
    assert(s[i] == ')');
    u.push_back(make_pair(l, i));
    ++i;
    // cout << l << " " << i << endl;
}

int main(){
    int n;
    cin >> n;
    vector<pii> v(n);
    rep(i, n) {
        int j;
        cin >> j;
        --j;
        v[i] = make_pair(j, i);
    }
    sort(all(v));
    priority_queue<int,vector<int>,greater<int> > q;
    int cur = 0;
    rep(i, n) {
        s += '(';
        q.push(v[i].se);
        while(q.top() == cur) {
            q.pop();
            ++cur;
            s+=')';
        }
    }
    int id = 0;
    while(id < s.size()) calc(id);

    dumpV(u);

    {
        map<int, int> mp;
        for(auto&& p : u) mp[p.se] = 1;
        int cnt = 0;
        for(auto&& p : mp) p.se = cnt++;
        for(auto&& p : u) p.se = mp[p.se];
    }
    {
        map<int, int> mp;
        for(auto&& p : u) mp[p.fi] = 1;
        int cnt = 0;
        for(auto&& p : mp) p.se = cnt++;
        for(auto&& p : u) p.fi = mp[p.fi];
    }
    sort(all(u));
    if(v == u) cout << s <<endl;
    else cout << ":(" << endl;

    dump(s);
    dumpV(v);
    dumpV(u);
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 13132
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
#define int long long
using namespace std;
typedef pair<int,int>P;
typedef pair<P,int>P2;
#define fi first
#define se second
 
 
void END(){
  cout<<":("<<endl;
  exit(0);
}
 
signed main(){
  int n;
  cin>>n;
  int a[n];
  vector<P>ve;
  r(i,n){
    cin>>a[i];
    ve.push_back(P(a[i],i+1));
  }
 
  sort(ve.begin(),ve.end());
 
  r(i,n){
    a[i]=ve[i].second;
  }
 
  stack<P>st1;
  stack<P>st2;
 
  int del=1;
 
  string s;
 
  r(i,n){
 
    s+="(";
 
    ///st1.push(P(a[i],i+1));
    st2.push(P(a[i],i+1));
 
    while(!st2.empty()){
      if(st2.top().first!=del)break;
      s+=")";
      st2.pop();
      del++;
    }
 
    //cout<<st1.size()<<' '<<st2.size()<<endl;
  }
 
  if(!st1.empty()) END();
  if(!st2.empty()) END();
 
  cout<<s<<endl;
 
}
accuracy: 1
time_ms: 20
memory_kb: 6892
final_rank: 15

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

int main(){
    int N, P[100000];
    cin >> N;
    for(int i=0; i<N; i++) cin >> P[i];
    int now = 0;
    string ans = "";
    stack<int> st;
    for(int i=0; i<N; i++){
        while(now < P[i]){
            now++;
            st.push(now);
            ans.push_back('(');
        }
        if(st.size() && st.top() == P[i]){
            st.pop();
            ans.push_back(')');
        }else{
            cout << ":(" << endl;
            return 0; 
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3700
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
	int n;
	cin >> n;
	vector<int> p(n);
	for(int i=0; i<n; i++){
		cin >> p[i];
	}
	vector<int> st(1, 0);
	int curr = 0;
	string ans = "";
	for(int i=0; i<n; i++){
		if(p[i] < st.back()){
			ans = ":(";
			break;
		}
		while(curr < p[i]){
			curr++;
			ans += "(";
			st.push_back(curr);
		}
		st.pop_back();
		ans += ")";
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3804
final_rank: 13

code_snippet: #include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int p[100010];
vector<int> v;
string s;
int main(){
	int i,n;
	cin >> n;
	for(i=0;i<n;i++){
		cin >> p[i];
	}
	v.push_back(1); s.push_back('(');
	int cnt = 2;
	for(i=0;i<n;i++){
		if(i!=0 && p[i] - p[i-1]==-1){
			v.pop_back();
			s.push_back(')');
		}else{
			while(1){
				int x = v.back();
				if(x<p[i]){
					v.push_back(cnt); cnt++;
					s.push_back('(');
				}else if(x==p[i]){
					v.pop_back(); s.push_back(')');
					break;
				}else{
					cout << ":(" << endl;
					return 0;
				}
			}
		}
	}
	cout << s << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 3764
final_rank: 11