101952024-03-29 11:43:38111Zárójelekcpp17Time limit exceeded 73/100563ms4820 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	vector<tuple<int,int,int>>v(N);
	int x=0;
	for(int i=0;i<N;i++){
		string S;
		cin>>S;
		int y=0,z=0;
		for(char c:S){
			if(c=='('){
				y++;
			}
			else{
				y--;
				z=min(z,y);
			}
		}
		x+=y;
		v[i]={z,y,i};
	}
	if(x!=0){
		cout<<-1<<'\n';
		return 0;
	}
	vector<int>ans;
	while(ans.size()<N){
		int bi=-1,by=-1e6;
		int dc=1;
		for(int i=0;i<v.size();i++){
			auto[z,y,_]=v[i];
			if(y>=0){
				dc=0;
			}
		}
		for(int i=0;i<v.size();i++){
			auto[z,y,_]=v[i];
			if(x+z>=0&&(dc?y-z>by:y>by)){
				bi=i;
				by=dc?y-z:y;
			}
		}
		if(bi==-1){
			cout<<-1<<'\n';
			return 0;
		}
		x+=get<1>(v[bi]);
		if(x<0)exit(1);
		ans.push_back(get<2>(v[bi]));
		v.erase(v.begin()+bi);
	}
	for(int i:ans){
		cout<<i+1<<' ';
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1696 KiB
2Accepted10ms2096 KiB
subtask211/11
3Accepted3ms2076 KiB
4Accepted3ms2288 KiB
5Accepted3ms2500 KiB
6Accepted3ms2712 KiB
7Accepted4ms3304 KiB
8Accepted4ms3260 KiB
subtask36/6
9Accepted10ms3364 KiB
10Accepted3ms3280 KiB
11Accepted3ms3316 KiB
subtask414/14
12Accepted10ms3500 KiB
13Accepted7ms3492 KiB
subtask523/23
14Accepted9ms3456 KiB
15Accepted7ms3312 KiB
subtask619/19
16Accepted4ms3512 KiB
17Accepted6ms3508 KiB
18Accepted10ms3660 KiB
19Accepted7ms3648 KiB
20Accepted7ms3524 KiB
21Accepted10ms3672 KiB
22Accepted10ms3680 KiB
23Accepted10ms3672 KiB
24Accepted10ms3828 KiB
subtask70/27
25Accepted4ms3460 KiB
26Accepted10ms3728 KiB
27Accepted19ms3780 KiB
28Accepted32ms3984 KiB
29Accepted180ms4580 KiB
30Time limit exceeded563ms4092 KiB
31Accepted30ms3956 KiB
32Accepted46ms4144 KiB
33Accepted18ms3920 KiB
34Accepted65ms4144 KiB
35Accepted4ms4020 KiB
36Accepted89ms4440 KiB
37Accepted4ms3964 KiB
38Accepted7ms4256 KiB
39Time limit exceeded555ms4020 KiB
40Accepted8ms4464 KiB
41Accepted3ms4472 KiB
42Accepted14ms4752 KiB
43Accepted10ms4820 KiB