#C5766. Minimum Cost to Sort Candies

    ID: 49451 Type: Default 1000ms 256MiB

Minimum Cost to Sort Candies

Minimum Cost to Sort Candies

Given an array of candies labeled with integers, your task is to transform the array into its sorted (ascending) order at the minimum cost. Two operations are allowed:

  1. Swap Operation: Select any two candies and swap them. This operation costs (1) unit.
  2. Reverse Operation: Select any contiguous subarray and reverse it. This operation costs (2) units.

Formally, you are given an integer (N) and an array (candies = [a_1, a_2, \ldots, a_N]). Your goal is to achieve (sorted(candies)) with the least total cost. Note that (N) is small (e.g., (N \leq 8)), so an exhaustive search approach is acceptable.

Solve the problem using the standard input (stdin) and produce the result to standard output (stdout).

inputFormat

The first line contains an integer (N) representing the number of candies. The second line contains (N) space-separated integers which are the labels of the candies.

outputFormat

Output a single integer indicating the minimum cost required to sort the candy array in ascending order.## sample

5
4 3 5 1 2
3

</p>