#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define int long long
vector<int>d;
vector<pair<int,int> > p;
map<pair<int,int>,int > dst;
vector<vector<int> > g;
vector<bool> flag;
signed main(){
    int n,m;
    cin >> n >> m;
    d.resize(n);
    flag.resize(n);
    rep(i,n)cin >> d[i];
    set<pair<int,int> > st;
    g.resize(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
        if(a>b)swap(a,b);
        p.push_back(MP(a,b));
        st.insert(MP(a,b));
    }
    vector<pair<int,int> > ss;
    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq;
    vector<char> res(n,'.');
    rep(i,n){
        for(auto x:g[i]){
            int a = i;
            int b = x;
            if(a>b)swap(a,b);
            if(d[a]==d[b]){
                if(st.count(MP(a,b))){
                    flag[a] = 1;
                    flag[b] = 1;
                    if(res[a]=='.'&&res[b]=='.'){
                        res[a] = 'B';
                        res[b] = 'W';
                        dst[MP(a,b)] = d[i];
                    }else if(res[a]=='.'&&res[b]!='.'){
                        dst[MP(a,b)] = d[i];
                        if(res[b]=='B'){
                            res[a] = 'W';
                        }else{
                            res[a] = 'B';
                        }
                    }else if(res[a]!='.'&&res[b]=='.'){
                        dst[MP(a,b)] = d[i];
                        if(res[a]=='B'){
                            res[b] = 'W';
                        }else{
                            res[b] = 'B';
                        }
                    }
                }   
            }
        }
        if(!flag[i]){
            pq.push(MP(d[i],i));
        }
    }
    // rep(i,n)cerr<<res[i];
    // cerr << endl;
    while(!pq.empty()){
        auto a = pq.top();
        pq.pop();
        int i = a.second;
        int bb = a.first;
        // cerr << i << " " << bb << endl;
        for(auto x:g[i]){
            // cerr << x << " "<< flag[x] << " " << d[x] << endl;
            if(flag[x]&&bb>=d[x]){
                flag[i] = true;
                if(res[x]=='B'){
                    res[i] = 'W';
                }else{
                    res[i] = 'B';
                }
                int a = x;
                int b = i;
                if(a>b)swap(a,b);
                dst[MP(a,b)] = bb;
                break;
            }
        }
        if(!flag[i]){
            cout << -1 << endl;
            return 0;
        }
    }
    rep(i,n)cout <<res[i];
    cout << endl;
    for(auto x:p){
        if(dst.find(x)!=dst.end()){
            cout << dst[x] << "\n";
        }else{
            cout << 1000000000 << "\n";
        }
    }
    return 0;
}

Measured memory_bytes: 34540