all_data = readlines our_price = all_data[0].to_i other_price = all_data[1].to_i each_loss = our_price + other_price booking_num = all_data[2].to_i booked_rooms = all_data.shift(2) duplicated_booked_rooms = booked_rooms.group_by{|i| i}.reject{|k,v| v.one?}.keys duplicated_booked_rooms.each do |room_num| num_of_duplicating = booked_rooms.count(room_num) total_loss += (num_of_duplicating - 1) * each_loss end print total_loss