I have tried painter problem many times but i am getting 2 test cases wrong

#include
#include
using namespace std;
#define ll unsigned long long int

bool valid(ll ar[], ll n, ll k, ll t, ll mid)
{
ll np = 1, total_t = 0;
for(int i=0;i<n;i++)
{
if((total_t + ar[i]*t) < mid)
total_t += ar[i]*t;
else
{

        np++;
        total_t = ar[i]*t;
        if(total_t > mid &&  (np+1) > k)
            return false;
    }
    
}
if(np > k)
    return false;
return true;

}

int main()
{
ll ans;
ll n, k, t;
cin>>n>>k>>t;
ll sum = 0;
ll * ar = new ll[n];
for(ll i=0;i<n;i++)
{
cin>>ar[i];
sum += ar[i];
}
sort(ar, ar+n);
ll min_t = t;
ll max_t = sum*t;

while(min_t <= max_t)
{
    ll mid = (min_t + max_t)/2;

    if(valid(ar, n, k, t, mid))
    {
        ans = mid%10000003;
        // cout<<mid<<endl;
        max_t = mid-1;
    }
    else
    {
        min_t = mid+1;
        // cout<<"false "<<mid<<endl;
    }
}

cout<<ans;
return 0;

}

@Nitishkumar9711 You cannot sort the array as it will change the postition of the painters

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.