Problem F
DRIP
Instead of putting all their profits back into their own operations, some companies use a portion of profits to issue dividends, which are regular or intermittent payments to shareholders. A dividend is specified as an amount of money per share (this amount may be fairly consistent over time, or may change occasionally or frequently). For example, if a company issues a dividend of $\$ 2$ per share, and a particular shareholder owns $125$ shares, then that shareholder will receive $\$ 250.$
Because owning shares in a dividend-paying company can be a good investment, some investors use dividends to purchase more shares of the same company. This is such a common strategy that many investment accounts offer a feature called DRIP, which stands for Dividend Reinvestment Program. When the DRIP feature is turned on, dividends are automatically used to purchase as many shares as possible (of the company that issued the dividend) at the current share price. In the example above, if the share price was $\$ 53$ when the shareholder received a dividend of $\$ 250,$ then DRIP would result in the automatic purchase of $4~ \textrm{shares},$ for total cost of $4 \times \$ 53 = \$ 212,$ leaving the remaining $\$ 250 - \$ 212 = \$ 38$ in the investor’s account. Note that the number of shares purchased by DRIP is always as large as possible, and is always an integer (we don’t consider fractional shares). Also note that a DRIP purchase is made using only the dividend that was just paid; DRIP doesn’t touch any other money that was already in the investor’s account.
On top of the automatic purchase of shares through DRIP, an investor may, of course, use money in their account to purchase additional shares of the same company at any other time. When an investor opts for a non-DRIP transaction like this, we will assume that they always purchase as many shares as possible (again, an integer number of shares). If an investor attempts a non-DRIP transaction when there are insufficient funds in the account, the transaction will be cancelled without penalty.
Given an initial number of shares (of some unspecified dividend-paying company) and an initial amount of money in an investor’s account, together with information about a sequence of DRIP and non-DRIP transactions, determine the number of shares and the amount of money in the investor’s account after the transactions are finished.
Input
The first line of input contains three space-separated integers, $s,$ $m,$ and $n,$ where $s$ $(1 \leq s \leq 10\, 000)$ is the initial number of shares an investor owns, $m$ $(0 \leq m \leq 50\, 000)$ is the initial amount of money (in dollars) in the investor’s account, and $n$ $(1 \leq n \leq 200)$ is the number of transactions. This is followed by $n$ lines, each of which specifies either a DRIP or a non-DRIP transaction. A line for a DRIP transaction contains three space-separated elements, the first of which is the word DRIP, the second of which is an integer, $d$ $(1 \leq d \leq 100),$ the per-share amount of the dividend, and the third of which is an integer, $p$ $(1 \leq p \leq 500),$ the share price at the time of the dividend. A line for a non-DRIP transaction contains two space-separated elements, the first of which is the word NDRIP, and the second of which is an integer, $p$ $(1 \leq p \leq 500),$ the current share price. All per-share dividend amounts and share prices are in dollars. The transactions should be processed in the order given. After each transaction, neither the number of shares nor the amount of money in the investor’s account will exceed $\textrm{exceed}$ $20\, 000\, 000.$
Output
Output two integers on two lines, giving, respectively, the number of shares in the investor’s account and the amount of money (in dollars) in the investor’s account after the transactions are finished.
| Sample Input 1 | Sample Output 1 |
|---|---|
125 300 3 DRIP 2 53 NDRIP 85 NDRIP 84 |
132 83 |
