#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <functional>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }

const int INF = 1e9;

int main() {
    int n, m;
    cin >> n >> m;
    VI d(n);
    REP(i,n) d[i] = in();
    VVI e(n), id(n);
    VVI esame(n), idsame(n);;
    REP(i,m){
        int u = in() - 1, v = in() - 1;
        e[u].push_back(v);
        id[u].push_back(i);
        e[v].push_back(u);
        id[v].push_back(i);
        if (d[u] == d[v]){
            esame[u].push_back(v);
            idsame[u].push_back(i);
            esame[v].push_back(u);
            idsame[v].push_back(i);
        }
    }

    VI col(n, -1), ans(m, INF);
    REP(i,n){
        if (esame[i].empty() || col[i] != -1) continue;
        queue<int> que;
        col[i] = 0;
        que.push(i);
        while (!que.empty()){
            int now = que.front();
            que.pop();
            REP(j,esame[now].size()){
                int next = esame[now][j];
                int id = idsame[now][j];
                if (col[next] == -1){
                    col[next] = !col[now];
                    que.push(next);
                    ans[id] = d[now];
                }
            }
        }
    }
    // REP(u,n){
    //     REP(i,e[u].size()){
    //         int v = e[u][i];
    //         if (d[u] != d[v]) continue;
    //         if (col[u] != -1 && col[v] != -1) continue;
    //         if (col[u] == -1 && col[v] == -1){
    //             col[u] = 0;
    //             col[v] = 1;
    //         }else if (col[u] == -1){
    //             col[u] = !col[v];
    //         }else{
    //             col[v] = !col[u];
    //         }
    //         if (col[u] != col[v]) ans[id[u][i]] = d[u];
    //     }
    // }

    priority_queue<P, vector<P>, greater<P> > que;
    VI dist(n, INF + 10);
    REP(i,n){
        if (col[i] == -1) col[i] = 1;
        if (col[i] == 0){
            dist[i] = 0;
            que.push(P(0, i));
        }
    }
    while (!que.empty()){
        P tmp = que.top();
        que.pop();
        int now = tmp.second;
        if (dist[now] < tmp.first) continue;
        REP(j,e[now].size()){
            int next = e[now][j];
            if (col[now] == 0 && d[next] < d[now]) continue;
            if (col[next] == 0) continue;
            if (d[next] == dist[next]) continue;
            if (dist[now] < d[next]){
                dist[next] = d[next];
                ans[id[now][j]] = d[next] - dist[now];
                que.push(P(dist[next], next));
            }
        }
    }
    REP(i,n){
        if (col[i] == 1 && dist[i] != d[i]){
            cout << -1 << endl;
            return 0;
        }
    }
    REP(i,n) cout << "WB"[col[i]];
    cout << endl;
    REP(i,m) printf("%d\n", ans[i]);

    return 0;
}


Measured memory_bytes: 26620