APIO '07 P2 - Backup

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 1.0s
Memory limit: 32M

Problem type

You run an IT company that backs up computer data for large offices. Backing up data is not fun, and so you design your system so that the different offices can back up each others' data while you sit at home and play computer games instead.

The offices are all situated along a single street. You decide to pair up the offices, and for each pair of offices you run a network cable between the two buildings so that they can back up each others' data.

However, network cables are expensive. Your local telecommunications company will only give you k network cables, which means you can only arrange backups for k pairs of offices (2k offices in total). No office may belong to more than one pair (that is, these 2k offices must all be different).

Furthermore, the telecommunications company charges by the kilometre. This means that you need to choose these k pairs of offices so that you use as little cable as possible. In other words, you need to choose the pairs so that, when the distances between the two offices in each pair are added together, the total distance is as small as possible.

As an example, suppose you had five clients with offices on a street as illustrated below. These offices are situated 1\text{ km}, 3\text{ km}, 4\text{ km}, 6\text{ km} and 12\text{ km} from the beginning of the street. The telecommunications company will only provide you with k = 2 cables.

12 km 6 km 4 km 3 km 1 km

The best pairing in this example is created by linking the first and second offices together, and linking the third and fourth offices together. This uses k = 2 cables as required, where the first cable has length 3\text{ km} - 1\text{ km} = 2\text{ km}, and the second cable has length 6\text{ km} - 4\text{ km} = 2\text{ km}. This pairing requires a total of 4 km of network cables, which is the smallest total possible.


The first line of input will contain the integers n and k, representing the number of offices on the street (2 \le n \le 100\,000) and the number of available network cables (1 \le k \le \frac n 2).

The following n lines will each contain a single integer (0 \le s \le 1\,000\,000\,000), representing the distance of each office from the beginning of the street. These integers will appear in sorted order from smallest to largest. No two offices will share the same location.


Output should consist of a single positive integer, giving the smallest total length of network cable required to join 2k distinct offices into k pairs.

Sample Input

5 2

Sample Output



The sample input above represents the example scenario described earlier.


The score for each input scenario will be 100\% if the correct answer is written to the output file, and 0\% otherwise. For 30\% of the available marks, n \le 20. For 60\% of the available marks, n \le 10\,000.


  • 1
    d3story  commented on Feb. 11, 2021, 3:00 p.m. edit 2

    Can someone please help me with memory management? My code is producing the right answers but for the large data sets my priority queue randomly increases in size like after the size of 54167 it randomly jumps to a HUGE number. The funny thing is I switched from C++11 to C++20 it doesn't do it for some of the big test cases. PS NVM I am just special for some reason I thought N<=50000. And how do you delete a post?

    https://dmoj.ca/src/3392125 this is the c++11

    https://dmoj.ca/src/3392222 this is the exact same code in C++20