#include <bits/stdc++.h>
using namespace std;
vector<int> G[200010];
map<pair<int, int>, int> edge;
int d[200010];
int ans[200020] = {};
bool col[200010] = {};
bool vis[200010] = {};
bool mns[200020] = {};
int par[200010];
int rnk[200010];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rnk[i] = 0;
  }
}
int find(int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  if (rnk[x] < rnk[y])
    par[x] = y;
  else {
    par[y] = x;
    if (rnk[x] == rnk[y]) rnk[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
void dfs(int k) {
  vis[k] = 1;
  for (auto& i : G[k]) {
    if (!vis[i]) {
      if (!mns[i]) ans[edge[pair<int, int>(min(i, k), max(i, k))]] = d[i];
      col[i] = (1 ^ col[k]);
      dfs(i);
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > vet;
  for (int i = 0; i < n; i++) {
    cin >> d[i];
    vet.emplace_back(d[i], i);
  }
  sort((vet).begin(), (vet).end());
  int u[m + 5], v[m + 5];
  for (int i = 0; i < m; i++) {
    cin >> u[i] >> v[i];
    u[i]--, v[i]--;
    if (u[i] > v[i]) swap(u[i], v[i]);
    edge[pair<int, int>(u[i], v[i])] = i;
  }
  int mn_idx = 0;
  while (vet[0].first == vet[mn_idx].first) {
    mns[vet[mn_idx].second] = 1;
    mn_idx++;
  }
  cerr << mn_idx << endl;
  init(n);
  int ok = -1;
  set<int> mn;
  vector<int> tree;
  for (int i = 0; i < m; i++) {
    if (mns[u[i]] && mns[v[i]] && !same(u[i], v[i])) {
      if ((tree).size() == 0) ok = i;
      ans[i] = vet[0].first;
      unite(u[i], v[i]);
      tree.push_back(i);
      G[u[i]].push_back(v[i]);
      G[v[i]].push_back(u[i]);
      mn.insert(u[i]);
      mn.insert(v[i]);
    }
  }
  cerr << mn_idx << " " << (mn).size() << endl;
  if (mn_idx != (mn).size()) {
    cout << -1 << endl;
    return 0;
  }
  col[u[ok]] = 0;
  col[v[ok]] = 1;
  for (int i = 0; i < m; i++) {
    if (!same(u[i], v[i])) {
      tree.push_back(i);
      unite(u[i], v[i]);
      G[u[i]].push_back(v[i]);
      G[v[i]].push_back(u[i]);
    }
  }
  sort((tree).begin(), (tree).end());
  cerr << "tree: ";
  for (auto& i : tree) cerr << i << " ";
  cerr << endl;
  vis[v[ok]] = 1;
  dfs(u[ok]);
  vis[v[ok]] = 0;
  dfs(v[ok]);
  for (int i = 0; i < m; i++) {
    if (ans[i] == 0) ans[i] = 1e9;
  }
  for (int i = 0; i < n; i++) {
    cout << (col[i] ? 'B' : 'W');
  }
  cout << endl;
  for (int i = 0; i < m; i++) cout << ans[i] << endl;
}
