#include<bits/stdc++.h>
#define pb           push_back
#define pii          pair<int,int>
#define vi           vector<int>
#define vii         vector<pair<int,int>>
#define mp           make_pair
#define FOR(i, n) for (int i = 0; i <  (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD0(i, n) for (int i = (int)n; i >= 0; i--)
#define FORD1(i, n) for (int i = (int)n; i >= 1; i--)
#define debug(X) { cout << #X << " = " << (X) << endl; }
#define endl '\n'
#define int long long int
using namespace std;
vi adj[(int)2e5];
int dis[(int)2e5];
unordered_map<int, int> ind;
string ans;
int cost[(int)2e5];
bool vis[(int)2e5]= {0};
int has(int x, int y){
    int a= min(x,y);
    int b = max(x,y);
    return a*200000 + b;
}
void dfs(int cur, int P = -1){
    if(vis[cur])return;
//    int cnt =0;
//    debug(cur);
    vis[cur] = true;
    for(int child:adj[cur]){
        if(child == P)continue;
        int in = ind[has(child, cur)];
        if(!vis[child] && dis[child] > dis[cur]){
            ans[child] = ans[cur]; 
            cost[in] = dis[child] - dis[cur];
            dfs(child, cur);
        }else if(!vis[child] && dis[child] == dis[cur]){
            ans[child] = (ans[cur] == 'W') ? 'B' : 'W'; 
            cost[in] = dis[child];
            dfs(child, cur);
        }else if(!vis[child] && dis[child] < dis[cur]){
            cost[in] = 1e9;
        }else cost[in] = 1e9; 
    }
    /*
       while(!pq.empty()){
       cnt++;
       pii temp = pq.top();
       pq.pop();
       cur = temp.second;
       for(int child: adj[cur]){
       if(!vis[child]){//not inside pq
       if(dis[child] == dis[cur]){
       ans[child] = (ans[cur] == 'W') ? 'B' : 'W'; 
       }else if(dis[child] > dis[cur]){

       }   
       }else if(vis[child]){ //inside pq
       cost[ind[hash(child, cur)]] = 1e9;  
       }
       }

       }
       */
    //return cnt;
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int n,m;cin >> n >> m;
    vii info;
    FOR(i, n){
        cin >> dis[i];
        //    info.pb(mp(dis[i], i));
    }
    //sort(info.rbegin(), info.rend());
    int x,y;
    FOR(i,m){
        cin >> x >> y;
        x--, y--;
        adj[x].pb(y);
        adj[y].pb(x);
        ind[has(x,y)] = i;
    }

    ans = "";
    FOR(i, n)ans += 'W';
/*    pii temp = info.back();
    dfs(info.back().second);
    info.pop_back();*/
    //vis[temp.second] = true;
    bool fail =false;
    vi vals;
    FOR(i, n){
        bool found =false;
        bool found2= false;
        for(int child: adj[i]){
            if(dis[child] <= dis[i])found = true;
            if(dis[child] < dis[i])found2 = true;
        }
        if(!found2){
            vals.pb(i); 
        }
        if(!found){
            fail = true;
            break;
        }
    }
    if(fail){
        cout << -1;
        return 0;
    }
    for(int v: vals){
        if(!vis[v])dfs(v);
    }
    cout <<ans<< endl;
    FOR(i, m){
        cout << cost[i] << endl;
    }
    /*
       while(!info.empty()){
       pii temp = info.back();
       info.pop_back();
       bool done =false;
       int cur = temp.second;
       for(int child: adj[cur]){
       if(vis[child] && !done){
       if(dis[child] < dis[cur]){
       ans[cur] = ans[child];
       cost[ind[has(cur, child)]] = dis[cur] - dis[child]; 
       }else{//equal
       ans[cur] = (ans[child] == 'W' ? 'B' : 'W'); 
       cost[ind[has(cur, child)]] = dis[cur]; 
       } 
       done =true;
       }else if(vis[child] && done){
       cost[ind[has(cur, child)]] = 1e9;
       }
       }
// if(!done)
// done may be false
vis[cur] =true;
}*/

}




Measured memory_bytes: 26312