N, m = map(int, input().split())
x = [0] + list(map(int, input().split()))

t = sorted([tuple(map(int, input().split())) for _ in range(m)], key=lambda x: -x[2])

#各ノードの根（-1はそのノードが根）
follow = [-1]*(N+1) 
#（自分を含む）連結している数
num_follower = [1]*(N+1)

def root_index_of(A):
    r = A
    while follow[r] > -1:
        r = follow[r]
    return r
 
def connected(A,B):
    return root_index_of(A) == root_index_of(B)
 
def connect(A,B):
    rA = root_index_of(A)
    rB = root_index_of(B)
    s_rA = x[rA]
    s_rB = x[rB]
    if rA == rB:
        return
    if num_follower[rA] < num_follower[rB]:
        follow[rA] = rB
        follow[A] = rB
        num_follower[rB] += num_follower[rA]
        x[rB] += s_rA
    else:
        follow[rB] = rA
        follow[B] = rA
        num_follower[rA] += num_follower[rB]
        x[rA] += s_rB

count = dict()
for i in range(1, m+1):
    a, b, y = t.pop()
    if not connected(a, b):
        connect(a, b)
    if x[root_index_of(a)] >= y:
        count[root_index_of(a)] = i

if count == dict():
    print(m)
else:
    small_idx = min(count.values())
    print(m-small_idx)
