<p>You are given a <strong>1</strong><strong>-indexed</strong> array <code>nums</code>. Your task is to select a <strong>complete subset</strong> from <code>nums</code> where every pair of selected indices multiplied is a <span data-keyword="perfect-square">perfect square,</span>. i. e. if you select <code>a<sub>i</sub></code> and <code>a<sub>j</sub></code>, <code>i * j</code> must be a perfect square.</p>

<p>Return the <em>sum</em> of the complete subset with the <em>maximum sum</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [8,7,3,5,7,2,4,9]</span></p>

<p><strong>Output:</strong> <span class="example-io">16</span></p>

<p><strong>Explanation:</strong></p>

<p>We select elements at indices 2 and 8 and 2<code>&nbsp;* 8</code>&nbsp;is a perfect square.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [8,10,3,8,1,13,7,9,4]</span></p>

<p><strong>Output:</strong> <span class="example-io">20</span></p>

<p><strong>Explanation:</strong></p>

<p>We select elements at indices 1, 4, and 9. <code>1 * 4</code>, <code>1 * 9</code>, <code>4 * 9</code> are perfect squares.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>


class UnionFind:
    def __init__(self, size):
        self.root = [i for i in range(size)]
        self.rank = [1] * size

    def find(self, x):
        if x == self.root[x]:
            return x
        self.root[x] = self.find(self.root[x])
        return self.root[x]

    def union(self, x, y):
        rootX = self.find(x)
        rootY = self.find(y)
        if rootX != rootY:
            if self.rank[rootX] > self.rank[rootY]:
                self.root[rootY] = rootX
            elif self.rank[rootX] < self.rank[rootY]:
                self.root[rootX] = rootY
            else:
                self.root[rootY] = rootX
                self.rank[rootX] += 1
            return True
        else:
            return False

    def connected(self, x, y):
        return self.find(x) == self.find(y)
    
uf = UnionFind(10000)
d = {"":0}
factors = [[] for _ in range(10000)]
seen = [False for _ in range(10000)]

class Solution:
    def maximumSum(self, nums: List[int]) -> int:
        
        if seen[1]==False:
            
            for i in range(1,10000):
                if seen[i]==False:
                    for j in range(i+1,10001,i+1):
                        temp = j
                        seen[j-1]=True
                        num = 0
                        while temp%(i+1)==0:
                            num+=1
                            temp//=(i+1)
                        if num%2:
                            if len(factors[j-1])==0:
                                factors[j-1] = [str(i+1)]
                            else:
                                factors[j-1].append(str(i+1))
            for i in range(10000):
                temps = "-".join(factors[i])
                if temps in d:
                    uf.union(i,d[temps])
                else:
                    d[temps] = i
                    
        sms = [0]*10000
        n = len(nums)
        for i in range(n):
            sms[uf.find(i)]+=nums[i]
        return max(sms)
285