Problem:
Score : 900 points

Problem Statement

We have a connected undirected graph with N vertices and M edges.
Edge i in this graph (1 \leq i \leq M) connects Vertex U_i and Vertex V_i bidirectionally.
We are additionally given N integers D_1, D_2, ..., D_N.

Determine whether the conditions below can be satisfied by assigning a color - white or black - to each vertex and an integer weight between 1 and 10^9 (inclusive) to each edge in this graph.
If the answer is yes, find one such assignment of colors and integers, too.

There is at least one vertex assigned white and at least one vertex assigned black.

For each vertex v (1 \leq v \leq N), the following holds.

The minimum cost to travel from Vertex v to a vertex whose color assigned is different from that of Vertex v by traversing the edges is equal to D_v.

Here, the cost of traversing the edges is the sum of the weights of the edges traversed.

Constraints

2 \leq N \leq 100,000

1 \leq M \leq 200,000

1 \leq D_i \leq 10^9

1 \leq U_i, V_i \leq N

The given graph is connected and has no self-loops or multiple edges.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
D_1 D_2 ... D_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

If there is no assignment satisfying the conditions, print a single line containing -1.

If such an assignment exists, print one such assignment in the following format:

S
C_1
C_2
\vdots
C_M

Here,

the first line should contain the string S of length N. Its i-th character (1 \leq i \leq N) should be W if Vertex i is assigned white and B if it is assigned black.

The (i + 1)-th line (1 \leq i \leq M) should contain the integer weight C_i assigned to Edge i.

Sample Input 1

5 5
3 4 3 5 7
1 2
1 3
3 2
4 2
4 5

Sample Output 1

BWWBB
4
3
1
5
2

Assume that we assign the colors and integers as the sample output, and let us consider Vertex 5, for example. To travel from Vertex 5, which is assigned black, to a vertex that is assigned white with the minimum cost, we should make these moves: Vertex 5 \to Vertex 4 \to Vertex 2. The total cost of these moves is 7, which satisfies the condition. We can also verify that the condition is satisfied for other vertices.

Sample Input 2

5 7
1 2 3 4 5
1 2
1 3
1 4
2 3
2 5
3 5
4 5

Sample Output 2

-1

Sample Input 3

4 6
1 1 1 1
1 2
1 3
1 4
2 3
2 4
3 4

Sample Output 3

BBBW
1
1
1
2
1
1

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

// https://atcoder.jp/contests/keyence2020/tasks/keyence2020_e
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

const INF: i64 = 1000000000;

fn solve(d: Vec<(i64, usize)>, graph: Vec<Vec<(usize, usize)>>, m: usize) -> Option<(Vec<char>, Vec<i64>)> {
    let n = graph.len();
    if d[0].0 != d[1].0 {
        return None;
    }

    let mut node_value = vec![0; n];
    for i in 0..n {
        node_value[d[i].1] = d[i].0;
    }

    let mut color = vec![' '; n];
    let mut edge_len = dvec![0; m];

    let root_value = d[0].0;
    let root_black = d[0].1;
    let root_white = d[1].1;
    color[root_black] = 'B';
    color[root_white] = 'W';

    // debug!(root_white, root_black);

    let mut found_first = false;
    for &(to, eid) in &graph[root_black] {
        if to == root_white {
            found_first = true;
            edge_len[eid] = d[0].0;
            break;
        }
    }
    if !found_first {
        return None;
    }
    for i in 2..n {
        let val = d[i].0;
        let id = d[i].1;
        let mut best = (INF, 0);
        for &(to, eid) in &graph[id] {
            if val < node_value[to] {
                continue;
            }
            if best.0 >= node_value[to] {
                best = (node_value[to], to);
            }
        }
        if best.0 == INF {
            return None;
        }

        let mut ev = 0;
        let tt = best.1;
        if best.0 == val {
            ev = node_value[tt];
            if color[tt] == ' ' {
                color[tt] = 'B';
                color[id] = 'W';
            } else if color[tt] == 'B' {
                color[id] = 'W';
            } else {
                color[id] = 'B';
            }
        } else {
            ev = val - node_value[tt];
            color[id] = color[tt];
        }
        // debug!(id, best, val, ev);


        for &(to, eid) in &graph[id] {
            if best.1 == to {
                edge_len[eid] = ev;
            } else if val >= node_value[to] {
                edge_len[eid] = INF;
            }
        }
    }

    // debug!(edge_len);

    for i in 0..m {
        if edge_len[i] <= 0 {
            return None;
        }
    }
    Some((color, edge_len))
}

fn main() {
    input! {
        n: usize, m: usize,
        d: [i64; n],
        edges: [(usize1, usize1); m]
    };

    let mut g = vec![vec![]; n];
    for i in 0..m {
        let u = edges[i].0;
        let v = edges[i].1;
        g[u].push((v, i));
        g[v].push((u, i));
    }

    let mut d = d.into_iter().zip(0..n).collect::<Vec<_>>();
    d.sort();

    let out = stdout();
    let mut out = BufWriter::new(out.lock());

    if let Some(ans) = solve(d, g, m) {
        writeln!(out, "{}", join!(ans.0, ""));
        for i in 0..m {
            writeln!(out, "{}", ans.1[i]);
        }
    } else {
        writeln!(out, "-1");
    }
}