Space Emergency (Small)

Time limit5sMemory limit512 MB

Summary
Place up to two speed boosters that all finish at time t so the flagship moving from star 0 through every star in order reaches star N earliest.
Level

Medium5 of 10

Topics
Brute force, Simulation
Solved
No attempts yet

Problem

There is an emergency in space. You have to send your fleet's flagship from star 0 to star NN as fast as you can, and it passes through every star in between in increasing order of number: 0, then 1, then 2, and so on up to NN. The flagship normally travels at 0.5 parsecs per hour.

Separately from sending the flagship, you can order your engineers to build up to LL speed boosters at distinct stars. One booster takes tt hours to build, and all LL of them are built at the same time. While the flagship travels from a star with a finished booster to the next star, its speed is 1 parsec per hour.

If the booster at a star finishes while the flagship is already on its way from that star to the next one, the flagship speeds up the moment the booster finishes.

If you place the boosters so that the flagship reaches star NN as early as possible, how many hours does the trip take?

Input

The first line contains the number of test cases TT. Each of the next TT lines contains the integers LL, tt, NN and CC, followed by CC integers aia_i, all separated by spaces. aia_i is the distance in parsecs between star k×C+ik \times C + i and star k×C+i+1k \times C + i + 1, and the same value repeats for every integer kk.

For example, with N=8N = 8, C=3C = 3, a0=3a_0 = 3, a1=5a_1 = 5 and a2=4a_2 = 4, the distances between consecutive stars are [3, 5, 4, 3, 5, 4, 3, 5].

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤C≤10001 \le C \le 1000
  • C≤NC \le N
  • 1≤ai≤1041 \le a_i \le 10^4
  • 0≤t≤10110 \le t \le 10^{11}
  • tt is even
  • 1≤N≤10001 \le N \le 1000
  • 0≤L≤20 \le L \le 2

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the number of hours it takes to reach star NN. The answer is guaranteed to be an integer.

Note

Take the case L=1L = 1, t=4t = 4, N=2N = 2 with distances [10, 4]. Build the booster at star 0. After 4 hours the flagship has covered 2 parsecs and the booster is finished. It covers the remaining 8 parsecs at 1 parsec per hour, so it reaches star 1 eight hours later, and since star 1 has no booster it spends another 8 hours on the 4 parsecs to star 2, the destination. The trip takes 20 hours in total.

This problem takes place in a universe where the speed of light is far above 1 parsec per hour, so you do not have to worry about special relativistic effects.

Examples5

  1. Example 1

    Input
    2
    2 20 8 2 3 5
    1 4 2 2 10 4
    
    Expected output
    Case #1: 54
    Case #2: 20
    
  2. Example 2

    Input
    3
    0 0 5 1 7
    0 100000000000 1000 3 1 2 3
    0 2 4 2 6 6
    
    Expected output
    Case #1: 70
    Case #2: 3998
    Case #3: 48
    
  3. Example 3

    Input
    4
    1 0 5 1 7
    2 0 5 1 7
    2 0 3 3 4 9 2
    1 0 1 1 10000
    
    Expected output
    Case #1: 63
    Case #2: 56
    Case #3: 17
    Case #4: 10000
    
  4. Example 4

    Input
    4
    2 16 4 2 3 5
    2 14 4 2 3 5
    1 6 4 2 3 5
    2 0 4 2 3 5
    
    Expected output
    Case #1: 24
    Case #2: 24
    Case #3: 27
    Case #4: 22
    
  5. Example 5

    Input
    3
    1 12 6 6 5 4 3 2 1 6
    2 12 6 6 5 4 3 2 1 6
    2 40 6 6 5 4 3 2 1 6
    
    Expected output
    Case #1: 36
    Case #2: 33
    Case #3: 41