#include <bits/stdc++.h>
#define va first
#define vb second
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii,int> ppi;
using namespace std;
const int MN = 2e5+5;
int N,M,D[MN],C[MN],Q[MN],par[MN],cnt,num;
ppi E[MN];
bool ch[MN],tree[MN],col[MN];
vector<int> T[MN];

int fnd(int a)
{
    return par[a]==a ? a : par[a]=fnd(par[a]);
}

void join(int a, int b)
{
    a = fnd(a), b = fnd(b);
    par[b] = a;
}

void dfs(int s, int r)
{
    for(int e : T[s]){
        if(e==r) continue;
        col[e] = !col[s];
        dfs(e,s);
    }
}

bool cmp(ppi p, ppi q)
{
    return max(D[p.va.va],D[p.va.vb])<max(D[q.va.va],D[q.va.vb]);
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0);
    cin >> N >> M;
    for(int i=1; i<=N; i++){
        par[i] = i;
        cin >> D[i];
    }
    for(int i=1; i<=N; i++){
        Q[i] = 1e9+7;
    }
    for(int i=0; i<M; i++){
        cin >> E[i].va.va >> E[i].va.vb;
        E[i].vb = i;
    }
    sort(E,E+M,cmp);
    for(int i=0; i<M; i++){
        int a = E[i].va.va;
        int b = E[i].va.vb;
        if(fnd(a)!=fnd(b)){
            join(a,b);
            T[a].push_back(b);
            T[b].push_back(a);
            C[E[i].vb] = max(D[a],D[b]);
            Q[a] = min(Q[a],C[E[i].vb]);
            Q[b] = min(Q[b],C[E[i].vb]);
            tree[E[i].vb] = 1;
            cnt++;
        }
    }
    if(cnt<N-1){
        cout << -1;
        return 0;
    }
    for(int i=1; i<=N; i++){
        if(Q[i]!=D[i]){
            cout << -1;
            return 0;
        }
    }
    dfs(1,1);
    for(int i=1; i<=N; i++){
        if(col[i]) cout << 'B';
        else cout << 'W';
    }
    cout << '\n';
    for(int i=0; i<M; i++){
        if(tree[i]) cout << C[i] << '\n';
        else cout << "1000000000\n";
    }
}
