#include <bits/stdc++.h>
using namespace std;
void dfs(const vector<vector<int>> &G, const vector<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 = lower_bound(E.begin(), E.end(), e) - E.begin();
    assert(idx < E.size());
    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];
  }
  vector<pair<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.emplace_back(u, v);
    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<bool> visited(N);
  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;
    if (c == 'W')
      c = 'B';
    else
      c = 'W';
    dfs(G, E, W, D, C, v);
    C[v] = c;
  }
  for (auto &w : W) {
    if (w < 0) w = INF;
  }
  cout << C << endl;
  for (auto w : W) {
    cout << w << "\n";
  }
}
