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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define pb(e) push_back(e)
#define foreach(c, i) for (iter(c) i = (c).begin(); i != c.end(); ++i)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

const int sz = 4010;
int dp[sz][sz];
int last[sz][sz];
ll A[sz];
P S[sz];

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> A[i];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 0;
  rep(i, n) dp[0][i + 1] = 1;

  for (int l = 1; l < n; l++) {
    vector<pair<ll, P>> v;
    ll suml = 0;
    ll sumr = 0;
    for (int i = l - 1; i >= 0; i--) {
      suml += A[i];
      v.push_back(make_pair(suml, P(dp[i][l], i)));
    }
    sort(v.begin(), v.end());
    S[0] = P(-INF, -1);
    rep(i, v.size()) {
      if (v[i].second.first > S[i].first)
        S[i + 1] = v[i].second;
      else
        S[i + 1] = S[i];
    }
    for (int r = l + 1; r <= n; r++) {
      sumr += A[r - 1];
      int ind =
          lower_bound(v.begin(), v.end(), make_pair(sumr, P(0, 0))) - v.begin();
      dp[l][r] = S[ind].first + 1;
      last[l][r] = S[ind].second;
    }
  }

  int v = 0;
  int pre;
  int best = 0;

  rep(i, n) if (best < dp[i][n]) {
    best = dp[i][n];
    v = i;
  }
  pre = n;
  vector<int> res;
  rep(i, best - 1) {
    res.push_back(v);
    pre = v;
    v = last[v][n];
  }
  reverse(res.begin(), res.end());
  cout << best << endl;
  rep(i, res.size()) {
    if (i > 0)
      cout << ' ';
    cout << res[i];
  }
  cout << endl;
  // rep(i, n + 1)rep(j, i) cout << j << "  " << i << " " << dp[j][i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define pb(e) push_back(e)
#define foreach(c, i) for (iter(c) i = (c).begin(); i != c.end(); ++i)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

const int sz = 4010;
int dp[sz][sz];
int last[sz][sz];
ll A[sz];
P S[sz];

int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> A[i];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 0;
  rep(i, n) dp[0][i + 1] = 1;

  for (int l = 1; l < n; l++) {
    vector<pair<ll, P>> v;
    ll suml = 0;
    ll sumr = 0;
    for (int i = l - 1; i >= 0; i--) {
      suml += A[i];
      v.push_back(make_pair(suml, P(dp[i][l], i)));
    }
    sort(v.begin(), v.end());

    for (int r = l + 1; r <= n; r++) {
      sumr += A[r - 1];
      S[0] = P(-INF, -1);
      rep(i, v.size()) {
        if (v[i].second.first > S[i].first)
          S[i + 1] = v[i].second;
        else
          S[i + 1] = S[i];
      }

      int ind =
          lower_bound(v.begin(), v.end(), make_pair(sumr, P(0, 0))) - v.begin();
      dp[l][r] = S[ind].first + 1;
      last[l][r] = S[ind].second;
    }
  }

  int v = 0;
  int pre;
  int best = 0;

  rep(i, n) if (best < dp[i][n]) {
    best = dp[i][n];
    v = i;
  }
  pre = n;
  vector<int> res;
  rep(i, best - 1) {
    res.push_back(v);
    pre = v;
    v = last[v][n];
  }
  reverse(res.begin(), res.end());
  cout << best << endl;
  rep(i, res.size()) {
    if (i > 0)
      cout << ' ';
    cout << res[i];
  }
  cout << endl;
  // rep(i, n + 1)rep(j, i) cout << j << "  " << i << " " << dp[j][i] << endl;
  return 0;
}