#C11069. Bank Account Transactions Processing

    ID: 40344 Type: Default 1000ms 256MiB

Bank Account Transactions Processing

Bank Account Transactions Processing

You are given a series of banking transactions. Each transaction is represented as a string in one of the following formats:

  • D <account_number> <amount> for a deposit operation.
  • W <account_number> <amount> for a withdrawal operation.

For each transaction, update the balance of the corresponding account according to the following rules:

  • If the operation is a deposit (D), simply add the amount to the account balance.
  • If the operation is a withdrawal (W), subtract the amount from the account balance only if the account has sufficient funds. Otherwise, the withdrawal is ignored.

The final balances of the accounts must be output in ascending order of their account numbers.

Mathematically, if an account i starts with a balance of \( B_i \) and undergoes a series of deposits and withdrawals, its final balance is computed as:

\[ B_i = \sum_{j\in D_i} a_j - \sum_{k\in W_i} a_k \]

where \( D_i \) and \( W_i \) represent the sets of deposit and withdrawal transactions, respectively, and withdrawals that exceed the current balance are ignored.

inputFormat

The input is provided via standard input (stdin) and consists of multiple lines. The first line contains an integer T denoting the number of transactions. Each of the following T lines contains a transaction in the format described above.

Example:

6
D 1001 5000
D 1002 3000
W 1001 2000
D 1001 1000
W 1002 1000
W 1003 500

outputFormat

Output the final balance for each account to standard output (stdout). Each line of the output must contain two space-separated integers: the account number and its final balance. The accounts must be printed in ascending order by account number.

Example:

1001 4000
1002 2000
1003 0
## sample
6
D 1001 5000
D 1002 3000
W 1001 2000
D 1001 1000
W 1002 1000
W 1003 500
1001 4000

1002 2000 1003 0

</p>