[ABC064D] Insertion
BNU_ACM
编辑于 2023年06月19日 18:56
收录于文集
共87篇
  • 贪心、前缀和

代码块
C++
自动换行
复制代码
// Problem: D - Insertion
// URL: https://atcoder.jp/contests/abc064/tasks/abc064_d
// Author: Pengfei Xu

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

const int maxn=102;
int pre[maxn];

void solve(){
	int n; string s; cin >> n >> s;
	for(int i=1;i<=n;++i)
		if(s[i-1]=='(') pre[i]=pre[i-1]-1;
		else			pre[i]=pre[i-1]+1;
	int ls = *max_element(pre,pre+n+1);
	int rs = ls-pre[n];
	string t(ls,'(');
	string u(rs,')');
	cout << t+s+u << endl;
}

int main(){
	int T=1;
	while(T--) solve();
}
复制成功