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

    }
};