#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 7;
const long long INF = 1000 * 1000 * 1000;
pair<long long, long long> ed[N];
long long n, m;
long long d[N];
vector<long long> g[N];
long long ans[N];
bool color[N];
bool fix[N];
long long dist[N];
bool used[N];
void check(long long c) {
  priority_queue<pair<long long, long long> > q;
  for (long long i = 1; i <= n; ++i) {
    dist[i] = INF * INF;
    if (color[i] == c) {
      dist[i] = 0;
      q.push(make_pair(0ll, i));
    }
  }
  memset(used, 0, sizeof used);
  while (q.size()) {
    long long u = q.top().second;
    q.pop();
    if (used[u]) continue;
    used[u] = 1;
    for (long long i : g[u]) {
      long long v = ed[i].first ^ ed[i].second ^ u;
      long long w = ans[i];
      if (dist[u] + w < dist[v]) {
        dist[v] = dist[u] + w;
        q.push(make_pair(-dist[v], v));
      }
    }
  }
  for (long long i = 1; i <= n; ++i) {
    if (color[i] != c) {
      if (d[i] != dist[i]) {
        cout << i << ' ' << d[i] << ' ' << dist[i] << endl;
        cout << -1 << endl;
        exit(0);
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (long long i = 1; i <= n; ++i) {
    cin >> d[i];
  }
  for (long long i = 0; i < m; ++i) {
    long long u, v;
    cin >> u >> v;
    ed[i] = make_pair(u, v);
    g[u].push_back(i);
    g[v].push_back(i);
  }
  for (long long i = 0; i < m; ++i) ans[i] = INF;
  vector<long long> ord;
  for (long long i = 1; i <= n; ++i) ord.push_back(i);
  auto comp = [](long long u, long long v) { return d[u] < d[v]; };
  sort(ord.begin(), ord.end(), comp);
  for (long long u : ord) {
    long long have = INF * INF;
    for (long long i : g[u]) {
      long long v = ed[i].first ^ ed[i].second ^ u;
      if (color[u] == color[v])
        have = min(have, d[v] + ans[i]);
      else
        have = min(have, ans[i]);
    }
    if (have > d[u] && !fix[u]) {
      for (long long i : g[u]) {
        long long v = ed[i].first ^ ed[i].second ^ u;
        if (d[u] == d[v]) {
          ans[i] = d[u];
          if (color[u] == color[v]) color[u] ^= 1;
        }
      }
    }
    for (long long i : g[u]) {
      long long v = ed[i].first ^ ed[i].second ^ u;
      if (fix[v]) {
        ans[i] = INF;
        continue;
      }
      if (d[u] < d[v]) {
        ans[i] = d[v] - d[u];
        if (color[u] ^ color[v]) color[v] ^= 1;
        fix[v] = 1;
      }
    }
  }
  vector<bool> have(2);
  for (long long i = 1; i <= n; ++i) have[color[i]] = 1;
  if (!have[0] || !have[1]) {
    cout << -1 << '\n';
    exit(0);
  }
  for (long long t = 0; t < 2; ++t) check(t);
  for (long long i = 1; i <= n; ++i) {
    if (color[i]) {
      cout << 'B';
    } else {
      cout << 'W';
    }
  }
  cout << '\n';
  for (long long i = 0; i < m; ++i) {
    cout << ans[i] << '\n';
  }
}
