#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int N, M;
vector<long long> D;
vector<pair<int, int>> edges;
vector<int> g[100010];
int main() {
  cin >> N >> M;
  D.resize(N);
  for (int i = 0; i < (N); ++i) cin >> D[i];
  map<pair<int, int>, int> mp;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (a > b) swap(a, b);
    mp[make_pair(a, b)] = i;
    edges.push_back(make_pair(a, b));
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> ans(M, 0);
  vector<pair<long long, int>> v;
  for (int i = 0; i < (N); ++i) v.push_back(make_pair(D[i], i));
  sort(v.begin(), v.end());
  vector<int> col(N, 0);
  vector<int> check(N, 0);
  for (int i = 0; i < N; i++) {
    int now = v[i].second;
    bool ok = false;
    bool exi = false;
    for (auto nex : g[now]) {
      if (col[nex] != 0) {
        int tar = D[nex];
        if (col[nex] == 1) {
          tar = 0;
          if (check[nex] == 0 && D[now] > D[nex]) {
            cout << -1 << endl;
            return 0;
          }
          check[nex] = 1;
        }
        if (D[now] == D[nex] && col[nex] == -1) continue;
        int a = now, b = nex;
        if (a > b) swap(a, b);
        ans[mp[make_pair(a, b)]] = D[now] - tar;
        col[now] = -1;
      }
    }
    if (1) {
      if (col[now] == 0) col[now] = 1;
      for (auto nex : g[now]) {
        if (col[nex] == -1 && D[nex] == D[now]) {
          int a = now, b = nex;
          if (a > b) swap(a, b);
          ans[mp[make_pair(a, b)]] = D[now];
        }
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (col[i] == 1)
      cout << 'B';
    else
      cout << 'W';
  }
  cout << endl;
  for (int i = 0; i < M; i++) cout << ans[i] << endl;
  return 0;
}
