SALARY - Editorial

Can someone explain this to me in a little bit easier way?

Read both my answers there- https://discuss.codechef.com/questions/91459/explaniation-for-the-salary-editorial?page=1#91462

Can anyone pls tell me how to optimise this code : https://www.codechef.com/viewsolution/16553743

I am using Merge Sort AFAIK its the best sorting algorithm for time complexity but still Code Chef compiler says time limit exceeded. Kindly help me out in optimising it !

in the given test case

3

1 2 3

can i can do it in 2 steps?
by stopping 3 for the first

2 3 3
and now stopping 2 and 3 we get
3 3 3

@Vartult: You can’t stop 2 numbers at a time according to the question,“choose some worker and increase by 1 salary of each worker, except the salary of the chosen worker”.Here it is given as worker and not as workers. So 2 3 3 >>> 3 4 3 >>> 4 4 4

what can I do to optimise this code?
https://www.codechef.com/viewsolution/20272381

#include
#include<stdio.h>
#include<conio.h>
using namespace std;
int main()
{
int t,n,a[100],small=10001,sum=0,tot_sum=0;
cin>>t;
for(int i=0;i<t;i++)
{
cin>>n;
for(int j=0;j<n;j++)
{
cin>>a[j];
if(small>a[j])
{
small=a[j];
}
}
for(int k=0;k<n;k++)
{
sum=a[k]-small;
tot_sum+=sum;
}
cout<<tot_sum;

}

}

Its output is correct …why it is not getting accepted

here is my approach using binary_search!

def main():
#codechef question SALARY 
t = input()
t = int(t)
while t > 0:
    n = input()
    n = int(n)
    val = list(map(int, input().split(" ")))
    initial_sum = sum(val)
    min_value = min(val)
    left = 0
    right = 10000000000
    while left <= right:
        mid = (left + right) // 2
        may_be = initial_sum + (mid * (n-1))
        mean = may_be / n
        diff = mean - min_value
        if diff == mid:
            break
        elif diff < mid:
            right = mid - 1
        else:
            left = mid + 1
    print(mid)
    t -= 1

if name == ‘main’:
main()

Can anyone please tell me the derivation of that formula?

I am getting correct ans for my test case. But codechef compiler is giving time sigtstp error.



	int T,N,moves,minW;
	long sum = 0;
	cin >> T;
	
	for(int t=0; t<T; t++)
	{
	    cin >> N;
	    vector<int> W(N);
	    
	    for(int i=0; i<N; i++)
	        {
	            cin >> W[i];
	            sum += W[i];
	        }
	    
	    minW = *min_element(W.begin(),W.end());
	    
	    moves= sum -N*minW;
	    cout <<moves << endl;
	}
	
	return 0;
}