#include <stdio.h>                                                                                                                                                                                                                
#include <stdlib.h>                                                                                                                                                                                                               
#include <string.h>                                                                                                                                                                                                               
                                                                                                                                                                                                                                  
int cmp(const void *a, const void *b)                                                                                                                                                                                             
{                                                                                                                                                                                                                                 
        int x = *((int *)a);                                                                                                                                                                                                      
        int y = *((int *)b);                                                                                                                                                                                                      
                                                                                                                                                                                                                                  
        if (x < y) {                                                                                                                                                                                                               
                return -1;                                                                                                                                                                                                        
        } else if (x == y) {                                                                                                                                                                                                      
                return 0;                                                                                                                                                                                                         
        } else {                                                                                                                                                                                                                  
                return 1;                                                                                                                                                                                                         
        }                                                                                                                                                                                                                         
}                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                  
int main(void)                                                                                                                                                                                                                    
{                                                                                                                                                                                                                                  
        int N, K, *list, i, j, ans = 0;                                                                                                                                                                                            
                                                                                                                                                                                                                                   
        scanf("%d %d", &N, &K);                                                                                                                                                                                                    
        list = calloc(sizeof(int), N);                                                                                                                                                                                             
        for (i = 0; i < N; i++) {                                                                                                                                                                                                  
                scanf("%d", &list[i]);                                                                                                                                                                                            
        }                                                                                                                                                                                                                         
        qsort(list, N, sizeof(int), cmp);                                                                                                                                                                                         
                                                                                                                                                                                                                                  
        for (i = 0, j = N - 1; i < K; i++, j--) {                                                                                                                                                                                 
                ans += list[j];                                                                                                                                                                                                   
        }                                                                                                                                                                                                                         
        printf("%d\n", ans);                                                                                                                                                                                                      
        return 0;                                                                                                                                                                                                                 
}

Measured memory_bytes: 128