title: C++ having a glitch?
body: <p>So I was working on this <a href="http://codeforces.com/problemset/problem/218/B" rel="nofollow">Codeforces problem</a> and I wrote a program to solve it, but it wasn't printing the right answer so I decided to try debugging it. Attached below is the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int maxof(int a[], int m)
  {
    int x = a[0];
    for (int i = 1; i &lt; m; i++)
      if (x &lt; a[i])
        x = a[i];
    return x;
  }
  
  int maxind(int a[], int m)
  {
    int x = maxof(a,m);
    int n = 0;
    while (a[n] != x)
      n++;
    return n;
  }
  
  ...
  
  int maxcost(int a[], int n, int m)
  {
    int x = 0;
    for (int i = 0; i &lt; n; i++) {
      /*
      for (int j = 0; j &lt; m; j++)
        cout &lt;&lt; a[j] &lt;&lt; " ";
      cout &lt;&lt; endl;
      */
      x += maxof(a,m);
      a[maxind(a,m)]--;
    }
    return x;
  }
  
  ...
  
  int main()
  {
    int n,m;
    cin &gt;&gt; n &gt;&gt; m;
    int a[m];
    for (int i = 0; i &lt; m; i++)
      cin &gt;&gt; a[i];
    /*
    for (int i = 0; i &lt; m; i++)
      cout &lt;&lt; a[i] &lt;&lt; " ";
    cout &lt;&lt; endl;
    */
    cout &lt;&lt; maxcost(a,n,m) &lt;&lt; " " &lt;&lt; mincost(a,n,m);
  }
  </code></pre>
  
  <p>So if I take out the /* */ blocks and run the program with the input</p>
  
  <pre><code>4 3
  2 1 1
  </code></pre>
  
  <p>It SHOULD print out:</p>
  
  <pre><code>2 1 1
  2 1 1
  1 1 1
  0 1 1
  0 0 1
  0 0 0
  5 5
  </code></pre>
  
  <p>But it's printing out:</p>
  
  <pre><code>2 1 1 
  2 -3 1 
  1 -3 1 
  0 -3 1 
  0 -3 0 
  4 4
  </code></pre>
  
  <p>Where did this random -3 come from, and how do I fix this?</p>