The Circle

Time limit1sMemory limit128 MB

Summary
Choose n positive sector values (each at least k) so that consecutive circular block sums cover every integer from m to i, maximizing i.
Level

Hard8 of 10

Topics
Brute force, Combinatorics, Math, Implementation
Solved
No attempts yet

Problem

A circle is divided into nn sectors (1≤n≤61 \le n \le 6). You place one positive integer in each sector; every value must be at least kk.

A number is reachable if it equals the value of a single sector, or the sum of the values of two or more consecutive sectors along the circle. Because the sectors form a circle, a block of consecutive sectors may wrap around from the last sector back to the first. In total there are n(n−1)+1n(n-1)+1 distinct blocks: the nn single sectors, the blocks of length 2,3,…,n−12, 3, \dots, n-1, and the one whole circle.

Choose the sector values so that the reachable numbers contain every integer of the unbroken run m,m+1,m+2,…,im, m+1, m+2, \dots, i, and make the largest value ii as large as possible.

For example, with n=5n = 5, m=2m = 2, k=1k = 1 the arrangement (1,3,10,2,5)(1, 3, 10, 2, 5) around the circle makes every integer from 11 to 2121 reachable, so i=21i = 21.

Input

Three integers nn, mm, and kk (1≤n≤61 \le n \le 6, 1≤m≤201 \le m \le 20, 1≤k≤201 \le k \le 20), given in this order and separated by whitespace or newlines.

It is guaranteed that k≤mk \le m, so the value mm is always reachable (place a sector equal to mm).

Output

Print a single integer: the largest ii such that every integer from mm to ii inclusive is reachable.

Hint

Consider the example n=5n = 5, m=2m = 2, k=1k = 1 with the circular arrangement (1,3,10,2,5)(1, 3, 10, 2, 5). Summing every block of consecutive sectors (wrapping around when needed) yields all integers from 11 to 2121:

  • length 1: 1, 3, 10, 2, 51,\ 3,\ 10,\ 2,\ 5
  • length 2: 1+3=4, 3+10=13, 10+2=12, 2+5=7, 5+1=61+3=4,\ 3+10=13,\ 10+2=12,\ 2+5=7,\ 5+1=6
  • length 3: 1+3+10=14, 3+10+2=15, 10+2+5=17, 2+5+1=8, 5+1+3=91+3+10=14,\ 3+10+2=15,\ 10+2+5=17,\ 2+5+1=8,\ 5+1+3=9
  • length 4: 1+3+10+2=16, 3+10+2+5=20, 10+2+5+1=18, 2+5+1+3=11, 5+1+3+10=191+3+10+2=16,\ 3+10+2+5=20,\ 10+2+5+1=18,\ 2+5+1+3=11,\ 5+1+3+10=19
  • whole circle: 1+3+10+2+5=211+3+10+2+5=21

Every value in [2,21][2, 21] appears, so the answer for this case is i=21i = 21.

Examples3

  1. Example 1

    Input
    5
    2
    1
    
    Expected output
    21
    
  2. Example 2

    Input
    1
    5
    1
    
    Expected output
    5
    
  3. Example 3

    Input
    2
    1
    1
    
    Expected output
    3