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

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define PB push_back
using namespace std;
typedef pair<int, int> P1;
typedef pair<int, pair<int, int>> P2;
static const int INF = 1ll << 60;
static const int dx[] = {
    1,
    -1,
    0,
    0,
};
static const int dy[] = {0, 0, 1, -1};
static const int mod = 1000000007;

int N, M;
int v[3005], h[3005];
int t[3005], s[3005];

//?????¨       //??????
int imos[3005];
int dp[1005][3005];

P1 min_pair(P1 a, P1 b) {
  if (a.first < b.first)
    return a;
  else if (a.first > b.first)
    return b;
  else {
    if (a.second < b.second)
      return a;
    else if (a.second > b.second)
      return b;
    else
      return a;
  }
}

void solve() {

  for (int i = 0; i < 1005; ++i)
    for (int j = 0; j < 3005; ++j)
      dp[i][j] = -INF;

  dp[0][0] = 0;

  for (int i = 0; i < M; ++i) {
    for (int j = 0; j <= N; ++j) {
      int cost;
      if (j)
        cost = imos[j - 1];
      else
        cost = 0;
      if (cost > t[i])
        continue;
      if (j > 0) {
        dp[i + 1][j] = max(dp[i][j - 1] + h[j - 1], dp[i + 1][j]);
      }
      if (j > 1) {
        dp[i + 1][j] =
            max(dp[i + 1][j - 1] + h[j - 1] + abs(h[j - 2] - h[j - 1]),
                dp[i + 1][j]);
      }
    }
    for (int j = 0; j <= N; ++j)
      dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
    for (int j = 0; j <= N; ++j) {
      if (dp[i + 1][j] < s[i])
        dp[i + 1][j] = -INF;
    }
  }

  for (int i = 0; i <= N; ++i) {
    if (dp[M][i] != -INF) {
      if (!i)
        cout << t[M - 1] << endl;
      else
        cout << t[M - 1] - imos[i - 1] << endl;
      return;
    }
  }
  cout << -1 << endl;
}

signed main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i)
    cin >> v[i] >> h[i];
  for (int i = 0; i < M; ++i)
    cin >> t[i] >> s[i];
  for (int i = 0; i < N; ++i) {
    if (!i) {
      imos[i] = v[i];
    } else
      imos[i] = imos[i - 1] + v[i];
  }
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define PB push_back
using namespace std;
typedef pair<int, int> P1;
typedef pair<int, pair<int, int>> P2;
static const int INF = 1ll << 60;
static const int dx[] = {
    1,
    -1,
    0,
    0,
};
static const int dy[] = {0, 0, 1, -1};
static const int mod = 1000000007;

int N, M;
int v[3005], h[3005];
int t[3005], s[3005];

//?????¨       //??????
int imos1[3005], imos2[3005];
int bonus[3005];
int sum[3005];
P1 dp[1005][3005];

P1 min_pair(P1 a, P1 b) {
  if (a.first < b.first)
    return a;
  else if (a.first > b.first)
    return b;
  else {
    if (a.second < b.second)
      return a;
    else if (a.second > b.second)
      return b;
    else
      return a;
  }
}

void solve() {

  for (int i = 0; i < 1005; ++i)
    for (int j = 0; j < 3005; ++j)
      dp[i][j] = P1(INF, 0);

  for (int i = 0; i < N; ++i) {
    int V = imos1[i];
    int H = imos2[i] + bonus[i];
    if (t[0] - V < 0)
      continue;
    if (s[0] > H)
      continue;
    dp[0][i] = min(P1(V, -H), dp[0][i]);
  }

  for (int i = 0; i < M - 1; ++i) {
    for (int j = 0; j < N; ++j) {
      int cost = dp[i][j].first;
      int strength = -dp[i][j].second;
      if (cost > t[i] || strength < s[i])
        continue;
      /*for(int k=j;k<N;++k){
          imos2[k]-=imos2[j];
      }
      //cout<<s[i+1]-strength<<endl;
      int it = lower_bound(imos2+j,imos2+N,s[i+1]-strength)-imos2;
      for(int k=j;k<N;++k){
          imos2[k]+=imos2[j];
      }*/
      // cout<<"//////////"<<endl;
      // cout<<it<<endl;
      for (int k = 0; k < N - j; ++k) {
        // cout<<k<<endl;
        int cur_v = imos1[j + k] - imos1[j];
        int cur_h = imos2[j + k] - imos2[j];
        int cur_b = bonus[j + k] - bonus[j + 1];
        if (k <= 1)
          cur_b = 0;
        if (t[i + 1] - (cost + cur_v) < 0)
          continue;
        if (s[i + 1] > strength + cur_h + cur_b)
          continue;
        dp[i + 1][j + k] = min_pair(
            P1(cost + cur_v, -(strength + cur_h + cur_b)), dp[i + 1][j + k]);
      }
    }
  }

  int ans = INF;
  for (int i = 0; i < 3005; ++i)
    ans = min(dp[M - 1][i].first, ans);
  if (ans >= INF)
    cout << "-1" << endl;
  else
    cout << t[M - 1] - ans << endl;
}

signed main() {
  cin >> N >> M;
  for (int i = 0; i < N; ++i)
    cin >> v[i] >> h[i];
  for (int i = 0; i < M; ++i)
    cin >> t[i] >> s[i];
  for (int i = 0; i < N; ++i) {
    if (!i) {
      imos1[i] = v[i];
      imos2[i] = h[i];
    } else {
      imos1[i] = imos1[i - 1] + v[i];
      imos2[i] = imos2[i - 1] + h[i];
    }
  }
  for (int i = 1; i < N; ++i) {
    bonus[i] = bonus[i - 1] + abs(h[i] - h[i - 1]);
  }
  solve();
  return 0;
}