#include <stdio.h>
void povuci(int a[200001], int index, int n)
{
    for (int i = index; i + 1 < n; i++)
    {
        a[i] = a[i + 1];
    }
}
int main()
{
    int a[200001], n, k, i, min, max, jacina = 0, mi = 0, mx = 0, j;
    scanf("%d%d", &n, &k);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        if (i == 0)
        {
            min = a[i];
            max = a[i];
        }
        if (min > a[i])
        {
            min = a[i];
            mi = i;
        }
        if (max < a[i])
        {
            max = a[i];
            mx = i;
        }
    }
    jacina = max - min;
    a[mx] = jacina;
    for (i = 2; i < k; i++)
    {
        povuci(a, mi, n);
        n--;
        max = a[mx];
        min = a[0];
        for (j = 0; j < n; j++)
        {
            if (min > a[i])
            {
                min = a[i];
                mi = i;
            }
            if (max < a[i])
            {
                max = a[i];
                mx = i;
            }
        }
        jacina = max - min;
    }
    printf("%d", jacina);
    return 0;
}