fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. int main() {
  6. ios::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8.  
  9. int n;
  10. cin >> n;
  11. vector<ll> u(n + 1);
  12. for (int i = 1; i <= n; i++) cin >> u[i];
  13.  
  14. sort(u.begin() + 1, u.end());
  15.  
  16. ll ans = 0;
  17. for (int k = 3; k <= n; k++) {
  18. int i = 1, j = k - 1;
  19. while (i < j) {
  20. if (u[i] + u[j] > u[k]) {
  21. ans += (j - i);
  22. j--;
  23. } else {
  24. i++;
  25. }
  26. }
  27. }
  28.  
  29. cout << ans;
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5316KB
stdin
6
4 2 13 7 5 10
stdout
7