#include #include #include #include #include #include #include #include #include using namespace std; #pragma warning (disable: 4996) long long N, K; long long A[1 << 22], B[1 << 22]; int main() { // Step #1. 入力 cin >> K >> N; for (int i = 0; i < K; i++) cin >> A[i]; for (int i = 0; i < K; i++) cin >> B[i]; // Step #2. 愚直解 long long ti = clock(), rem = K - 1; for (long long i = K; i <= min(N, (1LL << 21)); i++) { A[i] = -(1LL << 60); rem = i; for (int j = 0; j < K; j++) { A[i] = max(A[i], min(B[j], A[i - K + j])); } if (i % 1000 == 0 && clock() - ti >= 19 * CLOCKS_PER_SEC / 10) { break; } } if (rem == N) { cout << A[N] << endl; return 0; } // Step #3. 周期を見る 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 #4. 答えを出力 long long idx = (rem - pr) + (N - rem) % pr; cout << A[idx] << endl; return 0; }