#include <bits/stdc++.h>
#define rep(i,l,r) for (int i=l,i##end=r;i<=i##end;++i)
#define per(i,r,l) for (int i=r,i##end=l;i>=i##end;--i)
#define pr pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
const int N=2e5+5;
pr a[N]; int d[N],ans[N]; bool vis[N],c[N];
vector< pr > G[N];
void dfs(int u){
    vis[u]=true;
    rep(i,0,G[u].size()-1){
        int v=G[u][i].fi;
        if (d[u]==d[v]){
            ans[G[u][i].se]=d[u];
            if (!vis[v]){ c[v]=!c[u]; dfs(v); }
        }
    }
}
bool complete(int u){
    rep(i,0,G[u].size()-1){
        int v=G[u][i].fi;
        if (d[v]<d[u]) ans[G[u][i].se]=1e9;
    }
}
int main(){
    int n,m; scanf("%d%d",&n,&m);
    rep(i,1,n){ scanf("%d",&d[i]); a[i]=mp(d[i],i); }
    rep(i,1,m){
        int u,v; scanf("%d%d",&u,&v);
        G[u].pb(mp(v,i)),G[v].pb(mp(u,i));
    }
    rep(i,1,n){
        bool t=false;
        rep(j,0,G[i].size()-1)
            if (d[G[i][j].fi]<=d[i]){ t=true; break; }
        if (!t){ cout<<-1<<endl; return 0; }
    }
    sort(a+1,a+1+n);
    rep(i,1,n){
        int u=a[i].se;
        if (vis[u]){ complete(u); continue; }
        bool t=false;
        rep(j,0,G[u].size()-1)
            if (d[u]==d[G[u][j].fi]){ t=true; break; }
        if (t){ dfs(u); complete(u); }
        else{
            rep(j,0,G[u].size()-1){
                int v=G[u][j].fi;
                if (d[v]<d[u]) ans[G[u][j].se]=c[v]?d[u]:d[u]-d[v];
            }
        }
    }
    rep(i,1,n) printf("%c",c[i]?'W':'B'); cout<<endl;
    rep(i,1,m) printf("%d\n",ans[i]);
    return 0;
}
