#include <bits/stdc++.h>
using std::bitset;
using std::cerr;
using std::cin;
using std::cout;
using std::deque;
using std::endl;
using std::fill;
using std::fixed;
using std::ios_base;
using std::iterator;
using std::map;
using std::max_element;
using std::min_element;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::reverse;
using std::set;
using std::setprecision;
using std::sort;
using std::stable_sort;
using std::stack;
using std::string;
using std::swap;
using std::unique;
using std::vector;
long long min(long long a, long long b) { return a < b ? a : b; }
long long min(int a, long long b) { return a < b ? a : b; }
long long min(long long a, int b) { return a < b ? a : b; }
long long min(int a, int b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
long long max(int a, long long b) { return a > b ? a : b; }
long long max(long long a, int b) { return a > b ? a : b; }
long long max(int a, int b) { return a > b ? a : b; }
namespace MySpace {};
long long n, m;
long long d[600000];
long long ans[600000];
map<pair<long long, long long>, long long> id;
vector<long long> graph[600000];
long long p[600000];
long long color[600000];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    cin >> d[i];
  }
  for (long long i = 0; i < m; i++) {
    ans[i] = 1e9;
    long long v, u;
    cin >> v >> u, v--, u--;
    id[{v, u}] = i;
    id[{u, v}] = i;
    graph[v].push_back(u);
    graph[u].push_back(v);
  }
  for (long long v = 0; v < n; v++) {
    pair<long long, long long> t = {-1, -1};
    pair<long long, long long> x = {-1, -1};
    for (long long i = 0; i < graph[v].size(); i++) {
      long long u = graph[v][i];
      if (d[u] <= d[v]) {
        if (t == x) t = {d[u], u};
        t = min(t, {d[u], u});
      }
    }
    if (t == x) {
      cout << -1;
      return 0;
    }
    p[v] = t.second;
  }
  vector<pair<pair<long long, long long>, long long> > e;
  for (long long i = 0; i < n; i++)
    e.push_back({{d[i], d[p[i]]}, i}), color[i] = -1;
  sort((e).begin(), (e).end());
  for (long long i = 0; i < n; i++) {
    long long v = e[i].second;
    long long pv = p[v];
    if (color[pv] == -1) {
      color[v] = 0;
    } else {
      if (d[v] > d[pv]) {
        color[v] = color[pv];
        ans[id[{v, pv}]] = d[v] - d[pv];
      } else {
        color[v] = 1 - color[pv];
        ans[id[{v, pv}]] = d[v];
      }
    }
  }
  for (long long(i) = 0; (i) != (n); (i)++) {
    if (color[i] == 0)
      cout << "B";
    else
      cout << "W";
  }
  cout << "\n";
  for (long long(i) = 0; (i) != (m); (i)++) cout << ans[i] << "\n";
}
