#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
  if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
  if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}
int N, M;
int D[100011], E[100011];
int X[200011];
int Y[200011];
vector<int> G[100011];
char S[100011];
int C[200011];
bool in[100011];
pair<int, int> P[100011];
bool vis[100011];
int anti(int i, int v) { return v ^ X[i] ^ Y[i]; }
bool valid() {
  for (int v = 0, v_len = (N); v < v_len; ++v) {
    long long mi = 1LL << 60;
    for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end();
         e != e_end; ++e) {
      int w = anti(*e, v);
      if (S[v] != S[w])
        amin(mi, (long long)C[*e]);
      else
        amin(mi, (long long)D[w] + C[*e]);
    }
    if (mi != D[v]) return false;
  }
  return true;
}
void MAIN() {
  scanf("%d%d", &N, &M);
  for (int i = 0, i_len = (N); i < i_len; ++i) scanf("%d", D + i);
  memset(E, 0x3f, sizeof E);
  for (int i = 0, i_len = (M); i < i_len; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--;
    X[i] = x;
    Y[i] = y;
    amin(E[x], D[y]);
    amin(E[y], D[x]);
  }
  for (int v = 0, v_len = (N); v < v_len; ++v)
    if (E[v] > D[v]) {
      puts("-1");
      return;
    }
  for (int i = 0, i_len = (N); i < i_len; ++i) P[i] = make_pair(D[i], i);
  sort(P, P + N);
  for (int i = 0, i_len = (N); i < i_len; ++i) G[i].clear();
  for (int i = 0, i_len = (M); i < i_len; ++i) {
    int x = X[i], y = Y[i];
    G[x].push_back(i);
    G[y].push_back(i);
  }
  for (int i_ = 0, i__len = (N); i_ < i__len; ++i_) {
    int i = P[i_].second;
    if (vis[i]) continue;
    vector<int> ord;
    ord.push_back(i);
    vis[i] = true;
    S[i] = 'B';
    for (int j = 0; j < (int)ord.size(); j++) {
      int v = ord[j];
      for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end();
           e != e_end; ++e) {
        int w = anti(*e, v);
        if (!vis[w] && D[v] <= D[w]) {
          ord.push_back(w);
          vis[w] = true;
          if (D[v] == D[w]) {
            S[w] = (S[v] == 'B' ? 'W' : 'B');
            C[*e] = D[v];
          }
        }
      }
    }
  }
  priority_queue<pair<int, int> > Q;
  for (int i = 0, i_len = (N); i < i_len; ++i)
    if (S[i]) {
      Q.emplace(-D[i], i);
      in[i] = true;
    }
  while (!Q.empty()) {
    int v = Q.top().second;
    Q.pop();
    for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end();
         e != e_end; ++e) {
      int w = anti(*e, v);
      if (!in[w]) {
        Q.emplace(-D[w], w);
        in[w] = true;
      }
    }
    if (E[v] < D[v]) {
      S[v] = 'B';
      for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end();
           e != e_end; ++e) {
        int w = anti(*e, v);
        if (D[w] < D[v]) {
          if (S[w] == 'B')
            C[*e] = D[v] - D[w];
          else
            C[*e] = D[v];
        }
      }
    } else {
      for (__typeof((G[v]).begin()) e = (G[v]).begin(), e_end = (G[v]).end();
           e != e_end; ++e) {
        int w = anti(*e, v);
        if (D[w] == D[v]) {
          S[v] = (S[w] == 'B' ? 'W' : 'B');
          C[*e] = D[v];
          break;
        }
      }
    }
  }
  for (int i = 0, i_len = (M); i < i_len; ++i)
    if (C[i] == 0) C[i] = 1000000000;
  if (valid()) {
    puts(S);
    for (int i = 0, i_len = (M); i < i_len; ++i) printf("%d\n", C[i]);
  } else {
    puts("-1");
    return;
  }
}
int main() {
  int TC = 1;
  for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();
  return 0;
}
