//#pragma GCC optimize(3)

#include<bits/stdc++.h>
#define MST(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define eb emplace_back
#define rep(i,a,b) for(int i = a ; i < b ; ++i)
#define per(i,a,b) for(int i = a ; i > b ; --i)
#define ALL(v) v.begin(),v.end()
#define ft first
#define sd second
using namespace std;
namespace fast_IO{
    const int maxbuff = 1<<17;
    char ibuff[maxbuff],obuff[maxbuff];
    inline char Getchar(){
        static char *p1=ibuff,*p2=ibuff;
        if(p1 == p2) {
            p1 = ibuff;
            p2 = ibuff+fread(ibuff,1,maxbuff,stdin);
            if(p1 == p2) return -1;
        }
        return *p1++;
    }

    int optr = 0;
    inline void Putchar(char c) {
        if(optr == maxbuff) {fwrite(obuff,1,optr,stdout);optr = 0;}
        obuff[optr++] = c;
    }

    inline void Flush() {
        fwrite(obuff,1,optr,stdout);
    }

    template<class T>
    inline int read(T &x) {
        x = 0;
        char c = Getchar();
        if(c == EOF) return -1;
        for( ; (c < '0' || c > '9') && c != EOF ; c = Getchar());
        if(c == EOF) return -1;
        for( ; c >= '0' && c <= '9' ; c = Getchar()) x = x*10+c-'0';
        return 0;
    }

    template<class T>
    inline void write(const T &x,int tag = 0) {
        if(!x) {
            if(!tag) Putchar(x+'0');
            return ;
        }
        write(x/10,1);
        Putchar(x%10+'0');
    }

    template<class T>
    inline void writeln(const T &x) {
        write(x);Putchar('\n');
    }

    template<class T>
    inline void writesp(const T &x) {
        write(x);Putchar(' ');
    }
}
template<class T> void show(T *x,int len,int s = 0) {for(int i = s ; i < s+len-1 ; ++i) cerr << x[i] << ' ';cerr << x[s+len-1] << endl;}
void debug() {cerr << endl;}
template <typename T, typename... Args> void debug(T x,Args... args) {cerr << x << ' ';debug(args...);}
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pii;
#include <ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;
typedef __gnu_pbds::priority_queue<pii,greater<>,pairing_heap_tag> heap;
typedef heap::point_iterator iter;

template<class T>
inline void cmin(T &A,const T &B) {A = (A<B?A:B);}
template<class T>
inline void cmax(T &A,const T &B) {A = (A>B?A:B);}

const int N = 3e5+19;
vector<pii> e[N];
int clr[N],n,m,len[N],d[N];
int flag;
void bfs(int u) {
    queue<int> q;
    q.push(u);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(auto _ : e[u]) {
            int v = _.first,id = _.second;
            if(clr[v] != -1) continue;
            if(d[v] == d[u]) {
                clr[v] = !clr[u];
                len[id] = d[v];
                q.push(v);
            }
            if(d[v] > d[u]) {
                clr[v] = clr[u];
                len[id] = d[v]-d[u];
                q.push(v);
            }
        }
    }
}


void solve() {
    cin >> n >> m;
    MST(clr,-1);
    MST(len,-1);
    int mn = 1e9+7;
    for(int i = 1 ; i <= n ; ++i) {
        cin >> d[i];
        mn = min(mn, d[i]);
    }
    for(int i = 1 ; i <= m ; ++i) {
        int u,v;
        cin >> u >> v;
        e[u].emplace_back(v,i);
        e[v].emplace_back(u,i);
    }
    for(int i = 1 ; i <= n ; ++i)
        if(d[i] == mn) {
            int fflag = 0;
            for(auto v : e[i]) {
                if(d[v.first] == mn) {
                    fflag = 1;
                    break;
                }
            }
            if(fflag) {
                clr[i] = 1;
                bfs(i);
                break;
            }
        }
    for(int i = 1 ; i <= n ; ++i) if(clr[i] == -1) {
        puts("-1");
        return ;
    }
    for(int i = 1 ; i <= m ; ++i) if(len[i] == -1) len[i] = 1e9;
    for(int i = 1 ; i <= n ; ++i) cout << (clr[i] ? 'W' : 'B');
    cout << endl;
    for(int i = 1 ; i <= m ; ++i) cout << len[i] << endl;
}

signed main() {
#ifdef LOCAL
    freopen("input","r",stdin);
    //freopen("output","w",stdout);
#endif
    srand((int)time(0));
    ios::sync_with_stdio(0);
    int T = 1;
    //cin >> T;
    while(T--) solve();
}