#C7753. Stock Trading Profit with Delay Constraint

    ID: 51659 Type: Default 1000ms 256MiB

Stock Trading Profit with Delay Constraint

Stock Trading Profit with Delay Constraint

You are given a list of stock prices over N days and a minimum delay d. Your task is to determine the maximum profit achievable by buying on one day and selling on a later day, with the condition that the selling day is at least d days after the buying day.

Formally, you need to compute the value:

$$\max_{0 \leq i, j < N,\ j-i \ge d} \{prices[j] - prices[i]\}$$

If no transaction can yield a profit (i.e., all possible profits are non-positive) then output -1.

inputFormat

The input is given via stdin and consists of two lines:

  • The first line contains two integers N and d, where N is the number of days, and d is the minimum required delay between buying and selling.
  • The second line contains N space-separated integers representing the stock prices for each day.

outputFormat

Output a single integer to stdout representing the maximum achievable profit. If no profitable transaction exists, output -1.

## sample
8 2
3 1 4 1 5 9 2 6
8