#include <bits/stdc++.h>
constexpr int inf = 1000000000;
int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> D(N);
  for (auto &x : D) {
    std::cin >> x;
  }
  std::vector<int> U(M), V(M);
  std::vector<std::vector<int>> graph(N);
  for (int i = 0; i < M; ++i) {
    std::cin >> U[i] >> V[i];
    --U[i];
    --V[i];
    graph[U[i]].push_back(V[i]);
    graph[V[i]].push_back(U[i]);
  }
  std::vector<int> order(N);
  std::iota(order.begin(), order.end(), 0);
  std::stable_sort(order.begin(), order.end(),
                   [&](int i, int j) { return D[i] < D[j]; });
  std::vector<int> color(N);
  std::map<std::pair<int, int>, int> map;
  for (int i = 0; i < M; ++i) {
    map[std::minmax(U[i], V[i])] = inf;
  }
  for (auto u : order) {
    int v = -1;
    for (auto w : graph[u]) {
      if (v == -1 || D[v] > D[w] || (D[v] == D[w] && v > w)) {
        v = w;
      }
    }
    if (v == -1 || D[u] < D[v]) {
      std::cout << "-1\n";
      return 0;
    }
    if (D[u] > D[v]) {
      color[u] = color[v];
      map[std::minmax(u, v)] = D[u] - D[v];
    } else {
      if (u < v) {
        color[u] = 1;
        map[{u, v}] = D[u];
      } else {
        color[u] = -color[v];
        map[{v, u}] = D[u];
      }
    }
  }
  for (auto x : color) {
    std::cout << "B W"[x + 1];
  }
  std::cout << '\n';
  for (auto x : map) {
    std::cout << x.second << '\n';
  }
  return 0;
}
