I have applied the solution algorithm but still getting TLE

Acc. to the solution approach, I have used a max array and maintained a sorted list of elements and the used binary search to find the first element of the triplet.
here is my code,

int solve(int* A, int n1) {
int i,j,k,l;
int maxsum = 0, sum = 0, maxk =0, mini, key;
int left, right, mid;
int max[n1];

max[n1-1] = 0; 


for(i = n1-2; i>=0; i--){
    if(A[i+1]>max[i+1]) max[i] = A[i+1];
    else max[i] = max[i+1];
}

for(j=1;j<n1-1;j++){
    sum = 0;
    maxk = 0;
    mini = 0;
    
    if(max[j]>A[j]) maxk = max[j];
    
    key = A[j-1];
    l = j-2;
    while(l>=0 && A[l]>key){
        A[l+1] = A[l];
        l--;
    }
    
    A[l+1] = key;

    left = 0;
    right = j-1;
    
    if(A[j]<=A[0]) mini = 0;
    
    else if (A[j]>A[j-1]) mini = A[j-1];
    
    else {
        
        while(left<right){
            mid = (left+right+1)/2;
            if(A[mid]<A[j]) left = mid;
            else right = mid-1;
        }
        mini = A[left];
    }

    if(mini!=0)sum = A[j] + maxk + mini;
    if(sum > maxsum) maxsum = sum;
}

return maxsum;

}

Click here to start solving coding interview questions