#include #include #include #include #include #include #include #include #include using namespace std; #pragma warning (disable: 4996) int K; long long N; int A[1 << 22], B[1 << 22], C[1 << 22]; long long QA[1 << 22], QB[1 << 22]; int main() { // Crazy Solution // Step #1. 入力 cin >> K >> N; for (int i = 0; i < K; i++) cin >> QA[i]; for (int i = 0; i < K; i++) cin >> QB[i]; // Step #2. 座標圧縮 vector X; for (int i = 0; i < K; i++) X.push_back(QA[i]); for (int i = 0; i < K; i++) X.push_back(QB[i]); sort(X.begin(), X.end()); X.erase(unique(X.begin(), X.end()), X.end()); for (int i = 0; i < K; i++) A[i] = lower_bound(X.begin(), X.end(), QA[i]) - X.begin(); for (int i = 0; i < K; i++) B[i] = lower_bound(X.begin(), X.end(), QB[i]) - X.begin(); for (int i = 0; i < K; i++) C[K - i] = B[i]; // Step #3. 愚直解 long long rem = min(N, 1LL * K * K); for (int i = K; i <= rem; i++) A[i] = -1; for (int i = 0; i <= K; i++) { for (int j = 1; j <= K; j++) { if (i + j < K) continue; A[i + j] = max(A[i + j], min(A[i], C[j])); } } for (int i = K; i <= rem; i++) { for (int j = 1; j <= K; j++) { A[i + j] = max(A[i + j], min(A[i], C[j])); } } if (rem == N) { cout << X[A[N]] << endl; return 0; } // Step #4. 周期を見る long long pr = -1; for (long long i = rem - 1; i >= K; i--) { bool flag = false; for (int j = 0; j < K; j++) { if (A[rem - K + j] != A[i - K + j]) { flag = true; break; } } if (flag == false) { pr = rem - i; break; } } // Step #5. 答えを出力 long long idx = (rem - pr) + (N - rem) % pr; cout << X[A[idx]] << endl; return 0; }