import java.util.*;

public class Solution {
    public static int maxValue(String s, int k) {
        if (s.length() < k) {
            return -1;
        }
        char[] cs = s.toCharArray();
        int max = 0;
        int index = 0;
        for (int i = 0; i < cs.length - k; i++) {
            int val = cs[i] - '0';
            if (val > max) {
                max = val;
                index = i;
            } else if (val == max) {
                int p = index;//前
                int q = i;//后
                for (int j = 0; j < k; j++, p++, q++) {
                    if (cs[p] < cs[q]) {
                        index = i;
                        break;
                    } else if (cs[p] > cs[q]) {
                        break;
                    }
                }
                i = q;
            }
        }
        int res = 0;
        for (int i = index; i < index + k; i++) {
            res = res * 10 + (cs[i] - '0');
        }
        return res;
    }
}