#include <bits/stdc++.h>
using
namespace
std;
string lexicographicallyLargest(string S,
int
K)
{
int
n = S.length();
priority_queue<pair<
char
,
int
> > pqOdd, pqEven;
for
(
int
i = 2; i < n; i = i + 2) {
pqEven.push(make_pair(S[i], i));
}
for
(
int
i = 3; i < n; i = i + 2) {
pqOdd.push(make_pair(S[i], i));
}
for
(
int
i = 0; i < n; i++) {
if
(i % 2 == 0) {
while
(!pqEven.empty()
and pqEven.top().second <= i)
pqEven.pop();
if
(!pqEven.empty()
and pqEven.top().first > S[i]) {
swap(S[i], S[pqEven.top().second]);
int
idx = pqEven.top().second;
pqEven.pop();
pqEven.push({ S[idx], idx });
K--;
}
}
else
{
while
(!pqOdd.empty()
and pqOdd.top().second <= i)
pqOdd.pop();
if
(!pqOdd.empty()
and pqOdd.top().first > S[i]) {
swap(S[i], S[pqOdd.top().second]);
int
idx = pqOdd.top().second;
pqOdd.pop();
pqOdd.push({ S[idx], idx });
K--;
}
}
if
(K == 0)
break
;
}
return
S;
}
int
main()
{
string S =
"geeksforgeeks"
;
int
K = 2;
cout << lexicographicallyLargest(S, K);
return
0;
}