#include <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
#define Size 800050
using namespace std;
int D[Size];
struct graph{
	struct point{
		int next,to;
	}ar[Size];
	int head[Size],art=1;
	il void link(int u,int v){
		ar[++art]={head[u],v},head[u]=art;
		ar[++art]={head[v],u},head[v]=art;
	}
}G,G1;
int ans[Size],a[Size];
il void read(int&),dfs(int,bool);
int main(){
	//freopen("in","r",stdin);
	int N,M;read(N),read(M);
	for(int i(1);i<=N;++i)read(D[i]);
	for(int i(1),u,v;i<=M;++i)
		read(u),read(v),G.link(u,v),ans[i]=1e9;
	for(int i(1),j,i1;i<=N;++i){
		for(j=G.head[i],i1=0;j;j=G.ar[j].next)
			if(!i1||make_pair(D[G.ar[j].to],G.ar[j].to)<
			   make_pair(D[G.ar[i1].to],G.ar[i1].to))i1=j;
		if(D[G.ar[i1].to]>D[i])return puts("-1"),0;
		ans[i1>>1]=D[i],G1.link(i,G.ar[i1].to);
	}memset(a,-1,sizeof(a));
	for(int i(1);i<=N;++i)if(!~a[i])dfs(i,0);
	for(int i(1);i<=N;++i)putchar(a[i]?'B':'W');
	putchar('\n');
	for(int i(1);i<=M;++i)printf("%d\n",ans[i]);
	return 0;
}
il void dfs(int x,bool c){a[x]=c;
	for(int i(G1.head[x]),i1;i;i=G1.ar[i].next)
		if(!~a[i1=G1.ar[i].to])dfs(i1,c^1);
}
il void read(int&x){
	x^=x;ri char c;while(c=getchar(),c<'0'||c>'9');
	while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}
