All problems

Problem 898

Time Needed to Buy Tickets

Easy · 10 pts · Array, Queue, Simulation

Problem

People stand in a queue; person i wants to buy TICKETS[i] tickets. Each second the person at the front buys one ticket and, if they still need more, goes to the back of the queue; otherwise they leave. Find how many seconds it takes person K (0-based) to finish buying. Each person ahead of K (or K itself) buys at most TICKETS[K] tickets in that time, and each person behind buys at most TICKETS[K] - 1.

Input

The first line contains N and K. The second line contains the N ticket counts.

Output

Print the time when person K finishes.

Constraints

1 <= N <= 100; 1 <= TICKETS[i] <= 100; 0 <= K < N

Example input

3 2
2 3 2

Example output

6

Explanation

With tickets 2 3 2 and K = 2, the purchases go 0, 1, 2, 0, 1, 2, and person 2 finishes at second 6.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Number of Students Unable to Eat Lunch Next: Dota2 Senate