class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param tickets int整型vector 
     * @param k int整型 
     * @return int整型
     */
    int timeRequiredToBuy(vector<int>& tickets, int k) {
        int t = 0;
        queue<int> q;
        queue<int> r;
        int n = tickets.size();
        for (int j = 0; j < n; j++)
        {
            q.push(tickets[j]);
            r.push(j);
        }  
        while (1)
        {
            t++;
            if (q.front() > 1)
            {
                q.push(q.front() - 1);
                q.pop();
                r.push(r.front());
                r.pop();
            }
            else if (r.front() == k) break;
            else
            {
                q.pop();
                r.pop();
            }
        }
        return t;
        // write code here
    }
};