analysis:
This is a very classic model of cost flow.
First, because the topics in each point can only limit we select \ (k \) , for that reason because there \ (k \) times the limit, so we might be limiting with a maximum flow, which we will source demolition into two points \ (S_0 \) and \ (S_1 \) , from \ (S_0 \) points to \ (S_1 \) points connected to a flow k, a cost of \ (0 \) side. Up to representatives of size \ (k \) flows through all sides. So that we can guarantee that the final flow rate through the end that was tied up in k, but because the requirement is a maximum value, so we request is the biggest cost maximum flow, we only need this value becomes negative.
Because there will be at most \ (n \) on the range, \ (2n \) points, so we will consider the \ (2n \) points discrete. After discretization, we post these discrete \ (2n \) points in the adjacent discrete points are connected to a flow rate of \ (INF \) , cost \ (0 \) side, representatives of the neighboring the point can reach each other. Then we again before \ (n-\) for each interval of the interval \ ([L, R & lt] \) , the point \ (L \) and the point \ (R & lt \) between the cost of even a \ (- LR} {LEN_ \) , flow \ (1 \) side, representing the current interval if you want to select, then will spend \ (- len {lr} \ ) costs as well as all points interval will occupy \ ( 1 \) point traffic. Instead of taking the minimum cost after after the establishment of a good map, we ran the minimum cost while the maximum number of streams can be.
Code:
// luogu-judger-enable-o2
#include <bits/stdc++.h>
#define maxn 1006
#define maxm 10005
using namespace std;
int head[maxn],cnt=0;
int dis[maxn],vis[maxn],sp,ep,maxflow,cost;
int n,k;
const int INF=0x3f3f3f3f;
struct Node{
int to,next,val,cost;
}q[maxm<<1];
int L[maxn],R[maxn];
vector<int>vec;
void init(){
memset(head,-1, sizeof(head));
cnt=2;
maxflow=cost=0;
}
void addedge(int from,int to,int val,int cost){
q[cnt].to=to;
q[cnt].next=head[from];
q[cnt].val=val;
q[cnt].cost=cost;
head[from]=cnt++;
}
void add_edge(int from,int to,int val,int cost){
addedge(from,to,val,cost);
addedge(to,from,0,-cost);
}
bool spfa(){
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
dis[sp]=0;
vis[sp]=1;
queue<int>que;
que.push(sp);
while(!que.empty()){
int x=que.front();
que.pop();
vis[x]=0;
for(int i=head[x];i!=-1;i=q[i].next){
int to=q[i].to;
if(dis[to]>dis[x]+q[i].cost&&q[i].val){
dis[to]=dis[x]+q[i].cost;
if(!vis[to]){
que.push(to);
vis[to]=1;
}
}
}
}
return dis[ep]!=0x3f3f3f3f;
}
int dfs(int x,int flow){
if(x==ep){
vis[ep]=1;
maxflow+=flow;
return flow;
}//可以到达t,加流
int used=0;//该条路径可用流量
vis[x]=1;
for(int i=head[x];i!=-1;i=q[i].next){
int to=q[i].to;
if((vis[to]==0||to==ep)&&q[i].val!=0&&dis[to]==dis[x]+q[i].cost){
int minflow=dfs(to,min(flow-used,q[i].val));
if(minflow!=0){
cost+=q[i].cost*minflow;
q[i].val-=minflow;
q[i^1].val+=minflow;
used+=minflow;
}
//可以到达t,加费用,扣流量
if(used==flow)break;
}
}
return used;
}
int mincostmaxflow(){
while(spfa()){
vis[ep]=1;
while(vis[ep]){
memset(vis,0,sizeof(vis));
dfs(sp,INF);
}
}
return maxflow;
}
int main()
{
scanf("%d%d",&n,&k);
init();
for(int i=1;i<=n;i++){
scanf("%d%d",&L[i],&R[i]);
if(L[i]>R[i]) swap(L[i],R[i]);
vec.push_back(L[i]);
vec.push_back(R[i]);
}
sort(vec.begin(),vec.end());
int sz=vec.size();
sp=sz+1,ep=sz+2;
add_edge(sp,1,k,0);
add_edge(sz,ep,k,0);
for(int i=1;i<sz;i++) add_edge(i,i+1,k,0);
for(int i=1;i<=n;i++){
int l=lower_bound(vec.begin(),vec.end(),L[i])-vec.begin();
int r=lower_bound(vec.begin(),vec.end(),R[i])-vec.begin();
add_edge(l+1,r+1,1,vec[l]-vec[r]);
}
mincostmaxflow();
printf("%d\n",-cost);
return 0;
}