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

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()
 
const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9+7;

bool print_space_enable = false;
void print() { 
    cout << endl; 
    print_space_enable = false;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    if (print_space_enable) cout << " ";
    cout << fixed << setprecision(15) << head;
    print_space_enable = true;
    print(std::forward<Tail>(tail)...);
}

template<typename T>
void print(vector<T> v) {
    for (size_t i = 0; i < v.size(); i++) {
        if (i > 0) std::cout << " ";
        std::cout << v[i];
    }
    std::cout << std::endl;
}

int main() {
    string s;
    cin >> s;
    map<char, Int> mp;
    Int cnt = 0;
    for (Int i = 0; i < (Int)s.size(); i++) {
        if (isalpha(s[i])) cnt++;
    }
    for (Int i = 0; i < (Int)cnt; i++) {
        char a;
        Int x;
        cin >> a >> x;
        mp[a] = x;
    }
    stack<char> st;
    for (Int i = 0; i < (Int)s.size(); i++) {
        if (s[i] == '-') continue;
        if (s[i] == '[' or isalpha(s[i])) {
            st.push(s[i]);
        }
        if (s[i] == ']') {
            char a = st.top();
            st.pop();
            char b = st.top();
            st.pop();
            st.pop();
            if (mp[a] == 0 and mp[b] == 0) {
                print("No");
                return 0;
            } else if (mp[a] == 0) {
                mp[b]--;
                st.push(b);
            } else if (mp[b] == 0) {
                mp[a]--;
                st.push(a);
            } else {
                print("No");
                return 0;
            }
        }
    }
    if (st.size() == 1 and mp[st.top()] == 0) {
        print("Yes");
    } else {
        print("No");
    }
    return 0;
}
