#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;


template <typename T>
bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}


signed main(){
    int n, m;
    cin >> n >> m;
    vector<int> d(n);
    for(int i = 0; i < n; ++i)
        cin >> d[i];
    vector<int> u(m), v(m);
    vector<pair<int,int>> uvp;
    vector<vector<int>> edges(n);
    for(int i = 0; i < m; ++i){
        cin >> u[i] >> v[i];
        --u[i], --v[i];
        uvp.emplace_back(minmax(u[i], v[i]));
        edges[u[i]].emplace_back(v[i]);
        edges[v[i]].emplace_back(u[i]);
    }
    vector<int> col(n, -1);
    map<pair<int,int>, int> weight;

    function<void(int)> f = [&](int x){
        for(auto& y : edges[x]){
            if(col[y] != -1 || d[x] > d[y])
                continue;
            weight[minmax(x, y)] = d[y];
            col[y] = !col[x];
            f(y);
        }
    };

    for(int i = 0; i < m; ++i){
        if(d[u[i]] == d[v[i]] && col[u[i]] == -1 && col[v[i]] == -1){
            col[u[i]] = 0;
            col[v[i]] = 1;
            weight[uvp[i]] = d[u[i]];
            f(u[i]);
            f(v[i]);
        }
    }

    if(count(col.begin(), col.end(), -1)){
        cout << -1 << endl;
        return 0;
    }

    for(int i = 0; i < n; ++i)
        cout << (col[i] ? 'B' : 'W');
    cout << endl;
    for(int i = 0; i < m; ++i)
        cout << (weight[uvp[i]] == 0 ? 1000000000 : weight[uvp[i]]) << endl;
}

