#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream& operator<<(ostream& out, const pair<A, B>& a) {
  return out << "(" << a.first << ", " << a.second << ")";
}
template <class A>
ostream& operator<<(ostream& out, const vector<A>& v) {
  out << "[";
  for (int i = 0; i < int(int(v.size())); i++) {
    if (i) out << ", ";
    out << v[i];
  }
  return out << "]";
}
mt19937 rnd(time(NULL));
const int INF = int(1e9);
const long long INF64 = long long(1e18);
const int MOD = int(1e9) + 7;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);
const int N = 200 * 1000 + 13;
int n, m;
vector<pair<int, int>> g[N];
int a[N];
bool read() {
  if (scanf("%d%d", &n, &m) != 2) return false;
  for (int i = 0; i < int(n); i++) g[i].clear();
  for (int i = 0; i < int(n); i++) scanf("%d", &a[i]);
  for (int i = 0; i < int(m); i++) {
    int v, u;
    scanf("%d%d", &v, &u);
    --v, --u;
    g[v].push_back(make_pair(u, i));
    g[u].push_back(make_pair(v, i));
  }
  return true;
}
string ans;
int w[N];
long long dist[N];
void Dijkstra(char c1, char c2) {
  for (int i = 0; i < int(n); i++) dist[i] = INF64;
  set<pair<long long, int>> q;
  for (int i = 0; i < int(n); i++)
    if (ans[i] == c1) {
      dist[i] = 0;
      q.insert(make_pair(0, i));
    }
  while (!q.empty()) {
    int v = q.begin()->second;
    q.erase(q.begin());
    for (auto it : g[v]) {
      int u = it.first;
      int w = ::w[it.second];
      if (ans[u] == c2 && dist[u] > dist[v] + w) {
        if (q.count(make_pair(dist[u], u))) q.erase(make_pair(dist[u], u));
        dist[u] = dist[v] + w;
        q.insert(make_pair(dist[u], u));
      }
    }
  }
}
void solve() {
  vector<int> ord(n);
  iota(ord.begin(), ord.end(), 0);
  sort(ord.begin(), ord.end(),
       [](int first, int second) { return a[first] < a[second]; });
  ans = string(n, '.');
  for (int i = 0; i < int(m); i++) w[i] = INF;
  for (auto v : ord) {
    bool fl = false;
    for (auto it : g[v]) {
      int u = it.first;
      int i = it.second;
      if (a[u] < a[v]) {
        fl = true;
        w[i] = a[v] - a[u];
        ans[v] = ans[u];
        break;
      }
    }
    if (!fl) {
      for (auto it : g[v]) {
        int u = it.first;
        int i = it.second;
        if (a[u] == a[v]) {
          fl = true;
          w[i] = a[v];
          ans[v] = ans[u] == 'W' ? 'B' : 'W';
          break;
        }
      }
    }
    if (!fl) {
      puts("-1");
      return;
    }
  }
  Dijkstra('W', 'B');
  for (int i = 0; i < int(n); i++)
    if (ans[i] == 'B' && dist[i] != a[i]) {
      puts("-1");
      return;
    }
  Dijkstra('B', 'W');
  for (int i = 0; i < int(n); i++)
    if (ans[i] == 'W' && dist[i] != a[i]) {
      puts("-1");
      return;
    }
  puts(ans.c_str());
  for (int i = 0; i < int(m); i++) printf("%d\n", w[i]);
}
int main() {
  cerr.precision(15);
  cout.precision(15);
  cerr << fixed;
  cout << fixed;
  if (read()) {
    solve();
  }
}
