#include <bits/stdc++.h>
using namespace std;
const int INF=1e9;

const int MAX_N=1e5+10,MAX_M=2e5+10;

int N,M,Min,w,num;
vector<int> D(MAX_N),color(MAX_N,-1),ans(MAX_M,INF);
vector<vector<pair<int,int>>> G(MAX_N);
vector<vector<int>> nG(MAX_N);

void dfs(int v,int p){
    for (int u:nG[v]){
        if (u==p) continue;
        color[u]=color[v]^1;
        dfs(u,v);
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> M;
    for (int i=0;i<N;++i) cin >> D[i];
    for (int i=0;i<M;++i){
        int u,v; cin >> u >> v; --u,--v;
        G[u].emplace_back(v,i);
        G[v].emplace_back(u,i);
    }
    for (int v=0;v<N;++v){
        Min=INF+1;
        for (auto p:G[v]) if (D[p.first]<Min){
            Min=D[p.first]; w=p.first; num=p.second;
        }
        if (D[v]<Min){cout << -1 << '\n'; return 0;}
        ans[num]=D[v];
        nG[v].emplace_back(w);
        nG[w].emplace_back(v);
    }
    for (int v=0;v<N;++v){
        if (color[v]!=-1) continue;
        color[v]=0;
        dfs(v,-1);
    }
    for (int i=0;i<N;++i) cout << (color[i]?'B':'W');
    cout << '\n';
    for (int i=0;i<M;++i) cout << ans[i] << '\n';
}