#include <bits/stdc++.h>
using namespace std;
void Debug() { cerr << '\n'; }
template <class FIRST, class... REST>
void Debug(FIRST arg, REST... rest) {
  cerr << arg << " ";
  Debug(rest...);
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  if (!v.empty()) {
    for (int i = int(0); i < int((int)(v).size() - 1); i++) out << v[i] << ", ";
    out << v.back();
  }
  out << "]";
  return out;
}
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& v) {
  out << "(" << v.first << ", " << v.second << ")";
  return out;
}
const int MAX_N = 500010;
const int MAX_V = 100010;
const double eps = 1e-6;
const long long mod = 1000000007;
const int inf = (1 << 30) - 1;
const long long linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng;
int N, E;
struct UF {
  vector<int> par, ran;
  void init(int n) {
    par.resize(n);
    ran.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      ran[i] = 0;
    }
  }
  UF(int mx = 0) { init(mx); }
  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (ran[x] < ran[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (ran[x] == ran[y]) ran[x]++;
    }
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
struct edge {
  int u, v, cost, idx;
};
bool comp(const edge& e1, const edge& e2) { return e1.cost < e2.cost; }
edge es[MAX_N];
int ans[MAX_N];
int A[MAX_N], B[MAX_N];
vector<int> G[MAX_N];
int C[MAX_N];
void kruskal() {
  sort(es, es + E, comp);
  UF uf(N);
  for (int i = 0; i < E; i++) {
    edge e = es[i];
    if (!uf.same(e.u, e.v)) {
      uf.unite(e.u, e.v);
      G[e.u].push_back(e.v);
      G[e.v].push_back(e.u);
      ans[e.idx] = e.cost;
    }
  }
}
void add_edge(int s, int t, int cost, int idx) {
  es[E++] = edge{s, t, cost, idx};
}
void loop(int v, int p, int c) {
  C[v] = c;
  for (int i = int(0); i < int((int)(G[v]).size()); i++) {
    int n = G[v][i];
    if (n == p) continue;
    loop(n, v, c ^ 1);
  }
}
int M;
int D[MAX_N];
int R[MAX_N];
void solve() {
  cin >> N >> M;
  for (int i = int(0); i < int(N); i++) cin >> D[i];
  for (int i = int(0); i < int(M); i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    A[i] = a;
    B[i] = b;
    add_edge(a, b, max(D[a], D[b]), i);
  }
  fill(ans, ans + M, 1000000000);
  kruskal();
  loop(0, -1, 0);
  fill(R, R + M, 1000000001);
  for (int i = int(0); i < int(M); i++) {
    if ((C[A[i]] ^ C[B[i]]) == 1) {
      R[A[i]] = min(R[A[i]], ans[i]);
      R[B[i]] = min(R[B[i]], ans[i]);
    }
  }
  for (int i = int(0); i < int(N); i++) {
    if (R[i] != D[i]) {
      cout << -1 << "\n";
      return;
    }
  }
  for (int i = int(0); i < int(N); i++) {
    if (C[i])
      cout << 'B';
    else
      cout << 'W';
  }
  cout << "\n";
  for (int i = int(0); i < int(M); i++) {
    cout << ans[i] << "\n";
  }
}
uint32_t rd() {
  uint32_t res;
  res = std::random_device()();
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(20);
  cerr << fixed;
  cerr.precision(6);
  rng.seed(rd());
  solve();
  cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  return 0;
}
