#include <bits/stdc++.h>
using namespace std;
template <int um>
class UF {
 public:
  vector<int> par, rank;
  UF() {
    rank = vector<int>(um, 0);
    for (int i = 0; i < um; i++) par.push_back(i);
  }
  int operator[](int x) {
    return (par[x] == x) ? (x) : (par[x] = operator[](par[x]));
  }
  int operator()(int x, int y) {
    if ((x = operator[](x)) == (y = operator[](y))) return x;
    if (rank[x] > rank[y]) return par[x] = y;
    rank[x] += rank[x] == rank[y];
    return par[y] = x;
  }
};
UF<500000> uf;
int N, M;
signed long long D[202020];
pair<int, int> P[202020];
vector<pair<int, int>> E[101010];
int ret[202020], fix[101010];
string S;
void dfs(int cur, int pre, int i) {
  if (i)
    S[cur] = 'B';
  else
    S[cur] = 'W';
  for (auto& e : E[cur])
    if (e.first != pre && ret[e.second] < 1 << 30) dfs(e.first, cur, i ^ 1);
}
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> N >> M;
  for (i = 0; i < (N); i++) {
    cin >> D[i];
    P[i] = {D[i], i};
  }
  for (i = 0; i < (M); i++) {
    cin >> x >> y;
    E[x - 1].push_back({y - 1, i});
    E[y - 1].push_back({x - 1, i});
    ret[i] = 1 << 30;
  }
  sort(P, P + N);
  for (i = 0; i < (N); i++) {
    x = P[i].second;
    for (auto& e : E[x])
      if (uf[e.first] != uf[x] &&
          (D[e.first] == D[x] || (D[e.first] < D[x] && fix[e.first]))) {
        ret[e.second] = D[x];
        fix[x] = 1;
        uf(x, e.first);
      }
  }
  int num = 0;
  for (i = 0; i < (N); i++)
    if (uf[i] == i) num++;
  if (num > 1) return (void)printf("-1\n");
  S.resize(N);
  dfs(0, 0, 0);
  cout << S << endl;
  for (i = 0; i < (M); i++) {
    if (ret[i] == 1 << 30) ret[i] = 1000000000;
    cout << ret[i] << endl;
  }
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
