title: Max K-product of the numbers
body: <p>Task - Max K-product of the numbers<br>
  Time Limit: 1<br>
  Memory Limit: 64 M<br>
  Given a sequence of integers N (1 ≤ N ≤ 10 May, | A i | ≤ 2.10 9) and the number of K (1 ≤ K ≤ N). Find K sequence numbers whose product is maximum. </p>
  
  <p>Input data:<br>
  The first line contains two integers N and K.<br>
  In the second line lists the N elements of the sequence A. </p>
  
  <p>Output data:<br>
  Derive the maximum product. So the answer may be quite large, output it modulo 10^9+7.</p>
  
  <p>Example<br>
  The result of the input data<br>
  3 2<br>
  -2 -3 3 </p>
  
  <p>Answer - 6</p>
  
  <p>Below is my attempt. It is a mistake, that's what I do not know. Can you help find a mistake in my decision?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  using namespace std;
  typedef vector&lt;int&gt; v1;
  const int mod = 1000000007;
  int n, k, pos1 = 0, pos2 = 0, negative = 0;
  long long res = 1;
  void QuickSort(v1 &amp;a, int l, int r) {
    int i = l, j = r, pivot = abs(a[l + ((r - l) &gt;&gt; 1)]);
    do {
          while (abs(a[i]) &lt; pivot) i++;
          while (abs(a[j]) &gt; pivot) j--;
          if (i &lt;= j) {
              int temp = a[i];
              a[i++] = a[j];
              a[j--] = temp;
          }
      } while (i &lt; j);
      if (l &lt; j) QuickSort(a, l, j);
      if (i &lt; r) QuickSort(a, i, r);
  }
  
  long long product(v1 &amp;a, v1 &amp;b, int q, int j, char flag) {
      long long res = 1; int temppos;
      if (flag == 0 &amp;&amp; j) {
          temppos = b[pos1];
          b[pos1] = j;
      }
      if (flag == 1 &amp;&amp; q) {
          temppos = b[pos2];
          b[pos2] = q;
      }
      if (!pos2 &amp;&amp; (k &amp; 1)) {
          for (int i = 1; i &lt;= k; i++)
              res = (long long)((res % mod)*1ll*(a[i] % mod)) % mod;
      } else {
          for (int i = 1; b[i] != 0; i++)
              res = (long long)((res % mod)*1ll*(a[b[i]] % mod)) % mod;
      }
      if (flag == 0 &amp;&amp; j) b[pos1] = temppos;
      if (flag == 1 &amp;&amp; q) b[pos2] = temppos;
      return res;
  }
  
  int main()
  {
        v1 a(100002, 0);
          v1 b(100002, 0);  //index multiplied to the elements
          cin &gt;&gt; n &gt;&gt; k;
          for (int i = 1; i &lt;= n; i++) cin &gt;&gt; a[i];
          QuickSort(a, 1, n);
          for (int i = n, j = 1; i &gt; n - k; i--) {
                  b[j] = i;
                  if (a[i] &lt; 0) {                     
                      pos1 = j;    //index last positive number 
                      negative++;  //increase the counter negative numbers
                  }
                    else pos2 = j;  //index last positive number                
                  j++;
          }
          int j = n - k, q = j;
          if (negative &amp; 1) { //If an odd number of negative numbers
              while (j &gt; 0 &amp;&amp; a[j] &lt; 0) j--;
              while (q &gt; 0 &amp;&amp; a[q] &gt; 0) q--;
              res = max(product(a, b, q, j, 0), product(a, b, q, j, 1));
          } else res = product(a, b, q, j, 3);
          cout &lt;&lt; res &lt;&lt; endl;
      cin &gt;&gt; res;
      return 0;
  }
  </code></pre>