Chef and Work Problem Code: CHEFNWRK Codechef Solution

 #include <bits/stdc++.h>
using namespace std;

int main() {  
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  int t;
  cin>>t;
  while(t--){
      int n,k;
      cin>>n>>k;
      int a;
      int flag=0,curr=0,cnt=0;
      for(int i=0;i<n;i++){
          cin>>a;
          if(a>k)flag=1;
          else if(curr+a<=k)curr+=a;
          else{curr=a;cnt++;}
      }
      if(flag)cout<<-1;
      else cout<<cnt+1;
      cout<<'\n';

 }
}

Comments