Let the number of edges of the polygons be e1,e2,…,en. It’s easy to find that ei has to be a multiple of ei−1, thus we can rewrite e as e1⋅1,e1⋅k2,…,e1⋅kn. Hence if we know the number of edges of the first polygon, all we left if to find the longest sequence k1=1,k2,k3,…,kn such that ki is a multiple if ki−1 and ∑iki=K.
Note that k2,k3,…,kn are all multiple of k2, so if we divide them by k2 we get a sequence starting with 1 again! This means we get a smaller subproblem and we can use dynamic programming to solve it: let dpi be the length of the longest such sequence described above which sums to i. Since we can get a longer sequence by multiplying a shorter one by a constant and prepending a 1, so the transition is:
dpk⋅i+1:=max(dpk⋅i+1,dpi+1),k=2,3,…