import std.stdio;
import std.conv;
import std.c.stdio;
import std.array;
import std.algorithm;
import std.range;
import std.functional;

class UFTree {
    int[] parent;
    this(ulong size) {
        parent = new int[size];
        parent[] = -1;
    }
    int root(int k) {
        if (parent[k] >= 0) {
            return parent[k] = root(parent[k]);
        } else {
            return k;
        }
    }
    void unite(int x, int y) {
        x = root(x);
        y = root(y);
        parent[x] = y;
    }
    bool equivalent(int x, int y) {
        return root(x) == root(y);
    }
}

class Bridge {
    Island from;
    Island to;
    ulong cost;
    this(Island from, Island to, ulong cost) {
        this.from = from;
        this.to = to;
        this.cost = cost;
    }
}

class Island {
    int index;
    int sinkTime;
    Bridge[] bridges;
    this(int index, int sinkTime) {
        this.index = index;
        this.sinkTime = sinkTime;
    }
    override string toString() {
        return "Island " ~ index.to!string;
    }
    bool isAlive(int t) {
        return t <= sinkTime;
    }
}

bool connected(Island[] islands, int t = 0) {
    if (islands.empty) return false;
    bool[int] used;
    foreach (island; islands) 
        if (island.isAlive(t)) 
            used[island.index] = false;
    void dfs(Island c) {
        used[c.index] = true;
        foreach (bridge; c.bridges) {
            if (! (bridge.from.isAlive(t) && bridge.to.isAlive(t)) ) continue;
            if (used[bridge.to.index]) continue;
            dfs(bridge.to);
        }
    }
    foreach (island; islands) {
        if (island.isAlive(t)) {
            dfs(island);
            break;
        }
    }
    foreach (k, v; used) {
        if (!v) return false;
    }
    return true;
}

void main() {
    int N, M;
    while (scanf("%d %d\n", &N, &M), N || M) {
        Island[] islands;
        int[] events = [0];
        foreach (i; 0 .. N) {
            int h; scanf("%d\n", &h);
            islands ~= new Island(i, h);
            events ~= h;
        }
        events = events.sort.uniq.array;
        //events.writeln;
        foreach (i; 0 .. M) {
            int a, b; ulong c; scanf("%d %d %d\n", &a, &b, &c);
            a--; b--;
            islands[a].bridges ~= new Bridge(islands[a], islands[b], c); 
            islands[b].bridges ~= new Bridge(islands[b], islands[a], c);
        }

        if (!islands.connected) {
            writeln(0);
            continue;
        }

        int lastTimeIndex = 0;
        while (lastTimeIndex < events.length && islands.connected(events[lastTimeIndex])) {
            lastTimeIndex++;
        }

        ulong ans = 0;
        auto uftree = new UFTree(islands.length);
        Bridge[] bridges;
        foreach (island; islands) {
            bridges ~= island.bridges;
        }
        bridges.sort!((a, b) => a.cost < b.cost);
        //bridges.map!(a => a.cost).writeln;;
        foreach (t; events[0 .. lastTimeIndex].reverse) {
            foreach (bridge; bridges) {
                if (! (bridge.to.isAlive(t) && bridge.from.isAlive(t)) ) continue;
                if (uftree.equivalent(bridge.to.index, bridge.from.index)) continue;
                uftree.unite(bridge.to.index, bridge.from.index);
                ans += bridge.cost;
            }
        }
        ans.writeln;
    }
}

Measured memory_bytes: 15788