This problem can be solved by . For each type , Camila has three choices buy th type, buy th type or skip both. So, if we group each and type, there will be total groups. Now, take groups and buy count as state of dp and in each dp state there will be three transitions as skip this group, greedily buy books from th type and greedily buy books from th type and then return the minimum price among them. Overall complexity will be .