#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> D(N);
  for (int i = 0; i < (N); i++) cin >> D[i];
  vector<int> U(M), V(M);
  vector<vector<int>> G(N);
  for (int i = 0; i < (M); i++) {
    cin >> U[i] >> V[i];
    U[i]--;
    V[i]--;
    G[U[i]].push_back(i);
    G[V[i]].push_back(i);
  }
  vector<set<int>> T(N);
  vector<bool> vis(N);
  vector<int> color(N, -1);
  constexpr int INF = 1e9;
  vector<int> ans(N, INF + 1);
  for (int i = 0; i < (N); i++) {
    pair<int, int> mn(INF, INF);
    for (int k : G[i]) {
      int j = U[k] ^ V[k] ^ i;
      mn = min(mn, make_pair(D[j], k));
    }
    ans[mn.second] = D[i];
  }
  for (int i = 0; i < (N); i++)
    if (color[i] == -1) {
      color[i] = 0;
      queue<int> q;
      q.push(i);
      while (not q.empty()) {
        int v = q.front();
        q.pop();
        for (int j : G[v])
          if (ans[j] <= INF) {
            int u = U[j] ^ V[j] ^ v;
            if (color[u] == -1) {
              color[u] = color[v] ^ 1;
              q.push(u);
            }
          }
      }
    }
  for (int i = 0; i < (M); i++) ans[i] = min(ans[i], INF);
  for (int ii = 0; ii < 2; ii++) {
    constexpr ll INFL = 1e18;
    vector<ll> dist(N, INFL);
    minheap<pair<ll, int>> heap;
    for (int i = 0; i < (N); i++)
      if (color[i] == ii) {
        dist[i] = 0;
        heap.emplace(0, i);
      }
    while (not heap.empty()) {
      ll d;
      int u;
      tie(d, u) = heap.top();
      heap.pop();
      if (d > dist[u]) continue;
      for (int i : G[u]) {
        int v = U[i] ^ V[i] ^ u;
        if (dist[v] > dist[u] + ans[i]) {
          dist[v] = dist[u] + ans[i];
          heap.emplace(dist[v], v);
        }
      }
    }
    for (int i = 0; i < (N); i++)
      if (color[i] == 1 - ii and dist[i] != D[i]) {
        cout << -1 << endl;
        return 0;
      }
  }
  for (int i = 0; i < (N); i++) cout << (color[i] ? 'B' : 'W');
  cout << endl;
  for (int i = 0; i < (M); i++) {
    cout << ans[i] << '\n';
  }
}
