스택을 활용하면 간단하게 풀 수 있는 문제였다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
String num = br.readLine();
Stack<Character> stack = new Stack<>();
stack.push(num.charAt(0));
int count = 0;
for (int i = 1; i < n; i++) {
char c = num.charAt(i);
while (!stack.isEmpty() && count < k) {
if (stack.peek() < c) {
stack.pop();
count += 1;
} else {
break;
}
}
stack.push(c);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n - k; i++) {
sb.append(stack.get(i));
}
System.out.println(sb);
}
}