codeforces-scrape

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

1540A.md (2470B)


      1 Source: https://codeforces.com/problemset/problem/1540/A
      2 
      3 A. Great Graphs
      4 
      5 time limit per test
      6 
      7 2 seconds
      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 Farmer John has a farm that consists of $n$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.
     22 
     23 Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $d$, where $d_i$ is the smallest amount of time it took the cows to reach the $i$-th pasture from pasture $1$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.
     24 
     25 Input
     26 
     27 The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Then $t$ cases follow.
     28 
     29 The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of pastures.
     30 
     31 The second line of each test case contains $n$ space separated integers $d_1, d_2, \ldots, d_n$ ($0 \leq d_i \leq 10^9$) — the array $d$. It is guaranteed that $d_1 = 0$.
     32 
     33 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
     34 
     35 Output
     36 
     37 For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.
     38 
     39 Example
     40 
     41 Input
     42 
     43 ```
     44 3
     45 3
     46 0 2 3
     47 2
     48 0 1000000000
     49 1
     50 0
     51 ```
     52 
     53 Output
     54 
     55 ```
     56 -3
     57 0
     58 0
     59 ```
     60 
     61 Note
     62 
     63 In the first test case, you can add roads
     64 
     65 * from pasture $1$ to pasture $2$ with a time of $2$,
     66 * from pasture $2$ to pasture $3$ with a time of $1$,
     67 * from pasture $3$ to pasture $1$ with a time of $-3$,
     68 * from pasture $3$ to pasture $2$ with a time of $-1$,
     69 * from pasture $2$ to pasture $1$ with a time of $-2$.
     70 
     71 The total cost is $2 + 1 + -3 + -1 + -2 = -3$.
     72 
     73 In the second test case, you can add a road from pasture $1$ to pasture $2$ with cost $1000000000$ and a road from pasture $2$ to pasture $1$ with cost $-1000000000$. The total cost is $1000000000 + -1000000000 = 0$.
     74 
     75 In the third test case, you can't add any roads. The total cost is $0$.