// Mathematical formula derivation <br/> // sort first, it is easy to deduce and write <br/> // A1 A2 A3 A4 A5 <br/> // 1 2 3 4 5 <br/> // D1 D2 D3 D4 D5 (meaning very apparently) <br/> // 0 1 2 3 4 <br/> // 1 0 1 2 3 <br/> // 2 1 0 1 2 <br/>/3 2 1 0 1 <br/> // 4 3 2 1 0 <br/> // It is a symmetric matrix, adding all the values together is the final answer <br/> // further induction brings the points of the matrix into these relations, such as A3-A2, and adds them all together to get the formula: <br/> // ans = 4 * (A5-A1) + 3 * (A4-A2) + 2 * (A3-A3) + 1 * (A2-A4) <br/> # include <iostream> <br/> # include <algorithm> <br/> using namespace STD; <br/> int Main () <br/>{< br/> long int ans = 0, arr [10010]; // It takes me a long int to survive, and it has affected my wa for a long time, previously, ANS used long int instead of arr. An error may occur when adding different types ...... N times more than wa! Lessons! <Br/> int n, m; <br/> CIN> N; <br/> for (INT I = 0; I <n; ++ I) <br/> {<br/> CIN> arr [I]; // It is faster to use scanf, but you do not know how to read the long int format ...... Which of the following experts will you advise ...... <Br/>}< br/> sort (ARR, arr + n); </P> <p> for (INT I = 0; I <n; ++ I) <br/> {<br/> ans + = (n-1-i) * (ARR [n-1-i]-Arr [I]); <br/>}< br/> cout <ans * 2 <Endl; <br/> return 0; <br/>}