#include <bits/stdc++.h>
using namespace std;
inline void Input_Output() {}
const double eps = 0.000001;
const long double pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const long long MOD = 1e18 + 9;
const long long INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int n, m, k;
int a[N];
vector<pair<int, int> > g[N];
vector<int> gr[N];
int w[N];
int c[N];
int u[N];
int d[N];
pair<int, int> e[N];
queue<int> q;
int p[N];
int get(int x) {
  if (p[x] != x) p[x] = get(p[x]);
  return p[x];
}
void uni(int a, int b) {
  a = get(a);
  b = get(b);
  p[a] = b;
}
void dfs(int v, int pr = 0) {
  for (auto to : gr[v]) {
    if (c[to] == -1) {
      c[to] = (c[v] ^ 1);
      dfs(to);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  bool equl = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i > 1 && a[i] != a[i - 1]) equl = 0;
    c[i] = -1;
    p[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    int l, r;
    cin >> l >> r;
    if (a[l] == a[r]) {
      w[i] = a[l];
      u[l] = u[r] = 1;
    }
    g[l].push_back({r, i});
    g[r].push_back({l, i});
    e[i] = make_pair(l, r);
  }
  set<pair<int, int> > s;
  for (int i = 1; i <= n; i++) {
    if (u[i]) {
      s.insert(make_pair(a[i], i));
      u[i] = 0;
    }
  }
  while (s.size()) {
    int st = s.begin()->second;
    s.erase(s.begin());
    if (u[st]) {
      continue;
    }
    u[st] = 1;
    q.push(st);
    while (q.size()) {
      int v = q.front();
      q.pop();
      for (auto e : g[v]) {
        int to = e.first;
        int id = e.second;
        if (!w[id]) w[id] = 1e9;
        if (u[to]) {
          continue;
        }
        if (a[to] > a[v]) {
          w[id] = a[to] - a[v];
          u[to] = 1;
          uni(v, to);
          q.push(to);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++)
    if (!u[i]) {
      cout << -1 << '\n';
      exit(0);
    }
  for (int i = 1; i <= m; i++) {
    int l, r;
    tie(l, r) = e[i];
    if (get(l) == get(r)) continue;
    l = get(l);
    r = get(r);
    gr[l].push_back(r);
    gr[r].push_back(l);
  }
  for (int i = 1; i <= n; i++)
    if (p[i] == i) {
      if (c[i] == -1) {
        c[i] = 0;
        dfs(i);
      }
    }
  for (int i = 1; i <= n; i++) {
    int x = get(i);
    if (c[x] == 1)
      cout << 'B';
    else
      cout << 'W';
  }
  cout << '\n';
  for (int i = 1; i <= m; i++) {
    if (!w[i]) {
      w[i] = 1e9;
    }
    cout << w[i] << '\n';
  }
  return 0;
}
