#include <stdio.h>
#include <math.h>

int main(){
    int N;
    int K;
    int p[N];
    int min[N];
    int sum=0;
    int mins;
    scanf("%d %d",&N,&K);
    
    for (int i=0;i<N;i++){
        scanf("%d",&p[i]);
    }
    for (int i=0;i<K;i++){  
    min[i] = 1000;
    }
    min[0] = p[0];

    for(int j=1;j<N;j++){
        for (int k=0;k<K;k++){
            if((p[j]>=min[k])&&(p[j]<=min[k+1])){
                min[k+1]=mins;
                min[k+1] = p[j];
                for (int x=k+2;x<K;x++){
                    min[x] = mins;
                    mins = min[x+1];
                }
            }

        }
    
    }
    
    for (int i=0;i<K;i++){  
    sum += min[i];
    }
    printf('%d',sum);
    return 0;
}