#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
#define f first
#define s second 
using namespace std;
const LL INF = (LL) 1e9 + 5;
const LL MOD = (LL) 1e9 + 7;
const int MAXN = 300005;

struct DSU {
	int pa[MAXN], sz[MAXN];
	void init(int n) {
		for (int i = 0; i <= n; i++) {
			pa[i] = i;
			sz[i] = 1;
		}
	}
	int find(int x) {
		return x == pa[x] ? x : x = find(pa[x]);
	}
	int merge(int x, int y) {
		x = find(x); y = find(y);
		if (x == y) return 0;
		if (sz[x] < sz[y]) swap(x, y);
		sz[x] += sz[y];
		pa[y] = x;
		return 1;
	}
} ds;

struct Edge {
	int to, id;
};

int n, m, d[MAXN], w[MAXN], col[MAXN], dep[MAXN];
Edge pa[MAXN];
vector<Edge> G[MAXN];
vector<int> ch[MAXN];

void dfs(int v, int d) {
	dep[v] = d;
//	cout << "DFS " << v << " " << d << '\n';
	for (int to : ch[v]) {
		dfs(to, d + 1);
	}
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);

	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> d[i];
	}
	for (int i = 1; i <= m; i++) {
		int u, v;
		cin >> u >> v;
		G[u].push_back({v, i});
		G[v].push_back({u, i});
		w[i] = (int) 1e9;
	}
	
	ds.init(n);
	for (int i = 1; i <= n; i++) {
		int ok = 0;
		for (Edge e : G[i]) {
			if (d[e.to] <= d[i]) {
				ok = 1;
				if (ds.merge(i, e.to)) {
					pa[i] = e;
					ch[e.to].push_back(i);
					w[e.id] = d[i];
				}
				break;
			}
		}
		if (!ok) {
			cout << "-1\n";
			return 0;
		}
	}
	
	for (int i = 1; i <= n; i++) {
//		cout << "At " << i << " " << pa[i].to << '\n';
		if (pa[i].to == 0) {
			dfs(i, 1);
		}
	}
	
	for (int i = 1; i <= n; i++) {
		assert(dep[i] != 0);
		cout << (dep[i] % 2 ? "B" : "W");
	}
	cout << '\n';
	for (int i = 1; i <= m; i++) {
		cout << w[i] << '\n';
	}

	return 0;
}