Problem:
Score : 200 points

Problem Statement

Snuke has N sticks.
The length of the i-th stick is l_i.

Snuke is making a snake toy by joining K of the sticks together.

The length of the toy is represented by the sum of the individual sticks that compose it.
Find the maximum possible length of the toy.

Constraints

1 \leq K \leq N \leq 50

1 \leq l_i \leq 50

l_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
l_1 l_2 l_3 ... l_{N}

Output

Print the answer.

Sample Input 1

5 3
1 2 3 4 5

Sample Output 1

12

You can make a toy of length 12 by joining the sticks of lengths 3, 4 and 5, which is the maximum possible length.

Sample Input 2

15 14
50 26 27 21 41 7 42 35 7 5 5 36 39 1 45

Sample Output 2

386

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>

#define SENTINEL 1000000000
int count = 0;
void merge(long long int A[], int left, int mid, int right);
void mergeSort(long long int A[], int left, int right) {
	int i, mid;
	if (left + 1 < right) {
		mid = (left + right) / 2;
		mergeSort(A, left, mid);
		mergeSort(A, mid, right);
		merge(A, left, mid, right);
	}
}



void merge(long long int A[], int left, int mid, int right) {
	int n1, n2, i, j, k;
	long long int *L, *R;
	n1 = mid - left;
	n2 = right - mid;
	L = (long long int *)malloc(sizeof(long long int)*(n1 + 1));
	R = (long long int *)malloc(sizeof(long long int)*(n2 + 1));
	for (i = 0; i <= n1 - 1; i++) {
		L[i] = A[left + i];
	}
	for (j = 0; j <= n2 - 1; j++) {
		R[j] = A[mid + j];
	}
	L[n1] = SENTINEL;
	R[n2] = SENTINEL;
	i = 0;
	j = 0;
	for (k = left; k <= right - 1; k++) {
		if (L[i] <= R[j]) {
			A[k] = L[i];
			i++;
			count++;
		}
		else {
			A[k] = R[j];
			j++;
			count++;
		}
	}
	free(L);
	free(R);
}

int main() {
	long long int a, b, c, d, n, m, num[2000]= {}, result = 0;
	char str[200005];
	scanf("%lld%lld", &n,&m);
	for (int i = 0; i < n; i++) {
		scanf("%lld", &num[i]);
	}
	mergeSort(num, 0, n);

	for (int i = n-1; i >=n-m; i--) {
		result += num[i];
	}
	printf("%lld", result);
}