fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll long long int
  6. #define ld long double
  7. #define pb push_back
  8. #define endl "\n"
  9. double pi = 2 * acos(0.0);
  10.  
  11. template<typename T>
  12. ostream& operator<<(ostream &os, const vector<T> &v) {
  13. os << '{';
  14. for (const auto &x : v) os << " " << x;
  15. return os << '}';
  16. }
  17.  
  18. void NOURIN(){
  19. int n;cin>>n;
  20. vector<int>v(n),pre(n);
  21. for(int i = 0;i<n;i++){
  22. cin>>v[i];
  23. }
  24. sort(v.begin(),v.end());
  25. pre[0] = v[0];
  26. for(int i = 1;i<n;i++){
  27. pre[i] = pre[i-1] + v[i];
  28. }
  29. ll ans = 0;
  30. for(int i = 0;i<n;i++){
  31. ll sum = pre[n-1] - pre[i];
  32. ll x = n-1-i;
  33. ll num = v[i]*x;
  34. ans+=abs(num - sum);
  35. }
  36. cout<<ans<<endl;
  37. }
  38.  
  39. int main()
  40. {
  41. ios_base::sync_with_stdio(false);
  42. cin.tie(NULL); cout.tie(NULL);
  43.  
  44. #ifndef ONLINE_JUDGE
  45. freopen("input.txt", "r", stdin);
  46. freopen("output.txt", "w", stdout);
  47. #endif
  48.  
  49. int test_cases = 1;
  50. // cin >> test_cases;
  51.  
  52. for (int tc = 1; tc <= test_cases; tc++) {
  53. NOURIN();
  54. }
  55. return 0;
  56. }
  57. // Querida de Pro...
Success #stdin #stdout 0.01s 5276KB
stdin
1
stdout
0