#include<bits/stdc++.h>
#define MOD 1000000007
using namespace std;

#define DEBUG 1

/* -------------------This is the part you need to copy ----------------------*/
#if DEBUG && !ONLINE_JUDGE
    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v);
    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::set<T>& v);
    template <typename T1, typename T2>
    inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p);
    template<typename T1, typename T2>
    inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v);
    #define debug(args...)     (Debugger()) , args
 
    class Debugger
    {
        public:
        Debugger(const std::string& _separator = ", ") :
        first(true), separator(_separator){}
        
        template<typename ObjectType>
        Debugger& operator , (const ObjectType& v)
        {
            if(!first)
                std::cout << separator;
            std::cout << v;
            first = false;
            return *this;
        }
        ~Debugger() {  std::cout << endl;}
        
        private:
        bool first;
        std::string separator;
    };
 
    template <typename T1, typename T2>
    inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
    {
        return os << "(" << p.first << ", " << p.second << ")";
    }
 
    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
    {
        bool first = true;
        os << "[";
        for(unsigned int i = 0; i < v.size(); i++)
        {
            if(!first)
                os << ", ";
            os << v[i];
            first = false;
        }
        return os << "]";
    }
 
    template<typename T>
    inline std::ostream &operator << (std::ostream & os,const std::set<T>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii;
            first = false;
        }
        return os << "]";
    }
 
    template<typename T1, typename T2>
    inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v)
    {
        bool first = true;
        os << "[";
        for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
        {
            if(!first)
                os << ", ";
            os << *ii ;
            first = false;
        }
        return os << "]";
    }
    
#else
    #define debug(args...)                  // Just strip off all debug tokens
#endif 
/*-------------------------------Stop here---------------------------------*/

typedef unsigned int uint;
typedef unsigned long int lu;
typedef unsigned long long llu;
typedef long int ld;
typedef long long lld;
typedef vector<int> vint;
typedef vector<unsigned int> vuint;
typedef vector<unsigned long int> vlu;
typedef vector<unsigned long long> vllu;
typedef vector<long int> vld;
typedef vector<long long> vlld;
typedef priority_queue<int> p_qd;

int main()          
{
    int n,k;
    cin>>n>>k;
    p_qd val;
    int num=0;
    lld sum=0;
    for(int i=0;i<n;i++)
    {
        int p;
        cin>>p;
        num++;
        if(num>k){
            if(p<val.top()){
                sum+=(p-val.top());
                val.pop();
                val.push(p);
            }
        }
        else
        {
            sum+=p;
            val.push(p);
        }
    }
    cout<<sum<<endl;
    return 0;
}


Measured memory_bytes: 3632