def monotone_minima(N,cost,cnt=0,inf=1<<60): dp=[inf]*(N+1) dp[0]=0 if cnt: for _ in range(cnt): prev=dp dp=[inf]*(N+1) idx=[None]*(N+1) dp[0]=0 idx[0]=0 for n in range(N+1): if prev[n]+cost(n,N)