#if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>
#include <cmath>

#include <queue>
#include <vector>
#include <functional>
#include <utility>
#include <algorithm>
#include <iterator>
#include <assert.h>

auto& in = std::cin;
auto& out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;


template<typename T, typename U>
std::enable_if_t<std::rank<T>::value == 0> fill_all(T& arr, const U& v) {
    arr = v;
}
template<typename ARR, typename U>
std::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR& arr, const U& v) {
    for (auto& i : arr) {
        fill_all(i, v);
    }
}

constexpr int64_t MAX = 1000000000;
int32_t N, M;
int64_t D[200000];
std::pair<int64_t, int> Dsort[200000];
int64_t COST[230000];
struct edge {
    uint32_t to;
    int32_t i;
    edge() {}
    edge(uint32_t to_, int32_t i_)
        :to(to_), i(i_) {}
};
std::vector<edge> graph[200000];


char color[200000] = {};

char notColor(char b) {
    if (b == 'W') {
        return 'B';
    }
    return 'W';
}

int main()
{
    using std::endl;
    in.sync_with_stdio(false);
    out.sync_with_stdio(false);
    in.tie(nullptr);
    out.tie(nullptr);

    in >> N >> M;
    for (int32_t i = 0; i < N; i++)
    {
        in >> D[i];
    }
    for (int32_t i = 0; i < M; i++)
    {
        uint32_t from, to;
        in >> from >> to; --from; --to;
        graph[from].emplace_back(to, i);
        graph[to].emplace_back(from, i);
    }
    fill_all(color, '\0');

    for (int32_t i = 0; i < N; i++)
    {
        Dsort[i].first = D[i]; Dsort[i].second = i;
    }
    std::sort(Dsort, Dsort + N);

    fill_all(COST, MAX);
    for (int32_t SORT = 0; SORT < N; SORT++)
    {
        auto p = Dsort[SORT].second;
        if (color[p] != '\0') {
            continue;
        }
        
        for (auto& n : graph[p])
        {
            if (D[n.to] == D[p]) {
                color[p] = notColor(color[n.to]);
                assert(color[n.to] == '\0' || color[n.to] == notColor(color[p]));
                color[n.to] = notColor(color[p]);
                assert(COST[n.i] == MAX || COST[n.i] == D[p]);
                COST[n.i] = D[p];
                break;
            }
        }
        if (color[p] != '\0') {
            continue;//ok
        }
        for (auto& n : graph[p])
        {
            if (D[n.to] < D[p]) {
                assert(color[n.to] != '\0');
                color[p] = (color[n.to]);
                assert(COST[n.i] == MAX || COST[n.i] == D[p] - D[n.to]);
                COST[n.i] = D[p] - D[n.to];
                break;
            }
        }
        if (color[p] == '\0') {
            out << -1 << endl;
            return 0;
        }
    }

    out << color << '\n';
    for (size_t i = 0; i < M; i++)
    {
        out << COST[i] << '\n';
    }

    return 0;
}
#endif


Measured memory_bytes: 18688