Under construction

Still being built. Things may change

Stock Trading

Time limit1sMemory limit128 MB

Summary
Given known daily prices for n stocks over D days, starting capital C, and at most t buy or sell actions, maximize final cash.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy, Math, Implementation
Solved
No attempts yet

Problem

Stock trading is a great way to make money if you think the high-stakes tables in Las Vegas are not risky enough for you. The idea is simple: buy stock when the price is low, and sell when it is high. The only catch is that you do not know whether the price will go up or down next. But if you did, the problem would be much easier. Here we assume all prices are known in advance and explore how to find an optimal investment strategy in retrospect.

There are nn stocks in total and DD days. On each day you are given the price of every stock ii. You start with a capital of CC dollars. On each day you may buy or sell arbitrary amounts of any combination of stocks, of course never spending more than your current capital. There is one more rule: you may make at most tt trades in total. A trade is the act of buying or selling one kind of stock. You do not have to trade in integer amounts; for example you may buy 1.3641.364 units of a stock if you want. Buying several kinds of stock on the same day uses several trades. Your goal is to maximize the total amount of cash you hold after DD days. (Only cash counts; any stock you still hold after DD days is worthless.)

Input

The first line contains the number KK of data sets, followed by the KK data sets, each in the following form.

The first line of a data set contains integers nn, DD, tt and a floating point number CC. Here 1≤n≤1001 \le n \le 100 is the number of stocks, 1≤D≤1001 \le D \le 100 is the number of days, 0≤t≤1000 \le t \le 100 is the number of trades you may make, and C≥0C \ge 0 is your starting capital.

This is followed by DD lines, each containing nn floating point numbers rd,i≥0r_{d,i} \ge 0. The value rd,ir_{d,i} is the price at which stock ii trades on day dd. All prices for one day appear on the same line.

Output

For each data set, print a line Data Set x: by itself, where x is the data set number (starting from 11). On the next line, print the maximum amount of money you can have after the DD days, rounded to two decimals, having made at most tt trades in total. Print a blank line between consecutive data sets.

Examples3

  1. Example 1

    Input
    2
    1 2 1 100.0
    1.0
    98.0
    3 8 6 100.0
    1 1 9.5
    2 10.5 2
    4 15 4.1
    4 15 5
    5 5.1 1.8
    6 8 1
    6.5 2.5 1
    6.5 2 1
    
    Expected output
    Data Set 1:
    100.00
    
    Data Set 2:
    4265.62
    
  2. Example 2

    Input
    1
    1 3 2 10.0
    1.0
    5.0
    3.0
    
    Expected output
    Data Set 1:
    50.00
    
  3. Example 3

    Input
    1
    2 3 2 100.0
    1.0 1.0
    2.0 10.0
    1.0 1.0
    
    Expected output
    Data Set 1:
    1000.00