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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// typedef
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long ll;

#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)

// container util
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); --i)
#define RREP(i, n) RFOR(i, n, 0)

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << "[L " << __LINE__ << "] " << #x << " = " << (x) << "\\n";

const int _N = 3000, _M = 1000;
int n, m, v[_N + 1], h[_N], t[_M + 1], s[_M + 1];
int dp[_N + 1][_M + 1]
      [2]; // [boght items i, 1..][place, 1..][just boght] := max strength
// go next
// or
// buy one more

void smax(int &a, int b) { a = max(a, b); }

int main() {
  cin >> n >> m;
  REP(i, n) {
    cin >> v[i + 1] >> h[i];
    v[i + 1] += v[i];
  }
  REP(i, m) { cin >> t[i] >> s[i]; }
  REP(i, n + 1) REP(j, m + 1) REP(k, 2) dp[i][j][k] = -1;
  dp[0][0][0] = 0;
  REP(j, m) {
    REP(i, n + 1) {
      if (dp[i][j][1] >= 0) {
        // buy more
        if (i < n && t[j] >= v[i + 1])
          smax(dp[i + 1][j][1], dp[i][j][1] + h[i] + abs(h[i] - h[i - 1]));
        // go next
        if (s[j] <= dp[i][j][1])
          smax(dp[i][j + 1][0], dp[i][j][1]);
      }
      if (dp[i][j][0] >= 0) {
        // buy first one
        if (i < n && t[j] >= v[i + 1])
          smax(dp[i + 1][j][1], dp[i][j][0] + h[i]);
        // go next
        if (s[j] <= dp[i][j][0])
          smax(dp[i][j + 1][0], dp[i][j][0]);
      }
    }
  }
  REP(i, n + 1) {
    if (dp[i][m][0] >= 0) {
      cout << t[m - 1] - v[i] << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// typedef
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long ll;

// container util
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const int INF = 1e9;
const int _N = 3000, _M = 1000;

int n, m;
int dp[_N + 1][_M + 1];
// [i][j] :=
// ??¢?????????i?????§????????£???????????????????????????j?????§????????£????????¶?????§???
// ?????§???h(N/A=-1)

// [i+1][j+1] = max x=1 to i+2 [x-1][j] + h[x,  i+1) + dh[x , i+1) ([x][j]!=-1)
// = [i+1][j] + [i+2, i+1] + [i+2, i] , max x=1 to i+1 [x-1][j] + h[x,  i+1] +
// dh[x , i] ([x][j]!=-1) = max [i+1][j] + h[i+2, i+1] + dh[i+2, i] , dp[i][j]

// a b c // 0 - 2
// d e // 0 - 1

int v[_N + 2], h[_N + 2];
int dh[_N + 2];           // dh[i] = abs(h[i+1] - h[i])
int t[_N + 2], s[_N + 1]; // t time, s condition

int main() {
  cin >> n >> m;
  REP(i, n) cin >> v[i + 1] >> h[i + 1];
  REP(i, m) cin >> t[i + 1] >> s[i + 1];

  REP(i, n + 1) dh[i + 1] = abs(h[i + 1] - h[i]);
  REP(i, n + 1) dh[i + 1] += dh[i]; // dh[i,j) = dh[j]-dh[i]
  REP(i, n + 1) h[i + 1] += h[i];   // h[i,j) = h[j]-h[i]
  REP(i, n + 1) v[i + 1] += v[i];

  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;

  REP(j, m) REP(i, n) {
    if (v[i + 1] > t[j + 1])
      continue;
    int r = -1;
    FOR(x, 1, i + 3) {
      if (dp[x - 1][j] < 0)
        continue;
      // buy x-1 to i+1 // i+2-1 to i+1
      int f =
          dp[x - 1][j] + h[i + 1] - h[x - 1] + dh[i + 1] - dh[min(i + 1, x)];
      if (j + 1 == m - 1)
        if (i + 1 == 1) {
          //				dump(dp[x-1][j]);
          //				dump(h[i+1] - h[x-1]);
          //				dump(dh[i+1] - dh[min(i, x)]);
          //				dump(s[j+1]);
        }
      if (f >= s[j + 1]) {
        r = max(r, f);
      }
    }
    dp[i + 1][j + 1] = r;
  }

  int ans = -1;
  REP(i, n + 1) {
    if (dp[i][m] > -1) {
      //			dump(t[m]);
      //			dump(i);
      //			dump(dp[i][m]);
      //			dump(v[i+1]);
      ans = max(ans, t[m] - v[i]);
    }
  }
  cout << ans << endl;

  return 0;
}
/*
2 2
1 100
2 200
4 10
5 400
*/