import java.util.*;
class Main {
static class Gem {
int id;
int color;
long value;
Gem(int id, int color, long value) {
this.id = id;
this.color = color;
this.value = value;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if (!sc.hasNextInt()) return;
int n = sc.nextInt();
int k = sc.nextInt();
int m = sc.nextInt();
List<Gem> allGems = new ArrayList<>();
for (int i = 0; i < n; i++) {
int color = sc.nextInt();
long val = sc.nextLong();
allGems.add(new Gem(i, color, val));
}
// Sort all gems globally by value in descending order
allGems.sort((g1, g2) -> Long.compare(g2.value, g1.value));
// Track how many times each color appears in our current selection
Map<Integer, Integer> selectedColorCounts = new HashMap<>();
// Min-Heap to easily find the lowest-value "duplicate" gem in our hand
PriorityQueue<Gem> duplicatesInHand = new PriorityQueue<>((g1, g2) -> Long.compare(g1.value, g2.value));
long currentSum = 0;
// Greedily pick the top K gems
for (int i = 0; i < k; i++) {
Gem gem = allGems.get(i);
currentSum += gem.value;
selectedColorCounts.put(gem.color, selectedColorCounts.getOrDefault(gem.color, 0) + 1);
}
// Categorize the chosen K gems.
// If a gem's color appears more than once, it's a potential duplicate we can drop!
for (int i = 0; i < k; i++) {
Gem gem = allGems.get(i);
if (selectedColorCounts.get(gem.color) > 1) {
duplicatesInHand.add(gem);
}
}
// Find the best available gems outside our hand for the missing colors
// Map to store only the HIGHEST value gem for each color that we haven't picked yet
Map<Integer, Gem> bestMissingGems = new HashMap<>();
for (int i = k; i < n; i++) {
Gem gem = allGems.get(i);
// If we don't have this color in our hand at all
if (!selectedColorCounts.containsKey(gem.color)) {
// Since allGems is sorted by value, the first time we see a missing color,
// it is guaranteed to be the highest value gem for that color.
if (!bestMissingGems.containsKey(gem.color)) {
bestMissingGems.put(gem.color, gem);
}
}
}
// Max-Heap to easily find the highest-value missing color gem available
PriorityQueue<Gem> availableNewColors = new PriorityQueue<>((g1, g2) -> Long.compare(g2.value, g1.value));
availableNewColors.addAll(bestMissingGems.values());
// Forcefully swap if our distinct color count is less than M
int currentDistinctColors = selectedColorCounts.size();
while (currentDistinctColors < m) {
// If we run out of duplicates to sacrifice or new colors to bring in,
// then it's impossible to fix (though constraints say a choice is always possible)
if (duplicatesInHand.isEmpty() || availableNewColors.isEmpty()) {
break;
}
Gem outGem = duplicatesInHand.poll();
// Check if this gem is still actually a duplicate.
// (As we remove gems, a color's count drops. If it hits 1, it's no longer a duplicate!)
if (selectedColorCounts.get(outGem.color) <= 1) {
continue;
}
Gem inGem = availableNewColors.poll();
// Perform the swap
currentSum -= outGem.value;
currentSum += inGem.value;
selectedColorCounts.put(outGem.color, selectedColorCounts.get(outGem.color) - 1);
selectedColorCounts.put(inGem.color, 1);
currentDistinctColors++;
}
System.out.println(currentSum);
}
}