title: Time Limit Exceeding for spoj CANDYSTN
body: <pre><code>  #include &lt;iostream&gt;
      using namespace std;
      int main()
      {
          long long int i,a,b,arr[10000]={0},dif,sum=0,tem=1,t;
          cin&gt;&gt;t;
          for(int m=0;m&lt;t;m++)
          {
              cin&gt;&gt;a&gt;&gt;b;
              for(i=0;i&lt;b;i++)
              {
                  cin&gt;&gt;arr[i];
              }
              for(i=0;i&lt;(b-1);i++)
              {
                  dif=arr[i+1]-arr[i];
                  sum=sum+dif;
                  if(sum&lt;=0 &amp;&amp; tem&gt;sum)
                  {
                      tem=sum;
                  }
  
              }
              if(a&gt;(1-(tem)))
                  cout&lt;&lt;1-tem&lt;&lt;"\n";
              else
                  cout&lt;&lt;"-1"&lt;&lt;"\n";
  
          }
              return 0;
      }
  </code></pre>
  
  <p><a href="https://ideone.com/vi7TVl" rel="nofollow">https://ideone.com/vi7TVl</a>
  This is the link to my code.
  I am not able to implement Dynamic Programming.</p>
  
  <p>Please help.
  What can i do to decrease my time.</p>