Billboard challenge algorithm
The inputs have to be limited with constraints you have listed, then


use a TreeSet to store the N objects in it 
remove the K first  
print the Set


few lines in Java (no malloc problem, memory managed by JVM): 


read the first input
use String.split() the line,
use Integer.valueOf(String) to convert
compare N,K and stop if not correct or if negative
use a while(&lt;N) for input,
test limits of input whith if and negative values
you can use toString() to print the Set, but you have to use a 
foreach to sum the result 
print result