online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <cstring> #include <algorithm> #include <bits/stdc++.h> using namespace std; bool book(int a[],int n,int m,int mid){ int pag = 0,curr = 1; for(int i=0;i<n;i++){ if(pag+a[i]>mid){ pag = a[i]; curr++; if(curr>m){ return false; } } else{ pag+=a[i]; } } return true; } int main(){ int t; cin >> t; while(t--){ int n,m; cin >> n>>m; int a[n]; int end = 0; for(int i=0;i<n;i++){ cin >> a[i]; end +=a[i]; } if(n<m){ cout<<-1<<"\n"; } else{ int sta = 0,ans = INT_MAX; while(sta<=end){ int mid = (sta+end)/2; //cout<<mid<<"\n"; bool boo = book(a,n,m,mid); if(boo){ //cout<<mid<<"\n"; ans = min(ans,mid); //cout<<ans<<" "<<sta<<" "<<end<<" "<<mid<<" "<<"a"<<"\n"; end = mid-1; } else{ //cout<<mid<<"\n"; //cout<<ans<<" "<<sta<<" "<<end<<" "<<mid<<" "<<"b"<<"\n"; sta = mid+1; } } cout<<ans<<"\n"; } } }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue