#K46287. Minimum Delivery Cost

    ID: 27943 Type: Default 1000ms 256MiB

Minimum Delivery Cost

Minimum Delivery Cost

You are given an integer \(n\) representing the number of houses, a list of \(n\) integers representing the house numbers in the order of delivery, and an integer \(k\) representing the cost incurred for skipping a house that is not immediately consecutive. The task is to calculate the minimum total cost of delivering all packages in the given order.

The total cost is determined by the number of times the delivery order experiences a gap. More formally, if the list of house numbers is \(houses\), then for each index \(i\) (where \(2 \le i \le n\)) if \(houses[i] \neq houses[i-1] + 1\), a cost of \(k\) is added. In mathematical terms, the cost can be computed as:

[ \text{cost} = \sum_{i=2}^{n} \mathbb{1}(houses[i] \neq houses[i-1] + 1) \times k, ]

where \(\mathbb{1}(\text{condition})\) is 1 if the condition is true, and 0 otherwise.

If there is only one house, the delivery cost is 0.

inputFormat

The input is read from stdin and consists of three parts:

  • An integer \(n\) representing the number of houses.
  • A line containing \(n\) space-separated integers representing the house numbers in the order of delivery.
  • An integer \(k\) which is the cost incurred for each skipped (non-consecutive) delivery.

outputFormat

The output is a single integer printed to stdout which denotes the minimum total cost of the deliveries.

## sample
4
5 6 7 8
3
0