#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int rt[100010];
int get_r(int x) { return (x == rt[x] ? x : rt[x] = get_r(rt[x])); }
int n, m;
int d[100010], st[200020], ed[200020];
int col[100010];
int val[200020];
int point[100010], te = 1;
struct e {
  int to, next, val;
} edge[1000010];
void add(int x, int y, int z) {
  te++;
  edge[te].to = y;
  edge[te].next = point[x];
  edge[te].val = z;
  point[x] = te;
}
int solved[100010];
void e_search(int x) {
  for (int e = point[x]; e; e = edge[e].next) {
    int to = edge[e].to;
    if (d[to] != d[x]) continue;
    if (col[to]) continue;
    if (col[x] == 1) col[to] = 2;
    if (col[x] == 2) col[to] = 1;
    val[e / 2] = d[x];
    e_search(to);
  }
}
void l_search(int x) {
  for (int e = point[x]; e; e = edge[e].next) {
    int to = edge[e].to;
    if (col[to]) {
      continue;
    }
    if (d[to] <= d[x]) continue;
    col[to] = col[x];
    val[e / 2] = d[to] - d[x];
    l_search(to);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> d[i];
  for (int i = 1; i <= m; i++) {
    cin >> st[i] >> ed[i];
    add(st[i], ed[i], 0);
    add(ed[i], st[i], 0);
  }
  for (int i = 1; i <= n; i++) {
    if (solved[i]) continue;
    int mn = 2e9;
    for (int e = point[i]; e; e = edge[e].next) {
      int to = edge[e].to;
      mn = min(d[to], mn);
    }
    if (mn > d[i]) {
      cout << -1 << endl;
      exit(0);
    }
    if (mn == d[i]) {
      if (!col[i]) {
        col[i] = 1;
        e_search(i);
      }
      l_search(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (col[i] == 1)
      cout << 'B';
    else if (col[i] == 2)
      cout << 'W';
    else
      cout << ".";
  }
  cout << endl;
  for (int i = 1; i <= m; i++) {
    if (!val[i]) val[i] = 1000000000;
    cout << val[i] << '\n';
  }
  cout << endl;
  return 0;
}
