#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
const int N = 200005, INF = 1000000000;

int n, m;
int d[N];
vector<pair<int, int> > a[N];

char c[N];
int ans[N];

void dfs(int x)
{
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i].first;
        if (!c[h])
        {
            if (d[h] > d[x])
            {
                c[h] = c[x];
                ans[a[x][i].second] = d[h] - d[x];
                dfs(h);
            }
        }
    }
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &d[i]);
    for (int i = 0; i < m; ++i)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        a[x].push_back(m_p(y, i));
        a[y].push_back(m_p(x, i));
        ans[i] = INF;
        if (d[x] == d[y])
        {
            if (!c[x] && !c[y])
            {
                c[x] = 'W';
                c[y] = 'B';
                ans[i] = d[x];
            }
            else if (!c[y])
            {
                if (c[x] == 'W')
                    c[y] = 'B';
                else
                    c[y] = 'W';
                ans[i] = d[x];
            }
            else if (!c[x])
            {
                if (c[y] == 'W')
                    c[x] = 'B';
                else
                    c[x] = 'W';
                ans[i] = d[x];
            }
        }
    }
    for (int i = 1; i <= n; ++i)
    {
        if (c[i])
            dfs(i);
    }
    for (int i = 1; i <= n; ++i)
    {
        if (!c[i])
        {
            printf("-1\n");
            return 0;
        }
    }
    for (int i = 1; i <= n; ++i)
        printf("%c", c[i]);
    printf("\n");
    for (int i = 0; i < m; ++i)
        printf("%d\n", ans[i]);
    return 0;
}
