#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T x) {
  cout << x << endl;
  exit(0);
}
using ll = long long;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;
struct dsu0 {
  vector<int> par, siz;
  int n;
  int cc;
  int largest;
  void init(int n) {
    assert(n > 0);
    this->n = n;
    cc = n;
    par.resize(n + 10);
    siz.resize(n + 10);
    for (int i = 0; i < n; i++) par[i] = i, siz[i] = 1;
    largest = 1;
  }
  int parent(int x) {
    assert(x >= 0 && x < n);
    return par[x] == x ? x : par[x] = parent(par[x]);
  }
  void join(int x, int y) {
    x = parent(x);
    y = parent(y);
    if (x == y) return;
    cc--;
    if (siz[x] < siz[y]) swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    largest = max(largest, siz[x]);
  }
};
int n, m;
int a[maxn];
vector<pair<int, int>> g[maxn];
map<int, vector<int>> nodes;
int edges[maxn];
int color[maxn];
dsu0 dsu;
bool viz[maxn];
void dfs(int at, int col, int val) {
  viz[at] = true;
  color[at] = col;
  for (auto ed : g[at]) {
    int id = ed.first;
    int to = ed.second;
    if (!viz[to] && a[to] == val) {
      edges[id] = val;
      dfs(to, 1 - col, val);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  dsu.init(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    nodes[a[i]].push_back(i);
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back({i, v});
    g[v].push_back({i, u});
    edges[i] = 1e9;
  }
  if ((int)nodes.begin()->second.size() <= 1) out(-1);
  bool first = true;
  for (auto p : nodes) {
    if (first) {
      for (int i : p.second) dsu.join(i, p.second[0]);
      int par = dsu.parent(p.second[0]);
      for (int i : p.second) {
        if (dsu.parent(i) != par) out(-1);
      }
      dfs(p.second[0], 0, p.first);
      first = false;
      continue;
    }
    for (int at : p.second) {
      bool ok = false;
      for (auto ed : g[at]) {
        int id = ed.first;
        int to = ed.second;
        if (viz[to] && a[to] < a[at]) {
          ok = true;
          edges[id] = a[at] - a[to];
          color[at] = color[to];
          break;
        }
      }
      if (!ok) out(-1);
      viz[at] = true;
    }
  }
  for (int i = 0; i < n; i++) {
    if (color[i] == 0)
      cout << 'B';
    else
      cout << 'W';
  }
  cout << endl;
  for (int i = 0; i < m; i++) {
    cout << edges[i] << "\n";
  }
  return 0;
}
