#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
const int N=2e5+5;
struct node{
	int u,val;
	node () {}
	node (int _u,int _v){
		u = _u; val = _v;
	}
	bool operator < (const node &b) const {
		return val < b.val;
	}
};
priority_queue<node> q;
int a[N],n,m,col[N],val[N],vis[N];
vector< pair<int,int> > e[N];
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i)
		cin>>a[i];
	int fl=1,u,v;
	for(int i=1;i<=m;++i){
		cin>>u>>v;
		e[u].push_back(make_pair(v,i));
		e[v].push_back(make_pair(u,i));
		if( a[u] == a[v] && !vis[u] && !vis[v])
			q.push(node(u,a[u])),
			q.push(node(v,a[v])),
			col[u]=1,col[v]=0,
			vis[u]=1,vis[v]=1,
			val[i]=a[u];
	}
	while(!q.empty()){
		int u=q.top().u,V=q.top().val;q.pop();
		for(int i=0;i<e[u].size();++i)
			if(!vis[e[u][i].fi] && a[e[u][i].fi]>=V)
				vis[e[u][i].fi]=1,q.push(node(e[u][i].fi,a[e[u][i].fi])),
				col[e[u][i].fi]=col[u]^1,val[e[u][i].se]=a[e[u][i].fi];
	}
	for(int i=1;i<=n;++i)
		if(!vis[i]) return puts("-1"),0;
	for(int i=1;i<=n;++i)
		cout<<(col[i]==0?"B":"W");
	cout<<endl;
	for(int i=1;i<=m;++i)
		cout<<(val[i]==0?(int)1e9:val[i])<<endl;
	return 0;
}