title: Codeforces 13C Solution
body: <p><a href="http://codeforces.com/problemset/problem/13/C" rel="nofollow">http://codeforces.com/problemset/problem/13/C</a>
  I am having problem with the problem above. Can anyone explain the solution clearly. My code seems pretty okay to me, however it is not passing testcase 8.</p>
  
  <pre><code>lli n;
  cin&gt;&gt;n;
  lli arr[size];
  fi(i,n)
  cin&gt;&gt;arr[i];
  if(n==1)
      cout&lt;&lt;"0"&lt;&lt;endl;
  else
  {
      lli sum=0;
      for(lli i=n-1; i&gt;0; i--)
      {
          if(arr[i]&lt;arr[i-1])
          {
              lli k=abs(arr[i]-arr[i-1]);
              if(i==(n-1))
              {
                  arr[i]+=k;
                  sum+=k;
              }
              else
              {
                  if((arr[i]+k)&lt;=arr[i+1])
                      arr[i]+=k, sum+=k;
                  else
                      arr[i-1]-=k, sum+=k;
              }
          }
      }
      cout&lt;&lt;sum&lt;&lt;endl;
  }
  </code></pre>