
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>
#include <map>

template <class T>
struct fix_point: private T {
  explicit constexpr fix_point(T &&func): T(std::forward<T>(func)) { }
  template <class... Args>
  constexpr decltype(auto) operator () (Args &&... args) const {
    return T::operator()(*this, std::forward<Args>(args)...);
  }
};

template <class T>
constexpr decltype(auto) make_fix_point(T &&func) {
  return fix_point<T>(std::forward<T>(func));
}

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<std::vector<int>> graph(N);
  std::map<std::pair<int, int>, int> edge;
  for (int i = 0; i < M; ++i) {
    int u, v;
    std::cin >> u >> v;
    --u; --v;
    graph[u].push_back(v);
    graph[v].push_back(u);
    edge[std::minmax(u, v)] = i;
  }
  std::vector<std::vector<int>> children(N);
  for (int i = 0; i < N; ++i) {
    int to = -1;
    for (auto j: graph[i]) {
      if (to == -1 || (D[to] > D[j])) {
        to = j;
      }
    }
    if (to == -1 || D[to] > D[i]) {
      std::cout << "-1\n";
      return 0;
    }
    children[to].push_back(i);
  }
  std::vector<bool> done(N), color(N);
  std::vector<int> cost(M, inf);
  auto dfs = make_fix_point([&](auto dfs, int u, int p) -> void {
    done[u] = true;
    if (p != -1) {
      if (D[u] == D[p]) {
        color[u] = !color[p];
        cost[edge[std::minmax(u, p)]] = D[u];
      }
      else {
        color[u] = color[p];
        cost[edge[std::minmax(u, p)]] = D[u] - D[p];
      } 
    }
    for (auto v: children[u]) {
      if (!done[v]) {
        dfs(v, u);
      }
    }
  });
  std::vector<int> order(N);
  std::iota(order.begin(), order.end(), 0);
  std::sort(order.begin(), order.end(), [&](int i, int j) {
    return D[i] < D[j];
  });
  for (auto u: order) {
    if (!done[u]) {
      dfs(u, -1);
      continue;
    }
  }
  for (auto x: color) {
    std::cout << "BW"[x];
  }
  std::cout << '\n';
  for (auto x: cost) {
    std::cout << x << '\n';
  }
  return 0;
}
