#include <cstdio>
#include <utility>
#include <vector>
#include <algorithm>

using namespace std;

const int N = 200'009;

int a[N];
int o[N];
vector<pair<int, int>> g[N];
int color[N];
int q[N];
int ans[N];

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    g[i].clear();
  }
  for (int i = 0; i < m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x;
    --y;
    g[x].emplace_back(y, i);
    g[y].emplace_back(x, i);
    ans[i] = 1'000'000'000;
  }
  for (int i = 0; i < n; ++i) {
    o[i] = i;
    color[i] = -1;
  }
  sort(o, o + n, [](int i, int j) {
    return a[i] < a[j];
  });
  for (int i = 0; i < n; ++i) {
    int from = o[i];
    if (a[from] == a[o[0]]) {
      if (color[from] != -1) {
        continue;
      }
      color[from] = 0;
      q[0] = from;
      int qt = 1;
      for (int qh = 0; qh < qt; ++qh) {
        int x = q[qh];
        for (auto &e : g[x]) {
          int y = e.first;
          if (a[y] != a[x]) {
            continue;
          }
          if (color[y] != -1) {
            continue;
          }
          ans[e.second] = a[x];
          color[y] = 1 - color[x];
          q[qt++] = y;
        }
      }
      if (qt == 1) {
        puts("-1");
        return 0;
      }
    } else {
      bool ok = false;
      for (auto &e : g[from]) {
        int y = e.first;
        if (a[from] > a[y]) {
          color[from] = color[y];
          ans[e.second] = a[from] - a[y];
          ok = true;
          break;
        }
      }
      if (!ok) {
        puts("-1");
        return 0;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    putchar(color[i] == 0 ? 'W' : 'B');
  }
  putchar('\n');
  for (int i = 0; i < m; ++i) {
    printf("%d\n", ans[i]);
  }
}
