#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (n, m) = (v[0], v[1]);
    let d = read_vec::<i64>();
    let mut edges = vec![vec![]; n];
    for i in 0..m {
        let v = read_vec::<usize>();
        let (u, v) = (v[0] - 1, v[1] - 1);
        edges[u].push((v, i));
        edges[v].push((u, i));
    }
    let mut colored = vec![0; n];
    let mut answers = vec![1000000000i64; m];

    let mut didx = (0..n).map(|x| (d[x], x)).collect::<Vec<_>>();
    didx.sort();

    'outer: for (di, i) in didx {
        let mut found = false;
        for &(to, edge_i) in edges[i].iter() {
            if colored[to] != 0 {
                answers[edge_i] = di;
                colored[i] = -1 * colored[to];
                found = true;
            }
        }
        if found {
            continue;
        }
        for &(to, edge_i) in edges[i].iter() {
            if di == d[to] {
                colored[i] = 1;
                continue 'outer;
            }
        }
        // debug!(di, i, colored);
        println!("-1");
        return;
    }
    for color in colored {
        if color == 1 {
            print!("B");
        } else {
            print!("W");
        }
    }
    println!("");
    for ans in answers {
        println!("{}", ans);
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
