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

typedef long long LL;

const int N=100000,M=200000,INF=1000000000;

int n,m,d[N+9];
struct side{
  int y,next,ans;
}e[M*2+9];
int lin[N+9],cs;

void Ins(int x,int y){e[++cs].y=y;e[cs].next=lin[x];lin[x]=cs;}
void Ins2(int x,int y){Ins(x,y);Ins(y,x);}

void into(){
  scanf("%d%d",&n,&m);
  cs=1;
  for (int i=1;i<=n;++i)
	scanf("%d",&d[i]);
  for (int i=1;i<=m;++i){
	int x,y;
	scanf("%d%d",&x,&y);
	Ins2(x,y);
  }
}

int ans[N+9],vis[N+9];

void Dfs_ans(int k,int c){
  ans[k]=c;
  vis[k]=1;
  for (int i=lin[k];i;i=e[i].next)
	if (!vis[e[i].y]&&e[i].ans) Dfs_ans(e[i].y,c^d[k]==d[e[i].y]);
}

void Get_ans(){
  for (int i=1;i<=n;++i){
	int mn=lin[i];
	for (int j=lin[i];j;j=e[j].next)
	  if (d[e[j].y]<d[e[mn].y]) mn=j;
    if (d[i]<d[e[mn].y]) {ans[0]=-1;return;}
	e[mn].ans=e[mn^1].ans=d[i]==d[e[mn].y]?d[i]:d[i]-d[e[mn].y];
  }
  for (int i=1;i<=n;++i)
	if (!vis[i]) Dfs_ans(i,0);
}

void work(){
  Get_ans();
}

void outo(){
  if (ans[0]==-1) puts("-1");
  else{
	for (int i=1;i<=n;++i)
	  putchar(ans[i]?'B':'W');
	puts("");
	for (int i=2;i<=m<<1;i+=2)
	  printf("%d\n",e[i].ans?e[i].ans:INF);
	puts("");
  }
}

int main(){
  into();
  work();
  outo();
  return 0;
}