[C++] simple solution using sorted map faster than 98%
\nclass MKAverage {\npublic:\n    vector<int> items;\n    map<int,int> lastM;\n    int m,k,s,e;\n    long total;\n    \n    MKAverage(int m, int k) {\n        t
```\nclass MKAverage {\npublic:\n    vector<int> items;\n    map<int,int> lastM;\n    int m,k,s,e;\n    long total;\n    \n    MKAverage(int m, int k) {\n        this->m = m;\n        this->k = k;\n        s = 0;\n        e = 0;\n        total = 0;\n    }\n    \n\t// O(logM)\n    void addElement(int num) {\n        items.push_back(num);\n        lastM[num]++;\n        total += num;\n        e++;\n        \n        if(e-s>m) \n        {\n            if(--lastM[items[s]] == 0) lastM.erase(items[s]);\n            total -= items[s++];\n        }\n    }\n    \n\t// O(K)\n    int calculateMKAverage() {\n        if(e - s < m) return -1;\n        int n = k, minVal = 0, maxVal = 0;\n        auto itr = lastM.begin();\n        while(n>0) \n        {\n            minVal += itr->first * min(itr->second, n);\n            n -= min(itr->second, n);\n            if(n > 0)\n            {\n                itr++;\n            }\n        }\n        \n        auto _itr = lastM.rbegin();\n        n = k;\n        while(n>0) \n        {\n            maxVal += _itr->first * min(_itr->second, n);\n            n -= min(_itr->second, n);\n            if(n > 0)\n            {\n                _itr++;\n            }\n        }\n        \n        return (total - minVal - maxVal) / (m-2*k);\n    }\n};\n```
[]