#include <cstdio>
#include <queue>
using namespace std;
const int N = 1e6 + 10;
int m1[N], m2[N];
int main()
{
    typedef pair<int, int> pii;
    deque<pii> q1, q2;
    int n, k, x;
    while (~scanf("%d%d", &n, &k))
    {
        q1.clear(), q2.clear();
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &x);
            while (!q1.empty() && q1.back().first >= x)
                q1.pop_back();
            q1.push_back(make_pair(x, i));
            while (!q2.empty() && q2.back().first <= x)
                q2.pop_back();
            q2.push_back(make_pair(x, i));
            if (i >= k - 1)
            {
                while (!q1.empty() && q1.front().second <= i - k)
                    q1.pop_front();
                m1[i] = q1.front().first;
                while (!q2.empty() && q2.front().second <= i - k)
                    q2.pop_front();
                m2[i] = q2.front().first;
            }
        }
        for (size_t i = k - 1; i < n; i++)
            printf("%d ", m1[i]);
        putchar('\n');
        for (size_t i = k - 1; i < n; i++)
            printf("%d ", m2[i]);
        putchar('\n');
    }
    return 0;
}