I first wrote a program using defaultdict but later on i changes it to a 2d list for optimization purpose. I used the same indexing in both of them but the values at same index became different some how.[I know my claim sounds odd but pls take a look at the code]:-
‘’’
from collections import *
n=int(input())
li=list(map(int,input().split()))
co=Counter(li)
el=sorted(list(set(li)))
mx=len(el)-1
di=[[-1](mx+1)](mx+1)
dd=defaultdict(lambda:-1)
def disr(mi,mx):
if mi>=mx:
return 0
x=di[mi][mx]
y=dd[(mi,mx)]
if x!=y: #INSPITE OF ASSINING SAME VALUE di[mi][mx] becomes different from dd[(mi,mx)] IN SOME CASES
print('different',x,y,mi,mx)
if (x!=-1):
return x
a=el[mx]
b=el[mi]
d=a-b
ans=min((d*co[a]+disr(mi,mx-1)),(d*co[b]+disr(mi+1,mx)))
dd[(mi,mx)] = di[mi][mx] = ans #SAME VALUES ASSIGNED TO di AND dd
return ans
print(disr(0,mx))
‘’’
use any input like:-
5
1 2 3 4 5