#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
const long long N = 1e5 + 10;
vector<long long> edge[N];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<long long> d(n);
  vector<pair<long long, long long> > a(n);
  for (long long i = (0); i < ((n)); ++i)
    cin >> d[i], a[i] = pair<long long, long long>(d[i], i);
  sort(a.begin(), a.end());
  vector<pair<long long, long long> > e(m);
  for (long long i = (0); i < ((m)); ++i) {
    long long x, y;
    cin >> x >> y;
    x--;
    y--;
    if (x > y) swap(x, y);
    e[i] = pair<long long, long long>(x, y);
    edge[x].emplace_back(y);
    edge[y].emplace_back(x);
  }
  map<pair<long long, long long>, long long> mp;
  vector<long long> ans(n, 0);
  for (long long i = (0); i < ((n)); ++i) {
    long long no = a[i].second, v = a[i].first;
    if (ans[no]) continue;
    long long id = -1;
    for (long long to : edge[no]) {
      if (ans[to] && v > d[to]) id = to;
      if (!ans[to] && v == d[to]) id = to;
    }
    if (id == -1) {
      cout << -1 << endl;
      return 0;
    }
    long long x = no, y = id;
    if (x > y) swap(x, y);
    if (ans[id]) {
      ans[no] = ans[id];
      mp[pair<long long, long long>(x, y)] = d[no] - d[id];
    } else {
      ans[no] = 1, ans[id] = -1;
      mp[pair<long long, long long>(x, y)] = d[no];
    }
  }
  for (long long i = (0); i < ((n)); ++i) cout << (ans[i] == 1 ? 'B' : 'W');
  cout << endl;
  for (long long i = (0); i < ((m)); ++i) {
    long long v = mp[e[i]];
    if (!v) v = (long long)1e9;
    cout << v << "\n";
  }
  return 0;
}
