#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

int N,M;
vector<ll> D;
vector<pair<int,int>> edges;
vector<int> g[100010];

#define BL 1
#define WH -1

int main(){
  cin>>N>>M;
  D.resize(N);
  rep(i,N) cin>>D[i];
  map<pair<int,int>,int> mp;
  for(int i=0;i<M;i++){
    int a,b;
    cin>>a>>b;
    a--;b--;
    if(a>b) swap(a,b);
    mp[mkp(a,b)]=i;
    edges.push_back(mkp(a,b));
    g[a].push_back(b);
    g[b].push_back(a);
  }

  vector<int> ans(M,0);

  vector<pair<ll,int>> v;
  rep(i,N) v.push_back(mkp(D[i],i));
  sort(v.begin(),v.end());

  vector<int> col(N,0);
  vector<int> check(N,0);
  for(int i=0;i<N;i++){
    int now=v[i].second;
    bool ok=false;
    bool exi=false;
    for(auto nex:g[now]){
      if(col[nex]!=0){

        int tar=D[nex];
        if(col[nex]==BL){
          tar=0;
          if(check[nex]==0&&D[now]>D[nex]){
            cout<<-1<<endl;
            return 0;
          }
          check[nex]=1;
        }
        if(D[now]==D[nex]&&col[nex]==WH) continue;
        int a=now,b=nex;
        if(a>b) swap(a,b);
        ans[mp[mkp(a,b)]]=D[now]-tar;
        col[now]=WH;
      }
    }

    if(1){
      if(col[now]==0) col[now]=BL;
      for(auto nex:g[now]){
        if(col[nex]==WH&&D[nex]==D[now]){
          int a=now,b=nex;
          if(a>b) swap(a,b);
          ans[mp[mkp(a,b)]]=D[now];
          check[now]=1;
        }
      }
    }
  }

  for(int i=0;i<N;i++){
    if(col[i]==BL) cout<<'B';
    else cout<<'W';
  }
  cout<<endl;
  for(int i=0;i<M;i++) cout<<ans[i]<<endl;

  return 0;
}
