import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the luckBalance function below.
    static int luckBalance(int k, int[][] contests) {
        ArrayList<Integer> imp = new ArrayList<Integer>();
        ArrayList<Integer> unimp = new ArrayList<Integer>();
        for(int i=0;i<contests.length;i++){
            if(contests[i][1]==1) imp.add(contests[i][0]);
            else unimp.add(contests[i][0]);
        }
        Collections.sort(imp);
        Collections.sort(unimp);
        int total = 0;
        //ArraySta
        if(imp.size()-k>=0){
            for(int i=0;i<imp.size()-k;i++){
                total-=imp.get(i);
                System.out.println(total);
                //imp.remove(i);
            }            
        }
        else{
            for(int i=0;i<imp.size();i++){
                total+=imp.get(i);
                System.out.println(total);
                //imp.remove(i);
            } 
        }

        if(!imp.isEmpty() && imp.size()-k>=0){
            for(int i=imp.size()-k;i<imp.size();i++){
                total+=imp.get(i);
                System.out.println(total);
            }            
        }
        if(!unimp.isEmpty()){
            for(int i : unimp){
                total+=i;
                System.out.println(total);
            }
        }
        return total;
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] nk = scanner.nextLine().split(" ");

        int n = Integer.parseInt(nk[0]);

        int k = Integer.parseInt(nk[1]);

        int[][] contests = new int[n][2];

        for (int i = 0; i < n; i++) {
            String[] contestsRowItems = scanner.nextLine().split(" ");
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

            for (int j = 0; j < 2; j++) {
                int contestsItem = Integer.parseInt(contestsRowItems[j]);
                contests[i][j] = contestsItem;
            }
        }

        int result = luckBalance(k, contests);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

add LuckBalance.java