You are given transaction records for a payment platform. Each record has an id, timestamp, type, from_account, to_account, and amount. All account balances start at 0.
Process transactions in increasing order of timestamp. If multiple transactions have the same timestamp, preserve their original input order.
Rules:
- A deposit adds amount to to_account.
- A transfer normally moves amount from from_account to to_account.
- If a transfer would fail because from_account is short by d, the platform automatically lends exactly d to from_account, and then the transfer succeeds.
- Track the total amount borrowed by each account across all transfers.
Return the final balances and the per-account borrowed totals.
Examples
Input: [{"id": "t1", "timestamp": 1, "type": "deposit", "from_account": "", "to_account": "A", "amount": 50}, {"id": "t2", "timestamp": 2, "type": "transfer", "from_account": "A", "to_account": "B", "amount": 80}, {"id": "t3", "timestamp": 3, "type": "transfer", "from_account": "B", "to_account": "C", "amount": 60}]
Expected Output: ({"A": 0, "B": 20, "C": 60}, {"A": 30, "B": 0, "C": 0})
Explanation: A is short by 30 when sending 80, so the platform lends 30. B later has enough to send 60 to C without borrowing.
Input: [{"id": "t1", "timestamp": 1, "type": "transfer", "from_account": "A", "to_account": "B", "amount": 10}, {"id": "t2", "timestamp": 2, "type": "deposit", "from_account": "", "to_account": "A", "amount": 3}, {"id": "t3", "timestamp": 3, "type": "transfer", "from_account": "A", "to_account": "C", "amount": 5}]
Expected Output: ({"A": 0, "B": 10, "C": 5}, {"A": 12, "B": 0, "C": 0})
Explanation: A borrows 10 for the first transfer, then later borrows 2 more for the second transfer, for a total of 12.
Input: [{"id": "t1", "timestamp": 1, "type": "transfer", "from_account": "A", "to_account": "B", "amount": 5}, {"id": "t2", "timestamp": 1, "type": "transfer", "from_account": "B", "to_account": "C", "amount": 3}]
Expected Output: ({"A": 0, "B": 2, "C": 3}, {"A": 5, "B": 0, "C": 0})
Explanation: Same timestamp: the first transfer is processed first, giving B enough balance to send 3 in the second transfer.
Input: []
Expected Output: ({}, {})
Explanation: Edge case: no transactions means no balances and no borrowing.