Maximum Value

Time limit5sMemory limit128 MB

Summary
Given bounds and a fixed sum constraint on real numbers, compute the maximum possible sum of their p-th powers using a convexity-based extremal argument.
Level

Hard8 of 10

Topics
Math, Greedy, Combinatorics
Solved
No attempts yet

Problem

Let x1,x2,…,xmx_1, x_2, \dots, x_m be real numbers that, for some integers aa and bb with a>0a > 0, satisfy the following conditions:

  1. −1a≤xi≤a-\dfrac{1}{\sqrt{a}} \le x_i \le \sqrt{a} for every ii;
  2. x1+x2+⋯+xm=bax_1 + x_2 + \dots + x_m = b\sqrt{a}.

Given an even positive integer pp, write a program that finds the maximum possible value of x1p+x2p+⋯+xmpx_1^p + x_2^p + \dots + x_m^p.

Input

The first line contains the number of test cases TT. Each test case consists of a single line containing mm, pp, aa, and bb (m≤2000m \le 2000, p≤12p \le 12, and pp is even).

Only inputs for which real numbers x1,x2,…,xmx_1, x_2, \dots, x_m satisfying the conditions exist are given.

Output

For each test case, print on its own line the maximum value of the expression, rounded to the nearest integer (rounded at the first digit after the decimal point).

Examples3

  1. Example 1

    Input
    2
    1997 12 3 -318
    10 2 4 -1
    
    Expected output
    189548
    6
    
  2. Example 2

    Input
    1
    1 2 1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    1
    5 4 3 5
    
    Expected output
    45