#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>
#include <algorithm>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

struct Edge {
	int u, v, c, id;
	void read() {
		std::cin >> u >> v;
		c = 1e9;
		u--;v--;
	}
};

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int n, m;
	std::cin >> n >> m;
	std::vector<int> dist(n);
	for(int i = 0; i < n; i++) {
		std::cin >> dist[i];
	}
	std::vector<int> col(n, 0);
	std::vector<Edge> edges(m);
	int c = 0;
	for(auto &e : edges) {
		e.read();
		e.id = c++;
	}
	bool got = false;
	std::sort(edges.begin(), edges.end(), [&](Edge e1, Edge e2) { return std::min(dist[e1.u], dist[e1.v]) < std::min(dist[e2.u], dist[e2.v]); });
	for(auto &e : edges) {
		int u = e.u, v = e.v;
		if(col[u] && col[v]) continue;
		if(dist[u] != dist[v]) continue;
		if(!col[u] && !col[v]) {
			col[u] = -1;
			col[v] = 1;
		} else if (!col[u]) {
			col[u] = -col[v];
		} else {
			col[v] = -col[u];
		}
		e.c = dist[u];
		got = true;
	}
	if(!got) {
		std::cout << "-1\n";
		return 0;
	}
	for(auto &e : edges) {
		int u = e.u, v = e.v;
		if(col[u] && col[v]) continue;
		if(!col[u] && !col[v]) {
			continue;
		}
		if(!col[u] && dist[u] > dist[v]) {
			col[u] = col[v];
		} else if(!col[v] && dist[v] > dist[u]) {
			col[v] = col[u];
		} else {
			continue;
		}
		e.c = abs(dist[u] - dist[v]);
	}
	std::sort(edges.begin(), edges.end(), [&](Edge e1, Edge e2) { return e1.id < e2.id; });
	for(int i = 0; i < n; i++) {
		if(!col[i]) {
			std::cout << "-1\n";
			return 0;
		}
	}
	for(int i = 0; i < n; i++) {
		std::cout << char(col[i] == 1 ? 'W' : 'B');
	}
	std::cout << '\n';
	for(auto e : edges) {
		std::cout << e.c << '\n';
	}
}