Sub-prime
Time limit2sMemory limit512 MB
Given each bank's reserve and a list of debentures between banks, check whether every bank ends with a nonnegative balance after all debentures settle at once.
- Level
Easy2 of 10
- Topics
- Array, Implementation, Simulation
- Solved
- No attempts yet
Problem
One cause of the recent economic crisis is that banks lent money to people who could not repay it, then bundled those loans into debentures and sold them on to other banks. Once the borrowers stopped paying, the whole structure collapsed.
The crisis reached Nlogonia. Prime minister Man Dashuva asked the chairman of the Central Bank for a way out, and the chairman proposed this: if every bank can clear all the debentures it issued using only its own monetary reserve and the debentures owed to it, no bailout is needed.
The number of banks and debentures makes this impossible to check by hand. Given the banks and the debentures they issued, decide whether all debentures can be cleared without a bailout from the Central Bank of Nlogonia.
All debentures settle at the same time. After settlement, bank holds its reserve minus the total value of the debentures it issued, plus the total value of the debentures for which it is the creditor. If that amount is negative for at least one bank, a bailout is necessary. If every bank ends at or more, no bailout is necessary.
Input
The input holds several test cases. The first line of each test case has two integers and separated by a space, the number of banks () and the number of debentures the banks issued (). Banks are numbered from to . The second line has integers separated by spaces, where is the monetary reserve of bank (). Each of the next lines has three integers separated by spaces: , the debtor bank (), , the creditor bank ( and ), and , the debenture value ().
Several debentures may connect the same pair of banks.
The end of the input is a line holding only two zeros separated by a space. Do not process that line.
Output
For each test case print one line holding a single character: S if all debentures can be cleared without a bailout from the Central Bank of Nlogonia, or N if a bailout is necessary.