implement the topK algorithm