#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long inf64 = 1e18;
const int N = 2e5 + 5;
int n, m;
int d[N];
pair<int, int> edge[N]{};
vector<int> g[N];
int res[N];
int color[N];
int pos[N];
inline int get_to(int id, int v) {
  return edge[id].first == v ? edge[id].second : edge[id].first;
}
int used[N];
vector<int> tops;
void dfs(int v) {
  used[v] = 1;
  for (int id : g[v]) {
    int to = get_to(id, v);
    if (!used[to]) dfs(to);
  }
  tops.push_back(v);
}
void go(int v) {
  used[v] = 1;
  for (int id : g[v]) {
    int to = get_to(id, v);
    if (!used[to]) {
      res[id] = max(1, d[to] - d[v]);
      go(to);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> d[i];
    color[i] = -1;
  }
  for (int u, v, i = 0; i < m; i++) {
    res[i] = inf;
    cin >> u >> v;
    edge[i] = {u, v};
    if (d[u] <= d[v]) g[u].push_back(i);
    if (d[v] <= d[u]) g[v].push_back(i);
  }
  for (int v = 1; v <= n; v++) {
    if (!used[v]) dfs(v);
  }
  reverse(tops.begin(), tops.end());
  for (int i = 0; i < n; i++) {
    pos[tops[i]] = i;
  }
  for (int v = 1; v <= n; v++) {
    g[v].clear();
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    tie(u, v) = edge[i];
    if (pos[u] < pos[v])
      g[u].push_back(i);
    else
      g[v].push_back(i);
  }
  fill(used + 1, used + n + 1, 0);
  for (int v : tops) {
    if (used[v]) continue;
    int any = 0;
    for (int id : g[v]) {
      int to = get_to(id, v);
      if (d[v] == d[to]) any = 1;
    }
    if (!any) {
      cout << "-1\n";
      return 0;
    }
    color[v] = 0;
    d[v] = 0;
    go(v);
  }
  for (int v = 1; v <= n; v++) {
    if (color[v])
      cout << "B";
    else
      cout << "W";
  }
  cout << "\n";
  for (int i = 0; i < m; i++) {
    cout << res[i] << "\n";
  }
  return 0;
}
