<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>

from typing import List
import math

class Solution:
    def maximumSum(self, nums: List[int]) -> int:
        n = len(nums)
        
        # Helper function to find the root of a node
        def find(x):
            if parent[x] != x:
                parent[x] = find(parent[x])
            return parent[x]
        
        # Helper function to union two nodes
        def union(x, y):
            rootX = find(x)
            rootY = find(y)
            if rootX != rootY:
                parent[rootY] = rootX
        
        # Initialize union-find structure
        parent = list(range(n + 1))
        
        # Iterate over all pairs (i, j) to find perfect square products
        for i in range(1, n + 1):
            for k in range(1, int(math.sqrt(n // i)) + 1):
                j = k * k * i
                if j <= n:
                    union(i, j)
        
        # Calculate the sum of each connected component
        component_sum = {}
        for i in range(1, n + 1):
            root = find(i)
            if root not in component_sum:
                component_sum[root] = 0
            component_sum[root] += nums[i - 1]
        
        # Return the maximum sum of any component
        return max(component_sum.values())

# Example usage:
# sol = Solution()
# print(sol.maximumSum([8,7,3,5,7,2,4,9]))  # Output: 16
# print(sol.maximumSum([8,10,3,8,1,13,7,9,4]))  # Output: 20
366