G - Garlands

08.01.2010
TrudnośćTrudnośćTrudnośćTrudność

Time limit: 30 s
Memory limit: 32 MB

Garland decorator is a profession which recently gained in importance, especially during Christmas time. Any kid can decorate a christmas tree, any parent can put gifts in sockets, and even anyone can start believing in Santa Claus, but hanging christmas garlands is a completely different story. As you will learn, it is an extremely important, responsible and tough job.

A garland consists of $ n $ pieces of equal length. Due to decorations like christmas balls attached to garlands, piece $ i $ has its own weight $ w_i $. The garland has to be attached to the ceiling in $ m $ spots, where the very beginning of the garland should be attached to spot $ 1 $ and its end to spot $ m $. The garland should also be hooked to the remaining spots, which divides it into segments, each consisting of several consecutive pieces. There are, however, several rules that every respectable garland decorator should keep in mind.

  1. Each segment should contain a positive even number of pieces. Due to this condition, we may divide a segment into two half-segments.
  2. To minimize the chance that a guest hits your precious garland with their head (and tears it into pieces), the garland cannot hang too low: each half-segment can contain at most $ d $ pieces.
  3. Finally, to keep the ceiling from falling on people heads, the decorator should minimize the weight of the heaviest half-segment.
An example of an optimally hanging garland (consisting of twelve pieces in three segments) is presented below; weights of respective pieces are given in circles.

Multiple Test Cases

The input contains several test cases. The first line of the input contains a positive integer $ Z \leq 100 $, denoting the number of test cases. Then $ Z $ test cases follow, each conforming to the format described in section Single Instance Input. For each test case, your program has to write an output conforming to the format described in section Single Instance Output.

Single Instance Input

The description of each garland consists of two lines. The first line describing a particular garland contains three positive integers $ n $, $ m $, and $ d $ ($ 1\leq n\leq 40000 $, $ 2\leq m\leq 10000 $, $ 1\leq d \leq 10000 $) separated by single spaces and described above. The second line contains $ n $ positive integers $ w_1 $, $ w_2 $, ..., $ w_n $ ($ 1\leq w_i \leq 10000 $), being the weights of the corresponding pieces.

Single Instance Output

For each garland, your program should output a single line containing one integer, being the weight of the heaviest half-segment in an optimal attachment of the garland. If it is not possible to hang the garland satisfying conditions (i) and (ii), then your program should output word BAD.

Example

Input

4
4 3 10
10 10 20 20
6 4 10
1 1 100 100 1 1 
6 3 10
1 1 100 100 1 1 
1 2 2
333

Output

20
100
200
BAD
Nie możesz wysyłać i oglądać rozwiązań tego zadania ponieważ nie jesteś zalogowany. Zaloguj się lub załóż konto.
0
Twoja ocena: Brak

Copyright © 2008-2010 Wrocławski Portal Informatyczny

design: rafalpolito.com