Souvenirs

No attempts yetTime limit1sMemory limit256 MB

Problem

Buy souvenirs from merchants in order using gold coins (worth gg silver) and silver coins. Merchants round change in silver packages as greedy, honest, or generous. Generous merchants require exact silver payment when possible; otherwise pay one gold coin. Other merchants accept exact silver or one gold coin. Maximize purchases.

Input

gg, cc, nn, then nn lines with merchant type, package size pip_i, and price sis_i.

Output

Maximum number of souvenirs you can buy.