Python Solution SortedList
\nfrom sortedcontainers import SortedList\n\nclass MKAverage:\n\n    def __init__(self, m: int, k: int):\n        self.arr = SortedList()\n        self.m = m\n 
```\nfrom sortedcontainers import SortedList\n\nclass MKAverage:\n\n    def __init__(self, m: int, k: int):\n        self.arr = SortedList()\n        self.m = m\n        self.k = k\n        self.q = deque()\n        self.total = None\n\n    def addElement(self, num: int) -> None:\n        self.q.append(num)\n        m = self.m\n        k = self.k\n        if len(self.q) > m:\n            val = self.q.popleft()            \n            ind1 = self.arr.bisect_left(val)\n            ind2 = self.arr.bisect_right(val)\n            left, right, mid = False, False, False\n            kth, mth = self.arr[k], self.arr[m - k - 1]\n            if k <= ind1 < m - k or k <= ind2 - 1 < m - k or (ind1 <= k and  m - k <= ind2 - 1 < m):\n                mid = True\n            elif ind1 < k:\n                left = True\n            elif ind2 - 1 >= m - k:\n                right = True            \n            \n            self.arr.remove(val)\n            ind1 = self.arr.bisect_left(num)\n            ind2 = self.arr.bisect_right(num)            \n            self.arr.add(num)            \n            \n            if k <= ind1 < m - k or k <= ind2 < m - k or (ind1 <= k and  m - k <= ind2 < m):\n                if mid:\n                    self.total += num - val\n                if left:\n                    self.total += num - kth\n                if right:\n                    self.total += num - mth\n            elif ind1 < k:\n                if mid:\n                    self.total += self.arr[k] - val\n                if left:\n                    pass\n                if right:\n                    self.total += self.arr[k] - mth\n            elif ind2 >= m - k:\n                if mid:\n                    self.total += self.arr[m - k - 1] - val\n                if left:\n                    self.total += self.arr[m - k - 1] - kth\n                if right:\n                    pass\n        else:\n            self.arr.add(num)\n            if self.total is None and len(self.arr) == self.m:\n                self.total = sum(self.arr[k:m-k])\n            \n\n    def calculateMKAverage(self) -> int:\n        if len(self.arr) < self.m:\n            return -1\n        \n        return int(self.total / (self.m - 2 * self.k))\n```
['Binary Search Tree', 'Python3']