Dynamic Programming Problem

Problem :- https://open.kattis.com/problems/orders
I get that the solution for this problem is based on DP.
But the problem is when the answer is Ambiguous , since 4 4 5 and 5 4 4 are not considered as different , i am stuck on this case .
How to solve above problem ?