Help me in solving REDUARRAY problem

My issue

I am unable to find the wrong point in my code

My code

#include<bits/stdc++.h>
using namespace std;
typedef long long int lwi;
int main(){
    lwi t;
    cin>>t;
    while(t--){
        lwi n;
        cin>>n;
        lwi arr[n];
        for(lwi i=0;i<n;i++){
            cin>>arr[i];
        }
        sort(arr,arr+n);
        lwi l=(n-1)*arr[n-1],k=arr[0];
        lwi i=1,p=1;
        while(i<n){
            if(arr[i]==k){
                p++;
                i++;
            }else{
                lwi s=(n-p)*arr[i-1];
                if(s<l){
                    l=s;
                }
                p=1;
                k=arr[i];
            }
        }
        if(arr[0]!=arr[n-1]){
       cout<<l<<endl;
        }else{
            cout<<0<<endl;
        }
    }
    
}

Problem Link: Redundant Array Practice Coding Problem

your code mai fail on testcase like this
1000000 500000
in this if you convert all no to x=1
minimum ans=2;