#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;
 
using namespace std;

class UF
{
private:
    int         num;
    vector<int> par;
    vector<int> siz;
public:
    //vector<int> val;
public:
    UF(int n): num(n) {
        par.resize(n);
        siz.resize(n);
        //val.resize(n);
        int i;
        for(i=0; i<n; i++) {
            par[i]=i; siz[i]=1;
            //val[i]=0;
        }
    }
	
    int find(int x) {
        if(x==par[x]) return x;
        return par[x] = find(par[x]);
    }

    void unite(int x, int y) {
        x=find(x);
        y=find(y);
        if(x==y) return;
        if(siz[x]<siz[y]) {
            par[x]=y;
            siz[y]=siz[x]+siz[y];
            //val[y]=val[x]+val[y];
        }
        else {
            par[y]=x;
            siz[x]=siz[x]+siz[y];
            //val[x]=val[x]+val[y];
        }
    }

    bool same(int x, int y) {
        return find(x)==find(y);
    }

    int size(int x) {
        return siz[find(x)];
    }

    int ngroup() {
    //int ngroup( int& ans ) {
        int count=0;
        int i;
        for(i=0; i<num; i++) {
            if(par[i]==i) {
                count++;
                //ans += (val[i]? siz[i]: siz[i]-1);
            }
        }
        return count;
    }
};

vector<vector<int> > gg;
vector<int> flag;

void dfs(int par, int curr)
{
    int siz=gg[curr].size();
    int i;
    for(i=0; i<siz; i++) {
        int next=gg[curr][i];
        if(next==par) continue;

        flag[next]=3-flag[curr];

        dfs(curr,next);
    }
    return;
}

int main(int argc, char* argv[])
{
    int n,m;
    scanf("%d%d", &n, &m);
    vector<int> d(n);
    vector<pair<int,int> > z(n);
    int i,j;
    for(i=0; i<n; i++) {
        scanf("%d", &d[i]);
        z[i]=make_pair(d[i],i);
    }
    sort(z.begin(),z.end());

    vector<int> u(m), v(m);
    vector<vector<pair<int,int> > > g(n);   // to, edgeid
    for(i=0; i<m; i++) {
        scanf("%d%d", &u[i], &v[i]); u[i]--; v[i]--;
        g[u[i]].push_back(make_pair(v[i],i));
        g[v[i]].push_back(make_pair(u[i],i));
    }

    vector<int> ans(m,-1);
    UF uf(n);
    vector<pair<int,int> > save;
    for(i=0; i<n; i++) {
        int id=z[i].second;
        int siz0=(int)g[id].size();
        int other=-1;
        int max=-INF;
        for(j=0; j<siz0; j++) {
            int next=g[id][j].first;
            int edge=g[id][j].second;
            max=MAX(max,ans[edge]);
        }
        if(max>=0) {
            if(max!=d[id]) {
                printf("-1\n"); return 0;
            }
            continue;
        }
        for(j=0; j<siz0; j++) {
            int next=g[id][j].first;
            int edge=g[id][j].second;
            assert(ans[edge]<0);
            if(d[next]<=d[id]) {
                if(!uf.same(next,id)) {
                    other=next;
                    ans[edge]=d[id];
                    uf.unite(other,id);
                    save.push_back(make_pair(other,id));
                    break;
                }
            }
        }
        if(other<0) {
            printf("-1\n"); return 0;
        }
    }

    for(i=0; i<m; i++) {
        if(!uf.same(u[i],v[i])) {
            uf.unite(u[i],v[i]);
            save.push_back(make_pair(u[i],v[i]));
        }
    }

    gg.resize(n);
    flag.resize(n);
    for(i=0; i<(int)save.size(); i++) {
        int p=save[i].first;
        int q=save[i].second;
        gg[p].push_back(q);
        gg[q].push_back(p);
    }
    flag[0]=1;
    dfs(-1,0);
    
    vector<char> aaa;
    for(i=0; i<n; i++) {
        if(flag[i]==1) aaa.push_back('B');
        else aaa.push_back('W');
    }
    aaa.push_back('\0');
    printf("%s\n", &aaa[0]);

    int mm=1000000000;
    for(i=0; i<m; i++) {
        if(ans[i]<0) printf("%d\n", mm);
        else printf("%d\n", ans[i]);
    }

    return 0;
}


Measured memory_bytes: 23028