#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
pair<int, int> a[N];
int d[N], ans[N];
bool vis[N], c[N];
vector<pair<int, int> > G[N];
void dfs(int u) {
  vis[u] = true;
  for (int i = 0, iend = G[u].size() - 1; i <= iend; ++i) {
    int v = G[u][i].first;
    if (d[u] == d[v]) {
      ans[G[u][i].second] = d[u];
      if (!vis[v]) {
        c[v] = !c[u];
        dfs(v);
      }
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1, iend = n; i <= iend; ++i) {
    scanf("%d", &d[i]);
    a[i] = make_pair(d[i], i);
  }
  for (int i = 1, iend = m; i <= iend; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(make_pair(v, i)), G[v].push_back(make_pair(u, i));
  }
  for (int i = 1, iend = n; i <= iend; ++i) {
    bool t = false;
    for (int j = 0, jend = G[i].size() - 1; j <= jend; ++j)
      if (d[G[i][j].first] <= d[i]) {
        t = true;
        break;
      }
    if (!t) {
      cout << -1 << endl;
      return 0;
    }
  }
  sort(a + 1, a + 1 + n);
  for (int i = 1, iend = n; i <= iend; ++i) {
    int u = a[i].second;
    if (vis[u]) {
      for (int j = 0, jend = G[u].size() - 1; j <= jend; ++j) {
        int v = G[u][j].first;
        if (d[v] < d[u]) ans[G[u][j].second] = 1e9;
      }
      continue;
    }
    bool t = false;
    for (int j = 0, jend = G[u].size() - 1; j <= jend; ++j)
      if (d[u] == d[G[u][j].first]) {
        t = true;
        break;
      }
    if (t)
      dfs(i);
    else {
      for (int j = 0, jend = G[u].size() - 1; j <= jend; ++j) {
        int v = G[u][j].first;
        if (d[v] < d[u]) ans[G[u][j].second] = c[v] ? d[u] : d[u] - d[v];
      }
    }
  }
  for (int i = 1, iend = n; i <= iend; ++i) printf("%c", c[i] ? 'W' : 'B');
  cout << endl;
  for (int i = 1, iend = m; i <= iend; ++i) printf("%d\n", ans[i]);
  return 0;
}
