Problem:
Score : 900 points

Problem Statement

We have a connected undirected graph with N vertices and M edges.
Edge i in this graph (1 \leq i \leq M) connects Vertex U_i and Vertex V_i bidirectionally.
We are additionally given N integers D_1, D_2, ..., D_N.

Determine whether the conditions below can be satisfied by assigning a color - white or black - to each vertex and an integer weight between 1 and 10^9 (inclusive) to each edge in this graph.
If the answer is yes, find one such assignment of colors and integers, too.

There is at least one vertex assigned white and at least one vertex assigned black.

For each vertex v (1 \leq v \leq N), the following holds.

The minimum cost to travel from Vertex v to a vertex whose color assigned is different from that of Vertex v by traversing the edges is equal to D_v.

Here, the cost of traversing the edges is the sum of the weights of the edges traversed.

Constraints

2 \leq N \leq 100,000

1 \leq M \leq 200,000

1 \leq D_i \leq 10^9

1 \leq U_i, V_i \leq N

The given graph is connected and has no self-loops or multiple edges.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
D_1 D_2 ... D_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

If there is no assignment satisfying the conditions, print a single line containing -1.

If such an assignment exists, print one such assignment in the following format:

S
C_1
C_2
\vdots
C_M

Here,

the first line should contain the string S of length N. Its i-th character (1 \leq i \leq N) should be W if Vertex i is assigned white and B if it is assigned black.

The (i + 1)-th line (1 \leq i \leq M) should contain the integer weight C_i assigned to Edge i.

Sample Input 1

5 5
3 4 3 5 7
1 2
1 3
3 2
4 2
4 5

Sample Output 1

BWWBB
4
3
1
5
2

Assume that we assign the colors and integers as the sample output, and let us consider Vertex 5, for example. To travel from Vertex 5, which is assigned black, to a vertex that is assigned white with the minimum cost, we should make these moves: Vertex 5 \to Vertex 4 \to Vertex 2. The total cost of these moves is 7, which satisfies the condition. We can also verify that the condition is satisfied for other vertices.

Sample Input 2

5 7
1 2 3 4 5
1 2
1 3
1 4
2 3
2 5
3 5
4 5

Sample Output 2

-1

Sample Input 3

4 6
1 1 1 1
1 2
1 3
1 4
2 3
2 4
3 4

Sample Output 3

BBBW
1
1
1
2
1
1

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)(((n)>>(m))&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
//#define MOD 998244353
#define MOD 1000000007
#define invp(a,p)pom(a,p-2,p)

//辺の情報を個別に持つタイプ
typedef struct edge{ll s,g,c,f,i;}E;
typedef struct graph{
	int vcnt,ecnt;
	E  e[400010];//適宜変える(ecnt)
	int id[100010];//適宜変える(vcnt)
}G;
G g;

int a[100010];
int esort(const void*a,const void*b){
	E*p=(E*)a,*q=(E*)b;
	if((*p).s<(*q).s)return -1;
	if((*p).s>(*q).s)return  1;
	if((*p).g<(*q).g)return -1;
	return 1;
}
int csort(const void*a,const void*b){ll p=(*(E*)a).c,q=(*(E*)b).c;return p<q?-1:p>q?1:0;}
void readgraph(){
	//適宜変える
	ll n,m;
	scanf("%lld%lld",&n,&m);
	rep(i,0,n)scanf("%d",a+i);
	rep(i,0,m){
		ll x,y;
		scanf("%lld%lld",&x,&y);
		x=1,y=2;
		x--,y--;
		g.e[i].s=x;
		g.e[i].g=y;
		g.e[i].c=max(a[x],a[y]);
		g.e[i].f=0;
		g.e[i].i=i;
	}
	g.vcnt=n;
	g.ecnt=m;
	qsort(g.e,g.ecnt,sizeof(E),csort);
}


//union-find
#define UFLIMIT (1<<17)
int unicnt[UFLIMIT+10];//正ならcnt、非正なら根のindex
void ufinit(int n){rep(i,0,n)unicnt[i]=1;}
int ufroot(int x){return unicnt[x]<=0?-(unicnt[x]=-ufroot(-unicnt[x])):x;}
int ufsame(int x,int y){return ufroot(x)==ufroot(y);}
void uni(int x,int y){if((x=ufroot(x))==(y=ufroot(y)))return;if(unicnt[x]<unicnt[y]){int t=x;x=y;y=t;}unicnt[x]+=unicnt[y];unicnt[y]=-x;}
#undef UFLIMIT

//クラスカル法
//最小全域木を構成する辺とそのコストの合計を求める
//union findが必要、事前にコストでソートが必要
//O(ElogE)
//*
void kurasukaru(){
	ufinit(g.vcnt);
	rep(i,0,g.ecnt){
		if(!ufsame(g.e[i].s,g.e[i].g)){
			uni(g.e[i].s,g.e[i].g);
			g.e[i].f=1;
		}
	}
}
//*/

//*
void makeinvedge(){
	//逆辺とidx
	qsort(g.e,g.ecnt,sizeof(E),esort);
	int nnn=g.ecnt;
	rep(i,0,g.ecnt){
		int l=0,r=g.ecnt;
		while(r-l>1){
			int m=(l+r)/2;
			if(g.e[m].s<g.e[i].g||(g.e[m].s==g.e[i].g&&g.e[m].g<=g.e[i].s))l=m;
			else r=m;
		}
		g.e[nnn].s=g.e[i].g;
		g.e[nnn].g=g.e[i].s;
		g.e[nnn].c=g.e[i].c;
		g.e[nnn].f=g.e[i].f;
		g.e[nnn].i=g.e[i].i;
		nnn++;
	}
	g.ecnt*=2;
	qsort(g.e,g.ecnt,sizeof(E),esort);
	int p=0;
	rep(i,0,g.vcnt){
		while(p<g.ecnt&&g.e[p].s<i)p++;
		g.id[i]=p;
	}
	g.id[g.vcnt]=g.ecnt;//番兵
}
//*/


int col[100010];
//dfs
int dfs(int v,int pre,int c){
	int flag=1;
	col[v]=c;
	rep(i,g.id[v],g.id[v+1]){
		if(g.e[i].f==1&&g.e[i].g!=pre){
			flag&=(a[g.e[i].g]>=a[v]);
			flag&=dfs(g.e[i].g,v,3-c);
		}
	}
	return flag;
}

int ans[200010];
int main(){
	readgraph();
	//次数が小さいところから木にできるか？
	kurasukaru();
//	rep(i,0,g.ecnt)printf("%d %d %d %d\n",g.e[i].s,g.e[i].g,g.e[i].c,g.e[i].f);puts("");
	makeinvedge();
//	rep(i,0,g.ecnt)printf("%d %d %d %d\n",g.e[i].s,g.e[i].g,g.e[i].c,g.e[i].f);puts("");
	
	int v=-1,m=2e9;
	rep(i,0,g.vcnt)if(m>a[i])v=i,m=a[i];
	int cnt=0;
	rep(i,0,g.vcnt)if(m==a[i])cnt++;
	if(cnt==1){
		puts("-1");
		return 0;
	}
	
	if(!dfs(v,-1,1)){
		puts("-1");
		return 0;
	}
	rep(i,0,g.vcnt)putchar(col[i]==1?'B':'W');puts("");
	rep(i,0,g.ecnt)ans[g.e[i].i]=g.e[i].f?g.e[i].c:1e9;
	rep(i,0,g.ecnt/2)printf("%d\n",ans[i]);
}