#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll inf = 1e15;
template <typename T>
void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}
struct Edge {
  int from, to, index;
  ll cost;
};
using Graph = vector<vector<Edge>>;
std::vector<ll> dijkstra(const Graph &g, vector<int> s) {
  std::vector<ll> d((int)g.size(), inf);
  for (auto cur : s) d[cur] = 0;
  std::priority_queue<P, std::vector<P>, std::greater<P>> que;
  for (auto cur : s) que.push(P(0, cur));
  while (!que.empty()) {
    ll dist = que.top().first;
    int v = que.top().second;
    que.pop();
    if (d[v] < dist) continue;
    for (const auto &e : g[v]) {
      if (d[e.to] <= d[v] + e.cost) continue;
      d[e.to] = d[v] + e.cost;
      que.push(P(d[e.to], e.to));
    }
  }
  return d;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<vector<Edge>> g(n);
  vector<Edge> es(m);
  vector<ll> d(n);
  for (int i = 0; i < n; i++) {
    cin >> d[i];
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    es[i] = Edge{u, v, i, (ll)1e9};
    g[u].push_back(Edge{u, v, i, -1});
    g[v].push_back(Edge{v, u, i, -1});
  }
  vector<int> col(n, -1);
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < n; i++) {
    que.push(P(d[i], i));
  }
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    int cur = p.second;
    ll mi = inf;
    for (const auto &e : g[cur]) {
      int nxt = e.to;
      if (d[nxt] < mi) {
        mi = d[nxt];
      }
    }
    if (mi < d[cur]) {
      for (const auto &e : g[cur]) {
        int nxt = e.to, index = e.index;
        if (d[nxt] == mi) {
          assert(col[nxt] != -1);
          col[cur] = col[nxt];
          es[index].cost = d[cur] - d[nxt];
          break;
        }
      }
    } else if (mi == d[cur]) {
      bool f = false;
      for (const auto &e : g[cur]) {
        int nxt = e.to, index = e.index;
        if (d[nxt] == mi && col[nxt] != -1) {
          col[cur] = 1 - col[nxt];
          es[index].cost = d[cur];
          f = true;
          break;
        }
      }
      if (f) continue;
      col[cur] = 0;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }
  Graph ng(n);
  for (int i = 0; i < m; i++) {
    int u = es[i].from, v = es[i].to;
    ll cost = es[i].cost;
    ng[u].push_back(Edge{u, v, i, cost});
    ng[v].push_back(Edge{v, u, i, cost});
  }
  vector<int> ws, bs;
  for (int i = 0; i < n; i++) {
    if (col[i]) {
      bs.push_back(i);
    } else {
      ws.push_back(i);
    }
  }
  auto wdist = dijkstra(ng, ws);
  auto bdist = dijkstra(ng, bs);
  for (int i = 0; i < n; i++) {
    if (col[i]) {
      if (d[i] != wdist[i]) {
        cout << -1 << endl;
        return 0;
      }
    } else {
      if (d[i] != bdist[i]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << (col[i] ? "B" : "W");
  }
  cout << endl;
  for (int i = 0; i < m; i++) {
    cout << es[i].cost << endl;
  }
}
