Codeforces Round 1086 (Div. 2)
B
Cyclists
Codeforces
1100
原题
← AC →
翻译中…

Bob likes to play an interesting tower defense game on his mobile phone. In the game, he must play cards to defeat his opponents!

翻译中…

There are $n$ cards placed in a queue called a deck. At any moment, Bob is only able to play the cards that are currently placed in the first $k$ positions in the deck. In each turn, Bob selects a card placed in the first $k$ positions, removes it from the deck, plays it, and then places the same card back at the bottom of the deck. In other words, in each turn an element from the first $k$ elements in the queue is selected, moved to the end of the queue, and all elements placed after it are moved one index to the front.

翻译中…

One card is called the win-condition, and Bob wants to play it as many times as possible. However, each card also has a cost needed to play. The $i$-th card (initially placed at the $i$-th position) costs Bob $a_i$ energy every time it is played. The total cost of cards played must not exceed $m$. Initially, the win-condition card is placed at the $p$-th place in the queue.

翻译中…

You need to find out the maximum number of times the win-condition card can be played, ensuring that the total cost does not exceed $m$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.

翻译中…

The first line of each test case contains four integers $n,k,p,m$ ($1\le k,p\le n\le 5000$, $1\le m\le 5000$), denoting the number of cards, the number of cards that are playable at a time, the initial position of the win-condition, and the total energy available.

翻译中…

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le m$) denoting the cost of each card.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$.

翻译中…

Output

翻译中…

For each test case, output one integer in one line, denoting the maximum times the win-condition can be played.

翻译中…

Note

翻译中…

In the first test case, we can only play the first card in the deck, and playing it will use all the energy. Since the win-condition is the second card, we can't play it before energy runs out. Therefore, the answer is $0$.

翻译中…

In the second test case, we can play all the cards in the deck. The optimal strategy is obviously only playing the win-condition. Since it costs $1$ energy to play and we have $6$ energy in total, we can play it $6$ times before energy runs out. Therefore, the answer is $6$.

翻译中…

In the third test case, we can play cards as follows: (the win condition is colored red)

翻译中…

The initial deck is $[2,\color{red}{1},2]$.

翻译中…

Play the first card: The deck becomes $[\color{red}{1},2,2]$.

翻译中…

Play the first card again: The deck becomes $[2,2,\color{red}{1}]$.

翻译中…

Play the first card once again: The deck becomes $[2,\color{red}{1},2]$.

翻译中…

Play the second card: The deck becomes $[2,2,\color{red}{1}]$.

翻译中…

In the process, we have played the win-condition for $2$ times, and we used $6$ energy in total. It can be shown that no strategy allows us to play the win-condition more than $2$ times with no more than $6$ points of energy; therefore, the answer is $2$.

翻译中…

In the fourth test case, it can be shown that we can play the win-condition no more than once. This is achievable by always playing the $4$-th card in the deck. Therefore, the answer is $1$.

翻译中…

样例

Input #1
4
2 1 2 42
42 1
3 3 2 6
2 1 2
3 2 2 6
2 1 2
8 4 7 10
3 4 4 2 1 1 4 2
Output #1
0
6
2
1
InputCopy OutputCopy
题目信息
题号
2208B
时间限制
1 s
内存限制
256 MB
标签
暴力
博弈论
贪心
模拟
数学
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。