Under construction

Still being built. Things may change

Doubletrouble

Time limit1sMemory limit512 MB

Summary
Given N potions in a line, move the potions labeled 1 to N into sorted order, where each spell costs the squared distance moved, using at most E total energy and the fewest spells.
Level

Medium7 of 10

Topics
Array, Greedy, Dynamic programming
Solved
No attempts yet

Problem

The legendary wizard Merlin is having fun in his lab. He has N magical potions (labeled 1, 2, ..., N) arranged on a single line in a given order. He wants to rearrange them in ascending order. The potions are moved one at a time. The first potion moved is the one labeled 1, then the potion labeled 2, and so on. Each potion can be moved using one or more spells, passing through one or more intermediate positions. Moving a potion from position I to position J costs (I−J)2(I-J)^2 energy units. For example, if there are 7 potions in the order 1, 7, 3, 4, 5, 2, 6 and Merlin moves the potion labeled 2 from the sixth position to the second position, the new arrangement is 1, 2, 7, 3, 4, 5, 6, and (6−2)2(6-2)^2 units of energy are consumed. Merlin has set two criteria. First, he does not want to be too tired at the end of the day, so he will not consume more than E units of energy. Second, he gets bored quickly, so he wants to solve the problem with the minimum number of spells. Calculate the minimum number of spells Merlin can use to solve the problem.

Input

The first line contains two positive integers N and E separated by a space. The second line contains N positive integers separated by spaces, where the K-th integer is the label of the potion at position K, 1 ≤ K ≤ N.

Output

Print a single integer, the minimum number of spells Merlin uses, or -1 if there is no solution.

Constraints

1 ≤ N ≤ 10510^5; 1 ≤ E ≤ 101510^{15}

Hint

2 3 5 1 4 --> 2 3 1 5 4 --> 1 2 3 5 4 --> 1 2 3 5 4 --> 1 2 3 4 5. A minimum of three spells is used. The first two spells cost 121^2 and 222^2 energy and move potion 1 to the first position. The third spell costs 121^2 energy and moves potion 4 to the fourth position. The total energy consumed is 12+22+12=61^2 + 2^2 + 1^2 = 6.

Examples1

  1. Example 1

    Input
    5 6
    2 3 5 1 4
    
    Expected output
    3