#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef pair<int,int>P;
typedef long long ll;

const int MOD=1000000007;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;

class UnionFind {
	vector<int>par, sz;
public:
	UnionFind() {}
	UnionFind(int n) {
		par = sz = vector<int>(n);
		for (int i = 0; i < n; i++) {
			par[i] = i;
			sz[i] = 1;
		}
	}
	int find(int x) {
		if (par[x] == x)return x;
		return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x); y = find(y);
		if (x == y)return;
		if (sz[x] > sz[y]) {
			par[y] = x;
			sz[x] += sz[y];
		}
		else {
			par[x] = y;
			sz[y] += sz[x];
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
	int size(int x) {
		return sz[find(x)];
	}
};

int d[200000];
P p[200000];
vector<int>E[200000];
int col[200000];
int A[200000],B[200000];

int main(){
	int n,m;cin>>n>>m;
	rep(i,n){
		scanf("%d",&d[i]);
		p[i].first=d[i];
		p[i].second=i;
	}
	rep(i,m){
		int a,b;scanf("%d%d",&a,&b);a--;b--;
		E[a].push_back(b);
		E[b].push_back(a);
		A[i]=a;B[i]=b;
	}
	sort(p,p+n);
	memset(col,-1,sizeof(col));
	rep(x,n){
		int i=p[x].second;
		bool ok=false;
		for(int u:E[i]){
			if(d[u]<=d[i])ok=true;
		}
		if(!ok){
			puts("-1");return 0;
		}
		if(col[i]==-1){
			col[i]=0;
			for(int u:E[i]){
				if(d[u]<=d[i])col[u]=1;
			}
		}
		queue<int>que;
		que.push(i);
		while(!que.empty()){
			int s=que.front();que.pop();
			for(int u:E[s]){
				if(d[u]>=d[s]&&col[u]==-1){
					col[u]=!col[s];
					que.push(u);
				}
			}
		}
	}
	rep(i,n){
		if(col[i])cout<<"W";
		else cout<<"B";
	}
	cout<<endl;
	rep(i,m){
		printf("%d\n",max(d[A[i]],d[B[i]]));
	}
}