#include <bits/stdc++.h>
using namespace std;
#define mp(a, b) make_pair(a, b)
typedef pair<int, int> pii;
const int maxn = 2e5 + 10;
const int inf = 1e9;
int n, m;
int D[maxn], col[maxn];
vector<pii> E[maxn];
vector<int> G[maxn];
bool flag[maxn];
pii ed[maxn];
void dfs(int x, int fa)
{
	col[x] = col[fa] ^ 1;
	for(auto v : G[x])
		if(v != fa)
			dfs(v, x);
}
int main()
{
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; ++i) scanf("%d", &D[i]);
	for(int i = 1; i <= m; ++i)
	{
		int u, v;
		scanf("%d%d", &u, &v);
		E[u].push_back(mp(v, i));
		E[v].push_back(mp(u, i));
		ed[i] = mp(u, v);
	}
	for(int i = 1; i <= n; ++i)
	{
		pii Min = E[i][0];
		for(int j = 1; j < E[i].size(); ++j)
		{
			pii cur = E[i][j];
			if(D[Min.first] > D[cur.first] || (D[Min.first] == D[cur.first] && Min.second > cur.second))
				Min = cur;
		}
		if(D[i] < D[Min.first])
		{
			puts("-1");
			return 0;
		}
		flag[Min.second] = 1;
	}
	for(int i = 1; i <= m; ++i)
		if(flag[i])
		{
			int u = ed[i].first, v = ed[i].second;
			G[u].push_back(v);
			G[v].push_back(u);
		}
	memset(col, -1, sizeof col);
	col[0] = 0;
	string ans;
	for(int i = 1; i <= n; ++i)
	{
		if(col[i] == -1)
			dfs(i, 0);
		ans += col[i] ? 'W' : 'B';
	}
	cout << ans << endl;
	for(int i = 1; i <= m; ++i)
		if(flag[i])
		{
			int u = ed[i].first, v = ed[i].second;
			printf("%d\n", max(D[u], D[v]));
		}
		else
			printf("%d\n", inf);
	return 0;
}