#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using P = pair<int,int>;
int n, m, v, u, d[100005], ans[200005];
char ans2[100005];
vector<P> G[100005], G2[100005];
void dfs(int u, int p, int c){
	ans2[u] = c ? 'B' : 'W';
	for(P nx: G2[u]) if(nx.first != p) dfs(nx.first, u, c ^ nx.second);
}
int main(){
	scanf("%d%d", &n, &m);
	fill(ans, ans+m, (int)1e9);
	rep(i,n) scanf("%d", d+i);
	rep(i,m){
		scanf("%d%d", &v, &u);
		v--; u--;
		G[v].push_back(P{u,i});
		G[u].push_back(P{v,i});
	}
	rep(i,n){
		bool ok = false;
		for(P p: G[i]) if(d[p.first] <= d[i]){
			int tmp = d[p.first] == d[i];
			ans[p.second] = d[i] - (1-tmp) * d[p.first];
			G2[p.first].push_back({i,tmp});
			G2[i].push_back({p.first,tmp});
			ok = true;
			break;
		}
		if(!ok) return puts("-1") * 0;
	}
	rep(i,n) if(ans2[i] == '\0') dfs(i,i,0);
	printf("%s\n", ans2);
	rep(i,m) printf("%d\n", ans[i]);
}