#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define int ll
ll v_color[200005]={0};
vector<P> knode[200005],node[200005];
const ll inf=1e9;
ll ans[200005]={0};
ll dn[200005]={0};
void dfs(ll pos,ll val){
  v_color[pos]=val;
  for(int i=0;i<node[pos].size();i++){
    ll to=node[pos][i].first;
    if(v_color[to]==0){
      ans[node[pos][i].second]=dn[to];
      dfs(to,val%2+1);
    }
  }
}
signed main(){
  ll n,m;
  cin>>n>>m;
  vector<P> vec;
  for(int i=1;i<=n;i++){
    ll val;
    cin>>val;
    dn[i]=val;
    vec.push_back(P(val,i));
  }
  sort(vec.begin(),vec.end());
  for(int i=1;i<=m;i++){
    ll u,v;
    cin>>u>>v;
    knode[u].push_back(P(v,i));
    knode[v].push_back(P(u,i));
  }
  for(int i=1;i<=n;i++){
    P min_v=P(inf,inf);
    for(int j=0;j<knode[i].size();j++){
      ll to=knode[i][j].first;
      min_v=min(min_v,P(dn[to],j));
    }
    node[i].push_back(knode[i][min_v.second]);
    node[knode[i][min_v.second].first].push_back(P(i,knode[i][min_v.second].second));
  }
  for(int i=1;i<=n;i++){
    bool f=false;
    for(int j=0;j<knode[i].size();j++){
      ll to=knode[i][j].first;
      if(dn[to]<=dn[i]) f=true;
    }
    if(!f){
      cout<<-1<<endl;
      return 0;
    }
  }
  for(int i=0;i<n;i++){
    ll pos=vec[i].second;
    if(v_color[pos]==0){
      dfs(pos,1);
    }
  }
  for(int i=1;i<=n;i++) {
    if(v_color[i]==0) cout<<"0";
    else if(v_color[i]==1) cout<<"B";
    else cout<<"W";
  }
  cout<<endl;
  for(int i=1;i<=m;i++){
    cout<<(ans[i]==0?inf:ans[i])<<endl;
  }
}
