#include <cstddef>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
namespace loquat {
using vertex_t = size_t;
}
namespace loquat {
namespace edge_param {
struct to_ {
	vertex_t to;
	explicit to_(vertex_t t = 0)
		: to(t)
	{ }
};
}
namespace detail {
template <typename T, typename... Params>
struct edge_param_wrapper : public T, edge_param_wrapper<Params...> {
};
template <typename T>
struct edge_param_wrapper<T> : public T {
	using self_type = edge_param_wrapper<T>;
	edge_param_wrapper(const self_type& e)
		: T(e)
	{ }
	template <typename U>
	explicit edge_param_wrapper(U&& x)
		: T(std::forward<U>(x))
	{ }
};
}
template <typename... Params>
struct edge : public detail::edge_param_wrapper<edge_param::to_, Params...> {
	using self_type = edge<Params...>;
	using wrapper_type = detail::edge_param_wrapper<edge_param::to_, Params...>;
	edge(const self_type& e)
		: wrapper_type(static_cast<const wrapper_type&>(e))
	{ }
	template <typename... Args>
	explicit edge(vertex_t to, Args&&... rest)
		: wrapper_type(to, std::forward<Args>(rest)...)
	{ }
};
}
namespace loquat {
template <typename EdgeType>
class adjacency_list {
public:
	using edge_type = EdgeType;
	using edge_list = std::vector<edge_type>;
private:
	std::vector<std::vector<EdgeType>> m_edges;
public:
	explicit adjacency_list(size_t n)
		: m_edges(n)
	{ }
	edge_list& operator[](vertex_t u){
		return m_edges[u];
	}
	template <typename... Args>
	void add_edge(vertex_t from, Args&&... args){
		m_edges[from].emplace_back(std::forward<Args>(args)...);
	}
};
}
using namespace std;
using pii = pair<int, int>;
using edge = loquat::edge<>;
static constexpr int INF = 1000000000;
int main(){
	ios_base::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vector<int> d(n);
	for(int i = 0; i < n; ++i){ cin >> d[i]; }
	loquat::adjacency_list<edge> g(n);
	vector<pii> edges;
	for(int i = 0; i < m; ++i){
		int u, v;
		cin >> u >> v;
		--u; --v;
		g.add_edge(u, v);
		g.add_edge(v, u);
		edges.emplace_back(u, v);
	}
	vector<pii> qs(n);
	for(int i = 0; i < n; ++i){ qs[i] = pii(d[i], i); }
	sort(qs.begin(), qs.end());
	vector<int> color(n, 0);
	map<pii, int> solution;
	for(const auto q : qs){
		const int u = q.second;
		if(color[u] != 0){ continue; }
		bool accept = false;
		for(const auto& e : g[u]){
			const int v = e.to;
			if(color[v] != 0 && d[v] < d[u]){
				color[u] = color[v];
				solution.emplace(pii(u, v), d[u] - d[v]);
				accept = true;
				break;
			}
		}
		if(accept){ continue; }
		for(const auto& e : g[u]){
			const int v = e.to;
			if(color[v] != 0 && d[v] == d[u]){
				color[u] = -color[v];
				solution.emplace(pii(u, v), d[u]);
				accept = true;
				break;
			}
		}
		if(accept){ continue; }
		for(const auto& e : g[u]){
			const int v = e.to;
			if(color[v] == 0 && d[v] == d[u]){
				color[u] = -1;
				color[v] =  1;
				solution.emplace(pii(u, v), d[u]);
				accept = true;
				break;
			}
		}
		if(accept){ continue; }
		cout << -1 << endl;
		return 0;
	}
	for(int i = 0; i < n; ++i){ cout << (color[i] < 0 ? "W" : "B"); }
	cout << endl;
	for(int i = 0; i < m; ++i){
		auto it = solution.find(edges[i]);
		if(it != solution.end()){
			cout << it->second << endl;
			continue;
		}
		it = solution.find(pii(edges[i].second, edges[i].first));
		if(it != solution.end()){
			cout << it->second << endl;
			continue;
		}
		cout << INF << endl;
	}
	return 0;
}


Measured memory_bytes: 20068