import std.stdio;
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;
    }
    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) used[island.index] = false;
    void dfs(Island c) {
        used[c.index] = true;
        foreach (bridge; c.bridges) {
            if (!bridge.to.isAlive(t)) continue;
            if (used[bridge.to.index]) continue;
            dfs(bridge.to);
        }
    }
    dfs(islands[0]);
    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.sort;
        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;
        }

        Island[] aliveIslands(int t) {
            Island[] alive;
            foreach (island; islands) {
                if (island.isAlive(t)) {
                    alive ~= island;
                }
            }
            return alive;
        }

        int lastTimeIndex = 0;
        int lb = 0, ub = cast(int)events.length;
        while (lb + 1 < ub) {
            int mid = (lb + ub) / 2;
            int t = events[mid];
            if (aliveIslands(t).connected(t)) {
                lb = mid;
            } else {
                ub = mid;
            }
        }
        lastTimeIndex = ub;

        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;
    }
}