#P14741. [ICPC 2021 Seoul R] Postman

[ICPC 2021 Seoul R] Postman

题目描述

There is a straight road on which two types trams run. One is an east-to-west tram which moves from east to west, and the other is a west-to-east tram. For each type, several trams run regularly, so anyone can ride the tram in any direction at any time. To use the tram, you have to pay a ticket for each direction you take. In other words, to use a tram that moves from east to west, you must pay a W-ticket (west bound ticket), and conversely, to use a tram that moves from west to east, you must pay an E-ticket (east bound ticket). You can get on and off the tram at any time and place you want, and once you get on the tram, you can ride it as long as you want.

Bob, a post office worker, goes to the post office every day to deliver the mails assigned to him. He uses the tram to deliver them. Each location where mail will be delivered is represented by an xx-coordinate for convenience, and the post office locates at x=0x = 0.

To deliver nn pieces of mail, the post office gives Bob nn tram tickets. Bob uses one ticket to deliver one piece of mail. However, among the nn tickets provided by the post office, the number of W-tickets is ww and that of E-tickets is ee (e=nwe = n - w). By using the tickets he received at the post office, Bob wants not only to figure out the order in which the nn pieces of mail should be delivered, but also to minimize the distance he travels using the tram.

Depending on the order in which the pieces of mail are delivered, it is divided into two types. The first type, denoted by t=1t = 1, is the case that the order of mail delivery is not important. The second type, denoted by t=2t = 2, is the case one specific designated piece of mail must be delivered at last and all the others can be delivered in any order.

For example, suppose that n=5n = 5, w=4w = 4 (the number of W-tickets), t=2t = 2, and the xx-coordinates of the places where the mails should be delivered are (20,15,20,30,10)(-20, -15, 20, 30, 10), and that the xx-coordinate of the specific designated mail which must be finally delivered is x=10x = 10. The optimal delivery route is shown in Figure H.1 and the total distance moved using trams is 90. As shown in Figure H.1, four W-tickets and one E-ticket are used and the designated mail is delivered at last.

:::align{center} :::

Consider another example where all information is the same as above except for t=1t = 1. The optimal delivery route for this case is shown in Figure H.2 and the total distance is 80. In this case, you can see that four W-tickets and one E-ticket are used as well.

:::align{center} :::

Given information about the mail that Bob should deliver, write a program that finds the minimum distance he travels using trams.

输入格式

Your program is to read from standard input. The input starts with a line containing three integers, nn, ww and tt (1n3×1051 \leq n \leq 3 \times 10^5, 0wn0 \leq w \leq n, 1t21 \leq t \leq 2), where nn is the number of pieces of mail, ww is the number of W-tickets, and tt indicates the delivery order type as explained above. Note that the number of E-tickets is nwn - w. In the following line, nn integers are given. The ii-th integer xix_i (1in1 \leq i \leq n, 109xi109-10^9 \leq x_i \leq 10^9, xi0x_i \neq 0) is the xx-coordinate of the location where the ii-th mail should be delivered. When t=2t = 2, xnx_n denotes the xx-coordinate of the specific designated mail that must be delivered at last.

You can assume no two xix_i's are the same.

输出格式

Your program is to write to standard output. Print exactly one line. The line should contain the minimum distance Bob travels to deliver all the pieces of mail. If it is impossible for Bob to deliver them using the tickets print 1-1.

5 4 2
-20 -15 20 30 10
90
5 4 1
-20 -15 20 30 10
80
7 1 2
10 13 -30 24 50 -5 -21
-1