167772025-05-12 22:26:31TaxiradioZárójelekcpp17Accepted 100/10032ms1624 KiB
// Source: https://usaco.guide/general/io

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

array<int , 3> c(string b){
    int x = 0, y = 0 , z = 0;
    for(int i = b.size()-1; i >= 0; i--){
        if(b[i] == '('){
            x--;
            z = min(x , z);
        }else{
            x++;
        }
    }
    x = 0;
    for(int i = 0; i < b.size(); i++){
        if(b[i] == ')'){
            x--;
            y = min(x , y);
        }else{
            x++;
        }
    }
    return {x , y , z};
}

int main() {
	int n; cin >> n;
    vector<array<int , 3>> a , d;
    int k = 0;
    for(int i = 0; i < n; i++){
        string b; cin >> b;
        array<int , 3> o = c(b);
        k += o[0];
        if(o[0] >= 0)a.push_back({-o[1] , o[0] , i});else d.push_back({-o[2] , -o[0] , i});
    }
    if(k != 0){
        cout << "-1" << endl;
        return 0;
    }
    sort(a.begin() , a.end());
    sort(d.begin() , d.end());
    vector<int> ans1 , ans2;
    int h = 0;
    for(int i = 0; i < a.size(); i++){
        if(a[i][0] <= h){
            ans1.push_back(a[i][2]);
            h += a[i][1];
        }else{
            cout << "-1" << endl;
            return 0;
        }
    }
    h = 0;
    for(int i = 0; i < d.size(); i++){
        if(d[i][0] <= h){
            ans2.push_back(d[i][2]);
            h += d[i][1];
        }else{
            cout << "-1" << endl;
            return 0;
        }
    }
    for(int x : ans1)cout << x+1 << " ";
    reverse(ans2.begin() , ans2.end());
    for(int x : ans2)cout << x+1 << " ";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
2Accepted8ms316 KiB
subtask211/11
3Accepted1ms316 KiB
4Accepted1ms332 KiB
5Accepted1ms316 KiB
6Accepted1ms332 KiB
7Accepted8ms424 KiB
8Accepted8ms528 KiB
subtask36/6
9Accepted8ms448 KiB
10Accepted7ms452 KiB
11Accepted7ms440 KiB
subtask414/14
12Accepted7ms544 KiB
13Accepted7ms464 KiB
subtask523/23
14Accepted7ms512 KiB
15Accepted7ms444 KiB
subtask619/19
16Accepted8ms316 KiB
17Accepted8ms440 KiB
18Accepted8ms440 KiB
19Accepted7ms448 KiB
20Accepted7ms468 KiB
21Accepted6ms512 KiB
22Accepted4ms520 KiB
23Accepted6ms420 KiB
24Accepted4ms316 KiB
subtask727/27
25Accepted8ms432 KiB
26Accepted9ms476 KiB
27Accepted9ms436 KiB
28Accepted10ms460 KiB
29Accepted14ms604 KiB
30Accepted32ms1624 KiB
31Accepted8ms384 KiB
32Accepted8ms544 KiB
33Accepted8ms452 KiB
34Accepted9ms444 KiB
35Accepted7ms424 KiB
36Accepted9ms564 KiB
37Accepted7ms472 KiB
38Accepted7ms316 KiB
39Accepted17ms820 KiB
40Accepted4ms316 KiB
41Accepted2ms316 KiB
42Accepted7ms452 KiB
43Accepted6ms440 KiB