#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

int64 N, M;
vector<int64> D;
vector<PLL> G[112345];
vector<PII> es;
vector<int64> col;
vector<int64> cost;

void dfs(int64 v, int64 p) {
  for (auto &e : G[v]) {
    if (col[e.fs] != -1 || cost[e.sc] != -1) continue;
    if (D[e.fs] < D[v]) {
      cost[e.sc] = INF;
      continue;
    } else {
//      cout << v << " " << "(" << e.fs << " " << e.sc << "): " << D[e.fs] << endl;
      cost[e.sc] = D[e.fs];
      col[e.fs] = 1-col[v];
      dfs(e.fs, v);
    }
  }
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N >> M;
	D = vector<int64>(N);
	es.resize(M);
	col.resize(N, -1);
	cost.resize(M, -1);
	REP(i, N) cin >> D[i];
	REP(i, M) {
	  cin >> es[i].fs >> es[i].sc; es[i].fs--; es[i].sc--;
	  G[es[i].fs].emplace_back(es[i].sc, i);
	  G[es[i].sc].emplace_back(es[i].fs, i);
	}
	REP(i, M) {
	  if (D[es[i].fs] == D[es[i].sc]) {
	    cost[i] = D[es[i].fs];
	    col[es[i].fs] = 0;
	    col[es[i].sc] = 1;
	    dfs(es[i].fs, es[i].sc);
	    dfs(es[i].sc, es[i].fs);
	  }
	}
	vector<int64> check(N, INF_LL);
	REP(i, M) {
//	  cout << es[i].fs << " " << es[i].sc << "---" << cost[i] << endl;
    if (cost[i] == -1) cost[i] = INF;
	  if (col[es[i].fs] == col[es[i].sc]) continue;
	  chmin(check[es[i].fs], cost[i]);
	  chmin(check[es[i].sc], cost[i]);
	}
	REP(i, N) {
	  if (D[i] != check[i]) {
	    cout << -1 << endl;
	    return 0;
	  }
	}
	REP(i, N) {
	  if (col[i]) cout << 'B';
	  else cout << 'W';
	}
	cout << endl;
	REP(i, M) {
	  cout << cost[i] << endl;
	}
}
