Problem:
Sale Result

There is data on sales of your company. Your task is to write a program which identifies good workers.

The program should read a list of data where each item includes the employee ID i, the amount of sales q and the corresponding unit price p. Then, the program should print IDs of employees whose total sales proceeds (i.e. sum of p × q) is greater than or equal to 1,000,000 in the order of inputting. If there is no such employees, the program should print "NA". You can suppose that n < 4000, and each employee has an unique ID. The unit price p is less than or equal to 1,000,000 and the amount of sales q is less than or equal to 100,000.

Input

The input consists of several datasets. The input ends with a line including a single 0. Each dataset consists of:

n (the number of data in the list)
i p q
i p q
:
:
i p q

Output

For each dataset, print a list of employee IDs or a text "NA"

Sample Input

4
1001 2000 520
1002 1800 450
1003 1600 625
1001 200 1220
2
1001 100 3
1005 1000 100
2
2013 5000 100
2013 5000 100
0

Output for the Sample Input

1001
1003
NA
2013

Solution in Haskell (judge verdict: Accepted)

import Control.Monad
import Data.Functor
 
import qualified Data.Array as Array

threshold = 1000000
 
main :: IO ()
main = do
    n <- readLn :: IO Int
    if n == 0 then return ()
    else do
        salesData <- replicateM n $ do
            [i, p, q] <- map read <$> words <$> getLine :: IO [Int]
            return (i, p*q)
        let salesArray = Array.accumArray const 0 (1, 4000) []
            loop arr [] b = do
              when (not b) $ putStrLn "NA"
              return ()
            loop arr ((i,s):xs) b = do
              let v' = (arr Array.! i) + s
              if v' >= threshold then do
                print i 
                loop (arr Array.// [(i, minBound)]) xs True
              else 
                loop (arr Array.// [(i, v')]) xs b
        loop salesArray salesData False
        main