#include <bits/stdc++.h>
#pragma warning(disable : 4996)
const long long MOD = 1000000007;
using namespace std;
class UF {
 private:
  int num;
  vector<int> par;
  vector<int> siz;

 public:
 public:
  UF(int n) : num(n) {
    par.resize(n);
    siz.resize(n);
    int i;
    for (i = 0; i < n; i++) {
      par[i] = i;
      siz[i] = 1;
    }
  }
  int find(int x) {
    if (x == par[x]) return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (siz[x] < siz[y]) {
      par[x] = y;
      siz[y] = siz[x] + siz[y];
    } else {
      par[y] = x;
      siz[x] = siz[x] + siz[y];
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return siz[find(x)]; }
  int ngroup() {
    int count = 0;
    int i;
    for (i = 0; i < num; i++) {
      if (par[i] == i) {
        count++;
      }
    }
    return count;
  }
};
vector<vector<int> > gg;
vector<int> flag;
void dfs(int par, int curr) {
  int siz = gg[curr].size();
  int i;
  for (i = 0; i < siz; i++) {
    int next = gg[curr][i];
    if (next == par) continue;
    flag[next] = 3 - flag[curr];
    dfs(curr, next);
  }
  return;
}
int main(int argc, char* argv[]) {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> d(n);
  vector<pair<int, int> > z(n);
  int i, j;
  for (i = 0; i < n; i++) {
    scanf("%d", &d[i]);
    z[i] = make_pair(d[i], i);
  }
  sort(z.begin(), z.end());
  vector<int> u(m), v(m);
  vector<vector<pair<int, int> > > g(n);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &u[i], &v[i]);
    u[i]--;
    v[i]--;
    g[u[i]].push_back(make_pair(v[i], i));
    g[v[i]].push_back(make_pair(u[i], i));
  }
  vector<int> ans(m, -1);
  UF uf(n);
  vector<pair<int, int> > save;
  for (i = 0; i < n; i++) {
    int id = z[i].second;
    int siz0 = (int)g[i].size();
    int other = -1;
    int max = -2140000000;
    for (j = 0; j < siz0; j++) {
      int next = g[i][j].first;
      int edge = g[i][j].second;
      if (ans[edge] >= 0) max = ans[edge];
    }
    if (max >= 0) {
      if (max != d[id]) {
        printf("-1\n");
        return 0;
      }
      continue;
    }
    for (j = 0; j < siz0; j++) {
      int next = g[i][j].first;
      int edge = g[i][j].second;
      assert(ans[edge] < 0);
      if (d[next] <= d[id]) {
        if (!uf.same(next, id)) {
          other = next;
          ans[edge] = d[id];
          uf.unite(other, id);
          save.push_back(make_pair(other, id));
          break;
        }
      }
    }
    if (other < 0) {
      printf("-1\n");
      return 0;
    }
  }
  for (i = 0; i < m; i++) {
    if (!uf.same(u[i], v[i])) {
      uf.unite(u[i], v[i]);
      save.push_back(make_pair(u[i], v[i]));
    }
  }
  gg.resize(n);
  flag.resize(n);
  for (i = 0; i < (int)save.size(); i++) {
    int p = save[i].first;
    int q = save[i].second;
    gg[p].push_back(q);
    gg[q].push_back(p);
  }
  flag[0] = 1;
  dfs(-1, 0);
  vector<char> aaa;
  for (i = 0; i < n; i++) {
    if (flag[i] == 1)
      aaa.push_back('B');
    else
      aaa.push_back('W');
  }
  aaa.push_back('\0');
  printf("%s\n", &aaa[0]);
  int mm = 1000000000;
  for (i = 0; i < m; i++) {
    if (ans[i] < 0)
      printf("%d\n", mm);
    else
      printf("%d\n", ans[i]);
  }
  return 0;
}
