#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define rev(i,s,e) for(i64 (i) = (s);(i) --> (e);)
#define all(x) x.begin(),x.end()

template<class T>
static inline std::vector<T> ndvec(size_t&& n, T val) noexcept {
  return std::vector<T>(n, std::forward<T>(val));
}

template<class... Tail>
static inline auto ndvec(size_t&& n, Tail&&... tail) noexcept {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...));
}

i64 N, M;
vector<vector<pair<i64, i64>>> G;
vector<i64> D;
vector<i64> cost;
vector<i64> C;
vector<i64> R;

int main() {
  cin >> N >> M;
  G.resize(N);
  D.resize(N);
  C.resize(N, -1);
  R.resize(N);
  cost.resize(M, 1e9);
  rep(i,0,N) {
    cin >> D[i];
  }
  rep(i,0,M) {
    i64 a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back({b, i});
    G[b].push_back({a, i});
  }
  rep(i,0,N) {
    if(C[i] == -1) {
      queue<i64> que;
      que.push(i);
      while(!que.empty()) {
        i64 v = que.front();
        que.pop();
        for(auto e: G[v]) {
          i64 to = e.first;
          i64 ei = e.second;
          if(C[to] == -1 && D[v] == D[to]) {
            C[i] = 0;
            C[to] = C[v] ^ 1;
            cost[ei] = D[v];
            que.push(to);
          }
        }
      }
    }
  }
  rep(i,0,N) {
    if(C[i] != -1) {
      queue<i64> que;
      i64 color = C[i] ^ 1;
      que.push(i);
      while(!que.empty()) {
        i64 v = que.front();
        que.pop();
        for(auto e: G[v]) {
          i64 to = e.first;
          i64 ei = e.second;
          if(C[to] == -1 && D[v] < D[to]) {
            C[to] = color;
            cost[ei] = D[to] - D[v];
            que.push(to);
          }
        }
      }
    }
  }

  i64 OK = true;
  rep(i,0,N) {
    if(C[i] == -1) OK = false;
  }
  if(!OK) {
    cout << -1 << endl;
    return 0;
  }
  rep(i,0,N) {
    if(C[i] == 0) cout << "B";
    else cout << "W";
  }
  cout << endl;
  rep(i,0,M) {
    cout << cost[i] << endl;
  }
}

