#K43827. Calculate Net Shares
Calculate Net Shares
Calculate Net Shares
An investor carries out a number of transactions buying and selling shares. Each transaction is given as a string in the format action value
, where action
is either "buy" or "sell" and value
is an integer. The net number of shares held is given by the total shares bought minus the total shares sold.
Mathematically, if we denote each transaction as \( (a_i, v_i) \), where \( a_i \) is +1 for a buy and -1 for a sell, then the net shares can be computed as:
\( Net = \sum_{i=1}^{n} a_i \times v_i \)
Your task is to compute and output the net number of shares after processing all the transactions.
inputFormat
The first line contains an integer n (\(0 \leq n \leq 10^5\)), representing the number of transactions. The following n lines each contain a transaction in the format "action value". Here, action
is either "buy" or "sell" and value
is a non-negative integer.
outputFormat
Output a single integer representing the net number of shares held by the investor after processing all transactions.
## sample4
buy 100
sell 30
buy 50
sell 20
100
</p>