#include <bits/stdc++.h>
using namespace std;
// Cấu trúc cạnh trong mạng luồng
struct Edge {
int to; // Đỉnh đích
int rev; // Vị trí ngược trong danh sách cạnh của đỉnh 'to'
int capacity; // Sức chứa
long long cost; // Chi phí
};
// Hàm thêm cạnh vào mạng luồng
void add_edge(vector<vector<Edge>> &graph, int from, int to, int capacity, long long cost){
Edge a = {to, (int)graph[to].size(), capacity, cost};
Edge b = {from, (int)(graph[from].size()), 0, -cost};
graph[from].push_back(a);
graph[to].push_back(b);
}
// Hàm tính Min-Cost Max-Flow sử dụng thuật toán SPFA
pair<long long, long long> min_cost_flow(vector<vector<Edge>> &graph, int S, int T){
int n = graph.size();
long long flow = 0, cost = 0;
vector<long long> prevv(n, -1);
vector<long long> preve(n, -1);
while(1){
// Tìm đường đi có chi phí ngắn nhất từ S đến T
vector<long long> dist(n, 1e18);
vector<bool> inqueue(n, false);
dist[S] = 0;
queue<int> q;
q.push(S);
inqueue[S] = true;
while(!q.empty()){
int u = q.front(); q.pop();
inqueue[u] = false;
for(int i=0;i<graph[u].size();i++){
Edge &e = graph[u][i];
if(e.capacity >0 && dist[e.to] > dist[u] + e.cost){
dist[e.to] = dist[u] + e.cost;
prevv[e.to] = u;
preve[e.to] = i;
if(!inqueue[e.to]){
q.push(e.to);
inqueue[e.to] = true;
}
}
}
}
if(dist[T] == 1e18) break; // Không còn đường đi
// Tìm lượng luồng tăng thêm
long long d = 1e18;
int v = T;
while(v != S){
int u = prevv[v];
int idx = preve[v];
d = min(d, (long long)graph[u][idx].capacity);
v = u;
}
flow += d;
cost += d * dist[T];
v = T;
while(v != S){
int u = prevv[v];
int idx = preve[v];
graph[u][idx].capacity -= d;
graph[v][graph[u][idx].rev].capacity += d;
v = u;
}
}
return {flow, cost};
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int k, n, m;
cin >> k >> n >> m;
// Đọc trọng số a[i] và b[i]
vector<pair<int, int>> clusters(k);
for(int i=0;i<k;i++) cin >> clusters[i].first >> clusters[i].second;
// Định nghĩa các đỉnh trong mạng luồng
// 0: Source (S)
// 1 đến k: Cụm dữ liệu
// k+1: Nhóm loại 1
// k+2: Nhóm loại 2
// k+3: Sink (T)
int S = 0;
int T = k + 3;
int group1 = k + 1;
int group2 = k + 2;
int total_nodes = k + 4;
vector<vector<Edge>> graph(total_nodes, vector<Edge>());
// Thêm cạnh từ S đến từng cụm dữ liệu
for(int i=0;i<k;i++){
add_edge(graph, S, 1+i, 1, 0); // capacity 1, cost 0
}
// Thêm cạnh từ cụm dữ liệu đến nhóm loại 1 và nhóm loại 2
for(int i=0;i<k;i++){
// Chi phí là -a[i] để tối đa hóa tổng a[i] khi chọn vào nhóm 1
add_edge(graph, 1+i, group1, 1, - (long long)clusters[i].first);
// Chi phí là -b[i] để tối đa hóa tổng b[i] khi chọn vào nhóm 2
add_edge(graph, 1+i, group2, 1, - (long long)clusters[i].second);
}
// Thêm cạnh từ nhóm loại 1 và nhóm loại 2 đến T
add_edge(graph, group1, T, n, 0); // capacity n, cost 0
add_edge(graph, group2, T, m, 0); // capacity m, cost 0
// Tính luồng và chi phí
pair<long long, long long> result = min_cost_flow(graph, S, T);
// Tổng trọng số tối đa là - chi phí
cout << -result.second;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovLyBD4bqldSB0csO6YyBj4bqhbmggdHJvbmcgbeG6oW5nIGx14buTbmcKc3RydWN0IEVkZ2UgewogICAgaW50IHRvOyAgICAgICAgLy8gxJDhu4luaCDEkcOtY2gKICAgIGludCByZXY7ICAgICAgIC8vIFbhu4sgdHLDrSBuZ8aw4bujYyB0cm9uZyBkYW5oIHPDoWNoIGPhuqFuaCBj4bunYSDEkeG7iW5oICd0bycKICAgIGludCBjYXBhY2l0eTsgIC8vIFPhu6ljIGNo4bupYQogICAgbG9uZyBsb25nIGNvc3Q7ICAgIC8vIENoaSBwaMOtCn07CgovLyBIw6BtIHRow6ptIGPhuqFuaCB2w6BvI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VldWVbU10gPSB0cnVlOwogICAgICAgIAogICAgICAgIHdoaWxlKCFxLmVtcHR5KCkpewogICAgICAgICAgICBpbnQgdSA9IHEuZnJvbnQoKTsgcS5wb3AoKTsKICAgICAgICAgICAgaW5xdWV1ZVt1XSA9IGZhbHNlOwogICAgICAgICAgICBmb3IoaW50IGk9MDtpPGdyYXBoW3VdLnNpemUoKTtpKyspewogICAgICAgICAgICAgICAgRWRnZSAmZSA9IGdyYXBoW3VdW2ldOwogICAgICAgICAgICAgICAgaWYoZS5jYXBhY2l0eSA+MCAmJiBkaXN0W2UudG9dID4gZGlzdFt1XSArIGUuY29zdCl7CiAgICAgICAgICAgICAgICAgICAgZGlzdFtlLnRvXSA9IGRpc3RbdV0gKyBlLmNvc3Q7CiAgICAgICAgICAgICAgICAgICAgcHJldnZbZS50b10gPSB1OwogICAgICAgICAgICAgICAgICAgIHByZXZlW2UudG9dID0gaTsKICAgICAgICAgICAgICAgICAgICBpZighaW5xdWV1ZVtlLnRvXSl7CiAgICAgICAgICAgICAgICAgICAgICAgIHEucHVzaChlLnRvKTsKICAgICAgICAgICAgICAgICAgICAgICAgaW5xdWV1ZVtlLnRvXSA9IHRydWU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIAogICAgICAgIGlmKGRpc3RbVF0gPT0gMWUxOCkgYnJlYWs7IC8vIEtow7RuZyBjw7JuIMSRxrDhu51uZyDEkWkKICAgICAgICAKICAgICAgICAvLyBUw6xtIGzGsOG7o25nIGx14buTbmcgdMSDbmcgdGjDqm0KICAgICAgICBsb25nIGxvbmcgZCA9IDFlMTg7CiAgICAgICAgaW50IHYgPSBUOwogICAgICAgIHdoaWxlKHYgIT0gUyl7CiAgICAgICAgICAgIGludCB1ID0gcHJldnZbdl07CiAgICAgICAgICAgIGludCBpZHggPSBwcmV2ZVt2XTsKICAgICAgICAgICAgZCA9IG1pbihkLCAobG9uZyBsb25nKWdyYXBoW3VdW2lkeF0uY2FwYWNpdHkpOwogICAgICAgICAgICB2ID0gdTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgZmxvdyArPSBkOwogICAgICAgIGNvc3QgKz0gZCAqIGRpc3RbVF07CiAgICAgICAgdiA9IFQ7CiAgICAgICAgd2hpbGUodiAhPSBTKXsKICAgICAgICAgICAgaW50IHUgPSBwcmV2dlt2XTsKICAgICAgICAgICAgaW50IGlkeCA9IHByZXZlW3ZdOwogICAgICAgICAgICBncmFwaFt1XVtpZHhdLmNhcGFjaXR5IC09IGQ7CiAgICAgICAgICAgIGdyYXBoW3ZdW2dyYXBoW3VdW2lkeF0ucmV2XS5jYXBhY2l0eSArPSBkOwogICAgICAgICAgICB2ID0gdTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4ge2Zsb3csIGNvc3R9Owp9CgppbnQgbWFpbigpewogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZSgwKTsKICAgIAogICAgaW50IGssIG4sIG07CiAgICBjaW4gPj4gayA+PiBuID4+IG07CiAgICAKICAgIC8vIMSQ4buNYyB0cuG7jW5nIHPhu5EgYVtpXSB2w6AgYltpXQogICAgdmVjdG9yPHBhaXI8aW50LCBpbnQ+PiBjbHVzdGVycyhrKTsKICAgIGZvcihpbnQgaT0wO2k8aztpKyspIGNpbiA+PiBjbHVzdGVyc1tpXS5maXJzdCA+PiBjbHVzdGVyc1tpXS5zZWNvbmQ7CiAgICAKICAgIC8vIMSQ4buLbmggbmdoxKlhIGPDoWMgxJHhu4luaCB0cm9uZyBt4bqhbmcgbHXhu5NuZwogICAgLy8gMDogU291cmNlIChTKQogICAgLy8gMSDEkeG6v24gazogQ+G7pW0gZOG7ryBsaeG7h3UKICAgIC8vIGsrMTogTmjDs20gbG/huqFpIDEKICAgIC8vIGsrMjogTmjDs20gbG/huqFpIDIKICAgIC8vIGsrMzogU2luayAoVCkKICAgIGludCBTID0gMDsKICAgIGludC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