// TLE 解法: 区間スケジューリングを愚直に N - K + 1 回する #include using namespace std; using P = pair; int main(){ int N, K; cin >> N >> K; vector L(N), R(N); vector

v(N); for(int i = 0; i < N; i++){ cin >> L[i] >> R[i]; v[i] = P(R[i], L[i]); } sort(v.begin(), v.end()); const int inf = 1'000'000'100; // 1e9 + 100 int ans = inf; for(int i = 0; i < N; i++){ int cur = i, cnt = 1, next = i + 1; while(cnt != K && next < N){ if(v[next].second >= v[cur].first){ cur = next; cnt++; } next++; } if(cnt == K) ans = min(ans, v[cur].first - v[i].second); } if(ans != inf) cout << ans << endl; else cout << -1 << endl; }