#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T x) {
  cout << x << endl;
  exit(0);
}
using ll = long long;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;
const int inf = 1e9;
int a[maxn];
int n, m;
vector<pair<int, int>> g[maxn];
bool act[maxn];
int color[maxn];
int edges[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(color, -1, sizeof(color));
  cin >> n >> m;
  vector<pair<int, int>> nodes;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    nodes.push_back({a[i], i});
  }
  sort(nodes.begin(), nodes.end());
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back({i, v});
    g[v].push_back({i, u});
  }
  for (int i = 0; i < n; i++) {
    bool ok = false;
    for (auto ed : g[i]) {
      int to = ed.second;
      if (a[to] <= a[i]) {
        ok = true;
        break;
      }
    }
    if (!ok) out(-1);
  }
  for (auto node : nodes) {
    int at = node.second;
    bool done = false;
    for (auto ed : g[at]) {
      int eid = ed.first;
      int to = ed.second;
      if (a[at] == a[to]) {
        if (act[to]) {
          if (color[at] == -1 || color[at] == 1 - color[to]) {
            color[at] = 1 - color[to];
            edges[eid] = a[at];
            done = true;
          } else {
            assert(color[at] == color[to]);
            edges[eid] = inf;
          }
        } else {
          act[to] = true;
          if (color[at] == -1) {
            color[at] = 0;
            color[to] = 1;
          } else {
            color[to] = 1 - color[at];
          }
          edges[eid] = a[at];
          done = true;
        }
      }
    }
    if (done) {
      for (auto ed : g[at]) {
        int eid = ed.first;
        int to = ed.second;
        if (act[to]) {
          if (a[to] < a[at]) {
            edges[eid] = inf;
          }
        }
      }
    } else {
      for (auto ed : g[at]) {
        int eid = ed.first;
        int to = ed.second;
        if (act[to]) {
          if (a[to] < a[at]) {
            color[at] = color[to];
            edges[eid] = a[at] - a[to];
          } else {
            edges[eid] = inf;
          }
        }
      }
    }
    act[at] = true;
  }
  for (int i = 0; i < n; i++) {
    cout << (color[i] ? 'B' : 'W');
  }
  cout << endl;
  for (int i = 0; i < m; i++) {
    cout << edges[i] << "\n";
  }
  return 0;
}
