title: arithmetic overflow in codility test
body: <p>I did the codility demo test "NumberOfDiscIntersections":
  <a href="https://codility.com/programmers/lessons/4" rel="nofollow">https://codility.com/programmers/lessons/4</a></p>
  
  <p>I've got: perf = 100% and correctness 87%  </p>
  
  <p>All tests but one went fine: </p>
  
  <pre><code>overflow 
  arithmetic overflow tests
  </code></pre>
  
  <p>Why was my long long, not enough ? I can't figure what went wrong! </p>
  
  <pre><code>#include &lt;algorithm&gt;
  
  
  int solution(const vector&lt;int&gt; &amp;A) 
  {
      // write your code in C++11
      vector&lt;long long &gt; vec_max;
      for(int i = 0; i &lt; A.size(); ++i)
      {
          vec_max.push_back( A[i] + i );
      }
      std::sort(vec_max.begin(),vec_max.end()); // sort by max
  
      int step = 1;
      int counter = 0;
      for(int i = A.size() - 1; i &gt; -1; --i)
      {
          std::vector&lt;long long&gt;::iterator low;
  
          int nb_upper = A.size() - ( lower_bound( vec_max.begin(),vec_max.end(), (long long) (i - A[i]) ) - vec_max.begin() );
          counter += nb_upper - step;
          ++step;
      }
  
      if (counter &gt; 10000000)
      {
          return -1;
      }
      else
      {
          return counter;
      }
  }
  </code></pre>