#include <bits/stdc++.h>
using namespace std;

vector<int> d, ans, c, par;
vector<pair<int, int>> edge;
vector<vector<int>> g;

void dfs(int x, int last) {
  if (~c[x]) return ;
  int v = edge[par[x]].second;
  if (v == last) {
    c[x] = 0;
    return ;
  }
 
  dfs(v, x);
  ans[par[x] - (par[x]&1)] = d[x];
  c[x] = c[v]^1;
}

int main() {
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  
  g.resize(n);
  d.resize(n);
  c.resize(n, -1);
  par.resize(n, -1);
  ans.resize(2 * m, 1e9);

  for (int i = 0; i < n; ++i) cin >> d[i];
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u; --v;
  
    g[u].push_back(edge.size());
    g[v].push_back(edge.size()+1);
    edge.emplace_back(u, v);
    edge.emplace_back(v, u);
  }

  for (int i = 0; i < n; ++i) {
    int p = -1, val = 1e9 + 5;
    for (auto e : g[i]) {
      int v = edge[e].second;
      if (d[v] < val) {
        val = d[v];
        p = e;
      } else if (d[v] == val) p = min(p, e);
    }
    if (val > d[i]) {
      cout << -1 << '\n';
      return 0;
    }
    par[i] = p;
  }

  for (int i = 0; i < n; ++i) {
    dfs(i, i);
  }
  
  for (int i = 0; i < n; ++i) cout << (c[i] ? 'B' : 'W');
  cout << '\n';
  for (int i = 0; i < m; ++i) cout << ans[2 * i] << '\n';
}
