from collections import defaultdict
import random
def sumdisttest(arr):
n = len(arr)
res = 0
for i in range(n):
ctr = set()
for j in range(i, n):
ctr.add(arr[j])
res += len(ctr)
return res
def sumdist(arr):
n = len(arr)
last = defaultdict(lambda: -1)
res = 0
for i in range(n):
res += (i - last[arr[i]]) * (n - i)
last[arr[i]] = i
return res
for _ in range(10):
arr = random.choices(range(1, 51), k = 1000)
print("PASS" if sumdist(arr) == sumdisttest(arr) else "FAIL")
ZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgZGVmYXVsdGRpY3QKaW1wb3J0IHJhbmRvbQoKZGVmIHN1bWRpc3R0ZXN0KGFycik6CiAgICBuID0gbGVuKGFycikKICAgIHJlcyA9IDAKICAgIGZvciBpIGluIHJhbmdlKG4pOgogICAgICAgIGN0ciA9IHNldCgpCiAgICAgICAgZm9yIGogaW4gcmFuZ2UoaSwgbik6CiAgICAgICAgICAgIGN0ci5hZGQoYXJyW2pdKQogICAgICAgICAgICByZXMgKz0gbGVuKGN0cikKICAgIHJldHVybiByZXMKICAgIApkZWYgc3VtZGlzdChhcnIpOgogICAgbiA9IGxlbihhcnIpCiAgICBsYXN0ID0gZGVmYXVsdGRpY3QobGFtYmRhOiAtMSkKICAgIHJlcyA9IDAKICAgIGZvciBpIGluIHJhbmdlKG4pOgogICAgICAgIHJlcyArPSAoaSAtIGxhc3RbYXJyW2ldXSkgKiAobiAtIGkpCiAgICAgICAgbGFzdFthcnJbaV1dID0gaQogICAgcmV0dXJuIHJlcwoKZm9yIF8gaW4gcmFuZ2UoMTApOgogICAgYXJyID0gcmFuZG9tLmNob2ljZXMocmFuZ2UoMSwgNTEpLCBrID0gMTAwMCkKICAgIHByaW50KCJQQVNTIiBpZiBzdW1kaXN0KGFycikgPT0gc3VtZGlzdHRlc3QoYXJyKSBlbHNlICJGQUlMIik=