Problem p00912 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
array<int, (int)(1e5)> ok;
array<int, (int)(1e5)> oksum;
array<ll, 100000> ubs;
array<ll, 100000> sumx;

bool check(int w, int n, ll sp) {
  fill(ok.begin(), ok.begin() + n + 1, false);
  fill(oksum.begin(), oksum.begin() + n + 2, false);
  ok[n] = true;
  ok[n - 1] = true;
  oksum[n + 1] = 0;
  oksum[n] = 1;
  oksum[n - 1] = 2;
  for (int i = n - 2; i >= 0; i--) {
    // cerr<<"1check"<<i<<endl;
    int j = ubs[i];
    int lb = i + 1; //[i,lb)de tarinai
    int ub = j;
    while (ub - lb > 1) {
      int mid = ((lb + ub) >> 1);
      ll mot = w - (sumx[mid] - sumx[i]);
      ll chi = mid - i - 1;
      if ((mot + chi - 1) / chi > sp) {
        lb = mid;
      } else {
        ub = mid;
      }
    }
    // cerr<<"2check"<<i<<endl;
    int b = ub;
    // check [b,j)
    if (j == n + 1 || oksum[b] - oksum[j] > 0) {
      ok[i] = true;
    } else {
      ok[i] = false;
    }
    // cerr<<"#"<<i<<" "<<b<<" "<<j<<" "<<ok[i]<<endl;
    oksum[i] = oksum[i + 1] + ok[i];
  }
  // cerr<<sp<<endl;
  // string buf;
  // cin>>buf;

  return ok[0];
}

int solve(int w, int n) {
  vector<ll> x(n);
  for (int i = 0; i < n; i++)
    cin >> x[i];
  for (int i = 0; i < n; i++) {
    sumx[i + 1] = sumx[i] + x[i];
  }
  for (int i = n - 2; i >= 0; i--) {
    int lb = i;
    int ub = n + 1;
    // cerr<<"check"<<i<<endl;
    //[i,lb) is ok
    // w-(sumx[lb]-sumx[i])>=lb-i-1
    // w+i+1>=sumx[lb]-sumx[i]+lb
    while (ub - lb > 1) {
      int mid = (lb + ub) / 2;
      ll sux = sumx[mid] - sumx[i] + mid;
      // cerr<<"#"<<mid<<" "<<sux<<" "<<w+i+1<<endl;
      if (w + i + 1 >= sux) {
        lb = mid;
      } else {
        ub = mid;
      }
    }
    ubs[i] = ub;
  }
  int lb = 0;
  int ub = w;
  // cerr<<"End summation"<<endl;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    if (!check(w, n, mid)) {
      lb = mid;
    } else {
      ub = mid;
    }
  }
  return ub;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int w, n;
  while (cin >> w >> n, w) {
    cout << solve(w, n) << "\n";
  }
  return 0;
}


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

using ll = long long;
array<int, (int)(1e5)> ok;
array<int, (int)(1e5)> oksum;
array<ll, 100000> ubs;
array<ll, 100000> sumx;

bool check(int w, int n, ll sp) {
  fill(ok.begin(), ok.begin() + n + 1, false);
  fill(oksum.begin(), oksum.begin() + n + 2, false);
  ok[n] = true;
  ok[n - 1] = true;
  oksum[n + 1] = 0;
  oksum[n] = 1;
  oksum[n - 1] = 2;
  for (int i = n - 2; i >= 0; i--) {
    // cerr<<"1check"<<i<<endl;
    int j = ubs[i];
    int lb = i + 1; //[i,lb)de tarinai
    int ub = j;
    while (ub - lb > 1) {
      int mid = ((lb + ub) >> 1);
      ll mot = w - (sumx[mid] - sumx[i]);
      ll chi = mid - i - 1;
      // floor((mot+chi-1)/chi)>sp
      //
      if (mot > sp * chi) {
        lb = mid;
      } else {
        ub = mid;
      }
    }
    // cerr<<"2check"<<i<<endl;
    int b = ub;
    // check [b,j)
    if (j == n + 1 || oksum[b] - oksum[j] > 0) {
      ok[i] = true;
    } else {
      ok[i] = false;
    }
    // cerr<<"#"<<i<<" "<<b<<" "<<j<<" "<<ok[i]<<endl;
    oksum[i] = oksum[i + 1] + ok[i];
  }
  // cerr<<sp<<endl;
  // string buf;
  // cin>>buf;

  return ok[0];
}

int solve(int w, int n) {
  vector<ll> x(n);
  for (int i = 0; i < n; i++)
    cin >> x[i];
  for (int i = 0; i < n; i++) {
    sumx[i + 1] = sumx[i] + x[i];
  }
  for (int i = n - 2; i >= 0; i--) {
    int lb = i;
    int ub = n + 1;
    // cerr<<"check"<<i<<endl;
    //[i,lb) is ok
    // w-(sumx[lb]-sumx[i])>=lb-i-1
    // w+i+1>=sumx[lb]-sumx[i]+lb
    while (ub - lb > 1) {
      int mid = (lb + ub) / 2;
      ll sux = sumx[mid] - sumx[i] + mid;
      // cerr<<"#"<<mid<<" "<<sux<<" "<<w+i+1<<endl;
      if (w + i + 1 >= sux) {
        lb = mid;
      } else {
        ub = mid;
      }
    }
    ubs[i] = ub;
  }
  int lb = 0;
  int ub = w;
  // cerr<<"End summation"<<endl;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    if (!check(w, n, mid)) {
      lb = mid;
    } else {
      ub = mid;
    }
  }
  return ub;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int w, n;
  while (cin >> w >> n, w) {
    cout << solve(w, n) << "\n";
  }
  return 0;
}