#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include using namespace __gnu_pbds; using namespace std; using namespace atcoder; using ll = long long; using ull = unsigned long long; using Mint = modint998244353; using mint = modint; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define rep2(i, a, b) for (int i = (int)a; i < (int)(b); ++i) #define rrep2(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); --i) template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr int dx[] = {-1,0,1,0}; constexpr int dy[] = {0,-1,0,1}; constexpr int MAX_N = 100000; //ダイクストラ法:makedijkstra //エラトステネスの篩:makesieve //テストケースが複数の場合:multest int main(){ int N, K; scanf("%d %d", &N, &K); int A[N]; for(int i = 0; i < N; ++i) scanf("%d", A + i); int last = A[0], l = 0; int diff_idx[N]; bool is_bigger_than_next[N]; rep(i, N-1){ if(A[i+1] != A[i]){ rep2(j, l, i+1){ diff_idx[j] = i+1; is_bigger_than_next[j] = A[i] > A[i+1]; } l = i+1; } } rep2(i, l, N){ diff_idx[i] = N; is_bigger_than_next[i] = false; } int answer[N]; iota(answer, answer + N, 0); auto comp = [&](int a, int b){ assert(a < b); if(diff_idx[a] > b) return false; return is_bigger_than_next[a]; }; nth_element(answer, answer + K - 1, answer + N, [&](int a, int b){ return a < b ? comp(a, b) : !comp(b, a); }); rep(i, answer[K-1]){ printf("%d", A[i]); printf(i < N-1 ? " " : "\n"); } rep2(i, answer[K-1]+1, N){ printf("%d", A[i]); printf(i < N-1 ? " " : "\n"); } }