/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class EBichromization {
public:

    vector<P> graph[100005];
    // 1: white, -1: black, 0: undefined
    vector<int> w;
    vector<int> cost;

    void solve(istream& cin, ostream& cout) {
      SPEED;
      int n, m; cin >> n >> m;
      w.resize(n);
      cost.resize(m);
      vector<int> d(n);
      // id -> u, v
      vector<P> edge(m);

      rep(i, n) {
        cin >> d[i];
      }

      priority_queue<P, vector<P>, greater<>> que;

      rep(i, m) {
        int u, v; cin >> u >> v;
        u--; v--;
        graph[u].emplace_back(v, i);
        graph[v].emplace_back(u, i);
        edge[i].first = u;
        edge[i].second = v;

        if (d[u] == d[v]) {
          que.emplace(d[u], i);
        }
      }

      while(!que.empty()) {
        auto p = que.top(); que.pop();
        ll c = p.first;
        ll i = p.second;
        ll u = edge[p.second].first;
        ll v = edge[p.second].second;

        priority_queue<P, vector<P>, greater<>> que2;
        if (w[u] == 0) {
          que2.emplace(d[u], u);
        }
        if (w[v] == 0) {
          que2.emplace(d[v], v);
        }

        if (w[u] == 0 && w[v] == 0) {
          w[u] = 1;
          w[v] = -1;
          cost[i] = c;
        } else if (w[u] == 0 && w[v] != 0) {
          w[u] = -w[v];
          cost[i] = c;
        } else if (w[v] == 0 && w[u] != 0) {
          w[v] = -w[u];
          cost[i] = c;
        } else if (w[u] == w[v]) {
          // cut
          cost[i] = 1e+9;
        }

        while(!que2.empty()) {
          P pa = que2.top();
          que2.pop();
          ll c = pa.first;
          ll u = pa.second;
          for (auto e: graph[u]) {
            if (w[e.first] == 0 && d[e.first] > c) {
              cost[e.second] = d[e.first] - c;
              w[e.first] = w[u];
              que2.emplace(d[e.first], e.first);
            }
          }
        }

      }

      rep(i, n) {
        if (w[i] == 0) {
          cout << -1 << endl;
          return;
        }
      }

      rep(i, n) {
        if (w[i] == 1) {
          cout << 'W';
        } else {
          cout << 'B';
        }
      }
      cout << endl;
      rep(i, m) {
        if (cost[i] == 0) {
          cost[i] = 1e+9;
        }
        cout << cost[i] << '\n';
      }


    }
};

signed main() {
  EBichromization solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}

Measured memory_bytes: 20208