codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1909C.md (3432B)


      1 Source: https://codeforces.com/problemset/problem/1909/C
      2 
      3 C. Heavy Intervals
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 [Shiki - Pure Ruby](https://soundcloud.com/shiki-8/pure-rubyversoundcloud)
     22 
     23 ⠀
     24 
     25 You have $n$ intervals $[l_1, r_1], [l_2, r_2], \dots, [l_n, r_n]$, such that $l_i \lt r_i$ for each $i$, and all the endpoints of the intervals are distinct.
     26 
     27 The $i$-th interval has weight $c_i$ per unit length. Therefore, the weight of the $i$-th interval is $c_i \cdot (r_i - l_i)$.
     28 
     29 You don't like large weights, so you want to make the sum of weights of the intervals as small as possible. It turns out you can perform all the following three operations:
     30 
     31 * rearrange the elements in the array $l$ in any order;
     32 * rearrange the elements in the array $r$ in any order;
     33 * rearrange the elements in the array $c$ in any order.
     34 
     35 However, after performing all of the operations, the intervals must still be valid (i.e., for each $i$, $l_i \lt r_i$ must hold).
     36 
     37 What's the minimum possible sum of weights of the intervals after performing the operations?
     38 
     39 Input
     40 
     41 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
     42 
     43 The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of intervals.
     44 
     45 The second line of each test case contains $n$ integers $l_1, l_2, \ldots, l_n$ ($1 \le l_i \le 2 \cdot 10^5$) — the left endpoints of the initial intervals.
     46 
     47 The third line of each test case contains $n$ integers $r_1, r_2, \ldots, r_n$ ($l_i \lt r_i \le 2 \cdot 10^5$) — the right endpoints of the initial intervals.
     48 
     49 It is guaranteed that $\{l_1, l_2, \dots, l_n, r_1, r_2, \dots, r_n\}$ are all distinct.
     50 
     51 The fourth line of each test case contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^7$) — the initial weights of the intervals per unit length.
     52 
     53 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
     54 
     55 Output
     56 
     57 For each test case, output a single integer: the minimum possible sum of weights of the intervals after your operations.
     58 
     59 Example
     60 
     61 Input
     62 
     63 ```
     64 2
     65 
     66 2
     67 
     68 8 3
     69 
     70 12 23
     71 
     72 100 100
     73 
     74 4
     75 
     76 20 1 2 5
     77 
     78 30 4 3 10
     79 
     80 2 3 2 3
     81 ```
     82 
     83 Output
     84 
     85 ```
     86 2400
     87 42
     88 ```
     89 
     90 Note
     91 
     92 In the first test case, you can make
     93 
     94 * $l = [8, 3]$;
     95 * $r = [23, 12]$;
     96 * $c = [100, 100]$.
     97 
     98 In that case, there are two intervals:
     99 
    100 * interval $[8, 23]$ with weight $100$ per unit length, and $100 \cdot (23-8) = 1500$ in total;
    101 * interval $[3, 12]$ with weight $100$ per unit length, and $100 \cdot (12-3) = 900$ in total.
    102 
    103 The sum of the weights is $2400$. It can be shown that there is no configuration of final intervals whose sum of weights is less than $2400$.
    104 
    105 In the second test case, you can make
    106 
    107 * $l = [1, 2, 5, 20]$;
    108 * $r = [3, 4, 10, 30]$;
    109 * $c = [3, 3, 2, 2]$.
    110 
    111 In that case, there are four intervals:
    112 
    113 * interval $[1, 3]$ with weight $3$ per unit length, and $3 \cdot (3-1) = 6$ in total;
    114 * interval $[2, 4]$ with weight $3$ per unit length, and $3 \cdot (4-2) = 6$ in total;
    115 * interval $[5, 10]$ with weight $2$ per unit length, and $2 \cdot (10-5) = 10$ in total;
    116 * interval $[20, 30]$ with weight $2$ per unit length, and $2 \cdot (30-20) = 20$ in total.
    117 
    118 The sum of the weights is $42$. It can be shown that there is no configuration of final intervals whose sum of weights is less than $42$.