Advertisement
LEGEND2004

Sum of difference

Aug 10th, 2023
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.38 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. signed main()
  5. {
  6. int n;
  7. cin>>n;
  8. vector<int> v(n);
  9. int sum = 0;
  10. for(int i =0; i<n; i++){
  11.     cin>>v[i];
  12.     sum += v[i];
  13. }
  14. sort(v.begin() , v.end());
  15. reverse(v.begin() , v.end());
  16.  
  17. int m = n-1,ans = 0;
  18. for(int i=0; i<n; i++){
  19.     ans += v[i]*(n - i);
  20.     ans -= sum;
  21.     sum -= v[i];
  22. }
  23. cout<<ans;
  24. }
  25.  
  26.  
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement