Problem p00912 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

bool ok(int w, const vi &xs, int sp) {
  int n = xs.size();
  vi sum(n + 1);
  rep(i, n) sum[i + 1] = sum[i] + xs[i];

  vi dp(n + 1);
  dp[0] = 1;
  int l = 0, r = 0, cnt = 0;
  rep(i, n) {
    for (; l < i; cnt -= !!dp[l++])
      if (sum[i + 1] - sum[l] + (i - l) <= w)
        break;
    for (; r < i; cnt += !!dp[r++])
      if (sum[i + 1] - sum[r] + (i - r) * sp < w)
        break;
    dp[i + 1] = !!cnt;
  }

  int res = dp[n];
  for (int i = n; i--;) {
    if (sum[n] - sum[i] + (n - i - 1) > w)
      break;
    res |= dp[i];
  }
  return res;
}

int main() {
  for (int w, n; cin >> w >> n && w | n;) {
    vi xs(n);
    rep(i, n) cin >> xs[i];

    int lo = 1, hi = w;
    while (lo < hi) {
      int mi = (lo + hi) / 2;
      if (ok(w, xs, mi))
        hi = mi;
      else
        lo = mi + 1;
    }
    cout << lo << endl;
  }
}

Error: TLE

Fixed submission:

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

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int main() {
  for (int w, n; cin >> w >> n && w | n;) {
    vi xs(n);
    rep(i, n) cin >> xs[i];

    vi dp(n + 1, INF);
    dp[0] = 1;
    rep(i, n) {
      int xsum = xs[i];
      repi(j, i + 1, n) {
        xsum += xs[j];
        if (xsum + (j - i) > w)
          break;
        int wsum = w - xsum;
        int wmax = (wsum + (j - i) - 1) / (j - i);
        dp[j + 1] = min(dp[j + 1], max(dp[i], wmax));
        if (j == n)
          dp[j + 1] = min(dp[j + 1], dp[i]);
      }
    }

    int res = dp[n], sum = 0;
    per(i, n) {
      sum += xs[i];
      if (sum + (n - i - 1) > w)
        break;
      res = min(res, dp[i]);
    }
    cout << res << endl;
  }
}