fork download
  1. #include <bits/stdc++.h>
  2. #define File_Test if(fopen("test.inp","r")){ freopen("test.inp","r",stdin);freopen("test.out","w",stdout);}
  3. #define ll long long
  4. using namespace std;
  5.  
  6. const ll MAX = 1e6 +5;
  7.  
  8. ll n,q,res,x,a[MAX], mx[MAX];
  9.  
  10. ll tknp(ll d, ll c, ll i, ll j)
  11. {
  12. ll res = d;
  13. while(d <= c)
  14. {
  15. ll g = (d + c) / 2;
  16. if(a[g] < a[i] + a[j]) res = g, d = g + 1;
  17. else c = g - 1;
  18. }
  19. return res;
  20. }
  21.  
  22. int main()
  23. {
  24. File_Test;
  25. ios_base::sync_with_stdio(0);
  26. cin.tie(0);
  27. cout.tie(0);
  28. cin >> n;
  29. for(ll i = 1; i <= n; i ++) cin >> a[i];
  30. sort(a + 1, a + 1 + n);
  31. for(ll i = 1; i <= n - 1; i ++)
  32. for(ll j = i + 1; j <= n; j ++)
  33. {
  34. res += (tknp(j, n, i, j) - j);
  35. }
  36. cout << res;
  37. }
Success #stdin #stdout 0.01s 5688KB
stdin
4
1 1 1 1
stdout
4