import java.util.*; class Jewel implements Comparable { int weight, cost; public Jewel(int weight, int cost) { this.weight = weight; this.cost = cost; } @Override public int compareTo(Jewel o) { return Integer.compare(o.cost, this.cost); } } public class _1202 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); ArrayList jewels = new ArrayList<>(); for(int i=0; i Integer.compare(a.weight, b.weight)); int[] bags = new int[K]; for(int i=0; i pq = new PriorityQueue<>(); for (int m : bags) { while(idx < N && m >= jewels.get(idx).weight) pq.add(jewels.get(idx++)); if(!pq.isEmpty()) ans += pq.poll().cost; } System.out.println(ans); } }