// They can't break me, as long as I know who I am...

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int maxn = 2e5 + 10, inf = 1e9;

int a[maxn], ans[maxn];
bool col[maxn], mark[maxn];
vector<int> v[maxn], g[maxn], vid[maxn], gid[maxn];
map<int, vector<int> > mp;

void dfs(int u, int w){
    mark[u] = 1;
    for(int i = 0; i < sz(g[u]); i++){
	if(mark[g[u][i]])
	    continue;
	col[ g[u][i] ] = col[u] ^ 1;
	ans[ gid[u][i] ] = w;
	dfs(g[u][i], w);
    }
}

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

    int n ,m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
	cin >> a[i];
	mp[a[i]].PB(i);
    }
    for(int i = 0; i < m; i++){
	int x, y;
	cin >> x >> y;
	if(a[x] > a[y])
	    swap(x, y);
	if(a[x] < a[y])
	    v[y].PB(x), vid[y].PB(i);
	else
	    g[x].PB(y), g[y].PB(x), gid[x].PB(i), gid[y].PB(i);
    }
    for(int u = 1; u <= n; u++){
	if(g[u].empty() && v[u].empty())
	    return cout << -1 << endl, 0;
    }

    fill(ans, ans + m, inf);
    
    for(auto it : mp){
	for(int u : it.S){
	    if(mark[u] == 0){
		if(g[u].empty())
		    col[u] = col[ v[u].back() ], ans[ vid[u].back() ] = a[u] - a[ v[u].back() ];
		else
		    dfs(u, it.F);
	    }
	}
    }
    for(int i = 1; i <= n; i++)
	cout << (col[i] ? 'B' : 'W');
    cout<<endl;
    for(int i = 0; i < m; i++)
	cout << ans[i] << "\n";
    return 0;
}
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
