#include <map>
#include <algorithm>
#include <cassert>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;

void dfs(const vector<vector<int>> &G, const map<pair<int,int>> &E,
         vector<int> &W, const vector<int> &D, string &C, int v){
  for(auto v_ : G[v]){
    if(C[v_] != '?') continue;
    if(D[v_] <= D[v]) continue;
    pair<int,int> e(v,v_);
    if(v > v_) swap(e.first,e.second);
    int idx = E[e];

    W[idx] = D[v_]-D[v];
    C[v_] = C[v];
    dfs(G,E,W,D,C,v_);
  }
}

int main(){
  const int INF = 1e9;
  int N, M;
  cin >> N >> M;
  vector<int> D(N);
  for(int i = 0; i < N; ++i){
    cin >> D[i];
  }
  map<pair<int,int>,int> E;
  vector<vector<int>> G(N);
  for(int i = 0; i < M; ++i){
    int u, v;
    cin >> u >> v;
    --u,--v;
    if(u > v) swap(u,v);
    E[{u,v}] = i;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  int d_min = *min_element(D.begin(), D.end());
  // if(count(D.begin(), D.end(), d_min) < 2){
  //   cout << -1 << endl;
  //   return 0;
  // }
  priority_queue<pair<int,int>> pq;
  for(int i = 0; i < N; ++i){
    pq.emplace(-D[i],i);
  }
  vector<int> W(M,-1);
  string C(N,'?');
  char c = 'W';
  while(!pq.empty()){
    int v = pq.top().second;
    int d = -pq.top().first;
    pq.pop();
    if(C[v] != '?') continue;
    bool f = false;
    for(auto v_ : G[v]){
      if(D[v_] == D[v])
        f = true;
    }
    if(!f){
      cout << -1 << endl;
      return 0;
    }
    D[v] = 0;
    C[v] = c;
    dfs(G,E,W,D,C,v);
    if(c == 'W') c = 'B';
    else c = 'W';
    C[v] = c;
  }
  for(auto &w : W){
    if(w < 0) w = INF;
  }
  cout << C << endl;
  for(auto w : W){
    cout << w << "\n";
  }
}
