#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
void Debug() {cerr << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cerr<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 500010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = (1 << 30) - 1;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng; //use it by rng() % mod, shuffle(all(vec), rng)
///////////////////////////////////////////////////////////////////////////////////////////////////


int N, E; //erase this

struct UF {
	vector<int> par, ran;
	void init(int n) {
		par.resize(n); ran.resize(n);
		for(int i = 0; i < n; i++) {
			par[i] = i;
			ran[i] = 0;
		}
	}
	UF(int mx = 0) { init(mx); }

	int find(int x) {
		if(par[x] == x) return x;
		else return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if(x == y) return;
		if(ran[x] < ran[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if(ran[x] == ran[y]) ran[x]++;
		}
	}
	bool same(int x, int y) { return find(x) == find(y); }
};

//////////////


struct edge { int u, v, cost, idx; };

bool comp(const edge& e1, const edge& e2) {
	return e1.cost < e2.cost;
}

//int N, E; add this////////////////////////////
edge es[MAX_N]; //don't forget to change the array size
int ans[MAX_N];
int A[MAX_N], B[MAX_N];
vi G[MAX_N];
int C[MAX_N];

void kruskal() {
	sort(es, es + E, comp);
	UF uf(N);//init union_find
	for(int i = 0; i < E; i++) {
		edge e = es[i];
		if(!uf.same(e.u, e.v)) {
			uf.unite(e.u, e.v);
			G[e.u].pb(e.v);
			G[e.v].pb(e.u);
			ans[e.idx] = e.cost;
		}
	}
}

void add_edge(int s, int t, int cost, int idx) {
	es[E++] = edge{s, t, cost, idx};
}

void loop(int v, int p, int c) {
	C[v] = c;
	rep(i, 0, sz(G[v])) {
		int n = G[v][i];
		if(n == p) continue;
		loop(n, v, c ^ 1);
	}
}

int M;
int D[MAX_N];
int R[MAX_N];

void solve() {
	cin >> N >> M;
	rep(i, 0, N) cin >> D[i];
	rep(i, 0, M) {
		int a, b; cin >> a >> b; a--; b--;
		A[i] = a;
		B[i] = b;
		add_edge(a, b, max(D[a], D[b]), i);
	}
	fill(ans, ans + M, 1000000000);
	kruskal();
	loop(0, -1, 0);
	fill(R, R + N, 1000000001);
	// debug(vi(ans, ans + M));
	// debug(vi(C, C + N), vi(D, D + N));
	rep(i, 0, M) {
		if((C[A[i]] ^ C[B[i]]) == 1) {
			MIN(R[A[i]], ans[i]);
			MIN(R[B[i]], ans[i]);
		}
	}
	rep(i, 0, N) {
		if(R[i] != D[i]) {
			cout << -1 << "\n"; return;
		}
	}
	rep(i, 0, N) {
		if(C[i]) cout << 'B';
		else cout << 'W';
	}
	cout << "\n";
	rep(i, 0, M) {
		cout << ans[i] << "\n";
	}
}

uint32_t rd() {
	uint32_t res;
#ifdef __MINGW32__
	asm volatile("rdrand %0" :"=a"(res) ::"cc");
#else
	res = std::random_device()();
#endif
	return res;
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
    cerr << fixed;
	cerr.precision(6);
	rng.seed(rd());
#ifdef LOCAL
	//freopen("in.txt", "wt", stdout); //for tester
	if(!freopen("in.txt", "rt", stdin)) return 1;
#endif	
	solve();
    cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}



Measured memory_bytes: 35312