PROBLEM LINK:
Setter: Aakash Jaiswal
Tester: Deepansh Parmani
Editorialist: Akash Bhalotia
DIFFICULTY:
CAKEWALK
PREREQUISITES:
PROBLEM:
Given an array of size N, find the sum of the K largest elements of the array.
QUICK EXPLANATION:
Sort the array in nondecreasing or ascending order. Find the sum of the last K elements.
EXPLANATION:
One of the simplest ways to solve this problem is to use Sorting.
As 1 \leq N \leq 2* 10^5, O(N^2) sorting algorithms like Bubble Sort, Selection Sort, Insertion Sort, etc. would result in Time Limit Exceeded (TLE). Thus we have to use faster sorting algorithms like Merge Sort, Quick Sort, Heap Sort, etc. which generally result in a time complexity of O(NlogN).
In CP (Competitive Programming), one doesn’t generally implement the full sorting algorithm, but uses inbuilt library sorting function of the language;
For Example:

Sort the array in ascending order. Now the Klargest elements are all populated towards the end of the array, from index (NK+1) to index N, 1based indexing.

Using a loop, you can find the sum of the last K elements of this sorted array. A 32bit integer datatype should be sufficient to store the sum, as 1\leq a*\leq 10^4 and 1\leq K \leq 20.
Another way to solve this problem, using sorting, is to sort the array in descending order and find the sum of the first K elements in it.
COMPLEXITY:
Time complexity: O(NlogN) per test case
Space Complexity: O(N)
SOLUTIONS:
##Similar Problem:
Find the sum of the Ksmallest elements of the array. Same constraints.
===============================================
ALTERNATIVE SOLUTION:
 O(N*K) using Looping:
Click to view
Run a loop K times. Each time find the largest element in the array. Add it to the sum. Set the element equal to 1, so that it doesn’t get selected as the largest element again.
In my approach, I have used swapping instead of setting the next largest element =1 each time.
 O(N*logK) using a MinHeap:
Click to view
Create a MinHeap of size K. Insert the first K elements into it and each time add that element to the sum. For the next (NK) elements, if an element is greater than the root element of the heap,

Remove the root element

Subtract it from the sum

Add the current element to the heap

Add it to the sum.
USEFUL READUPS AND TUTORIALS:
Don’t know a topic? No worries You’ll find the links below useful.
Click to view
General Tutorials:
There are many sites which provide excellent tutorials on data structures and algorithms. Some of them, in no particular order, are:
===============================================
BONUS:
Can we solve this problem in O(N) time?
Click to view
==================================================================================================
Feel free to share your approach if it differs. If you have any doubts or you feel stuck at any point, feel free to ask them below. You can also reach me at akashbhalotia\.unofficial@gmail\.com . I tried my best to make the editorial beginnerfriendly. Suggestions are always welcomed