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 Ruby (judge verdict: Accepted)

def input_data_sets
  data_sets = []
  
  while data_count = gets.strip.to_i
    return data_sets if data_count.nil? || data_count.to_i == 0
    data_sets << input_data_set(data_count)
  end
  
  data_sets
end

def input_data_set(data_count)
  data_count.times.map{ gets.strip.split }
end

class Employee
  RENDERABLE_THRESHOLD = 1_000_000
  
  attr_reader :id
  
  def initialize(__id)
    @id = __id
    @sales = 0
  end
  
  def add_sales(unit_value, count)
    @sales += unit_value.to_i * count.to_i
  end
  
  def renderable?
    @sales >= RENDERABLE_THRESHOLD
  end
  
  def to_s
    id.to_s
  end
end

class EmployeesContainer
  def initialize(date_set)
    @employees = []
    date_set.each do |data|
      get_employee(data[0]).add_sales(*data[1, 2])
    end
  end
  
  def get_employee(id)
    @employees.find{ |e| e.id == id } || add_employee(id)
  end
  
  def add_employee(id)
    employee = Employee.new(id)
    @employees << employee
    employee
  end
  
  def add_sales(id, unit_value, count)
    get_employee(id).add_sales(unit_value, count)
  end
  
  def render_to_string
    renderable_employees = @employees.select(&:renderable?)
    if renderable_employees.empty?
      "NA"
    else
      renderable_employees.map(&:to_s).join("\n")
    end
  end
end

data_sets = input_data_sets
=begin
data_sets = [
  #4
  [
    %w(1001 2000 520),
    %w(1002 1800 450),
    %w(1003 1600 625),
    %w(1001 200 1220)
  ],
  #2
  [
    %w(1001 100 3),
    %w(1005 1000 100),
  ],
  #2
  [
    %w(2013 5000 100),
    %w(2013 5000 100),
  ],
  #0
]
=end

data_sets.each do |data_set|
  puts EmployeesContainer.new(data_set).render_to_string
end