#include<bits/stdc++.h>
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef pair<pii,int> ppii;
typedef pair<int,pii> pipi;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<ll,pll> plpl;
typedef tuple<ll,ll,ll> tl;
ll mod=1000000007;
ll mod2=998244353;
ll mod3=1000003;
ll mod4=998244853;
ll inf=1000000000000000000;
long double pi=3.14159265358979323846L;
double eps=1e-12;
#define rep(i,m,n) for(int i=m;i<n;i++)
#define rrep(i,n,m) for(int i=n;i>=m;i--)
int dh[4]={1,-1,0,0};
int dw[4]={0,0,1,-1};
int ddh[8]={-1,-1,-1,0,0,1,1,1};
int ddw[8]={-1,0,1,-1,1,-1,0,1};
ll lmax(ll a,ll b){
    if(a<b)return b;
    else return a;
}
ll lmin(ll a,ll b){
    if(a<b)return a;
    else return b;
}
ll gcd(ll a,ll b){
    if(a<b)swap(a,b);
    if(b==0)return a;
    if(a%b==0)return b;
    return gcd(b,a%b);
}
ll Pow(ll n,ll k){
    ll ret=1;
    ll now=n;
    while(k>0){
        if(k&1)ret*=now;
        now*=now;
        k/=2;
    }
    return ret;
}
ll gya[1000010];
ll kai[1000010];
ll beki(ll n,ll k,ll md){
  ll ret=1;
  ll now=n;
  while(k>0){
    if(k%2==1){
      ret*=now;
      ret%=md;
    }
    now*=now;
    now%=md;
    k/=2;
  }
  return ret;
}
ll gyaku(ll n,ll md){
  return beki(n,md-2,md);
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,m;cin>>n>>m;
    ll d[n+1];
    vector<pll> w;
    ll ff[m],gg[m];
    rep(i,1,n+1){
        cin>>d[i];
        w.push_back({d[i],i});
    }
    sort(w.begin(),w.end());
    vector<ll> v[n+1];
    rep(i,0,m){
        ll f,g;cin>>f>>g;
        ff[i]=f;
        gg[i]=g;
        v[f].push_back(g);
        v[g].push_back(f);
    }
    ll col[n+1];
    fill(col,col+n+1,-1);
    map<pll,ll> mp;
    rep(i,0,n){
        ll now=w[i].second,dis=w[i].first;
        //cout<<now<<" "<<col[now]<<endl;
        if(col[now]>=0){
            rep(j,0,v[now].size()){
                ll ne=v[now][j];
                if(col[ne]>=0&&mp[{now,ne}]==0){
                    mp[{now,ne}]=1000000000;
                    mp[{ne,now}]=1000000000;
                }
            }
            continue;
        }
        ll num=-1;
        rep(j,0,v[now].size()){
            ll ne=v[now][j];
            if(col[ne]>=0){
                num=ne;
                break;
            }
        }
        //cout<<now<<" "<<num<<endl;
        if(num==-1){
            rep(j,0,v[now].size()){
                ll ne=v[now][j];
                if(d[ne]==dis){
                    num=ne;
                    break;
                }
            }
            if(num==-1){
                cout<<-1<<endl;
                return 0;
            }
            mp[{now,num}]=dis;
            mp[{num,now}]=dis;
            col[now]=0;
            col[num]=1;
        }
        else{
            col[now]=col[num]^1;
            mp[{now,num}]=dis;
            mp[{num,now}]=dis;
            rep(j,0,v[now].size()){
                ll ne=v[now][j];
                if(num==ne)continue;
                if(col[ne]>=0){
                    mp[{now,ne}]=1000000000;
                    mp[{ne,now}]=1000000000;
                }
            }
        }
    }
    rep(i,1,n+1){
        if(col[i]==0)cout<<"B";
        else cout<<"W";
        
    }
    cout<<endl;
    rep(i,0,m){
        cout<<mp[{ff[i],gg[i]}]<<endl;
    }
}


Measured memory_bytes: 41840