We’re preparing your current view and syncing the latest data.
There is a queue for tickets at a cinema. The tickets are sold one at a time. Each person needs to buy a certain number of tickets, and the tickets are sold in order from the front of the queue to the back. After a person buys one ticket, if they still need more tickets, they go to the end of the queue. Given an array where each element represents the number of tickets a person needs, and an index representing your position in the queue, return the total time taken for you to buy all your tickets. Each ticket purchase takes exactly 1 unit of time.
An array
clike
ticketsclike
tickets[i]clike
kAn integer representing the total time taken for the person at index
clike
k1 <= tickets.length <= 100 1 <= tickets[i] <= 100 0 <= k < tickets.length
Example 1
Input
tickets = [2, 3, 2], k = 2
Output
6
Explanation
The order of ticket purchase is as follows: First person buys 1 ticket, second person buys 1 ticket, third person buys 1 ticket (your turn), first person buys the 2nd ticket and leaves, second person buys the 2nd ticket, third person buys the 2nd ticket (your turn). Total time is 6.