#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; //#define DEBUG_ //!!提出時にコメントアウト!! #ifdef DEBUG_ #define dump(x) cerr << #x << " = " << (x) << endl; #else #define dump(x) ; //何もしない文 #endif #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define SZ(x) ((int)(x).size()) //unsignedのサイズをint型に変換 #define pb push_back typedef long long LL; typedef vector VI; typedef vector VVI; typedef vector VS; typedef pair PII; typedef pair PLL; template std::string printVector(const std::vector &data) { std::stringstream ss; std::ostream_iterator out_it(ss, ", "); ss << "["; std::copy(data.begin(), data.end() - 1, out_it); ss << data.back() << "]"; return ss.str(); } const int MOD = 1e9+7; const LL LINF = 1001002003004005006ll; const int INF = 1001001001; int N,K; vector E; bool isOK(int mid, int key) { int cnt = 0; dump(mid) REP(i,N) { int A = E[i].first; int B = E[i].second; if(A <= mid && mid <= B) { cnt += mid - A + 1; } else if(B < mid) { cnt += B - A + 1; } } dump(cnt) if(cnt >= key) return true; else return false; } int binary_search(int key) { int ng = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1 int ok = 1e9; // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size() /* どんな二分探索でもここの書き方を変えずにできる! */ while (abs(ng - ok) > 1) { int mid = (ok + ng) / 2; if (isOK(mid, key)) ok = mid; else ng = mid; } /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている */ return ok; } int main(int argc, char const *argv[]) { cin.tie(0); ios::sync_with_stdio(false); cin >> N>>K; REP(i,N) { int ta,tb; cin >> ta >> tb; E.pb(make_pair(ta,tb)); } dump("check") int ans = binary_search(K); cout << ans << endl; }