using System; using System.Linq; using System.Collections; using System.IO; using System.Collections.Generic; using System.Text; using System.Numerics; using System.Runtime.Intrinsics.X86; using System.Buffers; using System.Diagnostics; using System.Runtime.CompilerServices; using static InputUtility; class Program { static void Main() { using Output output = new(false); InputNewLine(); var (n, k) = (NextInt32, NextInt32); var a = new (int l, int r)[n]; for (int i = 0; i < a.Length; i++) { InputNewLine(); a[i] = (NextInt32, NextInt32); } a = a.OrderBy(x => x.r).ThenBy(x => x.l).ToArray(); var times = a.Select(x => x.r - x.l).ToArray(); int[] nexts = Enumerable.Repeat(-1, n).ToArray(); { Queue<(int index, int right)> q = new(); for (int i = 0; i < a.Length; i++) { while (q.Count > 0 && q.Peek().right <= a[i].l) { var (index, _) = q.Dequeue(); nexts[index] = i; } q.Enqueue((i, a[i].r)); } } PriorityQueue<(long time, int count, int last), long> queue = new(); for (int i = 0; i < a.Length; i++) { var t = times[i]; queue.Enqueue((t, 1, i), t); } while (queue.Count > 0) { var (time, count, cur) = queue.Dequeue(); if (count == k) { Console.WriteLine(time); return; } if (nexts[cur] == -1) { continue; } var next = nexts[cur]; var nt = time + a[next].r - a[cur].r; queue.Enqueue((nt, count + 1, next), nt); } Console.WriteLine("-1"); } } public static class InputUtility { private static string[]? s_inputs; private static string? s_raw; private static int s_index = 0; private static void Init() => s_index = 0; public static int NextInt32 => int.Parse(s_inputs![s_index++]!); public static uint NextUInt32 => uint.Parse(s_inputs![s_index++]!); public static long NextInt64 => long.Parse(s_inputs![s_index++]!); public static string NextString => s_inputs![s_index++]; public static char NextChar => s_inputs![s_index++][0]; public static int[] GetInt32Array() => s_inputs!.Select(int.Parse).ToArray(); public static long[] GetInt64Array() => s_inputs!.Select(long.Parse).ToArray(); public static string GetRawString() => s_raw!; #if DEBUG private static TextReader? s_textReader; public static void SetSource(string path) => s_textReader = new StringReader(File.ReadAllText(path)); #endif public static bool InputNewLine() { #if DEBUG if (s_textReader is TextReader sr) { Init(); s_raw = sr.ReadLine()!; s_inputs = s_raw.Split(' ', StringSplitOptions.RemoveEmptyEntries); return true; } #endif Init(); s_raw = Console.ReadLine()!; s_inputs = s_raw.Split(' ', StringSplitOptions.RemoveEmptyEntries); return true; } } public readonly struct Output : IDisposable { private readonly StreamWriter _sw; #if DEBUG public Output(string path) { var fs = new FileStream(path, FileMode.Create, FileAccess.Write); _sw = new StreamWriter(fs); Console.SetOut(_sw); } #endif public Output(bool autoFlush) { _sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = autoFlush }; Console.SetOut(_sw); } public void Dispose() { _sw.Dispose(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void Flush() { _sw.Flush(); } } public static class ArrayExtensions { public static void Swap(this T[] array, int i, int j) => (array[i], array[j]) = (array[j], array[i]); public static int LowerBound(this T[] a, T target) where T : IComparable { int ok = a.Length; int ng = -1; while (Math.Abs(ok - ng) > 1 && (ok + ng) / 2 is int mid) (ok, ng) = a[mid].CompareTo(target) >= 0 ? (mid, ng) : (ok, mid); return ok; } public static int UpperBound(this T[] a, T target) where T : IComparable { int ok = a.Length; int ng = -1; while (Math.Abs(ok - ng) > 1 && (ok + ng) / 2 is int mid) (ok, ng) = a[mid].CompareTo(target) > 0 ? (mid, ng) : (ok, mid); return ok; } public struct IndexedEnumerable : IEnumerable<(T item, int index)> { private readonly T[] _a; private readonly int _startIndex; public IndexedEnumerable(T[] a, int startIndex = 0) { _a = a; _startIndex = startIndex; } public readonly IndexedEnumerator GetEnumerator() => new IndexedEnumerator(_a, _startIndex); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); IEnumerator<(T item, int index)> IEnumerable<(T item, int index)>.GetEnumerator() => GetEnumerator(); } public struct IndexedEnumerator : IEnumerator<(T item, int index)> { public readonly (T item, int index) Current => (_a[_index], _index + _startIndex); private int _index; private int _startIndex; private T[] _a; public IndexedEnumerator(T[] a, int startIndex) { _index = -1; _a = a; _startIndex = startIndex; } public bool MoveNext() => ++_index < _a.Length; readonly object IEnumerator.Current => Current; public readonly void Dispose() { } public void Reset() => _index = -1; } /// (T value, int index) public static IndexedEnumerable Enumerate(this T[] arr, int startIndex = 0) => new IndexedEnumerable(arr, startIndex); } public static class IEnumerableExtensions { public static IEnumerable Log(this IEnumerable source) { Console.WriteLine(string.Join(' ', source)); return source; } public static ScanEnumerable Scan( this IEnumerable source, TAccumulate seed, Func accumulator) where TSource : struct where TAccumulate : struct { return new ScanEnumerable(source, accumulator, seed); } public static IEnumerable ScanExSeed( this IEnumerable source, TAccumulate seed, Func accumulator) { var accumulation = new List(); var current = seed; foreach (var item in source) { current = accumulator(current, item); accumulation.Add(current); } return accumulation; } public readonly struct ScanEnumerable : IEnumerable where TSource : struct where TAccumulate : struct { private readonly IEnumerable _source; private readonly Func _accumulator; private readonly TAccumulate _seed; public ScanEnumerable(IEnumerable source, Func accumulator, TAccumulate seed) { _source = source; _accumulator = accumulator; _seed = seed; } public readonly ScanEnumerator GetEnumerator() => new(_source, _accumulator, _seed); readonly IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); readonly IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } public struct ScanEnumerator : IEnumerator where TSource : struct where TAccumulate : struct { private readonly Func _accumulator; private readonly IEnumerator _enumerator; private TAccumulate _current; private bool _secondOrLaterElement = false; public ScanEnumerator(IEnumerable source, Func accumulator, TAccumulate seed) { _enumerator = source.GetEnumerator(); _accumulator = accumulator; _current = seed; } public readonly TAccumulate Current => _current; readonly object IEnumerator.Current => Current; public readonly void Dispose() { } public bool MoveNext() { if (_secondOrLaterElement) { if (_enumerator.MoveNext()) { _current = _accumulator(_current, _enumerator.Current); return true; } return false; } else { _secondOrLaterElement = true; return true; } } public void Reset() { throw new NotSupportedException(); } } public static IEnumerable Scan( this IEnumerable source, Func accumulator) { if (source is null) throw new ArgumentNullException(paramName: nameof(source)); if (accumulator is null) throw new ArgumentNullException(paramName: nameof(accumulator)); var accumulation = new List(); if (source.Any() is false) { return accumulation; } var current = source.First(); accumulation.Add(current); foreach (var item in source.Skip(1)) { current = accumulator(current, item); accumulation.Add(current); } return accumulation; } } public class MyMath { public static long Pow(long x, int y) => Enumerable.Repeat(x, y).Aggregate(1L, (acc, x) => acc * x); public static long Pow10(int y) => Pow(10, y); public static long Pow2(int y) => Pow(2, y); } public class MyMathBigInteger { public static BigInteger Pow(long x, int y) => Enumerable.Repeat(x, y).Aggregate(new BigInteger(1), (acc, x) => acc * x); public static BigInteger Pow10(int y) => Pow(10, y); public static BigInteger Pow2(int y) => Pow(2, y); }