#include <bits/stdc++.h>
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
constexpr int inf = 1000000000;
int N, M;
int D[100000];
int color[100000];
int cost[200000];
bool used[200000];
std::vector<std::pair<int, int>> G[100000];
int main() {
  std::cin >> N >> M;
  for (int i = 0; i < (N); ++i) std::cin >> D[i];
  for (int i = 0; i < (M); ++i) {
    int u, v;
    std::cin >> u >> v;
    --u;
    --v;
    G[u].emplace_back(v, i);
    G[v].emplace_back(u, i);
  }
  for (int i = 0; i < (N); ++i) {
    bool flag = false;
    for (auto p : G[i]) {
      if (D[p.first] <= D[i]) {
        flag = true;
        break;
      }
    }
    if (!flag) {
      puts("-1");
      return 0;
    }
  }
  std::vector<int> order(N);
  std::iota(order.begin(), order.end(), 0);
  std::sort(order.begin(), order.end(), [](int x, int y) {
    if (D[x] == D[y]) return x < y;
    return D[x] < D[y];
  });
  std::set<std::pair<int, int>> remain;
  for (int i = 0; i < (N); ++i) {
    remain.insert(std::make_pair(D[i], i));
  }
  while (!remain.empty()) {
    int v = remain.begin()->second;
    int u = -1;
    for (auto p : G[v]) {
      if (D[p.first] == D[v]) {
        u = p.first;
        color[v] = 0;
        color[u] = 1;
        cost[p.second] = D[v];
        break;
      }
    }
    used[v] = used[u] = true;
    using tap = std::tuple<int, int, int, bool>;
    std::priority_queue<tap, std::vector<tap>, std::greater<tap>> que;
    for (auto p : G[v]) {
      if (p.first == u) continue;
      if (used[p.first]) {
        cost[p.second] = inf;
        continue;
      }
      used[p.first] = true;
      que.push(std::make_tuple(D[p.first], p.first, v, D[p.first] == D[v]));
    }
    for (auto p : G[u]) {
      if (p.first == v) continue;
      if (used[p.first]) {
        cost[p.second] = inf;
        continue;
      }
      used[p.first] = true;
      que.push(std::make_tuple(D[p.first], p.first, u, D[p.first] == D[u]));
    }
    remain.erase(std::make_pair(D[v], v));
    remain.erase(std::make_pair(D[u], u));
    while (!que.empty()) {
      int now, prev;
      bool flip;
      std::tie(std::ignore, now, prev, flip) = que.top();
      que.pop();
      for (auto p : G[now]) {
        if (p.first == prev) {
          cost[p.second] = flip ? D[now] : D[now] - D[prev];
          color[now] = flip ? 1 - color[prev] : color[prev];
          continue;
        }
        if (used[p.first]) {
          cost[p.second] = inf;
          continue;
        }
        used[p.first] = true;
        que.push(
            std::make_tuple(D[p.first], p.first, now, D[p.first] == D[now]));
      }
      remain.erase(std::make_pair(D[now], now));
    }
  }
  for (int i = 0; i < (N); ++i) {
    std::cout << (color[i] == 1 ? 'B' : 'W');
  }
  std::cout << '\n';
  for (int i = 0; i < (M); ++i) {
    std::cout << cost[i] << '\n';
  }
}
