Can somebody tell me what’s wrong with my solution (CHEFNWRK)?

Problem Link : https://www.codechef.com/COOK121B/problems/CHEFNWRK

My Solution :
#include
using namespace std;

void solve()
{
int n,k;
cin>>n>>k;
int arr[n];
int sum=0;
for(int i=0;i<n;i++)
{
cin>>arr[i];
if(arr[i]>k)
{
cout<<"-1\n";
return;
}
}

sum =0;
int count =1;
for(int i=0;i<n;i++)
{
    sum = sum +arr[i];
    if(sum<=k)
    {
        
    }
    else
    {
        count++;
        sum =0;
        i--;
    }
}
cout<<count<<"\n";

}

int main()
{
int t;
cin>>t;
while(t–)
{
solve();
}
}

if(sum<=k)
{
     
}

Fill in the blank space;

Here’s the problem. You do not read the rest of the input if there’s a arr[i] > k. So the rest of the weights get read as the n for the next test case. Also, there’s a flaw in the logic inside the for loop.

1 Like