#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;

llint n, m;
llint d[100005];
vector<P> G[100005];
vector<P> vec;
llint color[100005];
llint w[200005];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m;
	for(int i = 1; i <= n; i++){
		cin >> d[i];
		vec.push_back(P(d[i], i));
	}
	sort(vec.begin(), vec.end());
	
	llint u, v;
	for(int i = 1; i <= m; i++){
		cin >> u >> v;
		G[u].push_back(P(v, i));
		G[v].push_back(P(u, i));
	}
	
	for(int i = 0; i < vec.size(); i++){
		llint v = vec[i].second;
		if(color[v]) continue;
		bool flag = false;
		//cout << v << endl;
		for(int j = 0; j < G[v].size(); j++){
			llint u = G[v][j].first, e = G[v][j].second;
			if(d[u] > d[v]) continue;
			if(d[u] == d[v]){
				if(color[u] == 0) color[u] = -1, color[v] = 1;
				else color[v] = -color[u];
				//cout << v << " " << u << " " << color[u] << " " << color[v] << endl;
				w[e] = d[v];
				flag = true;
				break;
			}
			if(d[u] < d[v]){
				color[v] = color[u];
				w[e] = d[v] - d[u];
				flag = true;
				break;
			}
		}
		if(!flag){
			cout << -1 << endl;
			return 0;
		}
	}
	
	for(int i = 1; i <= n; i++){
		if(color[i] > 0) cout << "B";
		else cout << "W";
	}
	cout << endl;
	for(int i = 1; i <= m; i++){
		if(w[i] == 0) w[i] = 1000000000;
		cout << w[i] << endl;
	}
	
	return 0;
}