import std.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;
    int cost;
    this(Island from, Island to, int cost) {
        this.from = from;
        this.to = to;
        this.cost = cost;
    }
    override int opCmp(Object o) {
        return cost > (cast(Bridge)o).cost;
    }
}

class Island {
    int index;
    int sinkTime;
    Bridge[] bridges;
    override int opCmp(Object o) {
        return sinkTime < (cast(Island)o).sinkTime;
    }
    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[Island] used;
    foreach (island; islands) used[island] = false;
    void dfs(Island c) {
        used[c] = true;
        foreach (bridge; c.bridges) {
            if (!bridge.to.isAlive(t)) continue;
            if (used[bridge.to]) continue;
            dfs(bridge.to);
        }
    }
    dfs(islands[0]);
    foreach (k, v; used) {
        if (!v) return false;
    }
    return true;
}

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

        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;
        foreach (int i, t; events) {
            if (aliveIslands(t).connected(t)) {
                lastTimeIndex = i;
            } else {
                break;
            }
        }

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