Help me to optimize the code?Kindly suggest some idea

I solved this problem but this took more time…kindly help me to optimize this…


from itertools import permutations
n=int(input())
a1=list(map(int,input().split()))
l = list(permutations(range(0,n)) )
pp=0
ind=0
for i in range(len(l)):
    powp=0
    a=a1[:]
    lf=1
    lr=1
    for j in range(n):
         ppl=1
         ppr=1
         curr=l[i][j]
         ppc=a[curr]
         if True:
             left=curr-1
             while left>=0 and lf==1:
                 if a[left]==0:
                     left-=1
                 else:
                     break
             if left>=0 and left<n:
                 if a[left]!=0:
                     ppl=a[left]
             else:
                 lf=0
             right=curr+1
             while right<n and lr==1:
                 if a[right]==0:
                     right+=1
                 else:
                     break
             if right<n and right>=0:
                 if a[right]!=0:
                     ppr=a[right]
             else:
                 lr=0
         powp+=(ppl*ppr*ppc)
         a[curr]=0
    if powp>pp:
        pp=max(pp,powp)
        ind=i
print(pp)
print(ind)
print(l[i])
                
        
             
         

Question link?

2 Likes

bro that contest ended…Our clg platform…