Problem:
Score : 600 points

Problem Statement

You are given a forest with N vertices and M edges. The vertices are numbered 0 through N-1.
The edges are given in the format (x_i,y_i), which means that Vertex x_i and y_i are connected by an edge.

Each vertex i has a value a_i.
You want to add edges in the given forest so that the forest becomes connected.
To add an edge, you choose two different vertices i and j, then span an edge between i and j.
This operation costs a_i + a_j dollars, and afterward neither Vertex i nor j can be selected again.

Find the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Constraints

1 ≤ N ≤ 100,000

0 ≤ M ≤ N-1

1 ≤ a_i ≤ 10^9

0 ≤ x_i,y_i ≤ N-1

The given graph is a forest.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
a_0 a_1 .. a_{N-1}
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Sample Input 1

7 5
1 2 3 4 5 6 7
3 0
4 0
1 2
1 3
5 6

Sample Output 1

7

If we connect vertices 0 and 5, the graph becomes connected, for the cost of 1 + 6 = 7 dollars.

Sample Input 2

5 0
3 1 4 1 5

Sample Output 2

Impossible

We can't make the graph connected.

Sample Input 3

1 0
5

Sample Output 3

0

The graph is already connected, so we do not need to add any edges.

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using static System.Convert;
using static System.Math;
using static Constants;
using static Extensions;
using static MathExtensions;

public static class Program
{
    public static void Solve()
    {
        var n = I;
        var m = I;
        var a = Repeat(n, () => L).ToArray();
        var uf = new UnionFind(n);

        Repeat(m, () =>
        {
            var x = I;
            var y = I;
            uf.Unite(x, y);
        });

        var dic = new Dictionary<int, BinaryHeap<long>>();

        Repeat(n, i =>
        {
            var g = uf.Find(i);
            if (!dic.ContainsKey(g)) dic[g] = new BinaryHeap<long>();
            dic[g].Enqueue(a[i]);
        });

        if (dic.Count == 1) Answer(0);

        var ans = dic.Values.Select(pq => pq.Dequeue()).Sum();
        var cnt = 2;
        var noel = dic.Count * 2 - 2;

        while (cnt < noel)
        {
            var min = long.MaxValue;
            var mini = -1;

            foreach (var pq in dic)
            {
                if (pq.Value.Any() && pq.Value.Peek() < min)
                {
                    min = pq.Value.Peek();
                    mini = pq.Key;
                }
            }

            if (mini == -1) Answer(Impossible);
            ans += dic[mini].Dequeue();
            cnt++;
        }

        Answer(ans);
    }

    #region Scanners
    static TextScanner _ts;
    static char C => char.Parse(_ts.Next());
    static string S => _ts.Next();
    static int I => int.Parse(_ts.Next());
    static long L => long.Parse(_ts.Next());
    static BigInteger B => BigInteger.Parse(_ts.Next());
    static double D => double.Parse(_ts.Next());
    static decimal M => decimal.Parse(_ts.Next());
    #endregion

    public static void Main()
    {
        var sw = new StreamWriter(Console.OpenStandardOutput());
        sw.NewLine = "\n";
#if DEBUG
        sw.AutoFlush = true;
#else
        sw.AutoFlush = false;
#endif
        Console.SetOut(sw);
        _ts = new TextScanner(Console.In);
        Solve();
        Console.Out.Flush();
    }
}

public static partial class Extensions
{
}

#region Library
public interface IPriorityQueue<T>
{
    int Count { get; }
    bool Any();
    void Clear();
    void Enqueue(T item);
    T Dequeue();
    T Peek();
}

public class BinaryHeap<T> : IPriorityQueue<T>
{
    private readonly IComparer<T> _comparer;
    private readonly List<T> _t;

    public BinaryHeap() : this(Comparer<T>.Default) { }

    public BinaryHeap(IComparer<T> comparer)
    {
        _t = new List<T>();
        _comparer = comparer;
    }

    public BinaryHeap(int capacity) : this(capacity, Comparer<T>.Default) { }

    public BinaryHeap(int capacity, IComparer<T> comparer)
    {
        _t = new List<T>(capacity);
        _comparer = comparer;
    }

    public int Count { get; private set; }

    public int Capacity => _t.Capacity;

    public bool Any() => this.Count > 0;

    public void Clear()
    {
        _t.Clear();
        this.Count = 0;
    }

    public void TrimExcess() => _t.TrimExcess();

    public void Enqueue(T item)
    {
        var i = this.Count++;
        while (i > 0)
        {
            var p = (i - 1) >> 1;
            if (_comparer.Compare(_t[p], item) <= 0) break;
            Set(i, _t[p]);
            i = p;
        }
        Set(i, item);
    }

    public T Dequeue()
    {
        if (this.Count == 0) throw new InvalidOperationException();
        var ret = _t[0];
        var x = _t[--this.Count];
        var i = 0;
        while ((i << 1) + 1 < this.Count)
        {
            var a = (i << 1) + 1;
            var b = (i << 1) + 2;
            if (b < this.Count && _comparer.Compare(_t[b], _t[a]) < 0) a = b;
            if (_comparer.Compare(_t[a], x) >= 0) break;
            _t[i] = _t[a];
            i = a;
        }
        _t[i] = x;
        return ret;
    }

    public T Peek()
    {
        if (this.Count == 0) throw new InvalidOperationException();
        return _t[0];
    }

    private void Set(int i, T value)
    {
        if (i < _t.Count) _t[i] = value;
        else if (i == _t.Count) _t.Add(value);
        else throw new InvalidOperationException();
    }
}

public class UnionFind
{
    private readonly int[] _tree;

    public UnionFind(int length)
    {
        _tree = Enumerable.Repeat(-1, length).ToArray();
    }

    public bool Unite(int i, int j)
    {
        i = Find(i);
        j = Find(j);
        if (i == j) return false;
        if (_tree[i] > _tree[j]) { var t = i; i = j; j = t; }
        _tree[i] += _tree[j];
        _tree[j] = i;
        return true;
    }

    public bool AreUnited(int i, int j) => Find(i) == Find(j);

    public int Count(int i) => -_tree[Find(i)];

    public int CountGroups() => _tree.Count(x => x < 0);

    public int Find(int i) => _tree[i] < 0 ? i : _tree[i] = Find(_tree[i]);
}

public class TextScanner
{
    private readonly TextReader _tr;

    public TextScanner(TextReader tr)
    {
        _tr = tr;
    }

    public string Next()
    {
        var sb = new StringBuilder();
        int i;
        do
        {
            i = _tr.Read();
            if (i == -1) throw new EndOfStreamException();
        }
        while (char.IsWhiteSpace((char)i));
        while (i != -1 && !char.IsWhiteSpace((char)i))
        {
            sb.Append((char)i);
            i = _tr.Read();
        }
        return sb.ToString();
    }
}

public static class Constants
{
    public const int AnswerToLifeTheUniverseAndEverything = 42;
    public const string Yes = nameof(Yes);
    public const string No = nameof(No);
    public const string YES = nameof(YES);
    public const string NO = nameof(NO);
    public const string Possible = nameof(Possible);
    public const string Impossible = nameof(Impossible);
}

[DebuggerStepThrough]
public static partial class Extensions
{
    public static void Answer(string str)
    {
        Console.WriteLine(str);
        Exit(0);
    }

    public static void Answer(object obj) => Answer(obj.ToString());

    public static void Assert(bool condition)
    {
        if (!condition) throw new Exception("Assertion failed");
    }

    public static string AsString(this IEnumerable<char> source) => new string(source.ToArray());

    public static Dictionary<T, int> Bucket<T>(this IEnumerable<T> source) where T : IEquatable<T>
    {
        var dict = new Dictionary<T, int>();
        foreach (var item in source) if (dict.ContainsKey(item)) dict[item]++; else dict[item] = 1;
        return dict;
    }

    public static int[] Bucket<T>(this IEnumerable<T> source, int maxValue, Func<T, int> selector)
    {
        var arr = new int[maxValue + 1];
        foreach (var item in source) arr[selector(item)]++;
        return arr;
    }

    public static IEnumerable<int> CumSum(this IEnumerable<int> source)
    {
        var sum = 0;
        foreach (var item in source) yield return sum += item;
    }

    public static IEnumerable<long> CumSum(this IEnumerable<long> source)
    {
        var sum = 0L;
        foreach (var item in source) yield return sum += item;
    }

    public static void Exit(int exitCode)
    {
        Console.Out.Flush();
        Environment.Exit(exitCode);
    }

    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        foreach (var item in source) action(item);
    }

    public static void ForEach<T, _>(this IEnumerable<T> source, Func<T, _> func)
    {
        foreach (var item in source) func(item);
    }

    public static void ForEach<T>(this IEnumerable<T> source, Action<T, int> action)
    {
        var i = 0;
        foreach (var item in source) action(item, i++);
    }

    public static void ForEach<T, _>(this IEnumerable<T> source, Func<T, int, _> func)
    {
        var i = 0;
        foreach (var item in source) func(item, i++);
    }

    public static void Repeat(int count, Action action)
    {
        for (var i = 0; i < count; i++) action();
    }

    public static void Repeat(int count, Action<int> action)
    {
        for (var i = 0; i < count; i++) action(i);
    }

    public static IEnumerable<T> Repeat<T>(int count, Func<T> func)
    {
        for (var i = 0; i < count; i++) yield return func();
    }

    public static IEnumerable<T> Repeat<T>(int count, Func<int, T> func)
    {
        for (var i = 0; i < count; i++) yield return func(i);
    }

    public static void Swap<T>(ref T x, ref T y)
    {
        var tmp = x; x = y; y = tmp;
    }
}

[DebuggerStepThrough]
public static class MathExtensions
{
    public static int DivCeil(int left, int right)
        => left / right + (left % right == 0 ? 0 : 1);

    public static long DivCeil(long left, long right)
        => left / right + (left % right == 0L ? 0L : 1L);

    public static int Gcd(int left, int right)
    {
        int r;
        while ((r = left % right) != 0) { left = right; right = r; }
        return right;
    }

    public static long Gcd(long left, long right)
    {
        long r;
        while ((r = left % right) != 0L) { left = right; right = r; }
        return right;
    }

    public static BigInteger Gcd(BigInteger left, BigInteger right)
        => BigInteger.GreatestCommonDivisor(left, right);

    public static int HighestOneBit(int x)
    {
        x |= x >> 01;
        x |= x >> 02;
        x |= x >> 04;
        x |= x >> 08;
        x |= x >> 16;
        return x - (x >> 1);
    }

    public static long HighestOneBit(long x)
    {
        x |= x >> 01;
        x |= x >> 02;
        x |= x >> 04;
        x |= x >> 08;
        x |= x >> 16;
        x |= x >> 32;
        return x - (x >> 1);
    }

    public static int Lcm(int left, int right) => left / Gcd(left, right) * right;

    public static long Lcm(long left, long right) => left / Gcd(left, right) * right;

    public static BigInteger Lcm(BigInteger left, BigInteger right)
        => left / Gcd(left, right) * right;

    public static int Pow(int value, int exponent)
    {
        var r = 1;
        while (exponent > 0)
        {
            if ((exponent & 1) == 1) r *= value;
            value *= value;
            exponent >>= 1;
        }
        return r;
    }

    public static long Pow(long value, int exponent)
    {
        var r = 1L;
        while (exponent > 0)
        {
            if ((exponent & 1) == 1) r *= value;
            value *= value;
            exponent >>= 1;
        }
        return r;
    }

    public static long Fact(int value)
    {
        var r = 1L;
        for (var i = 2; i <= value; i++) r *= i;
        return r;
    }
}
#endregion