#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cassert>
using namespace std;
#define N 200020
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*f;
}
int n,m,d[N],t[N],ans[N];
pair<int,int> p[N];
vector<pair<int,int> > G[N];
struct Union{
	int f[N];
	void init(int n){
		for(int i=1;i<=n;++i)f[i]=i;
	}
	inline int getf(int x){
		return f[x]==x?x:f[x]=getf(f[x]);
	}
}S;
int main(){
	n=read(),m=read();
	for(int i=1;i<=n;++i){
		d[i]=read();
		t[i]=-1;
	}
	for(int i=1;i<=m;++i){
		int u=read(),v=read();
		G[u].emplace_back(v,i);
		G[v].emplace_back(u,i);
		ans[i]=1e9;
	}
	for(int i=1;i<=n;++i){
		p[i]=(*min_element(G[i].begin(),G[i].end(),[&](pair<int,int> i,pair<int,int> j){
			return d[i.first]==d[j.first]?i.first<j.first:d[i.first]<d[j.first];
		}));
		if(d[p[i].first]>d[i]){
			return !printf("-1\n");
		}
	}
	for(int i=1;i<=n;++i){
		int u=p[i].first;
		if(~t[i])continue;
		if(d[u]==d[i]){
			if(~t[u]){
				t[i]=t[u]^1;
			}
			else{
				t[i]=0,t[u]=1;
			}
			ans[p[i].second]=d[i];
		}
	}
	S.init(n);
	for(int i=1;i<=n;++i){
		if(~t[i])continue;
		int u=p[i].first;
		S.f[i]=u;
		ans[p[i].second]=d[i]-d[u];
	}
	for(int i=1;i<=n;++i){
		putchar(t[S.getf(i)]?'B':'W');
	}
	putchar('\n');
	for(int i=1;i<=m;++i){
		printf("%d\n",ans[i]);
	}
	return 0;
}
