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

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct RMQ {
  int n;
  vector<int> dat;
  const int def = 0;
  RMQ() {}
  RMQ(int n_) { init(n_); }
  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.clear();
    dat.resize(2 * n - 1, def);
  }
  void update(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return def;
    if (a <= l && r <= b)
      return dat[k];
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
  int query(int a, int b) { return query(a, b, 0, 0, n); }
};
int dp[114514];
int sum[114514];
int x[114514];
typedef pair<int, int> P;
int mceil(int p, int q) { return (p + q - 1) / q; }
signed main() {
  int w, n;
  while (cin >> w >> n, w || n) {
    for (int i = 0; i < n; i++)
      cin >> x[i];
    fill(dp, dp + 114514, w);
    priority_queue<P, vector<P>, greater<P>> q;
    q.push(P(0, 0));
    dp[0] = 0;
    sum[0] = 0;
    for (int i = 0; i < n; i++)
      sum[i + 1] = sum[i] + x[i];
    RMQ rmq(n + 1);
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int s = p.first, k = p.second;
      if (dp[k] < s)
        continue;
      if (k == n)
        break;
      int y = max(k, rmq.query(0, k)), z = 0;
      for (int i = y; i < n; i++) {
        int tmp = sum[i + 1] - sum[k];
        if (i != n - 1 && i == k)
          continue;
        if (w < tmp + i - k)
          break;
        int res;
        if (i == n - 1)
          res = max(s, 1LL);
        else
          res = max(s, mceil(w - tmp, i - k));
        // cout<<i<<":"<<res<<endl;
        if (res < dp[i + 1] && res < dp[n]) {
          dp[i + 1] = res;
          q.push(P(dp[i + 1], i + 1));
        }
        z = i;
      }
      rmq.update(k, z);
    }
    // for(int i=0;i<n;i++) cout<<i<<" "<<dp[i]<<endl;
    cout << dp[n] << endl;
    ;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[114514];
int x[114514];
typedef pair<int, int> P;
int mceil(int p, int q) { return (p + q - 1) / q; }
int main() {
  int w, n;
  while (cin >> w >> n, w || n) {
    for (int i = 0; i < n; i++)
      cin >> x[i];
    fill(dp, dp + 114514, w);
    priority_queue<P, vector<P>, greater<P>> q;
    q.push(P(0, 0));
    dp[0] = 0;
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int s = p.first, k = p.second;
      if (dp[k] < s)
        continue;
      if (k == n)
        break;
      int tmp = 0;
      for (int i = k; i < n; i++) {
        tmp += x[i];
        if (i != n - 1 && i == k)
          continue;
        if (w < tmp + i - k)
          break;
        int res;
        if (i == n - 1)
          res = max(s, 1);
        else
          res = max(s, mceil(w - tmp, i - k));
        // cout<<i<<":"<<res<<endl;
        if (res < dp[i + 1] && res < dp[n]) {
          dp[i + 1] = res;
          q.push(P(dp[i + 1], i + 1));
        }
      }
    }
    // for(int i=0;i<n;i++) cout<<i<<" "<<dp[i]<<endl;
    cout << dp[n] << endl;
    ;
  }
  return 0;
}