Three test cases are failing in Painter Partition Problem

my solution:

/*

  • @Date : 2020-06-14 12:35:32
  • @Author : Abhimanyu Kumar Maurya ([email protected])
    */
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    bool ib = ios_base::sync_with_stdio(0);
    bool it = cin.tie(0);

bool canPaint(multiset &boards, int k, int time)
{

int sum = 0, p = 0;
for (auto &i : boards)
{
    if (sum + i <= time)
        sum += i;
    else if (i <= time)
    {
        p++;
        sum = i;
    }
    else
        return false;
    if (p > k)
        return false;
}
return true;

}

signed main()
{
int n, k, temp;
cin >> k >> n;
multiset boards;
for (int i = 0; i < n; i++)
{
cin >> temp;
boards.insert(temp);
}

int lb = 0, ub = INTMAX_MAX, mid, ans = -1;
while (lb <= ub)
{
    mid = (lb + ub) / 2;
    if (canPaint(boards, k, mid))
    {
        ans = mid;
        ub = mid - 1;
    }
    else
        lb = mid + 1;
}
cout << ans;
return 0;

}

hello @abhimanyu_kumar_mauryavRu
a) dont use multiset , it will change the order.
painter are allowed to paint only contigous blocks ,so order of elements matters.

use array.

b) ur search space is not correct.
lb should max element in array
ub should summation of all elements in the array.

do this changes and ur code will work

i did the changes suggested by you bt it still fails the three cases .
MY CODE LINK: https://ide.codingblocks.com/s/257992
thanks.

initialise ur variable p with 1.

thankyou . i got AC now

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.