#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Tools/heap_alias.hpp" #include template using MaxHeap = std::priority_queue; template using MinHeap = std::priority_queue, std::greater>; #line 2 "main.cpp" #include #include #include using namespace std; using lint = long long; constexpr lint INF = 1LL << 60; void solve() { int k; lint n; cin >> k >> n; vector xs(k), ys(k); for (auto& x : xs) cin >> x; for (auto& y : ys) cin >> y; if (n < k) { cout << xs[n] << "\n"; return; } lint ok = -INF, ng = INF; while (ng - ok > 1) { auto mid = (ok + ng) / 2; vector ts; // { k-j | ys[j] >= mid } for (int j = 0; j < k; ++j) { if (ys[j] >= mid) ts.push_back(k - j); } if (ts.empty()) { ng = mid; continue; } auto m = ts.back(); // minimum while (m < k) m <<= 1; // 小さすぎると遷移できない場合があるので、十分に拡張 // mod m でDijkstra vector dist(m, INF); MinHeap> heap; for (int i = 0; i < k; ++i) { if (xs[i] >= mid) { dist[i] = i; heap.emplace(i, i); } } while (!heap.empty()) { auto [d, i] = heap.top(); heap.pop(); if (d > dist[i]) continue; for (auto t : ts) { auto ni = (i + t) % m; auto nd = d + t; if (nd < dist[ni]) { dist[ni] = nd; heap.emplace(nd, ni); } } } if (dist[n % m] <= n) { ok = mid; } else { ng = mid; } } cout << ok << "\n"; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }