Given a large dataset, how would you optimize a script to reduce execution time?
Implement top_k_frequent(data, k) as a concrete version of the task. Return up to k values with the highest frequencies. Ties are selected by first appearance in data, and the returned values are sorted in ascending order. Use an algorithm that avoids sorting every distinct value when k is small.
Input: a list of integers and an integer k. Output: a list of integers.
def top_k_frequent(data, k):