Server Client Model

0

0 votes
Hard
Problem

There are n pairs of clients, each is located at an integer coordinate on an infinite straight line on a network. Each pair consists of a client & clients friend (also a client).

You want to place k servers at integer coordinates on the same infinite straight line. Each pair of clients must be connected to a single server via wires, but a server can have any number of clients (even zero) connected to it. The length of a wire connecting a single client to a server is the absolute value of the distance between their respective coordinates on the infinite line. We consider the total length of the wire used to connect all the clients to servers to be the sum of the lengths of all the wires used to connect clients to servers.

NOTE - Both the client and clients friend in a pair must connect to the same server.

Given the locations of n pairs (client & clients friend) of clients and the value of k, place all k servers in such a way that the total length of wire needed to connect each pair of clients to server in minimal. Then print the total length of the new line.

Input Format -

The first line contains two space-separated integers denoting the respective values of n(the number of pairs of clients) and k(the number of servers). Each line i of the n subsequent lines contains two space-separated integers describing the respective values of ai(coordinate of the client) and bi(coordinate of the clients friend) for a pair of clients.

Output Format-

Print a single integer denoting the minimum total length of wire needed to connect all the pairs of clients to servers.

Constraints-

1<=n<=50000
1<=k<=1000
-1010<=ai, bi<=1010

Time Limit: 2.5
Memory Limit: 256
Source Limit:
Editor Image

?