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


        }
        return 0;
    }
};