#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <fstream>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <time.h>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) (min(a,b) <= (x) && (x) <= max(a,b)) //hei
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589


#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
	std::fill((T*)array, (T*)(array + N), val);
}
pll Dir[4] = {
	{ -1 ,0 },{ 0 , 1 },{ 1 ,0 },{ 0 ,-1 }
};

//[a, b)
#define Getsum(ar, a,b) (ar[b] - ar[a])
#define chmax(a,b) a = max(a,b)
#define chmin(a,b) a = min(a,b)

struct Edge {
	int from, to;
	ll w;
	bool operator<(const Edge& rhs) const {
		return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
	}

};
typedef vector<vector<Edge>> Graph;

ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

ll facmemo[200000] = {};

ll factorial(ll n) {//階乗
	if (facmemo[n] != 0) return facmemo[n];
	if (n == 0) return 1;
	ll ret = n * factorial(n - 1);
	ret %= MODU;
	return facmemo[n] = ret;
}

ll mod_pow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x %mod;
		x = x * x%mod;
		n >>= 1;
	}
	return res;
}
ll divmod(ll a, ll b, ll m) { // a/b(mod m)
								  // a/b (mod m)
								  // a*b^-1
								  // b^-1 * b = 1
								  // b^(m-1) = 1 
								  // b^(m-2) * b = 1
								  // b^-1 = b^(m-2)

								  // a/b = a * b^(m-2)

	return (a * 1LL * mod_pow(b, m - 2, m)) % m;
}
ll ncr(ll n, ll r) {
	if (r == 0) return 1;
	if (n < r) return 1;
	return divmod(factorial(n), (factorial(r) * (factorial(n - r))) % MODU, MODU);
}

bool sankaku(int a, int b, int c) {
	return a <= b + c && b <= a + c && c <= b + a;
}

template <typename T, typename E>
struct SegmentTree {
	typedef function<T(T, T)> F;
	typedef function<T(T, E)> G;
	int n;
	F f;
	G g;
	T d1;
	vector<T> dat;
	SegmentTree(int n_, F f, G g, T d1,
		vector<T> v = vector<T>()) {
		this->f = f;
		this->g = g;
		this->d1 = d1;
		init(n_);
		if (n_ == (int)v.size()) build(n_, v);
	}
	void init(int n_) {
		n = 1;
		while (n < n_) n *= 2;
		dat.clear();
		dat.resize(2 * n - 1, d1);
	}
	void build(int n_, vector<T> v) {
		for (int i = 0; i < n_; i++) dat[i + n - 1] = v[i];
		for (int i = n - 2; i >= 0; i--)
			dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
	}
	void update(int k, E a) {
		k += n - 1;
		dat[k] = g(dat[k], a);
		while (k > 0) {
			k = (k - 1) / 2;
			dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
		}
	}
	T query(int a, int b, int k, int l, int r) {
		if (r <= a || b <= l) return d1;
		if (a <= l && r <= b) return dat[k];
		T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
		T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
		return f(vl, vr);
	}
	T query(int a, int b) {
		return query(a, b, 0, 0, n);
	}
};

signed main() {
	int n, m;
	cin >> n >> m;

	vector<int> d(n);
	map<int ,set<int>> pd;
	REP(i, n) {
		cin >> d[i];
		pd[d[i]].insert(i);
	}

	vector<int> color(n, -1);
	map<pii, int> w;

	set<pii> pr;
	vector<pii> eds;
	set<pii> rec;
	set<int> al;

	Graph g(n);

	REP(i, m) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		g[u].push_back({ u,v });
		g[v].push_back({ v,u });
		pr.insert({ u,v });
		eds.push_back({ u,v });
	}

	for (auto cd : pd) {
		map<int, bool> death;

		for (auto i : cd.second) {
			if (death[i]) continue;

			for (auto ed : g[i]) {
				if (cd.second.count(ed.to)) {
					w[{i, ed.to}] = cd.first;

					if (color[ed.to] == -1 && color[i] == -1) {
						color[i] = 0;
						color[ed.to] = 1;
					}
					else if(color[ed.to] != -1)
						color[i] = 1-color[ed.to];
					else if(color[i] != -1)
						color[ed.to] = 1-color[ed.to];

					death[ed.to] = true;
					death[i] = true;
					break;
				}
			}

			if (death[i] == true) continue;

			for (auto ed : g[i]) {
				if (al.count(ed.to)) {
					w[{i, ed.to}] = cd.first - d[ed.to];
					color[i] = color[ed.to];
					death[i] = true;
					break;
				}
			}

			if (death[i] == false) {
				cout << -1 << endl;
				return 0;
			}
		}

		for (auto i : cd.second) {
			al.insert(i);
		}

	}

	REP(i, n) {
		cout << (color[i] ? "B" : "W");
	}
	cout << endl;
	REP(i, m) {
		if (w.count(eds[i])) {
			cout << w[eds[i]] << endl;
		}
		else if (w.count({ eds[i].second, eds[i].first })) {
			cout << w[{ eds[i].second, eds[i].first }] << endl;
		}
		else
			cout << 1000000000 << endl;
	}

	return 0;
}


Measured memory_bytes: 50800