#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define inf 0x3f3f3f3f
#define maxn 100010
inline ll read()
{
    ll x=0; char c=getchar(),f=1;
    for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1;
    for(;'0'<=c&&c<='9';c=getchar())x=x*10+c-'0';
    return x*f;
}
inline void write(ll x)
{
    static int buf[20],len; len=0;
    if(x<0)x=-x,putchar('-');
    for(;x;x/=10)buf[len++]=x%10;
    if(!len)putchar('0');
    else while(len)putchar(buf[--len]+'0');
}
inline void writeln(ll x){write(x); putchar('\n');}
inline void writesp(ll x){write(x); putchar(' ');}
int u[2*maxn],v[2*maxn];
std::vector<int> nxt[maxn];
int a[maxn],fa[maxn],col[maxn],id[maxn];
int n,m;
int cmp(int x,int y){return a[x]<a[y];}
void dfs(int now)
{
    // printf("%d %d\n",now,col[now]);
    for(int i=0;i<nxt[now].size();i++)
        if(!col[nxt[now][i]]){
            col[nxt[now][i]]=-col[now];
            dfs(nxt[now][i]);
        }
}
int main()
{
	n=read(); m=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    for(int i=1;i<=m;i++){
        u[i]=read(); v[i]=read();
        nxt[u[i]].push_back(v[i]);
        nxt[v[i]].push_back(u[i]);
    }
    for(int i=1;i<=n;i++){
        int mn=inf,mnid=0;
        for(int j=0;j<nxt[i].size();j++)
            if(a[nxt[i][j]]<mn){
                mn=a[nxt[i][j]];
                mnid=nxt[i][j];
            }
        if(mn>a[i]){
            puts("-1");
            return 0;
        }
        fa[i]=mnid;
    }
    // for(int i=1;i<=n;i++)
    //     writesp(fa[i]);
    // putchar('\n');
    for(int i=1;i<=n;i++)
        nxt[i].clear();
    for(int i=1;i<=n;i++){
        nxt[i].push_back(fa[i]);
        nxt[fa[i]].push_back(i);
    }
    for(int i=1;i<=n;i++)
        if(!col[i]){
            col[i]=1;
            dfs(i);
        }
    for(int i=1;i<=n;i++)
        if(col[i]==1)putchar('B');
        else putchar('W');
    putchar('\n');
    for(int i=1;i<=m;i++)
        if(fa[u[i]]==v[i]||fa[v[i]]==u[i])writeln(std::max(a[u[i]],a[v[i]]));
        else writeln(1e9);
    return 0;
}