/**
 * Created by hiroqn on 13/10/10.
 */
var fs = require('fs'),
    length = fs.fstatSync(process.stdin.fd).size,
    buffer = new Buffer(length),
    bytesRead = fs.readSync(process.stdin.fd, buffer, 0, length, 0),
    input = buffer.toString('utf8', 0, bytesRead).split('\n');
main();

function UF(n) {//union find
  var i,
      par = [];
  for (i = 0; i < n; i++) {
    par[i] = -1;
  }
  function union(x, y) {
    x = find(x);
    y = find(y);
    if (x !== y) {
      if (par[x] < par[y]) {
        par[x] += par[y];
        par[y] = x;
      } else {
        par[y] += par[x];
        par[x] = y;
      }
    }
    return x !== y;
  }

  function find(x) {
    return par[x] < 0 ? x : par[x] = find(par[x]);
  }

  function count(using) {
    var c = 0;
    for (i = 0; i < n; i++) {
      if (using[i] && par[i] < 0) {
        c++
      }
    }
    return c;
  }

  function same(x, y) {
    return find(x) === find(y);
  }

  return {
    union: union,
    find: find,
    count: count,
    same: same
  };
}
function main() {
  var temp, n, m,
      i, j, k,
      h,
      edges, e,
      uf,
      res,
      use;
  while (input[0]!=='0 0') {
    temp = input.shift().split(' ');
    n = +temp[0];
    m = +temp[1];

    if (m === 0) {
      break;
    }
    h = [];
    use = [];
    for (i = 0; i < n; i++) {
      h[i] = {
        node: i,
        days: +input.shift()
      };
      use[i] = false;
    }
    edges = [];

    for (i = 0; i < m; i++) {
      temp = input.shift().split(' ');
      edges[i] = [temp[0] - 1, temp[1] - 1, temp[2] - 0];
    }
    h.sort(function (a, b) {
      return a.days - b.days;
    });
    edges.sort(function (e1, e2) {
      return e1[2] - e2[2];
    });
    uf = UF(n);
    res = 0;
    for (i = n - 1; i >= 0; i--) {
      use[h[i].node] = true;
      while (i > 0 && h[i].days === h[i - 1].days) {
        i--;
        use[h[i].node] = true;
      }
      //kruskal
      for (j = 0; j < m; j++) {
        e = edges[j];
        if (use[e[0]] && use[e[1]] && !uf.same(e[0], e[1])) {
          uf.union(e[0], e[1]);
          res += e[2];
        }
      }
      if (uf.count(use) > 1) {
        res = 0;
        uf = UF(n);
      }
    }
    console.log(res);
  }
}