#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MZ 500000
struct ST{
int n;
vector<int>a,b;
ST(int n):n(n),a(n*4,INT_MIN),b(n*4,-1){
}
pair<int,int>query(int i,int l,int r,int ll,int rr){
if(l>rr||r<ll){
return {INT_MIN,-1};
}
if(l>=ll&&r<=rr){
return {a[i],b[i]};
}
auto[al,bl]=query(i*2+1,l,(l+r)/2,ll,rr);
auto[ar,br]=query(i*2+2,(l+r)/2+1,r,ll,rr);
return al>=ar?pair<int,int>{al,bl}:pair<int,int>{ar,br};
}
pair<int,int>query(int ll,int rr){
return query(0,0,n-1,ll,rr);
}
void update(int i,int l,int r,int ii,int xx){
if(l>ii||r<ii){
return;
}
if(l==r){
a[i]=xx;
b[i]=l;
return;
}
update(i*2+1,l,(l+r)/2,ii,xx);
update(i*2+2,(l+r)/2+1,r,ii,xx);
a[i]=a[i*2+1]>=a[i*2+2]?a[i*2+1]:a[i*2+2];
b[i]=a[i*2+1]>=a[i*2+2]?b[i*2+1]:b[i*2+2];
}
void update(int ii,int xx){
update(0,0,n-1,ii,xx);
}
};
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin>>N;
ST a(MZ),b(MZ);
vector<int>ma(MZ),mb(MZ),za(MZ),zb(MZ);
set<int>aa,bb;
for(int i=0;i<MZ;i++){
aa.insert(i);
bb.insert(i);
}
int x=0,c=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){
c++;
int zz=*aa.lower_bound(MZ/2-z);
aa.erase(zz);
a.update(zz,y);
ma[zz]=i;
za[zz]=z;
}
else{
int zz=*bb.lower_bound(MZ/2-z);
bb.erase(zz);
b.update(zz,y+z);
mb[zz]=i;
zb[zz]=z;
}
}
if(x!=0){
cout<<-1<<'\n';
return 0;
}
vector<int>ans;
while(ans.size()<N){
if(c>0){
c--;
auto[y,z]=a.query(MZ/2-x,MZ);
if(y==INT_MIN){
cout<<-1<<'\n';
return 0;
}
a.update(z,INT_MIN);
x+=y;
ans.push_back(ma[z]);
}
else{
auto[yz,z]=b.query(MZ/2-x,MZ);
if(yz==INT_MIN){
cout<<-1<<'\n';
return 0;
}
b.update(z,INT_MIN);
x+=yz-zb[z];
ans.push_back(mb[z]);
}
}
for(int i:ans){
cout<<i+1<<' ';
}
cout<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 455ms | 252164 KiB | ||||
2 | Time limit exceeded | 501ms | 252428 KiB | ||||
subtask2 | 0/11 | ||||||
3 | Accepted | 456ms | 252664 KiB | ||||
4 | Accepted | 456ms | 252804 KiB | ||||
5 | Accepted | 497ms | 252780 KiB | ||||
6 | Accepted | 500ms | 252992 KiB | ||||
7 | Accepted | 456ms | 253528 KiB | ||||
8 | Time limit exceeded | 501ms | 253724 KiB | ||||
subtask3 | 0/6 | ||||||
9 | Accepted | 458ms | 253508 KiB | ||||
10 | Time limit exceeded | 501ms | 253756 KiB | ||||
11 | Time limit exceeded | 501ms | 253984 KiB | ||||
subtask4 | 0/14 | ||||||
12 | Accepted | 460ms | 254224 KiB | ||||
13 | Time limit exceeded | 501ms | 254300 KiB | ||||
subtask5 | 0/23 | ||||||
14 | Wrong answer | 458ms | 254452 KiB | ||||
15 | Time limit exceeded | 504ms | 254672 KiB | ||||
subtask6 | 0/19 | ||||||
16 | Accepted | 456ms | 254788 KiB | ||||
17 | Time limit exceeded | 504ms | 254708 KiB | ||||
18 | Wrong answer | 460ms | 254816 KiB | ||||
19 | Wrong answer | 455ms | 254816 KiB | ||||
20 | Time limit exceeded | 508ms | 254816 KiB | ||||
21 | Time limit exceeded | 504ms | 254880 KiB | ||||
22 | Wrong answer | 460ms | 255124 KiB | ||||
23 | Wrong answer | 455ms | 255016 KiB | ||||
24 | Wrong answer | 458ms | 254944 KiB | ||||
subtask7 | 0/27 | ||||||
25 | Accepted | 453ms | 255068 KiB | ||||
26 | Accepted | 456ms | 255072 KiB | ||||
27 | Wrong answer | 458ms | 255076 KiB | ||||
28 | Time limit exceeded | 504ms | 255184 KiB | ||||
29 | Time limit exceeded | 514ms | 255076 KiB | ||||
30 | Time limit exceeded | 513ms | 255180 KiB | ||||
31 | Time limit exceeded | 507ms | 255124 KiB | ||||
32 | Time limit exceeded | 504ms | 255124 KiB | ||||
33 | Time limit exceeded | 505ms | 255188 KiB | ||||
34 | Time limit exceeded | 509ms | 255184 KiB | ||||
35 | Wrong answer | 458ms | 255076 KiB | ||||
36 | Wrong answer | 467ms | 255084 KiB | ||||
37 | Time limit exceeded | 501ms | 255192 KiB | ||||
38 | Wrong answer | 458ms | 255176 KiB | ||||
39 | Time limit exceeded | 527ms | 255084 KiB | ||||
40 | Time limit exceeded | 504ms | 255080 KiB | ||||
41 | Time limit exceeded | 501ms | 255080 KiB | ||||
42 | Wrong answer | 458ms | 255332 KiB | ||||
43 | Wrong answer | 456ms | 255428 KiB |