#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-12;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N >> M;
	vector<pair<int, pair<int, int>>>v(N);
	vector<int>w(N);
	for (int i = 0; i < N; i++) {
		cin >> K;
		v[i] = { (int)K,{(int)MOD,i} };
		w[i] = K;
	}
	vector<set<pair<int, int>>>edge(N);
	for (int i = 0; i < M; i++) {
		cin >> L >> R;
		L--, R--;
		edge[L].insert({ R,i });
		edge[R].insert({ L,i });
	}
	for (int i = 0; i < N; i++) {
		for (auto j : edge[i]) {
			v[i].second.first = min(v[i].second.first, w[j.first]);
		}
	}
	sort(v.begin(), v.end());


	vector<int>weight(M);
	string color(N, 'a');
	bool flag = true;
	for (auto i : v) {
		if (i.second.first > i.first) {
			cout << -1 << endl;
			return 0;
		}
		int skip = -1;
		for (auto j : edge[i.second.second]) {
			if (w[j.first] == i.second.first) {
				skip = j.first;
				if (w[j.first] < i.first) {
					color[i.second.second] = color[j.first];
					weight[j.second] = i.first - w[j.first];
				}
				else {
					if (color[j.first] == 'B')color[i.second.second] = 'W';
					else color[i.second.second] = 'B';
					weight[j.second] = i.first;
				}
				break;
			}
		}
		for (auto j : edge[i.second.second]) {
			if (j.first == skip)continue;
			if (weight[j.second])continue;
			if (w[j.first] > i.first)continue;
			weight[j.second] = i.first;
		}
	}
	bool flag = true;
	for (auto i : color)if (i == 'a')flag = false;
	assert(flag);
	cout << color << endl;
	for (auto i : weight)cout << i << endl;
	return 0;
}