use std::collections::{BinaryHeap, VecDeque};

const INF: i64 = 1e9 as i64;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

    let n: usize = sc.read();
    let m: usize = sc.read();
    let d: Vec<i64> = sc.vec(n);
    let mut graph = vec![vec![]; n];

    for i in 0..m {
        let u = sc.read::<usize>() - 1;
        let v = sc.read::<usize>() - 1;
        graph[u].push((v, i));
        graph[v].push((u, i));
    }

    let mut color = vec![0; n];
    let mut edge_cost = vec![INF; m];
    let min_d = *d.iter().min().unwrap();
    let mut heap = BinaryHeap::new();
    for i in 0..n {
        if d[i] != min_d {
            continue;
        }
        if color[i] == 0 {
            color[i] = 1;
        }
        for &(next, edge) in graph[i].iter() {
            if d[next] != min_d {
                continue;
            }
            if color[next] == color[i] {
                continue;
            }
            if color[next] == 0 {
                color[next] = if color[i] == 1 { 2 } else { 1 };
            }
            edge_cost[edge] = min_d;
        }
        heap.push((-d[i], i));
    }
    for i in 0..n {
        assert!(color[i] == 0 || d[i] == min_d);
        assert_ne!(color[i] == 0, d[i] == min_d);
    }

    while let Some((_, v)) = heap.pop() {
        for &(next, edge) in graph[v].iter() {
            if color[next] != 0 {
                continue;
            }

            // assert!(d[v] <= d[next]);
            color[next] = if color[v] == 1 { 2 } else { 1 };
            assert_eq!(edge_cost[edge], INF);
            edge_cost[edge] = d[next];
            heap.push((-d[next], next));
        }
    }

    for i in 0..n {
        let min_cost = graph[i]
            .iter()
            .filter(|&&(next, _)| color[i] != color[next])
            .map(|&(_, edge)| edge_cost[edge])
            .min()
            .unwrap();
        if min_cost != d[i] {
            println!("-1");
            return;
        }
    }

    for i in 0..n {
        if color[i] == 1 {
            sc.write("B");
        } else {
            assert_eq!(color[i], 2);
            sc.write("W");
        }
    }
    sc.write("\n");

    for i in 0..m {
        sc.write(edge_cost[i]);
        sc.write("\n");
    }
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
