Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	var n int
	fmt.Scan(&n)

	var sc = bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	ps := make([]int, n)
	for i := range ps {
		sc.Scan()
		p, _ := strconv.Atoi(sc.Text())
		ps[i] = p
	}

	mp := make([]int, n+1)
	ans := make([]byte, 0)
	var pp, max int
	for _, p := range ps {
		mp[p] = 1
		if p < pp {
			if mp[p+1] != 1 {
				fmt.Println(":(")
				return
			}
			ans = append(ans, ')')
		} else {
			for i := max; i < p-1; i++ {
				ans = append(ans, '(')
			}
			ans = append(ans, '(')
			ans = append(ans, ')')
		}
		if p > max {
			max = p
		}
		pp = p
	}
	fmt.Println(string(ans))
}

Python: from collections import deque
import sys
sys.setrecursionlimit(10**7)

N = int(input())
P = list(map(int,input().split()))

Q = [-1]*N
for i,p in enumerate(P):
    Q[p-1] = i

S = ''
a = 1
def rec(l,r):
    global S,a
    if l==r:
        return
    while l < r:
        ai = Q[a-1]
        if ai >= r:
            print(':(')
            exit()
        a += 1
        S += '('
        rec(l,ai)
        S += ')'
        l = ai + 1
rec(0,N)

print(S)

Python: n = int(input())
a = list(map(int,input().split()))
cnt = 0
ans = ''
st = list()
for x in a:
    while cnt < x:
        ans += '('
        cnt += 1
        st.append(cnt)
    if st[-1] == x:
        st.pop()
        ans += ')'
    else:
        ans = ':('
        break
print(ans)

C++: #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;
}

C++: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int get_num(char buf[7]){

	int ret = 0;

	for(int i = 0; buf[i] != '\0'; i++){

		ret = 10*ret+(buf[i]-'0');
	}

	return ret;
}


int main(){

	int N;
	scanf("%d",&N);

	queue<char> ANS;

	int open_num = 0;
	stack<int> S;

	char buf[7];
	int tmp;

	for(int i = 0; i < N; i++){

		scanf("%s",buf);
		tmp = get_num(buf);

		while(open_num < tmp){ //閉じカッコの数と同じだけ開きカッコをpush
			ANS.push('(');
			S.push(++open_num);
		}

		if(tmp == S.top()){
			ANS.push(')');
			S.pop();
		}
	}

	if(!S.empty()){

		printf(":(\n");
		return 0;

	}else{

		while(!ANS.empty()){
			printf("%c",ANS.front());
			ANS.pop();
		}
		printf("\n");
	}

	return 0;
}

C++: // vvvvvvvvvvvv TEMPLATE vvvvvvvvvvvv
#include <bits/stdc++.h>
using namespace std; using ll = long long; using P = pair<ll, ll>;
const ll linf = 1e18; const double eps = 1e-12, pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define each(i,a) for (auto&& i : a)
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define eb emplace_back
#define all(a) begin(a),end(a)
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)
#define min(x,y) (x < y ? x : y)
#define max(x,y) (x < y ? y : x)
template<typename Head> void out(Head h) { cout << h << endl; } template<typename Head, typename... Tail>void out(Head h, Tail... t) { cout << h << " "; out(t...); }
template<typename T> istream& operator>>(istream& is, vector<T>& v) { each(x,v) is >> x; return is; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) { rep(i,v.size()) { if (i) os << " "; os << v[i]; } return os; }
template<typename T> ostream& operator<<(ostream& os, const vector<string>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
struct yes_no : std::numpunct<char> { string_type do_truename() const { return "Yes"; } string_type do_falsename() const { return "No"; } };
void solve(); int main() {
  ios::sync_with_stdio(false); cin.tie(0); locale loc(locale(), new yes_no); cout.imbue(loc); cout << fixed << setprecision(10) << boolalpha;
  solve();
}
// ^^^^^^^^^^^^ TEMPLATE ^^^^^^^^^^^^

void solve() {
  ll n; cin >> n;
  vector<ll> a(n); cin >> a;
  each(x, a) --x;
  stack<ll> S;
  string ans = "";
  ll cnt = 0, pot = 0, pos = 0;
  rep(i, 2*n) {
    // cout << cnt << " " << pot << " " << ans << endl;
    if (S.size() > 0 && pos < n && S.top() == a[pos]) {
      S.pop();
      ans += ')';
      --pot;
      ++pos;
    }
    else {
      S.push(cnt++);
      ans += '(';
      ++pot;
    }
    if (pot < 0) {
      cout << ":(" << endl;
      return;
    }
  }
  if (pot > 0) {
    cout << ":(" << endl;
    return;
  }
  cout << ans << endl;
}

C++: #include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<(n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
struct Grid{ll x,y,t;};
struct Edge{ll to,cost;};
struct Graph{vector<vector<Edge>>E;int V;
  const ll Inf = llINF;const int MAX_V=201010;vector<ll>d;
  Graph(int n):E(n){d.resize(MAX_V);E.resize(n);V=n;}
  void init(){for(int i=0;i<MAX_V;i++)d[i]=Inf;}
  void add_edge(ll from,ll to,ll cost){E[from].pb({to,cost});}
};
bool used[111111];
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;cin>>n;
  string ans="";
  bool hog=true;
  vi A(n);
  ll mi=1;
  ll ma=0;
  for(int i=0;i<n;i++){
    cin>>A[i];
    ma=max(A[i],ma);
    used[A[i]]=true;
    if(A[i]==mi){
      for(int j=mi;j<=ma;j++)
	if(used[j]==false)hog = false;
      if(hog){
	string s="";
	for(int j=0;j<(ma-mi+1)*2;j++)s+='(';
	ll po=0;
	ll cnt=0;
	for(int j=mi-1;j<ma;j++){
	  for(int k=po;k<s.size();k++){
	    if(cnt<=A[j]-mi){
	      cnt++;
	    }else{
	      s[k]=')';
	      po=k+1;
	      break;
	    }
	  }
	}
	mi=ma+1;
	ma=0;
	ans+=s;
      }
    }
  }
  if(hog){
    cout<<ans<<endl;
  }else{
    cout<<":("<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;

int main() {
	int N; cin >> N;
	vector<int> p(N) , q(N);
	for (int i = 0; i < N; ++i) cin >> p[i], --p[i], q[p[i]] = i;
	
	string res = "";
	int num = 0;
    stack<int> st;
	for (int i = 0; i < N; ++i) {
        res += "(";
        st.push(i);
        while (!st.empty() && q[st.top()] == num) {
            res += ")";
            ++num;
            st.pop();
        }
	}
	if (st.empty()) cout << res << endl;
	else cout << ":(" << endl;
}