There are people in a line buying tickets, where the person at index is at the front of the line and index is at the back of the line. Every person (0-indexed) would like to buy tickets.
Each person takes exactly second to buy a ticket, and only one ticket at a time. After purchasing a ticket, they have to go to the end of the line (which takes seconds) to buy more tickets. A person leaves the line entirely when they have no more tickets to buy. Find the time it takes for the person initially at position (0-indexed) to finish buying tickets.
Input Specification
The first line of input will contain two space-separated integers and , representing the number of people in the line and the initial index of the person.
The second line of input will contain space-separated integers , representing the number of tickets each person would like to buy.
Output Specification
Output one integer, representing the time it takes for the person originally at to finish buying their tickets.
Constraints
Sample Input 1
4 0
5 1 1 1
Sample Output 1
8
Sample Explanation
The queue starts at person , which moves to the back of the line after 1 second (array: ).
After another seconds, the array will be (the person still needs to purchase 4 tickets).
After another seconds, the array will be empty. In total, it took seconds.
Sample Input 2
3 2
2 3 2
Sample Output 2
6
Comments