#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int maxm = 2e5 + 10;
int n, m;
long long d[maxn], w[maxm];
int u[maxm], v[maxm], col[maxn], od[maxm];
map<long long, int> mp;
vector<pair<int, int> > g[maxn];
bool less2(int a, int b) {
  if (d[u[a]] != d[u[b]]) return d[u[a]] < d[u[b]];
  return d[v[a]] < d[v[b]];
}
void dfs(int x) {
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i].first;
    if (col[to] != -1) {
      continue;
    }
    if (d[to] <= d[x]) {
      w[g[x][i].second] = 1000000000ll;
      continue;
    }
    col[to] = 1;
    w[g[x][i].second] = d[to] - d[x];
    dfs(to);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%lld", &d[i]), mp[d[i]]++;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &u[i], &v[i]);
    u[i]--, v[i]--;
    if (d[u[i]] > d[v[i]]) swap(u[i], v[i]);
    od[i] = i;
    g[u[i]].push_back(make_pair(v[i], i));
    g[v[i]].push_back(make_pair(u[i], i));
  }
  if (mp.begin()->second == 1) {
    printf("-1\n");
    return 0;
  }
  sort(od, od + m, less2);
  memset(col, -1, sizeof(col));
  for (int i = 0; i < m; i++) {
    int x = od[i];
    if (col[u[x]] != -1) continue;
    if (d[u[x]] != d[v[x]]) {
      printf("-1\n");
      return 0;
    }
    col[u[x]] = 0;
    for (int j = 0; j < g[u[x]].size(); j++) {
      int to = g[u[x]][j].first;
      int eid = g[u[x]][j].second;
      if (col[to] != -1) continue;
      w[eid] = d[to];
      col[to] = 1;
      dfs(to);
    }
  }
  for (int i = 0; i < n; i++) {
    printf("%c", col[i] ? 'B' : 'W');
  }
  printf("\n");
  for (int i = 0; i < m; i++) {
    printf("%lld\n", w[i]);
  }
  return 0;
}
