Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
1.0 kB
2
Indexable
Never
#include <bits/stdc++.h>
using namespace std;
#define ll long long

ll a[27][27];

ll solve(string s){
    string tr, ph;
    ll i = 4,cnt = 0;
    if(s[1]>='0' && s[1]<='9'){
        return a[s[1]-'0'][s[0]-'A'+1];
    }
    for(;i<s.size();i++){
        if(s[i]==','){
            if(cnt==0)    break;
            cnt--;
        }
        if(s[i]=='(')    cnt++;
        tr+=s[i];
    }
    i++;
    for(;i<s.size()-1;i++){
        ph+=s[i];
    }
    if(s[0]=='S'){
        return solve(tr)+solve(ph);
    }else if(s[0]=='M'){
        return max(solve(tr),solve(ph));
    }
    return 0;
}

int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//  freopen("TEST.inp","r",stdin);
//     freopen("TEST.out","w",stdout);
    ll m,n,q;
    string s;
    cin >> m >> n;
    for(ll i=1;i<=m;i++){
        for(ll j=1;j<=n;j++){
            cin >> a[i][j];
        }
    }
    cin >> q;
    for(ll i=1;i<=q;i++){
        string n;
        cin >> s;
        cout << solve(s) << endl;
    }
}
Leave a Comment