#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf(" %d %d", &n, &m);
  int d[n];
  for (int i = 0; i < n; i++) scanf(" %d", &d[i]);
  vector<vector<pair<int, int> > > adj(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf(" %d %d", &u, &v);
    u--;
    v--;
    adj[u].push_back({v, i});
    adj[v].push_back({u, i});
  }
  const int WMAX = 1e9;
  int w[m], col[n];
  fill(col, col + n, -1);
  fill(w, w + m, WMAX);
  vector<int> roots;
  for (int i = 0; i < n; i++) {
    bool eq = false, down = false;
    for (auto e : adj[i]) {
      const int j = e.first;
      if (d[j] < d[i]) {
        down = true;
        break;
      } else if (d[j] == d[i])
        eq = true;
    }
    if (!down) {
      if (!eq) {
        puts("-1");
        return 0;
      } else
        roots.push_back(i);
    }
  }
  function<void(int, int)> process_roots = [&](int i, int c) {
    col[i] = c;
    for (auto e : adj[i]) {
      int j = e.first, k = e.second;
      if (d[j] == d[i] && col[j] == -1) {
        w[k] = d[i];
        process_roots(j, 1 ^ c);
      }
    }
  };
  for (int r : roots)
    if (col[r] == -1) process_roots(r, 0);
  function<void(int, int)> process_tree = [&](int i, int c) {
    for (auto e : adj[i]) {
      const int j = e.first, k = e.second;
      if (d[j] > d[i] && col[j] == -1) {
        w[k] = col[i] == c ? d[j] - d[i] : d[j];
        col[j] = c;
        process_tree(j, c);
      }
    }
  };
  for (int r : roots) process_tree(r, 1 ^ col[r]);
  for (int i = 0; i < n; i++) putchar(col[i] == 0 ? 'B' : 'W');
  putchar('\n');
  for (int i = 0; i < m; i++) printf("%d\n", w[i]);
}
