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

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
typedef double DOUBLE;
#define xx real()
#define yy imag()

#define REP(i, a, b) for(int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define FA(it, x) for(__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const ll LL_INF = 1e17 + 10;
const int INF = 1e9 + 10;
const ll MOD = 1e9 + 7;

const int MAXN = 2e5 + 5;
const int MAXW = 1e9;

pair<pii, pii> ee[MAXN];

int v[MAXN];
int w[MAXN];
char col[MAXN];

void solve(){
	int n, m;
	scanf("%d%d", &n, &m);
	REPN(i, 1, n) {
		scanf("%d", &v[i]);
	}
	REP(i, 0, m) {
		int a, b;
		scanf("%d%d", &a, &b);
		ee[i] = mp(mp(max(v[a], v[b]), i), mp(a, b));
	}
	sort(ee, ee + m);
	REP(i, 0, m) {
		int a = ee[i]._2._1, b = ee[i]._2._2;
		int ind = ee[i]._1._2;
		if (!col[a] && !col[b]) {
			if (v[a] != v[b]) {
				printf("-1\n");
				return;
			}
			col[a] = 'B';
			col[b] = 'W';
			w[ind] = v[a];
		} else if (col[a] && col[b]) {
			w[ind] = MAXW;
		} else {
			if (!col[a]) {
				swap(a, b);
			}
			if (v[a] < v[b]) {
				col[b] = col[a];
				w[ind] = v[b] - v[a];
			} else if (v[a] == v[b]) {
				col[b] = 'W' - col[a] + 'B';
				w[ind] = v[a];
			}
		}
	}
	printf("%s\n", col + 1);
	REP(i, 0, m) {
		printf("%d\n", w[i]);
	}
}

int main(){

    //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();    
    }
}
