#include <bits/stdc++.h>
using namespace std;
long long v_color[100005] = {0};
vector<pair<long long, long long> > node[100005];
const long long inf = 1e9;
long long ans[200005] = {0};
long long dn[100005] = {0};
int main() {
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long> > vec;
  for (int i = 1; i <= n; i++) {
    long long val;
    cin >> val;
    dn[i] = val;
    vec.push_back(pair<long long, long long>(val, i));
  }
  sort(vec.begin(), vec.end());
  for (int i = 1; i <= m; i++) {
    long long u, v;
    cin >> u >> v;
    node[u].push_back(pair<long long, long long>(v, i));
    node[v].push_back(pair<long long, long long>(u, i));
  }
  for (int i = 0; i < n; i++) {
    long long cos = vec[i].first, pos = vec[i].second;
    if (v_color[pos] == 0) {
      bool f = false;
      for (int j = 0; j < node[pos].size(); j++) {
        long long to = node[pos][j].first;
        if (dn[to] == cos) {
          if (v_color[to] > 0) {
            v_color[pos] = (v_color[to] % 2) + 1;
          } else {
            v_color[pos] = 1;
            v_color[to] = 2;
          }
          ans[node[pos][j].second] = cos;
          f = true;
        }
      }
      if (!f) {
        cout << -1 << endl;
        return 0;
      }
    }
    for (int j = 0; j < node[pos].size(); j++) {
      long long to = node[pos][j].first;
      if (dn[to] > cos && v_color[to] == 0) {
        v_color[to] = v_color[pos];
        ans[node[pos][j].second] = dn[to] - cos;
      }
    }
  }
  for (int i = 1; i <= n; i++) cout << (v_color[i] == 1 ? "B" : "W");
  cout << endl;
  for (int i = 1; i <= m; i++) {
    cout << (ans[i] == 0 ? inf : ans[i]) << endl;
  }
}
