#include <bits/stdc++.h>
using namespace std;
int DFS(int v, int cur, int mi, vector<int>& d, vector<int>& color,
        vector<int>& len, vector<vector<pair<int, int>>>& g) {
  if (color[v] != -1) return 0;
  color[v] = cur;
  int result = 1;
  for (auto p : g[v]) {
    int w = p.first;
    if (d[w] != mi) continue;
    len[p.second] = mi;
    result += DFS(w, cur ^ 1, mi, d, color, len, g);
  }
  return result;
}
bool DFS2(int v, vector<int>& d, vector<int>& color, vector<int>& len,
          vector<vector<pair<int, int>>>& g) {
  if (color[v] == -2) return false;
  if (color[v] != -1) return true;
  color[v] = -2;
  for (auto p : g[v]) {
    int w = p.first;
    if (d[w] < d[v] || d[w] == d[v] && DFS2(w, d, color, len, g)) {
      len[p.second] = d[v];
      color[v] = color[w] ^ 1;
      return true;
    }
  }
  return false;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> d(n), color(n, -1);
  int mi = 1e9;
  for (int i = 0; i < n; ++i) {
    cin >> d[i];
    mi = min(mi, d[i]);
  }
  vector<vector<pair<int, int>>> g(n);
  vector<int> a(m), b(m), len(m);
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
    g[a[i]].push_back(make_pair(b[i], i));
    g[b[i]].push_back(make_pair(a[i], i));
    len[i] = 1e9;
  }
  for (int i = 0; i < n; ++i)
    if (d[i] == mi && color[i] == -1) {
      int comp = DFS(i, 0, mi, d, color, len, g);
      if (comp == 1) {
        cout << -1 << endl;
        return 0;
      }
    }
  vector<pair<int, int>> v;
  for (int i = 0; i < n; ++i) v.push_back(make_pair(d[i], i));
  sort(v.begin(), v.end());
  for (auto q : v) {
    int i = q.second;
    if (!DFS2(i, d, color, len, g)) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    char cur = 'B';
    if (color[i]) cur = 'W';
    cout << cur;
  }
  cout << endl;
  for (int i = 0; i < m; ++i) cout << len[i] << endl;
  return 0;
}
