#P9474. [yLOI2022] 长安幻世绘

[yLOI2022] 长安幻世绘

Background

The bright moon over Chang'an is clear. Shadows remain, pipa songs go on, and the drinkers are half awake.
You ask, but cannot see clearly. Red gauze curtains, green silk ribbons, and the flying apsaras descend.
A beauty bends her slim waist, her jingling necklaces crisp. With a delicate hand she raises the pipa alone and kneels, her eyes still full of charm.
Musk touches her lips, and the wine becomes more intoxicating. In the drunken lantern light, the colors grow messier yet more beautiful.

— Yin Lin, “Chang'an Fantasy Paintings”

Problem Description

There are nn colored lanterns arranged in a row from left to right, numbered from 11 to nn. The brightness of lantern ii is aia_i. For 1i<n1 \leq i < n, we say lantern ii and lantern i+1i + 1 are adjacent.

It is guaranteed that the brightness values of these nn lanterns are all distinct.

The harmony of a group of lanterns is defined as the difference between the maximum and minimum brightness among the lanterns in the group.

Fusu wants to choose mm non-adjacent lanterns from these nn lanterns as a group, and she wants the harmony of this group to be as small as possible. Please help her find this minimum value.

Formally, you need to select a subsequence of length mm with pairwise non-adjacent elements from the sequence aa (where all elements are distinct), such that the range of the subsequence is minimized.

Input Format

The first line contains two integers, representing the number of lanterns nn and the number of lanterns to pick mm, respectively.
The second line contains nn integers. The ii-th integer represents the brightness aia_i of lantern ii.

Output Format

Output one line with one integer, the answer.

5 3
1 2 3 4 5
4
6 3
1 7 8 3 4 6
4
见附加文件中的 D3.in
见附加文件中的 D3.ans

Hint

Explanation for Sample 1

You can only choose lanterns 1,3,51, 3, 5, because any other selection would cause some lanterns to be adjacent.

Explanation for Sample 2

You can choose lanterns 2,4,62, 4, 6. Their brightness values are 7,3,67, 3, 6, and the range is 44.

Constraints

  • For 12%12\% of the testdata, n6n \leq 6.
  • For 36%36\% of the testdata, n100n \leq 100.
  • For another 4%4\% of the testdata, m=n2m = \lceil\frac{n}{2}\rceil.
  • For another 12%12\% of the testdata, aia_i is strictly increasing.
  • For 76%76\% of the testdata, n103n \leq 10^3.
  • For 100%100\% of the testdata, 1n1051 \leq n \leq 10^5, 1mn21 \leq m \leq \lceil\frac{n}{2}\rceil, 1ai1091 \leq a_i \leq 10^9, and all aia_i are distinct.

Translated by ChatGPT 5