#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param tickets int整型一维数组 
# @param k int整型 
# @return int整型
#
class Solution:
    def timeRequiredToBuy(self , tickets: List[int], k: int) -> int:
        # write code here
        queue = []
        n = len(tickets)
        for i in range(n):
            queue.append((i, tickets[i]))
        time = 0
        while queue:
            idx, t = queue.pop(0)
            time += 1            
            if t > 1:
                # 如果票大于1,减1后放回队尾
                queue.append((idx, t - 1))
            # 如果移除的是目标元素,结束循环
            if idx == k and t == 1:
                break
        return time