#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MZ 200001
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin>>N;
vector<vector<pair<int,int>>>a(MZ),b(MZ);
int x=0,ca=0,cb=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=max(z,-y);
}
}
x+=y;
if(y>=0){
ca++;
a[z].emplace_back(y,i);
}
else{
cb++;
b[z].emplace_back(y,i);
}
}
if(x!=0){
cout<<-1<<'\n';
return 0;
}
vector<int>ans;
multimap<int,int>m;
int xx=x;
while(ca--){
while(xx<=x){
for(auto[y,i]:a[xx]){
m.emplace(y,i);
}
xx++;
}
if(m.empty()){
cout<<-1<<'\n';
return 0;
}
auto it=--m.end();
x+=it->first;
ans.push_back(it->second);
m.erase(it);
}
multimap<int,pair<int,int>>mm;
for(int z=0;z<MZ;z++){
for(auto[y,i]:b[z]){
mm.emplace(y+z,pair<int,int>{y,i});
}
}
while(cb--){
while(!mm.empty()&&(--mm.end())->first-(--mm.end())->second.first>x){
mm.erase(--mm.end());
}
if(mm.empty()){
cout<<cb<<" "<<x<<endl;
cout<<-1<<'\n';
return 0;
}
auto it=--mm.end();
x+=it->second.first;
ans.push_back(it->second.second);
mm.erase(it);
}
for(int i:ans){
cout<<i+1<<' ';
}
cout<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 12ms | 20576 KiB | ||||
2 | Accepted | 14ms | 21248 KiB | ||||
subtask2 | 11/11 | ||||||
3 | Accepted | 12ms | 20976 KiB | ||||
4 | Accepted | 10ms | 21188 KiB | ||||
5 | Accepted | 10ms | 21404 KiB | ||||
6 | Accepted | 10ms | 21616 KiB | ||||
7 | Accepted | 13ms | 22092 KiB | ||||
8 | Accepted | 12ms | 22292 KiB | ||||
subtask3 | 6/6 | ||||||
9 | Accepted | 12ms | 22240 KiB | ||||
10 | Accepted | 12ms | 22124 KiB | ||||
11 | Accepted | 12ms | 22128 KiB | ||||
subtask4 | 14/14 | ||||||
12 | Accepted | 12ms | 22492 KiB | ||||
13 | Accepted | 13ms | 22448 KiB | ||||
subtask5 | 23/23 | ||||||
14 | Accepted | 14ms | 22332 KiB | ||||
15 | Accepted | 12ms | 22592 KiB | ||||
subtask6 | 19/19 | ||||||
16 | Accepted | 13ms | 22420 KiB | ||||
17 | Accepted | 14ms | 22988 KiB | ||||
18 | Accepted | 14ms | 23260 KiB | ||||
19 | Accepted | 12ms | 23000 KiB | ||||
20 | Accepted | 13ms | 23260 KiB | ||||
21 | Accepted | 12ms | 23492 KiB | ||||
22 | Accepted | 10ms | 23508 KiB | ||||
23 | Accepted | 10ms | 23624 KiB | ||||
24 | Accepted | 13ms | 23712 KiB | ||||
subtask7 | 27/27 | ||||||
25 | Accepted | 13ms | 23404 KiB | ||||
26 | Accepted | 13ms | 23660 KiB | ||||
27 | Accepted | 14ms | 24060 KiB | ||||
28 | Accepted | 14ms | 24164 KiB | ||||
29 | Accepted | 17ms | 25088 KiB | ||||
30 | Accepted | 35ms | 30660 KiB | ||||
31 | Accepted | 13ms | 23924 KiB | ||||
32 | Accepted | 13ms | 24360 KiB | ||||
33 | Accepted | 12ms | 24216 KiB | ||||
34 | Accepted | 13ms | 24652 KiB | ||||
35 | Accepted | 10ms | 23716 KiB | ||||
36 | Accepted | 16ms | 24840 KiB | ||||
37 | Accepted | 10ms | 23712 KiB | ||||
38 | Accepted | 13ms | 23716 KiB | ||||
39 | Accepted | 19ms | 26704 KiB | ||||
40 | Accepted | 10ms | 23784 KiB | ||||
41 | Accepted | 12ms | 23716 KiB | ||||
42 | Accepted | 12ms | 24156 KiB | ||||
43 | Accepted | 10ms | 24032 KiB |