fork(2) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define maxn 100005
  5. ll a[maxn];
  6. ll i, j, k, n, m, res, sum;
  7. map<ll, vector<ll>> mapping;
  8. ll solve(vector<ll> u)
  9. {
  10. sort(u.begin(), u.end());
  11. ll pre_sum[u.size()];
  12. memset(pre_sum, 0, sizeof(pre_sum));
  13. pre_sum[0] = u[0];
  14. for (ll i = 1; i < u.size(); i++)
  15. pre_sum[i] = pre_sum[i - 1] + u[i];
  16. ll tong = 0;
  17. for (ll n = u.size() - 1; n >= 1; n--)
  18. {
  19. tong = tong + n * u[n] - pre_sum[n - 1];
  20. }
  21. return tong;
  22. }
  23. int main()
  24. {
  25. cin >> n;
  26. for (i = 0; i < n; i++)
  27. {
  28. ll x;
  29. cin >> x;
  30. mapping[x].push_back(i);
  31. }
  32. for (auto p : mapping)
  33. {
  34. if (p.second.size() >= 2)
  35. {
  36. res = res + solve(p.second);
  37. }
  38. }
  39. cout << res;
  40. return 0;
  41. }
Success #stdin #stdout 0s 4924KB
stdin
Standard input is empty
stdout
Standard output is empty