Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
 
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _]   // we can only make one bouquet.
After day 2: [x, _, _, _, x]   // we can only make two bouquets.
After day 3: [x, _, x, _, x]   // we can make 3 bouquets. The answer is 3.

Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.

Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.

Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.

Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9

 
Constraints:

bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

def _disabled_input(*args, **kwargs):
    raise RuntimeError("input() is disabled in callable mode")
builtins.input = _disabled_input

ns = {"__name__": "__judge__"}  # prevent if __name__ == "__main__" blocks
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = 'class Solution:\n    IMPOSSIBLE = -1\n    \n#     @staticmethod\n#     def canMakeBouquetsOnDay(bloom_day: List[int], m: int, k: int, day: int) -> bool:\n#         n = len(bloom_day)\n#         bouquets_made = 0\n#         i = 0\n#         while i < n and bouquets_made < m:\n#             j = i\n#             while j < n and bloom_day[j] <= day:\n#                 j += 1\n#             if i == j:\n#                 i += 1\n#             else:\n#                 bouquets_made += (j - i) // k\n#                 i = j\n#         return bouquets_made >= m\n    \n#     def minDays(self, bloom_day: List[int], m: int, k: int) -> int:\n#         n = len(bloom_day)\n#         if k * m > n:\n#             return Solution.IMPOSSIBLE\n#         l = min(bloom_day) - 1\n#         r = max(bloom_day)\n#         # The invariant is it is possible to make k bouquets on day r.\n#         while r - l > 1:\n#             middle = (l + r) // 2\n#             if Solution.canMakeBouquetsOnDay(bloom_day, m, k, middle):\n#                 r = middle\n#             else:\n#                 l = middle\n#         return r\n\n    def bouquetsFrom(self, l: int, r: int) -> int:\n        return (r - l + 1) // self.k\n\n    def minDays(self, bloom_day: List[int], m: int, k: int) -> int:\n        self.k = k\n        n = len(bloom_day)\n        if k * m > n:\n            return Solution.IMPOSSIBLE\n        bloom_order = list(range(n))\n        bloom_order.sort(key=lambda index: bloom_day[index])\n        bloomed = [False for _ in range(n)]\n        to_left = [0 for _ in range(n)]\n        to_right = [0 for _ in range(n)]\n        bouquets_made = 0\n        for index in bloom_order:\n            bloomed[index] = True\n            l = index\n            r = index\n            if index != 0 and bloomed[index - 1]:\n                bouquets_made -= self.bouquetsFrom(\n                    to_left[index - 1], to_right[index - 1])\n                l = to_left[index - 1]\n            if index + 1 != n and bloomed[index + 1]:\n                bouquets_made -= self.bouquetsFrom(\n                    to_left[index + 1], to_right[index + 1])\n                r = to_right[index + 1]\n            bouquets_made += self.bouquetsFrom(l, r)\n            to_left[l] = to_left[r] = l\n            to_right[l] = to_right[r] = r\n            if bouquets_made >= m:\n                return bloom_day[index]\n        return Solution.IMPOSSIBLE\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

_target = None
_fn_name = 'minDays'
if _fn_name:
    if "Solution" in ns:
        try:
            _obj = ns["Solution"]()
            if hasattr(_obj, _fn_name):
                _target = getattr(_obj, _fn_name)
        except Exception:
            _target = None
    if _target is None and _fn_name in ns and callable(ns[_fn_name]):
        _target = ns[_fn_name]
# Fallback: if there's exactly one user-defined function, use it
if _target is None:
    _cands = [v for k, v in ns.items() if callable(v) and getattr(v, "__module__", "") in ("__judge__", "__main__")]
    _user_funcs = [c for c in _cands if getattr(c, "__name__", "").startswith("_") is False]
    if len(_user_funcs) == 1:
        _target = _user_funcs[0]
if _target is None:
    raise RuntimeError(f"Could not resolve callable '{_fn_name}'")

_raw = sys.stdin.read().strip()
if _raw:
    _parsed = json.loads(_raw)
    if isinstance(_parsed, dict):
        _kwargs = _parsed.get("kwargs", {})
        _args = _parsed.get("args", [])
    else:
        _args, _kwargs = _parsed, {}
else:
    _args, _kwargs = [], {}
if not isinstance(_args, (list, tuple)):
    _args = [_args]
_res = _target(*_args, **_kwargs)
try:
    sys.stdout.write(repr(_res) + "\n")
except Exception:
    pass

```

Measured memory_bytes: 6564