#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = 1e18;
constexpr long long MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, d[112345], u[212345], v[212345];
  vector<pair<int, int> > g[112345];
  queue<int> qq;
  int c[112345], res[212345];
  bool used[212345];
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> d[i + 1], c[i + 1] = 2;
  for (int i = 0; i < m; i++) {
    cin >> u[i] >> v[i];
    g[u[i]].push_back({v[i], i});
    g[v[i]].push_back({u[i], i});
    if (d[u[i]] == d[v[i]]) {
      qq.push(i);
    }
    res[i] = 1e9 + 10;
    used[i] = false;
  }
  if (qq.size() == 0) {
    cout << -1 << endl;
    return 0;
  }
  queue<int> q;
  while (qq.size()) {
    int pp = qq.front();
    qq.pop();
    if (c[u[pp]] != 2 || c[v[pp]] != 2) continue;
    c[u[pp]] = 0;
    c[v[pp]] = 1;
    res[pp] = d[u[pp]];
    q.push(pp);
    while (q.size()) {
      int p = q.front();
      q.pop();
      if (used[p]) continue;
      used[p] = true;
      int arr[2] = {u[p], v[p]};
      for (int j = 0; j < 2; j++) {
        int cur = arr[j];
        for (pair<int, int> i : g[cur]) {
          int e = i.second;
          int k = i.first;
          if (res[e] != 1e9 + 10) continue;
          if (c[k] != 2) {
            res[e] = max(d[cur], d[k]);
            continue;
          }
          if (d[cur] == d[k]) {
            c[k] = 1 - c[cur];
            if (used[e]) continue;
            res[e] = d[cur];
            q.push(e);
            continue;
          }
          if (d[cur] > d[k]) {
            res[e] = d[cur];
            continue;
          }
          if (d[cur] < d[k]) {
            c[k] = c[cur];
            if (used[e]) continue;
            res[e] = d[k] - d[cur];
            q.push(e);
            continue;
          }
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (c[i] == 2) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 1; i <= n; i++) cout << (c[i] == 0 ? "B" : "W");
  cout << endl;
  for (int i = 0; i < m; i++) cout << res[i] << endl;
  return 0;
}
