A university research office tracks n labs that share a common grant pool. Each lab starts the funding cycle with its own allocation, which may already be positive or negative (a lab can begin a cycle already carrying forward debt from a prior one). Over the course of the cycle, labs transfer money directly to one another to cover shared equipment and staff costs: a transfer records the paying lab, the receiving lab, and the amount moved. After every transfer has been applied — the paying lab's balance decreases by the amount and the receiving lab's balance increases by it — the office needs to know exactly which labs ended the cycle in deficit (a final balance strictly below zero), so it can flag them for audit.
n m, the number of labs and the number of transfers.n lines each contain labId name balance: a distinct integer lab identifier, the lab's name (a non-empty string of letters, digits, and underscores, at most 20 characters, no spaces), and its integer starting balance.m lines each contain fromLab toLab amount: the identifiers of the paying lab and the receiving lab (both guaranteed to be identifiers listed above, with fromLab != toLab), and the positive integer amount transferred.First print a single integer k, the number of labs whose final balance is strictly less than zero. Then print k lines, one per such lab sorted by increasing labId, each containing labId name balance where balance is that lab's final balance.
Example 1
Input
3 3 1 Helios 500 2 Zenith -200 3 Vantage 100 1 2 300 2 3 150 3 1 50
Expected
1 2 Zenith -50
Explanation
Starting balances are Helios=500, Zenith=-200, Vantage=100. Transfer 1->2 of 300 makes Helios=200, Zenith=100. Transfer 2->3 of 150 makes Zenith=-50, Vantage=250. Transfer 3->1 of 50 makes Vantage=200, Helios=250. Final balances are Helios=250, Zenith=-50, Vantage=200, so only Zenith is in deficit: the output is 1 followed by "2 Zenith -50".
Example 2
Input
2 1 1 Alpha 1000 2 Beta 500 1 2 300
Expected
0
Explanation
Starting balances are Alpha=1000, Beta=500. After the transfer of 300 from Alpha to Beta, Alpha=700 and Beta=800. Neither balance is negative, so no lab is in deficit and the output is just 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →