#include <bits/stdc++.h>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/adaptor/indexed.hpp>

using namespace std;
using namespace boost::adaptors;
using namespace std::string_literals;
using ll = int64_t;
using vecint = vector<int>;
using vecll = vector<ll>;
using boost::irange;

struct Edge {
  int to;
  int id;
};

using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph& g, int u, int v, int id) {
  g[u].push_back(Edge{v,id});
  g[v].push_back(Edge{u,id});
}

int main()
{
  int n,m;
  cin>>n>>m;
  vector<int> d;
  vector<tuple<int,int>> dt;
  for(auto&& i:irange(0,n)){
    int e;
    cin>>e;
    d.push_back(e);
    dt.emplace_back(e,i);
  }
  boost::sort(dt);
  Graph g(n);
  for(auto&& i:irange(0,m)) {
    int u,v;
    cin>>u>>v;
    --u;--v;
    add_edge(g,u,v,i);
  }
  vecint clr(n,-1);
  vecint w(m);
  bool valid = true;
  for(auto&& t:dt) {
    int di,i;
    tie(di,i) = t;
    for(auto&& e:g[i]) {
      if (d[e.to] >= di) continue;
      if (clr[e.to] == -1) valid = false;
      if (clr[i] == -1) {
        clr[i] = clr[e.to];
        w[e.id] = di - d[e.to];
      } else {
        if (clr[i] == clr[e.to]) {
          w[e.id] = di - d[e.to];
        } else {
          w[e.id] = 1e9;
        }
      }
    }
    if (clr[i] == -1) {
      bool ok = false;
      for(auto&& e:g[i]) {
        if (d[e.to] != di) continue;
        ok = true;
        if (clr[e.to] != -1) {
          clr[i] = 1-clr[e.to];
          w[e.id] = di;
          break;
        }
      }
      if(!ok) valid = false;
      if (clr[i] == -1) clr[i] = 0;
    }
    for(auto&& e:g[i]) {
      if (d[e.to] != di) continue;
      if (clr[e.to] == clr[i]) {
        w[e.id] = 1e9;
      } else if (clr[e.to] != -1) {
        w[e.id] = di;
      }
    }
  }
  for(auto&& e:clr) {
    if(e == -1) valid = false;
  }
  for(auto&& e:w) {
    if(e <= 0) valid = false;
  }
  if (valid) {
    for(auto&& e:clr) {
      if(e==0) {
        cout<<'B';
      }else{
        cout<<'W';
      }
    }
    cout<<endl;
    for(auto&& e:w) {
      cout<<e<<endl;
    }
  } else {
    cout<<-1<<endl;
  }
  return 0;
}
