Can someone suggest to me how to reduce time complexity for the solution…?

class Solution:

# @param A : list of integers

# @param B : list of integers

# @return a list of integers

def solve(self, A, B):

G = []

n = len(A)

for i in range(n):

for j in range(i+1, n+1):

G.append(max(A[i:j]))

# print(A[i:j])

# print(G)

mod = 1000000007

for i in range(len(G)):

f = 1

j = 1

while(j<=math.sqrt(G[i])):

if G[i]%j == 0:

if G[i]/j==j:

f *= j
else:
f = f*j*(G[i]/j)

j += 1

G[i] = f%mod

# print(G)

G.sort(reverse = True)

# print(G)

ans = []

for i in B:

ans.append(G[i-1])

return ans