#include <bits/stdc++.h>
using namespace std;
const long long inf = 1001001001001001;
const int mod = 998244353;
template <class T>
bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
void out(T a) {
  cout << a << endl;
}
template <class T>
void outv(T v) {
  for (long long i = 0; i < (long long)(v.size()); i++) {
    if (i) cout << ' ';
    cout << v[i];
  }
  cout << endl;
}
template <class T>
void outvv(T v) {
  for (long long i = 0; i < (long long)(v.size()); i++) outv(v[i]);
}
int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> v(n);
  for (long long i = 0; i < (long long)(n); i++) cin >> v[i];
  vector<vector<pair<long long, long long>>> g(n);
  vector<pair<long long, long long>> hen(m);
  vector<long long> ans(m, 1000000000);
  vector<long long> c(n);
  for (long long i = 0; i < (long long)(n); i++) {
    long long a, b;
    cin >> a >> b;
    a--;
    b--;
    hen[i].first = a;
    hen[i].second = b;
    g[a].emplace_back(b, i);
    g[b].emplace_back(a, i);
    if (v[a] != v[b] || (c[a] > 0 && c[b] > 0)) continue;
    ans[i] = v[a];
    if (c[a] != 0)
      c[b] = 3 - c[a];
    else if (c[b] != 0)
      c[a] = 3 - c[b];
    else {
      c[a] = 1;
      c[b] = 2;
    }
  }
  vector<pair<long long, long long>> s(n);
  for (long long i = 0; i < (long long)(n); i++) s[i].first = v[i];
  for (long long i = 0; i < (long long)(n); i++) s[i].second = i;
  sort(s.begin(), s.end());
  for (long long i = 0; i < (long long)(n); i++) {
    if (c[s[i].second] != 0) continue;
    for (auto ne : g[s[i].second]) {
      if (v[ne.first] > s[i].first) continue;
      c[s[i].second] = 3 - c[ne.first];
      ans[ne.second] = s[i].first;
      break;
    }
    if (c[s[i].second] == 0) {
      out(-1);
      return 0;
    }
  }
  for (long long i = 0; i < (long long)(n); i++) {
    if (c[i] == 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (long long i = 0; i < (long long)(n); i++) {
    if (c[i] == 1)
      cout << 'B';
    else
      cout << 'W';
  }
  cout << endl;
  for (long long i = 0; i < (long long)(m); i++) cout << ans[i] << endl;
}
