#include <bits/stdc++.h>
using namespace std;
template <class T>
void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
const long long MOD = pow(10, 9) + 7;
const long long LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;
vector<long long> fac;
void c_fac(int x = pow(10, 6) + 10) {
  fac.resize(x, true);
  for (int i = (int)0; i < (int)x; i++) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
long long inv(long long a, long long m = MOD) {
  long long b = m, x = 1, y = 0;
  while (b != 0) {
    int d = a / b;
    a -= b * d;
    swap(a, b);
    x -= y * d;
    swap(x, y);
  }
  return (x + m) % m;
}
long long nck(long long n, long long k) {
  return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD;
}
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  return b == 0 ? a : gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
struct ver {
  int i;
  int w;
  ver(int i, int w) {
    this->i = i;
    this->w = w;
  };
};
bool operator<(const ver &x, const ver &y) { return x.w > y.w; };
int N, M;
vector<int> color, D, len;
map<int, map<int, int>> e;
priority_queue<ver> dq;
void dfs(int n, int p = -1) {
  for (auto x : e[n]) {
    if (x.first == p) continue;
    if (D[n] == D[x.first]) {
      if (color[x.first] != -1 && len[x.second] == 0)
        len[x.second] = pow(10, 9);
    } else if (D[n] < D[x.first]) {
      color[x.first] = color[n];
      len[x.second] = D[x.first] - D[n];
      dfs(x.first, n);
    }
  }
}
int main() {
  cin >> N >> M;
  color.resize(N, -1);
  D.resize(N);
  for (int i = (int)0; i < (int)N; i++) {
    cin >> D[i];
    dq.push(ver(i, D[i]));
  }
  len.resize(M, 0);
  for (int i = (int)0; i < (int)M; i++) {
    int U, V;
    cin >> U >> V;
    U--;
    V--;
    e[U][V] = i;
    e[V][U] = i;
  }
  while (dq.size()) {
    ver now = dq.top();
    dq.pop();
    bool ok = false;
    for (auto x : e[now.i]) {
      if (D[x.first] == D[now.i]) {
        ok = true;
        len[e[now.i][x.first]] = D[now.i];
        if (color[x.first] != -1) {
          color[now.i] = !color[x.first];
          dfs(now.i);
        } else {
          color[now.i] = 0;
          color[x.first] = 1;
          dfs(now.i);
          dfs(x.first);
        }
        break;
      }
    }
    if (!ok) {
      cout << -1 << endl;
      return 0;
    }
    while (dq.size() && color[dq.top().i] != -1) dq.pop();
  }
  for (auto x : color) cout << (x ? "B" : "W");
  cout << endl;
  for (auto x : len) cout << x << endl;
  return 0;
}
