Problem:
Problem K. Rough Sorting
For skilled programmers, it is very easy to implement a sorting function. Moreover, they often avoid full sorting to reduce computation time if it is not necessary. Here, we consider "rough sorting" which sorts an array except for some pairs of elements. More formally, we define an array is "$K$-roughly sorted" if an array is sorted except that
at most
$K$ pairs are in reversed order. For example, '
1 3 2 4
' is 1-roughly sorted because (3, 2) is only the reversed pair. In the same way, '
1 4 2 3
' is 2-roughly sorted because (4, 2) and (4, 3) are reversed.
Considering rough sorting by exchanging adjacent elements repeatedly, you need less number of swaps than full sorting. For example, '
4 1 2 3
' needs three exchanges for full sorting, but you only need to exchange once for 2-rough sorting.
Given an array and an integer $K$, your task is to find the result of the $K$-rough sorting with a minimum number of exchanges. If there are several possible results, you should output the lexicographically minimum result. Here, the lexicographical order is defined by the order of the first different elements.
Input
The input consists of a single test case in the following format.
$N$ $K$
$x_1$
$\vdots$
$x_N$
The first line contains two integers $N$ and $K$. The integer $N$ is the number of the elements of the array ($1 \leq N \leq 10^5$). The integer $K$ gives how many reversed pairs are allowed ($1 \leq K \leq 10^9$). Each of the following $N$ lines gives the element of the array. The array consists of the permutation of $1$ to $N$, therefore $1 \leq x_i \leq N$ and $x_i \ne x_j$ ($i \ne j$) are satisfied.
Output
The output should contain $N$ lines. The $i$-th line should be the $i$-th element of the result of the $K$-rough sorting. If there are several possible results, you should output the minimum result with the lexicographical order.
Examples
Sample Input 1
3 1
3
2
1
Output for Sample Input 1
1
3
2
Sample Input 2
3 100
3
2
1
Output for Sample Input 2
3
2
1
Sample Input 3
5 3
5
3
2
1
4
Output for Sample Input 3
1
3
5
2
4
Sample Input 4
5 3
1
2
3
4
5
Output for Sample Input 4
1
2
3
4
5
In the last example, the input array is already sorted, which means the input is already a 3-roughly sorted array and no swapping is needed.

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define pll pair<ll,ll>
#define vl vector<ll>
#define inf 100000000000000000LL

ll inversion_number(vl & v){
    int n = v.size();
    if (n==1) return 0;
    vl v1(v.begin(), v.begin() + n / 2);
    vl v2(v.begin() + n / 2, v.end());
    ll res = inversion_number(v1) + inversion_number(v2);
    int p = 0, q = 0;
    rep(i, n){
        if (q == v2.size() || (p < v1.size() && v1[p] <= v2[q])){
            v[i] = v1[p++];
        }else{
            v[i] = v2[q++];
            res += v1.size() - p;
        }
    }
    return res;
}

ll power(ll A, ll B){
    ll result = 1;
    rep(i, B){
        result *= A;
    }
    return result;
}

ll logax(ll a, ll x){
    if(x <= 1)return 0;
    ll result = 1;
    ll power = 1;
    while(power < (x + a - 1) / a){
        power *= a;
        result++;
    }
    return result;
}
template<typename T>
struct SegTree{
    ll size;
    ll tall;
    vector<T> data;
    function<T(T,T)> p;
    SegTree(vector<T> a, function<T(T,T)> put, T d) : data(power(2, logax(2,a.size()) + 1)){
        size = data.size() / 2;
        tall = logax(2, size) + 1;
        p = put;
        ll tmp = size;
        data = vector<T>(size*2, d);
        while(tmp != 0){
            if(tmp==size)rep(i, a.size())data[tmp + i] = a[i];
            else rep(i, tmp) data[tmp + i] = p(data[2*(tmp+i)], data[2*(tmp+i) + 1]);
            tmp /= 2;
        }
    }
    void update(ll t, T x){
        t += size;
        while(t != 0){
            if(t >= size)data[t] = x;
            else data[t] = p(data[2 * t], data[2 * t + 1]);
            t /= 2;
        }
    }

    T get(ll l, ll r){
        l = max(0LL,l);
        r = min(r, size - 1);
        r++;
        T ans = data[0];
        ll pos = l + size;
        ll wid = 1;
        while(l + (wid * 2) <= r){
            while(l % (wid * 2) == 0 && l + (wid * 2) <= r)pos/=2, wid*=2;
            ans = p(ans,data[pos]);
            pos++;
            l += wid;
        }
        while(l != r){
            while(l + wid > r)pos *= 2, wid /= 2;
            ans = p(ans, data[pos]);
            pos++;
            l += wid;
        }
        return ans;
    }
};

pll mn(pll a,pll b){return min(a,b);}
ll sums(ll a,ll b){return a+b;}

int main() {
	ll n,k;
	cin>>n>>k;
	vl x(n);
	rep(i,n)cin>>x[i];
	
	vl xcopy=x;
	k=inversion_number(xcopy)-k+1;
	
	if(k<=0){
		rep(i,n)cout<<x[i]<<endl;
		return 0;
	}

	vector<pll> segtmp(n);
	rep(i,n){
		segtmp[i]={x[i],i};
	}
	SegTree<ll> sumseg(vl(n,1),sums,0);
	SegTree<pll> mnseg(segtmp,mn,{inf,inf});

	rep(i,n){
		//sumのセグ木からmaxrightを取る
		ll l=0,r=n-1;
		while(l!=r){
			ll mid=(l+r)/2;
			if(sumseg.get(0,mid)<k)l=mid+1;
			else r=mid;
		}

		//mnのセグ木から、値とindexを出す
		pll tmp=mnseg.get(0,r);
		ll num=tmp.first;
		ll index=tmp.second;

		//両方のセグ木を更新する
		mnseg.update(index,{inf,inf});
		sumseg.update(index,0);

		//実際の交換回数をsumのセグ木から算出して、kを更新
		k-=sumseg.get(0,index);

		cout<<num<<endl;
	}
    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 18584
final_rank: 13

code_snippet: // AOJ #2914 Rough Sorting
// 2025.3.5

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

struct Fenw {
    int n;
    vector<int> fenw;
    Fenw(int n): n(n), fenw(n+1,0) {}
    void upd(int i, int d) {
        for(++i; i<=n; i += i&-i) fenw[i] += d;
    }
    int sum(int i) {
        int s = 0;
        for(++i; i; i -= i&-i) s += fenw[i];
        return s;
    }
    int find(int k) {
        int idx = 0;
        for (int bit = 1 << 20; bit; bit >>= 1) {
            int nxt = idx + bit;
            if(nxt <= n && fenw[nxt] < k){
                k -= fenw[nxt];
                idx = nxt;
            }
        }
        return idx;
    }
};

struct SegTree {
    int n;
    vector<pair<int,int>> seg;
    SegTree(const vector<int> &a) {
        n = a.size();
        seg.resize(4*n);
        build(a, 0, n-1, 0);
    }
    void build(const vector<int>& a, int l, int r, int idx) {
        if(l == r){
            seg[idx] = {a[l], l};
            return;
        }
        int mid = (l+r)/2;
        build(a, l, mid, idx*2+1);
        build(a, mid+1, r, idx*2+2);
        seg[idx] = min(seg[idx*2+1], seg[idx*2+2]);
    }
    pair<int,int> query(int ql, int qr, int l, int r, int idx) {
        if(ql > r || qr < l) return {INF, -1};
        if(ql <= l && r <= qr) return seg[idx];
        int mid = (l+r)/2;
        auto left = query(ql, qr, l, mid, idx*2+1);
        auto right = query(ql, qr, mid+1, r, idx*2+2);
        return min(left, right);
    }
    pair<int,int> query(int ql, int qr) {
        return query(ql, qr, 0, n-1, 0);
    }
    void update(int pos, int val, int l, int r, int idx) {
        if(l == r){
            seg[idx] = {val, pos};
            return;
        }
        int mid = (l+r)/2;
        if(pos <= mid) update(pos, val, l, mid, idx*2+1);
        else update(pos, val, mid+1, r, idx*2+2);
        seg[idx] = min(seg[idx*2+1], seg[idx*2+2]);
    }
    void update(int pos, int val) {
        update(pos, val, 0, n-1, 0);
    }
};

struct FenwCount {
    int n;
    vector<int> fenw;
    FenwCount(int n): n(n), fenw(n+1, 0) {}
    void upd(int i, int d) {
        for(++i; i<=n; i+= i&-i) fenw[i] += d;
    }
    int sum(int i) {
        int s = 0;
        for(++i; i; i-= i&-i) s += fenw[i];
        return s;
    }
};

int main(){
    int n = Cin(); ll K = Cin();
    vector<int> a(n);
    for(int i=0;i<n;i++) a[i] = Cin();

    ll inv = 0;
    FenwCount fw(n);
    for (int i = 0; i < n; i++){
        inv += i - fw.sum(a[i]-1);
        fw.upd(a[i]-1, 1);
    }
    if(inv <= K){
        for(auto v : a) Cout(v);
        return 0;
    }
    ll R = inv - K;
    Fenw fw2(n);
    for (int i = 0; i < n; i++) fw2.upd(i, 1);
    SegTree seg(a);
    vector<int> res;
    res.reserve(n);

    int rem = n;
    while(rem > 0){
        if(R == 0){
            for(int i = 0; i < n; i++){
                int cnt = fw2.sum(i) - (i==0?0:fw2.sum(i-1));
                if(cnt > 0) res.push_back(a[i]);
            }
            break;
        }
        rem = fw2.sum(n-1);
        int window = min((ll)rem, R + 1);
        int r_idx = fw2.find(window);
        int L = fw2.find(1);
        auto p = seg.query(L, r_idx);
        int pos = p.second;
        int val = p.first;
        int rank = fw2.sum(pos) - 1;
        R -= rank;
        res.push_back(val);
        seg.update(pos, INF);
        fw2.upd(pos, -1);
    }
    for(auto v : res) Cout(v);
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 7804
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0; i<ll(n); i++)
#define all(v) v.begin(), v.end()
template<class T> void chmin(T& l, const T& r){ if(r < l) l = r; }
template<class T> void chmax(T& l, const T& r){ if(l < r) l = r; }
const ll INF = 1ll << 60;

struct FWT{
    int N;
    vector<int> A;
    FWT(int n = 1){
        A.resize(n+1);
        N = n;
    }
    void add(int p, int v){
        p++;
        while(p <= N){ A[p] += v; p += p&-p; }
    }
    int sum(int r){
        int x = 0;
        while(r > 0){ x += A[r]; r -= r&-r; }
        return x;
    }
    int sum(int l, int r){ return sum(r) - sum(l); }
};

void testcase(){
    ll N, K; cin >> N >> K;
    vector<int> X(N);
    rep(i,N){ cin >> X[i]; X[i]--; }
    vector<int> A(N); rep(i,N) A[X[i]] = i;
    FWT ds(N);
    int D = N;
    ll F = 0;
    for(int i=N-1; i>=0; i--){
        int k = ds.sum(0, A[i]);
        ds.add(A[i], 1);
        D = i;
        if(k <= K){ K -= k; continue; }
        F = k - K;
        break;
    }
    vector<int> T(D);
    rep(i,D) T[i] = i;
    rep(i,N) if(X[i] >= D) T.push_back(X[i]);
    rep(i,N) X[i] = T[i];
    rep(i,N) A[X[i]] = i;
    priority_queue<int, vector<int>, greater<int>> que;
    rep(i,F+1) if(i<N) que.push(X[i]);
    rep(i,N){
        if(i+F < N) que.push(X[i + F]);
        while(true){
            int a = que.top(); que.pop();
            if(A[a] < i) continue;
            if(A[a] - i > F) continue;
            while(A[a] > i){
                int p = A[a];
                swap(X[p], X[p-1]);
                swap(A[X[p]], A[X[p-1]]);
                F--;
            }
            break;
        }
    }
    //cout << F << endl;
    //rep(i,N){
    //    if(i) cout << " ";
    //    cout << (X[i]+1);
    //} cout << "\n";
    rep(i,N) cout << (X[i]+1) << "\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    testcase();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5152
final_rank: 1

code_snippet: #include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip>
#include <functional>
#include<unordered_map>
#include <random>
#include<bitset>
#include<cassert>
using namespace std;  
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define arr(x) (x).rbegin(),(x).rend()
typedef long long ll;
typedef unsigned long long ull;
const ll inf=1e18;  
using graph = vector<vector<int> > ;
using vi=vector<int>;
using P= pair<ll,ll>;  
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vll=vector<ll>; 
using vvll=vector<vll>;
using vvvll=vector<vvll>;
using vp=vector<P>;
using vvp=vector<vp>;
using vd=vector<double>;
using vvd =vector<vd>;

const int mod=998244353;

struct UnionFind { vector<int> par, siz; UnionFind(int n) : par(n, -1) , siz(n, 1) { } int root(int x) { if (par[x] == -1) return x;
        else return par[x] = root(par[x]);
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    bool unite(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return false; 
        if (siz[x] < siz[y]) swap(x, y);
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }
    int size(int x) {
        return siz[root(x)];
    }
};
ll pow_pow(ll x,ll n,ll mod){
    if(n==0) return 1; 
    x%=mod;
    ll res=pow_pow(x*x%mod,n/2,mod);
    if(n&1)res=res*x%mod;
    return res;
}

template< typename Monoid >
struct SegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;

  int sz;
  vector< Monoid > seg;

  const F f;
  const Monoid M1;

  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while(sz < n) sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  void set(int k, const Monoid &x) {
    seg[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while(k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) L = f(L, seg[a++]);
      if(b & 1) R = f(seg[--b], R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) const {
    return seg[k + sz];
  }

  template< typename C >
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while(a < sz) {
      Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
      if(check(nxt)) a = 2 * a + type;
      else M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }


  template< typename C >
  int find_first(int a, const C &check) {
    Monoid L = M1;
    if(a <= 0) {
      if(check(f(L, seg[1]))) return find_subtree(1, check, L, false);
      return -1;
    }
    int b = sz;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) {
        Monoid nxt = f(L, seg[a]);
        if(check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }

  template< typename C >
  int find_last(int b, const C &check) {
    Monoid R = M1;
    if(b >= sz) {
      if(check(f(seg[1], R))) return find_subtree(1, check, R, true);
      return -1;
    }
    int a = sz;
    for(b += sz; a < b; a >>= 1, b >>= 1) {
      if(b & 1) {
        Monoid nxt = f(seg[--b], R);
        if(check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};
struct S{
    ll minv,num;
    S(ll minv=inf,ll num=0) :minv(minv),num(num) {}
};
S op(S a,S b){
    a.minv=min(a.minv,b.minv);
    a.num+=b.num;
    return a;
}

vll anss; 
void solve(int test){
    int n,k; cin >> n >> k;
    vi x(n);rep(i,n)cin >> x[i],x[i]--;
    vi invx(n);
    rep(i,n)invx[x[i]]=i;
    SegmentTree<S> seg(n,op,S());
    vi ans;
    ll inv=0;
    {
        SegmentTree<S> seginv(n,op,S());
        rep(i,n){
            inv+=seginv.query(x[i]+1,n).num;
            seginv.update(x[i],S(inf,1));
        }
    }
    if(inv<=k){
        rep(i,n)cout << x[i]+1 << endl;
        return;
    }
    rep(i,n)seg.update(i,S(x[i],1));
    ll res=inv-k;
    rep(i,n){
        {
            ll ac=0,wa=n;
            while(wa-ac>1){
                ll wj=(wa+ac)/2;
                if(seg.query(0,wj+1).num<=1+res)ac=wj;
                else wa=wj;
            }
            S p=seg.query(0,ac+1);
            int id=invx[p.minv];
            ans.push_back(x[id]+1);
            res-=seg.query(0,id+1).num-1;
            seg.update(id,S(inf,0));
        }
    }
    rep(i,n)cout << ans[i] << endl;
}
//g++ cf.cpp -std=c++17 -I .  
int main(){cin.tie(0);ios::sync_with_stdio(false);
    int t=1;//cin >>t;
    rep(test,t)solve(test);
    for(auto ans:anss){
        cout<< ans << endl;
    }
}
accuracy: 1
time_ms: 100
memory_kb: 12192
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

template <typename M, typename N, M (*f)(M, M), M (*g)(M, N), M (*m1)()>
struct SegmentTree {
    int sz, n;
    vector<M> data;
    SegmentTree(int _n = 0) : n(_n) {
        sz = 1;
        while (sz < _n)
            sz <<= 1;
        data.assign(2 * sz, m1());
    }
    void run(vector<M> &v) {
        for (int i = 0; i < (int)v.size(); i++)
            data[i + sz] = v[i];
        for (int k = sz - 1; k > 0; k--)
            data[k] = f(data[2 * k], data[2 * k + 1]);
    }
    void set(int k, const M &x) {
        k += sz;
        data[k] = x;
        while (k >>= 1)
            data[k] = f(data[2 * k], data[2 * k + 1]);
    }
    void update(int k, const N &x) {
        k += sz;
        data[k] = g(data[k], x);
        while (k >>= 1)
            data[k] = f(data[2 * k], data[2 * k + 1]);
    }
    M query(int a, int b) {
        M L = m1(), R = m1();
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1)
                L = f(L, data[a++]);
            if (b & 1)
                R = f(data[--b], R);
        }
        return f(L, R);
    }
    M operator[](const int &k) const {
        return data[k + sz];
    }
    vector<M> get() {
        return {data.begin() + sz, data.begin() + sz + n};
    }
    template <class F> int max_right(int L, F ch) const {
        int l = sz + L, w = 1;
        M ansL = m1();
        for (; L + w <= sz; l >>= 1, w <<= 1)
            if (l & 1) {
                if (not ch(f(ansL, data[l])))
                    break;
                ansL = f(ansL, data[l++]);
                L += w;
            }
        while (l <<= 1, w >>= 1) {
            if (L + w <= sz && ch(f(ansL, data[l]))) {
                ansL = f(ansL, data[l++]);
                L += w;
            }
        }
        return L;
    }
    template <class F> int min_left(int R, F ch) const {
        int r = sz + R, w = 1;
        M ansR = m1();
        for (; R - w >= 0; r >>= 1, w <<= 1)
            if (r & 1) {
                if (not ch(f(data[r - 1], ansR)))
                    break;
                ansR = f(data[--r], ansR);
                R -= w;
            }
        while (r <<= 1, w >>= 1) {
            if (R - w >= 0 && ch(f(data[r - 1], ansR))) {
                ansR = f(data[--r], ansR);
                R -= w;
            }
        }
        return R;
    }
};

/**
 * @brief Segment Tree
 */

ll f1(ll A, ll B) {return A+B;}
ll g1(ll A, ll B) {return A+B;}
ll e1() {return 0LL;}

ll f2(ll A, ll B) {return min(A,B);}
ll g2(ll A, ll B) {return min(A,B);}
ll e2() {return INF;}

int main() {
    ll N, K;
    cin >> N >> K;
    vector<int> A(N);
    rep(i,0,N) cin >> A[i], A[i]--;
    vector<int> RA(N);
    rep(i,0,N) RA[A[i]] = i;
    SegmentTree<ll,ll,f1,g1,e1> Seg(N);
    ll INV = 0;
    rep(i,0,N) {
        INV += Seg.query(A[i],N);
        Seg.set(A[i],1LL);
    }
    if (INV <= K) {
        rep(i,0,N) cout << A[i]+1 << endl;
        return 0;
    }
    ll Res = INV - K;
    rep(i,0,N) Seg.set(i,1);
    SegmentTree<ll,ll,f2,g2,e2> Seg2(N);
    rep(i,0,N) Seg2.set(i,A[i]);
    vector<int> ANS;
    rep(i,0,N) {
        int R = Seg.max_right(0,[&](ll X){return X-1 <= Res;});
        int MIN = Seg2.query(0,R);
        ANS.push_back(MIN);
        int ID = RA[MIN];
        Res -= Seg.query(0,ID);
        Seg.set(ID,0LL);
        Seg2.set(ID,INF);
    }
    rep(i,0,N) cout << ANS[i]+1 << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 8648
final_rank: 9

code_snippet: #line 1 "library/Template/template.hpp"
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}
#line 2 "library/Utility/fastio.hpp"
#include <unistd.h>
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf

uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio

using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */
#line 3 "sol.cpp"

#line 2 "library/DataStructure/bit.hpp"

template<typename T>struct BIT{
    int n; T all=0; vector<T> val;
    BIT(int _n=0):n(_n),val(_n+10){}
    void clear(){val.assign(n+10,0); all=T();}
    void add(int i,T x){
        for(i++;i<=n;i+=(i&-i))val[i]=val[i]+x;
        all+=x;
    }
    T sum(int i){
        T res=0;
        for(;i;i-=(i&-i))res+=val[i];
        return res;
    }
    T sum(int L,int R){return sum(R)-sum(L);} // [L,R)
    int lower_bound(T x){
        int ret=0,len=1;
        while(2*len<=n)len<<=1;
        for(;len>=1;len>>=1){
            if(ret+len<=n and val[ret+len]<x){
                ret+=len;
                x-=val[ret];
            }
        }
        return ret;
    }
};

/**
 * @brief Binary Indexed Tree
 */
#line 2 "library/DataStructure/segtree.hpp"

template <typename M, typename N, M (*f)(M, M), M (*g)(M, N), M (*m1)()>
struct SegmentTree {
    int sz, n;
    vector<M> data;
    SegmentTree(int _n = 0) : n(_n) {
        sz = 1;
        while (sz < _n)
            sz <<= 1;
        data.assign(2 * sz, m1());
    }
    void run(vector<M> &v) {
        for (int i = 0; i < (int)v.size(); i++)
            data[i + sz] = v[i];
        for (int k = sz - 1; k > 0; k--)
            data[k] = f(data[2 * k], data[2 * k + 1]);
    }
    void set(int k, const M &x) {
        k += sz;
        data[k] = x;
        while (k >>= 1)
            data[k] = f(data[2 * k], data[2 * k + 1]);
    }
    void update(int k, const N &x) {
        k += sz;
        data[k] = g(data[k], x);
        while (k >>= 1)
            data[k] = f(data[2 * k], data[2 * k + 1]);
    }
    M query(int a, int b) {
        M L = m1(), R = m1();
        for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if (a & 1)
                L = f(L, data[a++]);
            if (b & 1)
                R = f(data[--b], R);
        }
        return f(L, R);
    }
    M operator[](const int &k) const {
        return data[k + sz];
    }
    vector<M> get() {
        return {data.begin() + sz, data.begin() + sz + n};
    }
    template <class F> int max_right(int L, F ch) const {
        int l = sz + L, w = 1;
        M ansL = m1();
        for (; L + w <= sz; l >>= 1, w <<= 1)
            if (l & 1) {
                if (not ch(f(ansL, data[l])))
                    break;
                ansL = f(ansL, data[l++]);
                L += w;
            }
        while (l <<= 1, w >>= 1) {
            if (L + w <= sz && ch(f(ansL, data[l]))) {
                ansL = f(ansL, data[l++]);
                L += w;
            }
        }
        return L;
    }
    template <class F> int min_left(int R, F ch) const {
        int r = sz + R, w = 1;
        M ansR = m1();
        for (; R - w >= 0; r >>= 1, w <<= 1)
            if (r & 1) {
                if (not ch(f(data[r - 1], ansR)))
                    break;
                ansR = f(data[--r], ansR);
                R -= w;
            }
        while (r <<= 1, w >>= 1) {
            if (R - w >= 0 && ch(f(data[r - 1], ansR))) {
                ansR = f(data[--r], ansR);
                R -= w;
            }
        }
        return R;
    }
};

/**
 * @brief Segment Tree
 */
#line 6 "sol.cpp"

vector<int> a;
int f(int x, int y) {
    if (x == -1)
        return y;
    if (y == -1)
        return x;
    return a[x] < a[y] ? x : y;
}
int g(int x, int y) {
    return y;
}
int e() {
    return -1;
}

int main() {
    int n;
    ll k;
    read(n, k);
    a.resize(n);
    read(a);
    for (auto &x : a)
        x--;

    {
        k = -k;
        BIT<int> bit(n);
        rep(i, 0, n) {
            bit.add(a[i], 1);
            k += bit.sum(a[i] + 1, n);
        }
        if (k <= 0) {
            for (auto &x : a)
                x++;
            rep(i, 0, n) print(a[i]);
            return 0;
        }
    }

    SegmentTree<int, int, f, g, e> seg(n);
    rep(i, 0, n) seg.set(i, i);

    BIT<int> bit(n);
    rep(i, 0, n) bit.add(i, 1);
    vector<int> ret;
    while (k) {
        int R = -1;
        if (bit.all < k)
            R = n;
        else
            R = bit.lower_bound(k + 1) + 1;
        int x = seg.query(0, R);
        seg.update(x, -1);
        k -= bit.sum(0, x);
        bit.add(x, -1);
        ret.push_back(a[x]);
    }
    rep(i, 0, n) if (bit.sum(i, i + 1)) ret.push_back(a[i]);
    for (auto &x : ret)
        x++;
    rep(i, 0, n) print(ret[i]);
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 6108
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define foa(c, v) for (auto &c : v)
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) int(size(v))

template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec)
{
    os << "{ ";
    foa(c, vec)
    {
        os << c << ", ";
    }
    os << "}";
    return os;
}

template <class T>
ostream &operator<<(ostream &os, const vector<vector<T>> &vv)
{
    os << "{ ";
    foa(v, vv)
    {
        os << "\n\t" << v << ", ";
    }
    os << "\n}";
    return os;
}

#ifdef LOCAL
#define debug(x) cerr << __LINE__ << ", " << #x << ": " << x << endl;
#else
#define debug(x) void(0)
#endif

constexpr int inf = 1e9;
struct minst
{
    int n;
    vector<int> vec;
    minst(int len) : n(len)
    {
        vec.assign(n + n, inf);
    }

    void set(int idx, int val)
    {
        idx += n;
        vec[idx] = val;
        idx /= 2;
        while (idx)
        {
            vec[idx] = min(vec[idx << 1], vec[(idx << 1) | 1]);
            idx /= 2;
        }
    }

    int get(int left, int right)
    {
        left += n;
        right += n;
        int lv = inf, rv = inf;
        while (left < right)
        {
            if (left & 1)
            {
                lv = min(lv, vec[left]);
                left++;
            }
            if (right & 1)
            {
                right--;
                rv = min(vec[right], rv);
            }
            left /= 2;
            right /= 2;
        }
        return min(lv, rv);
    }
};

constexpr int e = 0;
int op(int a, int b) { return a + b; }
struct segtree
{
    int n;
    vector<int> vec;
    segtree(int len) : n(len)
    {
        vec.assign(n + n, e);
    }

    void set(int idx, int val)
    {
        idx += n;
        vec[idx] = val;
        idx /= 2;
        while (idx)
        {
            vec[idx] = op(vec[idx << 1], vec[(idx << 1) | 1]);
            idx /= 2;
        }
    }

    int get(int left, int right)
    {
        left += n;
        right += n;
        int lv = e, rv = e;
        while (left < right)
        {
            if (left & 1)
            {
                lv = op(lv, vec[left]);
                left++;
            }
            if (right & 1)
            {
                right--;
                rv = op(vec[right], rv);
            }
            left /= 2;
            right /= 2;
        }
        return op(lv, rv);
    }
};

int n;
void solve()
{
    ll k;
    cin >> k;
    vector<int> x(n);
    foa(c, x)
    {
        cin >> c;
        c--;
    }

    vector<int> pos(n);
    rep(i, n) pos[x[i]] = i;

    ll reduce;
    {
        ll inversion = 0;
        segtree st(n);

        rep(val, n)
        {
            debug(val);
            int p = pos[val];
            inversion += st.get(p + 1, n);
            st.set(p, 1);
            debug(inversion);
        }

        if (k >= inversion)
        {
            foa(c, x) cout << c + 1 << "\n";
            return;
        }
        reduce = ll(inversion - k);
    }

    segtree remain(n);
    minst remain_values(n);
    rep(i, n) remain_values.set(i, x[i]);
    rep(i, n) remain.set(i, 1);

    auto remove = [&](int place) -> void
    {
        remain_values.set(place, inf);
        remain.set(place, 0);
    };

    vector<int> ans(n, -1);

    debug(x);

    rep(place, n)
    {
        debug(place);
        int ok = 0;
        int ng = n + 1;
        while (abs(ok - ng) > 1)
        {
            int mid = (ok + ng) / 2;
            ((ll(remain.get(0, mid)) <= reduce + 1) ? ok : ng) = mid;
        }
        int val = remain_values.get(0, ok);
        remove(pos[val]);
        ans[place] = val;
        reduce -= remain.get(0, pos[val]);
    }

    foa(c, ans)
    {
        cout << c + 1 << "\n";
    }

    return;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    while (cin >> n)
    {
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 5864
final_rank: 4

code_snippet: #pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

struct BIT{
    int n;
    vector<ll> bit;
    BIT(int n):n(n),bit(n+1){}
    ll sum(int i){
        ll res = 0;
        for(;i>0;i-=(i&-i)) res+=bit[i];
        return res;
    }
    void add(int i,ll a){
        if(i == 0) return;
        for(;i<=n;i+=(i&-i)) bit[i] += a;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    ll K; cin >> K;
    vector<int> x(n);
    ll inv = 0;
    BIT bit(n);
    for (int i = 0; i < n; ++i) {
        cin >> x[i];
        inv += i-bit.sum(x[i]);
        bit.add(x[i], 1);
    }
    if(inv <= K){
        for (int i = 0; i < n; ++i) {
            cout << x[i] << "\n";
        }
        return 0;
    }
    vector<int> pos(n+1);
    BIT st(n);
    for (int i = 0; i < n; ++i) {
        pos[x[i]] = i+1;
        st.add(i+1, 1);
    }
    vector<int> v;
    for (int i = 1; i <= n; ++i) {
        int p = pos[i];
        int cnt = st.sum(p-1);
        if(inv-cnt >= K){
            inv -= cnt;
            cout << i << "\n";
            st.add(p, -1);
        }
        else{
            int pre = i;
            for(int k=i+1;k<=n;k++){
                int p = pos[k];
                int cnt = st.sum(p-1);
                if(inv-cnt >= K){
                    inv -= cnt;
                    cout << k << "\n";
                    st.add(p, -1);
                }
                else if(p < pos[pre]){
                    pre = k;
                }
                if(cnt == 0){
                    p = pos[pre];
                    cnt = st.sum(p-1);
                    if(inv-cnt >= K){
                        inv -= cnt;
                        cout << pre << "\n";
                        st.add(p, -1);
                        break;
                    }
                }
            }
            break;
        }
    }
    for (int i = 1; i <= n; ++i) {
        if(st.sum(i)-st.sum(i-1)){
            cout << x[i-1] << "\n";
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 5392
final_rank: 2

code_snippet: #ifdef noimi
#include <stdc++.h>
#else
#include <bits/stdc++.h>
#endif

#define ll long long
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define rep2(i, a, b) for(ll i = (a); i < (b); i++)
using namespace std;

template <typename T, typename F> struct segtree {
    int n, rn;
    vector<T> seg;
    F f;
    T id;
    segtree() = default;
    segtree(int rn, F f, T id) : rn(rn), f(f), id(id) {
        n = 1;
        while(n < rn) n <<= 1;
        seg.assign(n * 2, id);
    }
    void set(int i, T x) { seg[i + n] = x; }
    void build() {
        for(int k = n - 1; k > 0; k--) { seg[k] = f(seg[2 * k], seg[2 * k + 1]); }
    }
    void update(int i, T x) {
        set(i, x);
        i += n;
        while(i >>= 1) seg[i] = f(seg[i * 2], seg[i * 2 + 1]);
    }
    T get(int i) { return seg[i + n]; }
    T prod(int l, int r) {
        T L = id, R = id;
        for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if(l & 1) L = f(L, seg[l++]);
            if(r & 1) R = f(seg[--r], R);
        }
        return f(L, R);
    }

    template <typename C> int max_right(int l, const C &c) {
        assert(c(id));
        if(l >= rn) return rn;
        l += n;
        T sum = id;
        do {
            while((l & 1) == 0) l >>= 1;
            if(!c(f(sum, seg[l]))) {
                while(l < n) {
                    l <<= 1;
                    auto nxt = f(sum, seg[l]);
                    if(c(nxt)) {
                        sum = nxt;
                        l++;
                    }
                }
                return l - n;
            }
            sum = f(sum, seg[l++]);
        } while((l & -l) != l);
        return rn;
    }
    template <typename C> int min_left(int r, const C &c) {
        if(r <= 0) return -1;
        r += n;
        T sum = id;
        do {
            r--;
            while(r > 1 and (r & 1)) r >>= 1;
            if(!c(f(seg[r], sum))) {
                while(r < n) {
                    r = (r << 1) + 1;
                    auto nxt = f(seg[r], sum);
                    if(c(nxt)) {
                        sum = nxt;
                        r--;
                    }
                }
                return r + 1 - n;
            }
            sum = f(seg[r], sum);
        } while((r & -r) != r);
        return 0;
    }
};

// struct S {
//     int cnt, mi, pos;
//     friend ostream &operator<<(ostream &os, S s) { return os << "(" << s.cnt << ", " << s.mi << ", " << s.pos << ")"; }
// };
int main() {
    int n;
    ll k;
    cin >> n >> k;
    vector<int> a(n);
    for(auto &e : a) cin >> e;

    auto P = [](int x, int y) { return x + y; };
    {
        segtree<int, decltype(P)> seg(n, P, 0);
        vector<int> id(n);
        iota(begin(id), end(id), 0);
        sort(begin(id), end(id), [&](int i, int j) { return a[i] < a[j]; });
        ll sum = 0;
        for(auto i : id) {
            seg.update(i, 1);
            sum += seg.prod(i + 1, n);
        }
        k = sum - k;
        k = max(k, 0LL);
    }
    auto M = [](int x, int y) { return min(x, y); };
    const int inf = 1e9;

    segtree<int, decltype(P)> seg1(n, P, 0);
    segtree<int, decltype(M)> seg2(n, M, inf);
    vector<int> rev(n + 1);
    rep(i, n) rev[a[i]] = i;
    rep(i, n) seg1.set(i, 1);
    rep(i, n) seg2.set(i, a[i]);
    seg1.build(), seg2.build();

    vector<int> ans;
    rep(i, n) {
        int j = seg1.max_right(0, [&](int s) { return s - 1 <= k; });
        j = rev[seg2.prod(0, j)];

        k -= seg1.prod(0, j);
        ans.emplace_back(a[j]);

        seg1.update(j, 0);
        seg2.update(j, inf);
    }
    for(auto e : ans) cout << e << '\n';
}
accuracy: 1
time_ms: 40
memory_kb: 6788
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)

#define popcount __builtin_popcount
#define next __next
#define prev __prev

#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...)                                                               \
  cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif

// ---------------- Utility ------------------

template <class T>
bool chmin(T &a, const T &b) {
  if (a <= b) return false;
  a = b;
  return true;
}

template <class T>
bool chmax(T &a, const T &b) {
  if (a >= b) return false;
  a = b;
  return true;
}

template <class T>
using MaxHeap = priority_queue<T>;

template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

// ----------------- Input -------------------

template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}

// ----------------- Output ------------------

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ',' << p.second;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &e : v) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
  for (const T &e : d) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{ ";
  for (const T &e : s) os << e << " ";
  return os << "}";
}

template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << "{ ";
  for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
  return os << "}";
}

template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
  (..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}

template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << "(";
  dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
  return os << ")";
}

void dump_str_rec(ostringstream &) {}

template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << ", " << head;
  dump_str_rec(oss, forward<Tail>(tail)...);
}

template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
  ostringstream oss;
  oss << arg;
  dump_str_rec(oss, forward<U>(args)...);
  return oss.str();
}

// --------------- Fast I/O ------------------

void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

// ------------------ ACL --------------------

// #include <atcoder/modint>
// constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
// using mint = atcoder::static_modint<MOD>;
// ostream &operator<<(ostream &os, const mint &v) {
//   return os << v.val();
// }

// ------------ End of template --------------

#define endl "\n"
using ll = long long;
using pii = pair<int, int>;

const bool multitest = false;

template <class T>
struct BIT {
  int N;
  vector<T> bit;
  BIT(int N) : N(N) { bit = vector<T>(N + 1, T(0)); }
  void add(int i, T x) {
    i++;
    while (i <= N) {
      bit[i] += x;
      i += i & -i;
    }
    return;
  }
  T sum(int i) const {
    i++;
    T res = T(0);
    while (i > 0) {
      res += bit[i];
      i -= i & -i;
    }
    return res;
  }
  T sum(int l, int r) const { // [l,r]
    assert(l <= r);
    if (l == 0)
      return sum(r);
    else
      return sum(r) - sum(l - 1);
  }
};

template <class D>
struct SegmentTree {
  using DMerger = function<D(D, D)>;
  int length;
  vector<D> seg;
  const D d_unit;
  const DMerger dm;

  SegmentTree() {}
  SegmentTree(int n, const DMerger dm, const D &d_unit)
      : dm(dm), d_unit(d_unit) {
    length = 1;
    while (length < n) length <<= 1;
    seg.assign(2 * length, d_unit);
  }
  SegmentTree(vector<D> vec, const DMerger dm, const D &d_unit)
      : dm(dm), d_unit(d_unit) {
    length = 1;
    while (length < vec.size()) length <<= 1;
    seg.assign(2 * length, d_unit);
    for (int i = 0; i < vec.size(); i++) seg[length - 1 + i] = vec[i];
    for (int i = length - 2; i >= 0; i--)
      seg[i] = dm(seg[i * 2 + 1], seg[i * 2 + 2]);
  }

  void update(int k, D x) {
    k += length - 1;
    seg[k] = x;
    while (k) {
      k = (k - 1) / 2;
      seg[k] = dm(seg[k * 2 + 1], seg[k * 2 + 2]);
    }
  }

  D query(int a, int b, int k, int l, int r) const {
    if (r <= a || b <= l) {
      return d_unit;
    } else if (a <= l && r <= b) {
      return seg[k];
    } else {
      D lch = query(a, b, k * 2 + 1, l, (l + r) / 2);
      D rch = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return dm(lch, rch);
    }
  }
  D query(int a, int b) const { return query(a, b, 0, 0, length); }
  D get_point(int x) const { return seg[length - 1 + x]; }
};

void solve() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> x(N);
  cin >> x;

  ll rev = 0ll;
  {
    BIT<int> b(N + 1);
    for (int i = 0; i < N; i++) {
      rev += b.sum(x[i], N);
      b.add(x[i], 1);
    }
  }

  if (rev <= K) {
    for (int i = 0; i < N; i++) cout << x[i] << endl;
    return;
  }

  ll num_swap = rev - K;
  BIT<int> cnt(N);
  for (int i = 0; i < N; i++) cnt.add(i, 1);

  SegmentTree<pii> seg(
      x.size(), [](pii a, pii b) { return min(a, b); }, pii(INF, INF));
  for (int i = 0; i < N; i++) seg.update(i, pii(x[i], i));

  vector<int> ans;

  for (int i = 0; i < N; i++) {
    int l = 0, r = N;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (cnt.sum(0, mid) - 1 > num_swap)
        r = mid;
      else
        l = mid;
    }
    auto [val, idx] = seg.query(0, l + 1);
    assert(cnt.sum(idx, idx) == 1);
    ans.push_back(val);
    num_swap -= cnt.sum(0, idx) - 1;
    cnt.add(idx, -1);
    seg.update(idx, pii(INF, INF));
  }

  for (int i = 0; i < N; i++) cout << ans[i] << endl;

  return;
}

int main() {
  fastio();
  if (!multitest) {
    solve();
  } else {
    cerr << "[Warning] Multi testcase mode on" << endl;
    int t;
    cin >> t;
    while (t--) solve();
  }
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 6812
final_rank: 7

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
//const int mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}
	
	ll get(T k){
		if(k <= 0) return 0; 
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

int main(){
	ll n,k; cin >> n >> k;
	vl a(n); rep(i,n) cin >> a[i], a[i]--;
	BIT<ll> bit1(n), bit2(n);
	ll inv = 0;
	rep(i,n){
		inv += bit1.sum(n-1) - bit1.sum(a[i]);
		bit1.add(a[i],1);
	}
	if(inv <= k){
		rep(i,n) cout << a[i]+1 << "\n";
		return 0;
	}
	k = inv - k;
	vl rev(n);
	rep(i,n) rev[a[i]] = i;
	vl ans;
	vb moved(n,false);
	int mn = inf;
	priority_queue<P,vpl,greater<P>> pq;
	int cnt = 0;
	rep(i,n){
		if(moved[i]) continue;
		ll rem = bit2.sum(rev[i]);
		if(rev[i] + rem <= k){
			k -= rev[i] + rem;
			ans.push_back(i);
			moved[i] = true;
			bit2.add(rev[i],-1);
			cnt -= rev[i] + rem - 1;
			while(!pq.empty() && pq.top().first == cnt){
				chmin(mn,pq.top().second);
				pq.pop();
			}
			if(mn == inf) continue;
			ans.push_back(mn);
			moved[mn] = true;
			break;
		}else{
			pq.emplace(cnt+rev[i]+rem-k,i);
		}
		if(k == 0) break;
	}
	rep(i,n) if(!moved[a[i]]) ans.push_back(a[i]);
	rep(i,n) cout << ans[i]+1 << "\n";
}
accuracy: 1
time_ms: 20
memory_kb: 7916
final_rank: 6

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
//const int mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}
	
	ll get(T k){
		if(k <= 0) return 0; 
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;
  using G = function< Monoid(Monoid, OperatorMonoid) >;
  using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;

  int sz, height;
  vector< Monoid > data;
  vector< OperatorMonoid > lazy;
  const F f;
  const G g;
  const H h;
  const Monoid M1;
  const OperatorMonoid OM0;


  LazySegmentTree(int n, const F f, const G g, const H h,
                  const Monoid &M1, const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    height = 0;
    while(sz < n) sz <<= 1, height++;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) {
    data[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; k--) {
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
    }
  }

  inline void propagate(int k) {
    if(lazy[k] != OM0) {
      lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      data[k] = reflect(k);
      lazy[k] = OM0;
    }
  }

  inline Monoid reflect(int k) {
    return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
  }

  inline void recalc(int k) {
    while(k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
  }

  inline void thrust(int k) {
    for(int i = height; i > 0; i--) propagate(k >> i);
  }

  void update(int a, int b, const OperatorMonoid &x) {
    thrust(a += sz);
    thrust(b += sz - 1);
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) lazy[l] = h(lazy[l], x), ++l;
      if(r & 1) --r, lazy[r] = h(lazy[r], x);
    }
    recalc(a);
    recalc(b);
  }

  Monoid query(int a, int b) {
    thrust(a += sz);
    thrust(b += sz - 1);
    Monoid L = M1, R = M1;
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) L = f(L, reflect(l++));
      if(r & 1) R = f(reflect(--r), R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) {
    return query(k, k + 1);
  }

  template< typename C >
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while(a < sz) {
      propagate(a);
      Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
      if(check(nxt)) a = 2 * a + type;
      else M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }

  template< typename C >
  int find_first(int a, const C &check) {
    Monoid L = M1;
    if(a <= 0) {
      if(check(f(L, reflect(1)))) return find_subtree(1, check, L, false);
      return -1;
    }
    thrust(a + sz);
    int b = sz;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) {
        Monoid nxt = f(L, reflect(a));
        if(check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }


  template< typename C >
  int find_last(int b, const C &check) {
    Monoid R = M1;
    if(b >= sz) {
      if(check(f(reflect(1), R))) return find_subtree(1, check, R, true);
      return -1;
    }
    thrust(b + sz - 1);
    int a = sz;
    for(b += sz; a < b; a >>= 1, b >>= 1) {
      if(b & 1) {
        Monoid nxt = f(reflect(--b), R);
        if(check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};

ll f(ll a, ll b){return min(a,b);}
ll g(ll a, ll b){return a+b;}

int main(){
	ll n,k; cin >> n >> k;
	vl a(n); rep(i,n) cin >> a[i], a[i]--;
	BIT<ll> bit1(n), bit2(n);
	ll inv = 0;
	rep(i,n){
		inv += bit1.sum(n-1) - bit1.sum(a[i]);
		bit1.add(a[i],1);
	}
	if(inv <= k){
		rep(i,n) cout << a[i]+1 << "\n";
		return 0;
	}
	k = inv - k;
	vl rev(n);
	rep(i,n) rev[a[i]] = i;
	vl ans;
	vb moved(n,false);
	ll cnt = 0, mn = inf;
	LazySegmentTree<ll> seg(n,f,g,g,linf,0);
	rep(i,n){
		if(moved[i]) continue;
		ll rem = bit2.sum(rev[i]);
		if(rev[i] + rem <= k){
			k -= rev[i] + rem;
			ans.push_back(i);
			moved[i] = true;
			cnt -= rev[i] + rem;
			bit2.add(rev[i],-1);
			seg.update(rev[i],n,-1);
			auto check = [&](ll a){return a <= cnt;};
			while(1){
				int id = seg.find_first(0,check);
			 	if(id == -1) break;
				chmin(mn, a[id]);
				seg.update(id,id+1,linf);
			}
			if(mn == inf) continue;
			ans.push_back(mn);
			moved[mn] = true;
			break;
		}else{
			ll qu = seg.query(rev[i],rev[i]+1);
			seg.update(rev[i],rev[i]+1,rev[i]+cnt+rem-k-qu);
		}
		if(k == 0) break;
	}
	rep(i,n) if(!moved[a[i]]) ans.push_back(a[i]);
	rep(i,n) cout << ans[i]+1 << "\n";
}
accuracy: 1
time_ms: 70
memory_kb: 11348
final_rank: 11

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
//const int mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}
	
	ll get(T k){
		if(k <= 0) return 0; 
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;
  using G = function< Monoid(Monoid, OperatorMonoid) >;
  using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;

  int sz, height;
  vector< Monoid > data;
  vector< OperatorMonoid > lazy;
  const F f;
  const G g;
  const H h;
  const Monoid M1;
  const OperatorMonoid OM0;


  LazySegmentTree(int n, const F f, const G g, const H h,
                  const Monoid &M1, const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    height = 0;
    while(sz < n) sz <<= 1, height++;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) {
    data[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; k--) {
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
    }
  }

  inline void propagate(int k) {
    if(lazy[k] != OM0) {
      lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      data[k] = reflect(k);
      lazy[k] = OM0;
    }
  }

  inline Monoid reflect(int k) {
    return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
  }

  inline void recalc(int k) {
    while(k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
  }

  inline void thrust(int k) {
    for(int i = height; i > 0; i--) propagate(k >> i);
  }

  void update(int a, int b, const OperatorMonoid &x) {
    thrust(a += sz);
    thrust(b += sz - 1);
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) lazy[l] = h(lazy[l], x), ++l;
      if(r & 1) --r, lazy[r] = h(lazy[r], x);
    }
    recalc(a);
    recalc(b);
  }

  Monoid query(int a, int b) {
    thrust(a += sz);
    thrust(b += sz - 1);
    Monoid L = M1, R = M1;
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) L = f(L, reflect(l++));
      if(r & 1) R = f(reflect(--r), R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) {
    return query(k, k + 1);
  }

  template< typename C >
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while(a < sz) {
      propagate(a);
      Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
      if(check(nxt)) a = 2 * a + type;
      else M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }

  template< typename C >
  int find_first(int a, const C &check) {
    Monoid L = M1;
    if(a <= 0) {
      if(check(f(L, reflect(1)))) return find_subtree(1, check, L, false);
      return -1;
    }
    thrust(a + sz);
    int b = sz;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) {
        Monoid nxt = f(L, reflect(a));
        if(check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }


  template< typename C >
  int find_last(int b, const C &check) {
    Monoid R = M1;
    if(b >= sz) {
      if(check(f(reflect(1), R))) return find_subtree(1, check, R, true);
      return -1;
    }
    thrust(b + sz - 1);
    int a = sz;
    for(b += sz; a < b; a >>= 1, b >>= 1) {
      if(b & 1) {
        Monoid nxt = f(reflect(--b), R);
        if(check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};

ll f(ll a, ll b){return min(a,b);}
ll g(ll a, ll b){return a+b;}

int main(){
	int n,k; cin >> n >> k;
	vl a(n); rep(i,n) cin >> a[i], a[i]--;
	BIT<ll> bit1(n), bit2(n);
	ll inv = 0;
	rep(i,n){
		inv += bit1.sum(n-1) - bit1.sum(a[i]);
		bit1.add(a[i],1);
	}
	if(inv <= k){
		rep(i,n) cout << a[i]+1 << "\n";
		return 0;
	}
	k = inv - k;
	vl rev(n);
	rep(i,n) rev[a[i]] = i;
	vl ans;
	vb moved(n,false);
	ll cnt = 0, mn = inf;
	LazySegmentTree<ll> seg(n,f,g,g,linf,0);
	rep(i,n){
		if(moved[i]) continue;
		ll rem = bit2.sum(rev[i]);
		if(rev[i] + rem <= k){
			k -= rev[i] + rem;
			ans.push_back(i);
			moved[i] = true;
			cnt -= rev[i] + rem;
			bit2.add(rev[i],-1);
			seg.update(rev[i],n,-1);
			auto check = [&](ll a){return a <= cnt;};
			while(1){
				int id = seg.find_first(0,check);
			 	if(id == -1) break;
				chmin(mn, a[id]);
				seg.update(id,id+1,linf);
			}
			if(mn == inf) continue;
			ans.push_back(mn);
			moved[mn] = true;
			break;
		}else{
			ll qu = seg.query(rev[i],rev[i]+1);
			seg.update(rev[i],rev[i]+1,rev[i]+cnt+rem-k-qu);
		}
		if(k == 0) break;
	}
	rep(i,n) if(!moved[a[i]]) ans.push_back(a[i]);
	rep(i,n) cout << ans[i]+1 << "\n";
}
accuracy: 0.6071428571428571
time_ms: 50
memory_kb: 11392
final_rank: 18

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
//const int mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}
	
	ll get(T k){
		if(k <= 0) return 0; 
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;
  using G = function< Monoid(Monoid, OperatorMonoid) >;
  using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;

  int sz, height;
  vector< Monoid > data;
  vector< OperatorMonoid > lazy;
  const F f;
  const G g;
  const H h;
  const Monoid M1;
  const OperatorMonoid OM0;


  LazySegmentTree(int n, const F f, const G g, const H h,
                  const Monoid &M1, const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    height = 0;
    while(sz < n) sz <<= 1, height++;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) {
    data[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; k--) {
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
    }
  }

  inline void propagate(int k) {
    if(lazy[k] != OM0) {
      lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      data[k] = reflect(k);
      lazy[k] = OM0;
    }
  }

  inline Monoid reflect(int k) {
    return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
  }

  inline void recalc(int k) {
    while(k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
  }

  inline void thrust(int k) {
    for(int i = height; i > 0; i--) propagate(k >> i);
  }

  void update(int a, int b, const OperatorMonoid &x) {
    thrust(a += sz);
    thrust(b += sz - 1);
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) lazy[l] = h(lazy[l], x), ++l;
      if(r & 1) --r, lazy[r] = h(lazy[r], x);
    }
    recalc(a);
    recalc(b);
  }

  Monoid query(int a, int b) {
    thrust(a += sz);
    thrust(b += sz - 1);
    Monoid L = M1, R = M1;
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) L = f(L, reflect(l++));
      if(r & 1) R = f(reflect(--r), R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) {
    return query(k, k + 1);
  }

  template< typename C >
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while(a < sz) {
      propagate(a);
      Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
      if(check(nxt)) a = 2 * a + type;
      else M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }

  template< typename C >
  int find_first(int a, const C &check) {
    Monoid L = M1;
    if(a <= 0) {
      if(check(f(L, reflect(1)))) return find_subtree(1, check, L, false);
      return -1;
    }
    thrust(a + sz);
    int b = sz;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) {
        Monoid nxt = f(L, reflect(a));
        if(check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }


  template< typename C >
  int find_last(int b, const C &check) {
    Monoid R = M1;
    if(b >= sz) {
      if(check(f(reflect(1), R))) return find_subtree(1, check, R, true);
      return -1;
    }
    thrust(b + sz - 1);
    int a = sz;
    for(b += sz; a < b; a >>= 1, b >>= 1) {
      if(b & 1) {
        Monoid nxt = f(reflect(--b), R);
        if(check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};

ll f(ll a, ll b){return min(a,b);}
ll g(ll a, ll b){return a+b;}

int main(){
	int n,k; cin >> n >> k;
	vl a(n); rep(i,n) cin >> a[i], a[i]--;
	BIT<ll> bit1(n), bit2(n);
	ll inv = 0;
	rep(i,n){
		inv += bit1.sum(n-1) - bit1.sum(a[i]);
		bit1.add(a[i],1);
	}
	if(inv <= k){
		rep(i,n) cout << a[i]+1 << "\n";
		return 0;
	}
	k = inv - k;
	vl rev(n);
	rep(i,n) rev[a[i]] = i;
	vl ans;
	vb moved(n,false);
	ll cnt = 0, mn = inf;
	LazySegmentTree<ll> seg(n,f,g,g,linf,0);
	rep(i,n){
		if(moved[i]) continue;
		ll rem = bit2.sum(rev[i]);
		if(rev[i] + rem <= k){
			k -= rev[i] + rem;
			ans.push_back(i);
			moved[i] = true;
			cnt -= rev[i] + rem;
			bit2.add(rev[i],-1);
			seg.update(rev[i],n,-1);
			auto check = [&](ll a){return a <= cnt;};
			while(1){
				int id = seg.find_first(0,check);
			 	if(id == -1) break;
				assert(seg.query(0,id+1) == cnt);
				chmin(mn, a[id]);
				seg.update(id,id+1,inf);
			}
			if(mn == inf) continue;
			ans.push_back(mn);
			moved[mn] = true;
			break;
		}else{
			ll qu = seg.query(rev[i],rev[i]+1);
			seg.update(rev[i],rev[i]+1,rev[i]+cnt+rem-k-qu);
		}
		if(k == 0) break;
	}
	rep(i,n) if(!moved[a[i]]) ans.push_back(a[i]);
	rep(i,n) cout << ans[i]+1 << "\n";
}
accuracy: 0.6071428571428571
time_ms: 50
memory_kb: 11260
final_rank: 17

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
//const int mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}
	
	ll get(T k){
		if(k <= 0) return 0; 
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;
  using G = function< Monoid(Monoid, OperatorMonoid) >;
  using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;

  int sz, height;
  vector< Monoid > data;
  vector< OperatorMonoid > lazy;
  const F f;
  const G g;
  const H h;
  const Monoid M1;
  const OperatorMonoid OM0;


  LazySegmentTree(int n, const F f, const G g, const H h,
                  const Monoid &M1, const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    height = 0;
    while(sz < n) sz <<= 1, height++;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) {
    data[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; k--) {
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
    }
  }

  inline void propagate(int k) {
    if(lazy[k] != OM0) {
      lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      data[k] = reflect(k);
      lazy[k] = OM0;
    }
  }

  inline Monoid reflect(int k) {
    return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
  }

  inline void recalc(int k) {
    while(k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
  }

  inline void thrust(int k) {
    for(int i = height; i > 0; i--) propagate(k >> i);
  }

  void update(int a, int b, const OperatorMonoid &x) {
    thrust(a += sz);
    thrust(b += sz - 1);
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) lazy[l] = h(lazy[l], x), ++l;
      if(r & 1) --r, lazy[r] = h(lazy[r], x);
    }
    recalc(a);
    recalc(b);
  }

  Monoid query(int a, int b) {
    thrust(a += sz);
    thrust(b += sz - 1);
    Monoid L = M1, R = M1;
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) L = f(L, reflect(l++));
      if(r & 1) R = f(reflect(--r), R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) {
    return query(k, k + 1);
  }

  template< typename C >
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while(a < sz) {
      propagate(a);
      Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
      if(check(nxt)) a = 2 * a + type;
      else M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }

  template< typename C >
  int find_first(int a, const C &check) {
    Monoid L = M1;
    if(a <= 0) {
      if(check(f(L, reflect(1)))) return find_subtree(1, check, L, false);
      return -1;
    }
    thrust(a + sz);
    int b = sz;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) {
        Monoid nxt = f(L, reflect(a));
        if(check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }


  template< typename C >
  int find_last(int b, const C &check) {
    Monoid R = M1;
    if(b >= sz) {
      if(check(f(reflect(1), R))) return find_subtree(1, check, R, true);
      return -1;
    }
    thrust(b + sz - 1);
    int a = sz;
    for(b += sz; a < b; a >>= 1, b >>= 1) {
      if(b & 1) {
        Monoid nxt = f(reflect(--b), R);
        if(check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};

ll f(ll a, ll b){return min(a,b);}
ll g(ll a, ll b){return a+b;}

int main(){
	int n,k; cin >> n >> k;
	vl a(n); rep(i,n) cin >> a[i], a[i]--;
	BIT<ll> bit1(n), bit2(n);
	ll inv = 0;
	rep(i,n){
		inv += bit1.sum(n-1) - bit1.sum(a[i]);
		bit1.add(a[i],1);
	}
	if(inv <= k){
		rep(i,n) cout << a[i]+1 << "\n";
		return 0;
	}
	k = inv - k;
	vl rev(n);
	rep(i,n) rev[a[i]] = i;
	vl ans;
	vb moved(n,false);
	ll cnt = 0, mn = inf;
	LazySegmentTree<ll> seg(n,f,g,g,linf,0);
	rep(i,n){
		if(moved[i]) continue;
		ll rem = bit2.sum(rev[i]);
		if(rev[i] + rem <= k){
			k -= rev[i] + rem;
			ans.push_back(i);
			moved[i] = true;
			cnt -= rev[i] + rem;
			bit2.add(rev[i],-1);
			seg.update(rev[i],n,-1);
			auto check = [&](ll a){return a <= cnt;};
			while(1){
				int id = seg.find_first(0,check);
			 	if(id == -1) break;
				chmin(mn, a[id]);
				seg.update(id,id+1,inf);
			}
			if(mn == inf) continue;
			ans.push_back(mn);
			moved[mn] = true;
			break;
		}else{
			ll qu = seg.query(rev[i],rev[i]+1);
			seg.update(rev[i],rev[i]+1,rev[i]+cnt+rem-k-qu);
		}
		if(k == 0) break;
	}
	rep(i,n) if(!moved[a[i]]) ans.push_back(a[i]);
	rep(i,n) cout << ans[i]+1 << "\n";
}
accuracy: 0.6071428571428571
time_ms: 50
memory_kb: 11244
final_rank: 16

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
//const int mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}
	
	ll get(T k){
		if(k <= 0) return 0; 
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

int main(){
	int n,k; cin >> n >> k;
	vl a(n); rep(i,n) cin >> a[i], a[i]--;
	BIT<ll> bit1(n), bit2(n);
	ll inv = 0;
	rep(i,n){
		inv += bit1.sum(n-1) - bit1.sum(a[i]);
		bit1.add(a[i],1);
	}
	if(inv <= k){
		rep(i,n) cout << a[i]+1 << "\n";
		return 0;
	}
	k = inv - k;
	vl rev(n);
	rep(i,n) rev[a[i]] = i;
	vl ans;
	vb moved(n,false);
	int mn = inf;
	priority_queue<P,vpl,greater<P>> pq;
	int cnt = 0;
	rep(i,n){
		if(moved[i]) continue;
		ll rem = bit2.sum(rev[i]);
		if(rev[i] + rem <= k){
			k -= rev[i] + rem;
			ans.push_back(i);
			moved[i] = true;
			bit2.add(rev[i],-1);
			cnt -= rev[i] + rem - 1;
			while(!pq.empty() && pq.top().first == cnt){
				chmin(mn,pq.top().second);
				pq.pop();
			}
			if(mn == inf) continue;
			ans.push_back(mn);
			moved[mn] = true;
			break;
		}else{
			pq.emplace(cnt+rev[i]+rem-k,i);
		}
		if(k == 0) break;
	}
	rep(i,n) if(!moved[a[i]]) ans.push_back(a[i]);
	rep(i,n) cout << ans[i]+1 << "\n";
}
accuracy: 0.6071428571428571
time_ms: 20
memory_kb: 9424
final_rank: 14

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
//const int mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}
	
	ll get(T k){
		if(k <= 0) return 0; 
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

int main(){
	int n,k; cin >> n >> k;
	vl a(n); rep(i,n) cin >> a[i], a[i]--;
	BIT<ll> bit1(n), bit2(n);
	ll inv = 0;
	rep(i,n){
		inv += bit1.sum(n-1) - bit1.sum(a[i]);
		bit1.add(a[i],1);
	}
	if(inv <= k){
		rep(i,n) cout << a[i]+1 << "\n";
		return 0;
	}
	k = inv - k;
	vl rev(n);
	rep(i,n) rev[a[i]] = i;
	vl ans;
	vb moved(n,false);
	int mn = inf;
	priority_queue<P,vpl,greater<P>> pq;
	int cnt = 0;
	rep(i,n){
		if(moved[i]) continue;
		ll rem = bit2.sum(rev[i]);
		if(rev[i] + rem <= k){
			k -= rev[i] + rem;
			ans.push_back(i);
			moved[i] = true;
			bit2.add(rev[i],-1);
			if(rev[i] + rem == 0){
				cnt++;
				while(!pq.empty() && pq.top().first == cnt){
					chmin(mn,pq.top().second);
					pq.pop();
				}
				if(mn == inf) continue;
				ans.push_back(mn);
				moved[mn] = true;
				break;
			}else{
				cnt -= rev[i] + rem - 1;
			}
		}else{
			pq.emplace(cnt+rev[i]+rem-k,i);
		}
		if(k == 0) break;
	}
	rep(i,n) if(!moved[a[i]]) ans.push_back(a[i]);
	rep(i,n) cout << ans[i]+1 << "\n";
}
accuracy: 0.6071428571428571
time_ms: 20
memory_kb: 9452
final_rank: 15

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
//const int mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}
	
	ll get(T k){
		if(k <= 0) return 0; 
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

int main(){
	int n,k; cin >> n >> k;
	vl a(n); rep(i,n) cin >> a[i], a[i]--;
	BIT<ll> bit1(n), bit2(n);
	ll inv = 0;
	rep(i,n){
		inv += bit1.sum(n-1) - bit1.sum(a[i]);
		bit1.add(a[i],1);
	}
	if(inv <= k){
		rep(i,n) cout << a[i]+1 << "\n";
		return 0;
	}
	k = inv - k;
	vl rev(n);
	rep(i,n) rev[a[i]] = i;
	vl ans;
	vb moved(n,false);
	int mn = inf;
	priority_queue<P,vpl,greater<P>> pq;
	int cnt = 0;
	rep(i,n){
		if(moved[i]) continue;
		ll rem = bit2.sum(rev[i]);
		if(rev[i] + rem <= k){
			k -= rev[i] + rem;
			ans.push_back(i);
			moved[i] = true;
			bit2.add(rev[i],-1);
			if(rev[i] + rem == 0){
				cnt++;
				while(!pq.empty() && pq.top().first == cnt){
					chmin(mn,pq.top().second);
					pq.pop();
				}
				if(mn == inf) continue;
				ans.push_back(mn);
				moved[mn] = true;
				break;
			}
		}else{
			pq.emplace(cnt+rev[i]+rem-k,i);
		}
		if(k == 0) break;
	}
	rep(i,n) if(!moved[a[i]]) ans.push_back(a[i]);
	rep(i,n) cout << ans[i]+1 << "\n";
}
accuracy: 0.17857142857142858
time_ms: 20
memory_kb: 6868
final_rank: 20

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-9;
//const int mod = 1e9 + 7;
const int mod = 998244353;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}
	
	ll get(T k){
		if(k <= 0) return 0; 
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

int main(){
	int n,k; cin >> n >> k;
	vl a(n); rep(i,n) cin >> a[i], a[i]--;
	BIT<ll> bit1(n), bit2(n);
	ll inv = 0;
	rep(i,n){
		inv += bit1.sum(n-1) - bit1.sum(a[i]);
		bit1.add(a[i],1);
	}
	if(inv <= k){
		rep(i,n) cout << a[i]+1 << "\n";
		return 0;
	}
	k = inv - k;
	vl rev(n);
	rep(i,n) rev[a[i]] = i;
	vl ans;
	vb moved(n,false);
	int mn = inf;
	priority_queue<P,vpl,greater<P>> pq;
	int cnt = 0;
	rep(i,n){
		if(moved[i]) continue;
		ll rem = bit2.sum(rev[i]);
		if(rev[i] + rem <= k){
			k -= rev[i] + rem;
			ans.push_back(i);
			moved[i] = true;
			bit2.add(rev[i],-1);
			if(rev[i] + rem == 0){
				cnt++;
				while(!pq.empty() && pq.top().first == cnt){
					chmin(mn,pq.top().second);
					pq.pop();
				}
				if(mn == inf) continue;
				ans.push_back(mn);
				moved[mn] = true;
				break;
			}
		}else if(rev[i] + rem == k+1){
			pq.emplace(cnt+rev[i]+rem-k,i);
		}
		if(k == 0) break;
	}
	rep(i,n) if(!moved[a[i]]) ans.push_back(a[i]);
	rep(i,n) cout << ans[i]+1 << "\n";
}
accuracy: 0.17857142857142858
time_ms: 20
memory_kb: 6644
final_rank: 19

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,LL> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
template<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++){
        if(i)os<<" ";
        os<<v1[i];
    }
    return os;
}
template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    os<<p.first<<" "<<p.second;
    return os;
}
template<typename T> istream& operator>>(istream& is,vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++)is>>v1[i];
    return is;
}
template<typename T1,typename T2> istream& operator>>(istream& is,pair<T1,T2>& p){
    is>>p.first>>p.second;
    return is;
}

template<typename T> class bit{
private:
    vector<T> bit_vec;
    int bit_N=20;

    T bit_comp(T a,T b){
        return a+b;
    }

public:

    bit(int n){
        for(bit_N=0;bit_N<30;bit_N++){
            if(n<(1<<bit_N))break;
        }
        bit_vec.assign((1<<bit_N),0);
    }

    void set(int pos,T x){
        bit_vec[pos]=bit_comp(bit_vec[pos],x);
        for(int i=0;i<bit_N;i++){
            if(pos&(1<<i))continue;
            pos|=(1<<i);
            bit_vec[pos]=bit_comp(bit_vec[pos],x);
        }
    }

    T search(int pos){
        T s=bit_vec[pos];
        for(int i=0;pos>=(1<<i);i++){
            if(pos&(1<<i))continue;
            pos-=(1<<i);
            s=bit_comp(s,bit_vec[pos]);
        }
        return s;
    }
};

template <typename T> class seg_tree{
    //monoid
private:
    function<T(T,T)> func;
    T e;
    int N,n=-1;
    vector<T> seg;

    void init(){
        assert(n>=0);
        int i;
        for(i=0;(1<<i)<n;i++);
        N=(1<<i)-1;
        seg.assign(2*(N+1),e);
    }

    void init_reload(){
        for(int i=N-1;i>=0;i--){
            seg[i]=func(seg[2*i+1],seg[2*i+2]);
        }
    }
    
    void update(int pos){
        T a=func(seg[pos*2+1],seg[pos*2+2]);
        if(seg[pos]==a)return;
        seg[pos]=a;
        if(pos==0)return;
        update((pos-1)/2);
    }

public:
    seg_tree(function<T(T,T)> _func,T _e,int _n):func(_func),e(_e),n(_n){
        init();
    }
    seg_tree(function<T(T,T)> _func,T _e,vector<T> vec):func(_func),e(_e){
        n=vec.size();
        init();
        for(int i=0;i<n;i++){
            seg[N+i]=vec[i];
        }
        init_reload();
    }
    seg_tree(function<T(T,T)> _func,T _e,int _n,T a):func(_func),e(_e),n(_n){
        init(e);
        for(int i=0;i<n;i++){
            seg[N+i]=a;
        }
        init_reload();
    }

    int size()const{
        return n;
    }

    void set(int pos,T a){
        assert(pos>=0 && pos<=N);
        pos+=N;
        seg[pos]=a;
        update((pos-1)/2);
    }

    T get(const int pos)const{
        return seg[N+pos];
    }

    T search(int a,int b,int l,int r,int x){//[a,b) search
        if(a<=l && r<=b)return seg[x];
        int m=(l+r)/2;
        if(b<=m)return search(a,b,l,m,2*x+1);
        if(m<=a)return search(a,b,m,r,2*x+2);
        return func(search(a,m,l,m,2*x+1),search(m,b,m,r,2*x+2));
    }
    T search(int a,int b){
        assert(a<b);
        return search(a,b,0,N+1,0);
    }
    T search(){
        return search(0,size());
    }

    int max_right(function<bool(T)>& g,int pos,int l,int r,int x,T& y){
        //suppose that S is return value, g(func(pos,..,S-1))=true,g(func(pos,..,S))=false
        if(pos<=l && g(func(y,seg[x]))){
            y=func(y,seg[x]);
            return r;
        }
        if(l+1==r)return l;
        int m=(l+r)/2;
        if(pos<m){
            int s=max_right(g,pos,l,m,2*x+1,y);
            if(s<m)return s;
        }
        return max_right(g,pos,m,r,2*x+2,y);
    }
    int max_right(function<bool(T)> g,int pos){
        T y=e;
        int s=max_right(g,pos,0,N+1,0,y);
        return min(s,n);
    }

    int min_left(function<bool(T)>& g,int pos,int l,int r,int x,T& y){
        //suppose that S is return value, g(func(S,..,pos-1))=true,g(func(S-1,..,pos-1))=false
        int s;
        if(r<=pos && g(func(seg[x],y))){
            y=func(seg[x],y);
            return l;
        }
        if(l+1==r)return r;
        int m=(l+r)/2;
        if(m<pos){
            s=min_left(g,pos,m,r,2*x+2,y);
            if(m<s)return s;
        }
        return min_left(g,pos,l,m,2*x+1,y);
    }
    int min_left(function<bool(T)> g,int pos){
        assert(pos>=0);
        if(pos==0)return 0;
        T y=e;
        return min_left(g,pos,0,N+1,0,y);
    }
};

namespace sol{

    void solve(){
        LL n,m;
        int i,j,k;
        LL a,b,c;
        cin>>n>>m;
        vector<LL> v1(n),v2(n),vs(n);
        cin>>v1;
        bit<LL> bt(n),bs(n);
        seg_tree<P> seg([](P a,P b){
            if(a.first<b.first)return a;
            else return b;
        },{INF,0},n);
        for(i=0;i<n;i++){
            v1[i]--;
            m-=i-bt.search(v1[i]);
            bt.set(v1[i],1);
            v2[v1[i]]=i;
        }
        m=-m;
        for(i=0;i<n && 0<m;i++){
            a=v2[i]-bs.search(v2[i]);
            if(m<a)break;
            m-=a;
            bs.set(v2[i],1);
            vs[i]=i;
        }
        for(j=0,k=i;j<n;j++){
            if(v1[j]<i)continue;
            vs[k++]=v1[j];
        }
        for(i=0;i<n;i++){
            seg.set(i,{vs[i],i});
        }
        for(i=0;m>0 && i<n;i++){
            P pa=seg.search(i,i+m+1);
            m-=pa.second-i;
            for(j=pa.second-1;j>=i;j--){
                swap(vs[j],vs[j+1]);
            }
            for(j=i;j<=pa.second;j++){
                seg.set(j,{vs[j],j});
            }
        }
        for(i=0;i<vs.size();i++){
            cout<<vs[i]+1<<endl;
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    sol::solve();
}
accuracy: 1
time_ms: 100
memory_kb: 9660
final_rank: 10