#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair <int , int> pii;

const int N = 2e5+10 , mod = 1e9+7;

int32_t main(){
   ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
   int n , m;
   cin >> n >> m;
   vector <pii> vec;
   for(int i = 0 ; i < n ; i++){
      int x;
      cin >> x;
      vec.push_back({x , i});
   }
   sort(vec.begin() , vec.end());
   int mn[n];
   memset(mn , 63 , sizeof(mn));
   vector <pii> g[n];
   for(int i = 0 ; i < m ; i++){
      int u , v;
      cin >> u >> v;
      u--; v--;
      g[u].push_back({v , i});
      g[v].push_back({u , i});
   }
   int ans[m];
   bool mark[n];
   memset(mark , 0 , sizeof(mark));
   int col[n];
   memset(col , 0 , sizeof(col));
   for(pii x : vec){
      int v = x.second;
      for(pii y : g[v]){
         int u = y.first;
         if(mark[u]){
            if(mn[v] < mod){
               if(mn[u] > mod)
                  col[u] = 1 - col[v];
            }
            else{
               col[v] = 1 - col[u];
            }
            mn[u] = min(mn[u] , x.first);
            mn[v] = min(mn[v] , x.first);
            ans[y.second] = x.first;
         }
      }
      mark[x.second] = true;
   }
   for(pii x : vec)
      if(mn[x.second] != x.first)
         return cout << -1 , 0;
   for(int i = 0 ; i < n ; i++)
      cout << ((col[i] == 0) ? "B" : "W");
   cout << "\n";
   for(int i = 0 ; i < m ; i++)
      cout << ans[i] << "\n";
   return 0;
}
