#ifndef __UNION_FIND_H__
#define __UNION_FIND_H__

/* updated: 2019-10-05 */

#include <vector>
#include <cstdint>

class UnionFindTree {
	const int size_m;					// 頂点数
	mutable std::vector<int> parent_m;	// 親の添字, 根である場合は自身の添字を入れる
	std::vector<int> card_m;			// 部分木のサイズ

public:
	UnionFindTree(int size);

private:
	bool isValidIndex(int i) const;

public:
	int rootOf(int i) const;
	bool isSame(int i, int j) const;
	void unite(int i, int j);

	int size() const;
	int cardinalityOf(int i) const;
	std::vector<std::vector<int>> toGroups() const;
};

class WeightedUnionFindTree {
	const int size_m;
	mutable std::vector<int> parent_m;
	mutable std::vector<uint64_t> diff_m;	// diff_m[i] : i の親ノード p との間の重み差 (weight[i] - weight[p] = diff_m[i]) 
	std::vector<int> card_m;				// 部分木のサイズ

public:
	WeightedUnionFindTree(int size);

private:
	bool isValidIndex(int i) const;

public:
	int rootOf(int i) const;
	bool isSame(int i, int j) const;
	int64_t query(int i, int j) const;
	bool unite(int i, int j, int64_t diff);

private:
	int64_t weight(int i) const;

public:
	int size() const;
};

#endif

#if ONLY_MY_ENVIR
#include "UnionFind.h"
#endif

#include <cassert>
#include <utility>
#include <algorithm>

UnionFindTree::UnionFindTree(int size) 
	: size_m(size), parent_m(size), card_m(size, 1) {
	for (int i = 0; i < size; ++i) {
		parent_m[i] = i;
	}
}

bool UnionFindTree::isValidIndex(int i) const {
	return i >= 0 && i < size();
}

int UnionFindTree::rootOf(int i) const {
	assert(isValidIndex(i));

	if (parent_m[i] == i) return i;
	return parent_m[i] = rootOf(parent_m[i]);
}

bool UnionFindTree::isSame(int i, int j) const {
	assert(isValidIndex(i) && isValidIndex(j));

	int ri = rootOf(i);
	int rj = rootOf(j);
	return ri == rj;
}

void UnionFindTree::unite(int i, int j) {
	assert(isValidIndex(i) && isValidIndex(j));

	int ri = rootOf(i);
	int rj = rootOf(j);

	if (ri == rj) return;
	if (card_m[ri] < card_m[rj]) {
		std::swap(ri, rj);
	}

	// assert(card_m[ri] >= card_m[rj]);
	parent_m[rj] = ri;
	card_m[ri] += card_m[rj];
}

int UnionFindTree::size() const {
	return size_m;
}

/* 頂点 i の属する集合のサイズ */
int UnionFindTree::cardinalityOf(int i) const {
	assert(isValidIndex(i));

	int ri = rootOf(i);
	return card_m[ri];
}

std::vector<std::vector<int>> UnionFindTree::toGroups() const {
	std::vector<std::vector<int>> groups(size_m);

	for (int i = 0; i < size_m; ++i) {
		int ri = rootOf(i);
		groups[ri].push_back(i);
	}

	std::vector<std::vector<int>> ret;
	for (int i = 0; i < size_m; ++i) {
		if (groups[i].empty()) continue;
		ret.push_back(groups[i]);
	}
	return ret;
}

WeightedUnionFindTree::WeightedUnionFindTree(int size)
	: size_m(size), parent_m(size), diff_m(size), card_m(size, 1) {
	for (int i = 0; i < size; ++i) {
		parent_m[i] = i;
	}
}

bool WeightedUnionFindTree::isValidIndex(int i) const {
	return i >= 0 && i < size();
}

int WeightedUnionFindTree::rootOf(int i) const {
	assert(isValidIndex(i));

	if (parent_m[i] == i) return i;
	
	diff_m[i] += weight(parent_m[i]);
	return parent_m[i] = rootOf(parent_m[i]);
}

bool WeightedUnionFindTree::isSame(int i, int j) const {
	assert(isValidIndex(i) && isValidIndex(j));

	return rootOf(i) == rootOf(j);
}

// weight[j] - weight[i] を返す
int64_t WeightedUnionFindTree::query(int i, int j) const {
	assert(isValidIndex(i) && isValidIndex(j));

	return weight(j) - weight(i);
}

bool WeightedUnionFindTree::unite(int i, int j, int64_t diff) {
	assert(isValidIndex(i) && isValidIndex(j));

	int64_t diff_roots = diff + weight(i) - weight(j);
	int ri = rootOf(i);
	int rj = rootOf(j);
	if (ri == rj) return false;
	if (card_m[ri] < card_m[rj]) {
		std::swap(ri, rj);
		diff_roots = -diff_roots;
	}

	// assert(card_m[ri] >= card_m[rj]);
	parent_m[rj] = ri;
	card_m[ri] += card_m[rj];
	diff_m[rj] = diff_roots;

	return true;
}

int64_t WeightedUnionFindTree::weight(int i) const {
	rootOf(i);		// 再計算のため
	return diff_m[i];
}

int WeightedUnionFindTree::size() const {
	return size_m;
}
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>

using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
#define REP(i, a, n) for(LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template <class T> T Next() { T buf; cin >> buf; return buf; }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }

#ifdef ONLY_MY_ENVIR
#include "IntMod.h"
#include "Algebraic.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "BIT.h"
#include "DynamicMod.h"
#include "Factorization.h"
#include "FFT.h"
#include "FlowSolver.h"
#include "Graph.h"
#include "LazySegmentTree.h"
#include "Math.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Polynomial.h"
#include "Position.h"
#include "Range.h"
#include "Rational.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "SlidingWindow.h"
#include "StringSeaching.h"
#include "SuffixArray.h"
#include "Tree.h"
#include "UnionFind.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>>(istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; }
ostream& operator<<(ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

const int MAX = 1000000000;
int N, M;
int D[100000];
vector<PP> G[100000];
int ans[200000];
int check[200000];
int color[200000];
vector<int> H[200000];

void rec(int from, int prev) {
	if (prev != -1) {
		color[from] = !color[prev];
	}

	for (int to : H[from]) {
		if (to == prev) continue;
		rec(to, from);
	}
}

void solve() {
	Fill(ans, MAX);
	Fill(check, MAX);
	PriorityQ<PP> Q;
	
	REP(i, 0, N) {
		Q.push(PP(D[i], i));
	}

	UnionFindTree uf(N);
	vector<bool> used(N);
	while (!Q.empty()) {
		auto p = Q.top(); Q.pop();
		int d = p.first;
		int from = p.second;
		used[from] = true;

		for (PP e : G[from]) {
			int to = e.first;
			int id = e.second;
			if (used[to]) {
				if (uf.isSame(from, to)) continue;
				uf.unite(from, to);
				ans[id] = d;
				H[from].push_back(to);
				H[to].push_back(from);
			}
		}
	}

	rec(0, -1);
	
	REP(from, 0, N) {
		for (PP e : G[from]) {
			int to = e.first;
			if (color[from] == color[to]) continue;
			int id = e.second;
			check[from] = min(check[from], ans[id]);
		}
		if (check[from] != D[from]) {
			cout << -1 << endl;
			return;
		}
	}

	REP(i, 0, N) {
		cout << (color[i] ? 'B' : 'W');
	}
	cout << endl;

	REP(j, 0, M) {
		cout << ans[j] << endl;
	}
}

int main() {
	cin >> N >> M;
	REP(i, 0, N) cin >> D[i];
	REP(j, 0, M) {
		int a, b;
		cin >> a >> b;
		--a; --b;
		G[a].push_back(PP(b, j));
		G[b].push_back(PP(a, j));
	}
	solve();
	return 0;
}

Measured memory_bytes: 27248