Can anyone tell me approach to solve CHEMFACT (chemical factory) from contest quarantine fun

https://www.codechef.com/QFUN2020/problems/CHEMFACT