#include <cstdio>
#include <vector>
#include <queue>
#include <utility>
#include <string>
#include <algorithm>
using namespace std;

typedef pair<int,int> pii;

int main() try{
	const int INF = 1010101010;

	int n, m;
	scanf("%d%d", &n, &m);
	vector<int> ds(n + 1, INF);
	for(int i = 0; i < n; ++i){ scanf("%d", &ds[i]); }
	vector<vector<pii>> G(n);
	for(int i = 0; i < m; ++i){
		int a, b;
		scanf("%d%d", &a, &b);
		G[a - 1].emplace_back(b - 1, i);
		G[b - 1].emplace_back(a - 1, i);
	}

	string res1(n, '?');
	vector<int> res2(m, 1000000000);
	priority_queue<pii> pq;
	for(int i = 0; i < n; ++i){
		if(res1[i] != '?'){ continue; }
		int minadj = INF;
		pii pe(-1, -1);
		for(const pii &e : G[i]){
			int v = e.first;
			minadj = min(minadj, ds[v]);
			if(ds[v] == ds[i] && res1[v] == '?'){
				pe = e;
			}
		}
		if(minadj > ds[i]){ throw __LINE__; }
		if(pe.first >= 0){
			res1[i] = 'B';
			res1[pe.first] = 'W';
			res2[pe.second] = ds[i];
			for(const auto &e : G[i]){
				int v = e.first;
				pq.emplace(-ds[v], v);
			}
			for(const auto &e : G[pe.first]){
				int v = e.first;
				pq.emplace(-ds[v], v);
			}
		}
	}
	while(!pq.empty()){
		int u = pq.top().second;
		pq.pop();
		if(res1[u] != '?'){ continue; }
		int mindv = n;
		int eidx = -1;
		for(const pii &e : G[u]){
			int v = e.first;
			if(res1[v] != '?' && ds[mindv] > ds[v]){
				mindv = v;
				eidx = e.second;
			}
			pq.emplace(-ds[v], v);
		}
		if(eidx < 0){
			res1[u] = 'B';
		}
		else if(ds[mindv] > ds[u]){
			throw __LINE__;
		}
		else{
			res1[u] = res1[mindv] ^ 'B' ^ 'W';
			res2[eidx] = ds[u];
		}
	}

	if(count(res1.begin(), res1.end(), '?') > 0){ throw __LINE__; }
	puts(res1.c_str());
	for(int x : res2){ printf("%d\n", x); }
}
catch(int ln){
	fprintf(stderr, "catch %d\n", ln);
	puts("-1");
}
