#include <bits/stdc++.h>
using namespace std;
void fail() {
  cout << -1 << endl;
  exit(0);
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> D(N);
  map<int, vector<int>> d2p;
  for (int i = 0; i < N; i++) {
    cin >> D[i];
    d2p[D[i]].push_back(i);
  }
  vector<pair<int, int>> edges[100000];
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edges[a].emplace_back(b, i);
    edges[b].emplace_back(a, i);
  }
  vector<int> ans(M), col(N);
  bitset<100000> olds, news, ng;
  for (auto& ppp : d2p) {
    int d = ppp.first;
    auto& v = ppp.second;
    for (int i : v) {
      news[i] = ng[i] = 1;
      for (auto& p : edges[i]) {
        int j = p.first;
        if (news[j]) {
          if (ng[i]) col[i] = 1 - col[j];
          ans[p.second] = d;
          ng[i] = ng[j] = 0;
        }
      }
      for (auto& p : edges[i]) {
        int j = p.first;
        if (olds[j]) {
          ans[p.second] = (col[i] == col[j] ? d - D[j] : d);
          ng[i] = 0;
        }
      }
    }
    for (int i : v) {
      if (ng[i]) fail();
      news[i] = 0;
      olds[i] = 1;
    }
  }
  for (int i = 0; i < N; i++) cout << (col[i] ? "W" : "B");
  cout << endl;
  for (int a : ans) cout << a << endl;
  return 0;
}
