#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;

const ll mod = 1e9+7;
const int maxn = 1e6 + 5;
const int inf = 1e9;



int a[maxn];
int n, m;
vector<pair<int,int>> g[maxn];
bool act[maxn];
int color[maxn];
int edges[maxn];

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

    memset(color,-1,sizeof(color));

    
    cin>>n>>m;


    vector<pair<int,int>> nodes;
    for (int i=0; i<n; i++) {
	cin>>a[i];
	nodes.push_back({a[i],i});
    }

    sort(nodes.begin(), nodes.end());

    for (int i=0; i<m; i++) {
	int u,v; cin>>u>>v;--u;--v;
	g[u].push_back({i,v});
	g[v].push_back({i,u});
    }

    for (int i=0; i<n; i++) {
	bool ok = false;
	for (auto ed: g[i]) {
	    int to = ed.second;
	    if (a[to] <= a[i]) {
		ok = true;
		break;
	    }
	}
	if (!ok) out(-1);
    }

    for (auto node: nodes) {
	int at = node.second;
	bool done = false;
	for (auto ed: g[at]) {
	    int id = ed.first;
	    int to = ed.second;
	    if (act[to]) {
		if (a[at] == a[to]) {
		    if (color[to] == -1) {
			color[at] = 0;
			color[to] = 1;
		    } else if (color[at] == -1) {
			color[at] = 1-color[to];
		    }
		    edges[id] = a[at];
		    done = true;
		}
	    }
	}

	if (done) {
	    for (auto ed: g[at]) {
		int to = ed.second;
		int id = ed.first;
		if (act[to] && a[to] < a[at]) {
		    edges[id] = inf;
		}
	    }
	} else {
	    for (auto ed: g[at]) {
		int to = ed.second;
		int id = ed.first;
		if (act[to]) {
		    assert(a[to] < a[at]);
		    if (color[at] == -1) {
			color[at] = color[to];
			edges[id] = a[at] - a[to];
		    } else {
			edges[id] = inf;
		    }
		}
	    }
	    
	}
	
	act[at]=true;
    }

    for (int i=0; i<n; i++) {
	cout<<(color[i]?'B':'W');
    }
    cout<<endl;
    for (int i=0; i<m; i++) {
	cout<<edges[i]<<"\n";
    }
    
    
    return 0;
}
