#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
const int maxn = 2e5 + 33, mod = 1e9 + 7;
int n, m, d[maxn], col[maxn];
vector<vi> g;
vector<pi> e;
queue<int> q;
map<pi, int> cost;
pi minmax(pi a) {
  if (a.second < a.first) swap(a.second, a.first);
  return a;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> d[i];
  }
  g.resize(n + 1);
  for (int f, t, i = 0; i < m; i++) {
    cin >> f >> t;
    g[f].push_back(t);
    g[t].push_back(f);
    e.push_back({f, t});
    if (d[f] == d[t] && (!col[f] || !col[t])) {
      if (col[t])
        col[f] = col[t] ^ 3;
      else if (col[f])
        col[t] = col[f] ^ 3;
      else
        col[t] = 1, col[f] = 2;
      if (!col[f]) q.push(f);
      if (!col[t]) q.push(t);
      cost[minmax({f, t})] = d[f];
    }
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto i : g[u]) {
      if (col[i] || d[i] < d[u]) continue;
      col[i] = col[u];
      cost[minmax({u, i})] = d[i] - d[u];
      q.push(i);
    }
  }
  for (int i = 1; i <= n; i++)
    if (!col[i]) return cout << -1, 0;
  for (int i = 1; i <= n; i++) cout << ("BW"[col[i] - 1]);
  cout << '\n';
  for (auto i : e) {
    if (!cost.count(minmax(i)))
      cout << "1000000000\n";
    else
      cout << cost[minmax(i)] << '\n';
  }
}
