import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


class UnionFind:
    def __init__(self, size):
        self.table = [-1 for _ in range(size)]

    def find(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
            return self.table[x]

    def union(self, x, y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            if self.table[s1] <= self.table[s2]:
                self.table[s1] += self.table[s2]
                self.table[s2] = s1
            else:
                self.table[s2] += self.table[s1]
                self.table[s1] = s2
            return True
        return False

    def subsetall(self):
        a = []
        for i in range(len(self.table)):
            if self.table[i] < 0:
                a.append((i, -self.table[i]))
        return a

def main():
    rr = []

    def f(n,m):
        h = [I() for _ in range(n)]
        e = collections.defaultdict(list)
        for _ in range(m):
            a,b,c = LI_()
            c += 1
            e[a].append((b,c))
            e[b].append((a,c))
        hd = collections.defaultdict(list)
        for i in range(n):
            hd[h[i]].append(i)

        us = set()
        uf = UnionFind(n)
        uc = 1
        ek = inf
        for k in sorted(hd.keys(), reverse=True):
            us |= set(hd[k])
            for i in hd[k]:
                for b,c in e[i]:
                    if b not in us:
                        continue
                    if uf.union(i,b):
                        uc += 1
            if uc != len(us):
                ek = k

        mi = min(h)
        if ek == mi:
            return 0
        mk = max(c for c in h if c < ek)

        r = 0
        uf = UnionFind(n)
        us = set([i for i in range(n) if h[i] >= mk])
        el = []
        for i in us:
            for b,c in e[i]:
                if b not in us:
                    continue
                el.append((c,i,b))
        el.sort()
        for c,a,b in el:
            if uf.union(a,b):
                r += c
        for k in sorted([k for k in hd.keys() if k < mk], reverse=True):
            us |= set(hd[k])
            el = []
            for i in hd[k]:
                for b,c in e[i]:
                    if b not in us:
                        continue
                    el.append((c,i,b))
            el.sort()
            for c,a,b in el:
                if uf.union(a,b):
                    r += c
        return r

    while 1:
        n,m = LI()
        if n == 0:
            break
        rr.append(f(n,m))
        # print('rr', rr[-1])

    return '\n'.join(map(str,rr))


print(main())



Measured memory_bytes: 15888