#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll INFLL = (1ll<<60);
const int INF = (int)(1e9);

#define l_ength size

bool used[225816],visited[100100],color[100100];
int a[225816],b[225816],ans[225816],d[100100];

vector<P> g[100100];

void dfs(int u){
	int i,v,e;
	visited[u] = true;
	for(i=(g[u].l_ength()-1); i>=0; --i){
		v = g[u][i].first;
		e = g[u][i].second;
		if(!used[e]){
			continue;
		}
		if(visited[v]){
			continue;
		}
		color[v] = (!color[u]);
		dfs(v);
	}
}

int main(void){
	int n,m,u,v,i,j,e;
	bool flag;
	P tmp;
	fill(ans,ans+225816,INF);
	cin >> n >> m;
	for(i=0; i<n; ++i){
		cin >> d[i];
	}
	for(j=0; j<m; ++j){
		cin >> a[j] >> b[j];
		--a[j]; --b[j];
		g[a[j]].push_back(P(b[j],j));
		g[b[j]].push_back(P(a[j],j));
	}
	for(u=0; u<n; ++u){
		tmp = P(INF,INF);
		for(i=(g[u].l_ength()-1); i>=0; --i){
			v = g[u][i].first;
			e = g[u][i].second;
			tmp = min(tmp,P(d[v],v));
		}
		if(tmp.first > d[u]){
			cout << "-1" << endl;
			return 0;
		}
		for(i=(g[u].l_ength()-1); i>=0; --i){
			v = g[u][i].first;
			e = g[u][i].second;
			if(tmp.second == v){
				ans[e] = d[u];
				used[e] = true;
				break;
			}
		}
	}
	for(i=0; i<n; ++i){
		if(!visited[i]){
			dfs(i);
		}
		cout << ((color[i])?"B":"W");
	}
	cout << endl;
	for(j=0; j<m; ++j){
		cout << ans[j] << endl;
	}
	return 0;
}
