1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct op
{
    char co;
    int il, nr;
};

int t, n;
ll zl[100000];

int main()
{
    scanf("%d", &t);

    for(int qu = 0; qu < t; qu++)
    {
        ll wyn = LLONG_MAX;
        vector <op> v;
        vector <int> it;

        scanf("%d", &n);

        for(int i = 0; i < n; i++)
        {
            int l;

            scanf("%d", &l);

            for(int j = 0; j < l; j++)
            {
                char c;

                scanf(" %c", &c);

                if(c == 'W' || c == 'Z')
                    v.push_back({c, 0, i});
                else
                {
                    int tym;

                    scanf("%d", &tym);

                    if(c == '-')
                        tym *= -1;

                    v.push_back({'+', tym, i});
                }

                it.push_back(it.size());
            }
        }

        do
        {
            ll zg = 0;

            for(int i = 0; i < n; i++)
                zl[i] = 0;

            for(auto& i : it)
            {
                op tym = v[i];

                if(tym.co == 'W')
                    zl[tym.nr] = zg;
                if(tym.co == 'Z')
                    zg = zl[tym.nr];
                if(tym.co == '+')
                    zl[tym.nr] += ll(tym.il);
            }

            wyn = min(wyn, zg);
        }
        while(next_permutation(it.begin(), it.end()));

        printf("%lld\n", wyn);
    }
}