Tournament(ZCO '14) Need a efficient way!

getting out of time for 5 cases of total 13 cases and rest are correct.

Can anyone help me to make my approach more efficient;
here’s my code:

using namespace std;

int main()
{ long int n,i;
   int st[n];
   for(i=0; i<n; i++)

   long long int rev=0;

   for(i=0; i<n; i++)
   { long int j=i+1;
       for( ;j<n; j++ )
        {int tst = (st[i] - st[j] );
         if (tst<0)
            tst= -(tst);
            rev= rev + tst;


return 0;

The problem can be solved in O(n) time using basic computations .
observe that the ith element will appear i*(n-i) time .

so simply sum like this :-


while( i< n )

ans=ans+ array[i] ** i ** (n-i);



it is same as the problem

thanks for accepting the solution in advance

1 Like