<commit_before>import java.io.*;
import java.util.*;

public class LuckBalance {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        // Map<Integer, SortedSet<Integer>>
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int K = in.nextInt();

        Map<Integer, List<Integer>> mapping = new HashMap<>();
        mapping.put(0, new ArrayList<Integer>());
        mapping.put(1, new ArrayList<Integer>());
        
        for (int i = 0; i < N; i++) {
            int li = in.nextInt();
            int ti = in.nextInt();
        
            mapping.compute(ti, (k, v) -> process(v, li));                
        }

        System.out.println(computeLuck(mapping, K));        
    }

    static int computeLuck(Map<Integer, List<Integer>> mapping, int K) {

        List<Integer> impSet = mapping.get(1);
        List<Integer> unimpSet = mapping.get(0);

        Collections.sort(impSet, (e1, e2) -> e2.compareTo((Integer) e1));
        Collections.sort(unimpSet, (e1, e2) -> e2.compareTo((Integer) e1));
        
        return impSet.stream().mapToInt(Integer::intValue).limit(K).sum() - impSet.stream().skip(K).mapToInt(i -> i).sum() + unimpSet.stream().mapToInt(i -> i).sum();

    }

    static List process(List<Integer> set, Integer li) {
        set.add(li);<commit_msg>[chore]: indentation<commit_after>import java.io.*;
import java.util.*;

public class LuckBalance {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        // Map<Integer, SortedSet<Integer>>
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int K = in.nextInt();

        Map<Integer, List<Integer>> mapping = new HashMap<>();
        mapping.put(0, new ArrayList<Integer>());
        mapping.put(1, new ArrayList<Integer>());
        
        for (int i = 0; i < N; i++) {
            int li = in.nextInt();
            int ti = in.nextInt();
        
            mapping.compute(ti, (k, v) -> process(v, li));                
        }

        System.out.println(computeLuck(mapping, K));        
    }

    static int computeLuck(Map<Integer, List<Integer>> mapping, int K) {

        List<Integer> impSet = mapping.get(1);
        List<Integer> unimpSet = mapping.get(0);

        Collections.sort(impSet, (e1, e2) -> e2.compareTo((Integer) e1));
        Collections.sort(unimpSet, (e1, e2) -> e2.compareTo((Integer) e1));
        
        return impSet.stream().mapToInt(Integer::intValue).limit(K).sum() -
            impSet.stream().skip(K).mapToInt(i -> i).sum() +
            unimpSet.stream().mapToInt(i -> i).sum();

    }

    static List process(List<Integer> set, Integer li) {
        set.add(li);
{'fraction_non_alphanumeric': 0.15557088651259054, 'fraction_numerical': 0.008968609865470852, 'mean_word_length': 2.243847874720358, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5003583', 'n_tokens_mistral': 910, 'n_tokens_neox': 892, 'n_words': 218}