This page is still under construction.

Parts of this page are still being built. What you see may change.

Sub-prime

Time limit2sMemory limit512 MB

Summary
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 ii holds its reserve RiR_i 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 00 or more, no bailout is necessary.

Input

The input holds several test cases. The first line of each test case has two integers BB and NN separated by a space, the number of banks (1≤B≤201 \le B \le 20) and the number of debentures the banks issued (1≤N≤201 \le N \le 20). Banks are numbered from 11 to BB. The second line has BB integers RiR_i separated by spaces, where RiR_i is the monetary reserve of bank ii (0≤Ri≤1040 \le R_i \le 10^4). Each of the next NN lines has three integers separated by spaces: DD, the debtor bank (1≤D≤B1 \le D \le B), CC, the creditor bank (1≤C≤B1 \le C \le B and D≠CD \ne C), and VV, the debenture value (1≤V≤1041 \le V \le 10^4).

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.

Examples1

  1. Example 1

    Input
    3 3
    1 1 1
    1 2 1
    2 3 2
    3 1 3
    3 3
    1 1 1
    1 2 1
    2 3 2
    3 1 4
    3 3
    1 1 1
    1 2 2
    2 3 2
    3 1 2
    0 0
    
    Expected output
    S
    N
    S