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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;

public class Program
{
    private int N, M;
    private int[] D;
    private List<E>[] Edge;

    private HashSet<E>[] nokosu;
    private char[] S;
    private int[] C;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        M = sc.NextInt();
        D = sc.IntArray();
        Edge = new List<E>[N];
        for (int i = 0; i < N; i++)
        {
            Edge[i] = new List<E>();
        }

        for (int i = 0; i < M; i++)
        {
            int u = sc.NextInt() - 1;
            int v = sc.NextInt() - 1;
            Edge[u].Add(new E(u, v, i));
            Edge[v].Add(new E(v, u, i));
        }

        /*
         * 
         * 
         * D[u] uと繋がってる任意の頂点v
         * D[u] < D[v] なら-1
         *
         * uから一番近い頂点 パス u,u_2...u_k
         * 
         * k == 2
         * 一つのvが u[k]になる
         * D[v] <= D[u]
         * 
         * else
         * 一つのvが u_2になる
         * u ~ u_k > u_2 ~ u_k
         *
         * uに繋がってる D[v]が最小のvの辺を残す
         *
         * u,vは別色 　辺の長さは D[u]
         *
         * 消した辺は適当に大きい数を入れる
         * 
         */
        S = new char[N];
        C = new int[M];

        for (int i = 0; i < M; i++)
        {
            C[i] = 1000000000;
        }

        nokosu = new HashSet<E>[N];
        for (int i = 0; i < N; i++)
        {
            nokosu[i] = new HashSet<E>();
        }

        for (int i = 0; i < N; i++)
        {
            int min = int.MaxValue;
            E edge = new E(-1, -1, -1);
            foreach (E e in Edge[i])
            {
                if (D[e.V] < min)
                {
                    edge = e;
                    min = D[e.V];
                }
            }

            if (D[i] < min)
            {
                Console.WriteLine("-1");
                return;
            }

            C[edge.Index] = D[i];

            nokosu[edge.U].Add(edge);
            nokosu[edge.V].Add(new E(edge.V, edge.U, edge.Index));
        }

        for (int i = 0; i < N; i++)
        {
            if (S[i] == default(char))
            {
                Search(i, -1, 'W');
            }
        }

        Console.WriteLine(new string(S));
        Console.WriteLine(string.Join("\n", C));
    }

    private void Search(int i, int parent, char col)
    {
        S[i] = col;
        foreach (E e in nokosu[i])
        {
            if (e.V == parent) continue;
            Search(e.V, i, col == 'B' ? 'W' : 'B');
        }
    }


    public static void Main(string[] args)
    {
        new Program().Solve();
    }
}

struct E
{
    public int U, V;
    public int Index;

    public E(int u, int v, int i)
    {
        U = u;
        V = v;
        Index = i;
    }
}

class Scanner
{
    public Scanner()
    {
        _pos = 0;
        _line = new string[0];
    }

    const char Separator = ' ';
    private int _pos;
    private string[] _line;

    #region スペース区切りで取得

    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = Console.ReadLine().Split(Separator);
            _pos = 0;
        }

        return _line[_pos++];
    }

    public int NextInt()
    {
        return int.Parse(Next());
    }

    public long NextLong()
    {
        return long.Parse(Next());
    }

    public double NextDouble()
    {
        return double.Parse(Next());
    }

    #endregion

    #region 型変換

    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }

    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }

    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }

    #endregion

    #region 配列取得

    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = Console.ReadLine().Split(Separator);

        _pos = _line.Length;
        return _line;
    }

    public int[] IntArray()
    {
        return ToIntArray(Array());
    }

    public long[] LongArray()
    {
        return ToLongArray(Array());
    }

    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }

    #endregion
}