Problem p00885 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

const int INF = 1 << 29;

struct Balloon {
  int pos;
  int time;

  Balloon() {}
  ~Balloon() {}

  Balloon(int a, int b) : pos(a), time(b) {}
};

struct State {
  int index;
  int dist;
  int weight;

  State() {}
  ~State() {}

  State(int a, int b, int c) : index(a), dist(b), weight(c) {}

  bool operator<(const State &o) const {
    return dist == o.dist ? index < o.index : dist < o.dist;
  }

  bool operator>(const State &o) const {
    return dist == o.dist ? index > o.index : dist > o.dist;
  }
};

bool solve() {
  int n;
  int pos[45];
  int time[45];

  cin >> n;
  if (n == 0) {
    return false;
  }

  pos[0] = time[0] = 0;
  for (int j = 1; j <= n; ++j) {
    cin >> pos[j] >> time[j];
  }

  int dp[45][8];
  for (int j = 0; j <= n; ++j) {
    fill(dp[j], dp[j] + 8, INF);
  }

  dp[0][1] = 0;

  for (int j = 0; j < n; ++j) {
    int limitTime = time[j + 1] - time[j];
    for (int k = 1; k <= 4; ++k) {
      if (abs(pos[j] - pos[j + 1]) * k <= limitTime) {
        dp[j + 1][k + 1] =
            min(dp[j + 1][k + 1], dp[j][k] + abs(pos[j] - pos[j + 1]));
      }
      if (pos[j] * k + pos[j + 1] <= limitTime) {
        dp[j + 1][2] = min(dp[j + 1][2], dp[j][k] + pos[j] + pos[j + 1]);
      }
    }
  }

  int ret = INF;
  for (int j = 1; j <= 4; ++j) {
    ret = min(ret, dp[n][j] + pos[n]);
  }
  if (ret < INF) {
    cout << "OK" << ' ' << ret << endl;
    return true;
  }

  for (int j = n - 1; j >= 0; --j) {
    for (int k = 1; k <= 4; ++k) {
      if (dp[j][k] < INF) {
        cout << "NG" << ' ' << (j + 1) << endl;
        return true;
      }
    }
  }
  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

struct Balloon {
  int pos;
  int time;

  Balloon() {}
  ~Balloon() {}

  Balloon(int a, int b) : pos(a), time(b) {}
};

struct State {
  int index;
  int dist;
  int weight;

  State() {}
  ~State() {}

  State(int a, int b, int c) : index(a), dist(b), weight(c) {}

  bool operator<(const State &o) const {
    return dist == o.dist ? index < o.index : dist < o.dist;
  }

  bool operator>(const State &o) const {
    return dist == o.dist ? index > o.index : dist > o.dist;
  }
};

bool solve() {
  int n;
  int pos[45];
  int time[45];

  cin >> n;
  if (n == 0) {
    return false;
  }

  pos[0] = time[0] = 0;
  for (int j = 1; j <= n; ++j) {
    cin >> pos[j] >> time[j];
  }

  priority_queue<State, vector<State>, greater<State>> pq;
  pq.push(State(0, 0, 1));

  string result = "NG";
  int output = 0;

  while (!pq.empty()) {
    State s = pq.top();
    pq.pop();

    if (s.index == n) {
      result = "OK";
      output = s.dist + pos[n];
      break;
    }

    output = max(output, s.index + 1);

    int ind = s.index;
    int dist, limitTime;

    limitTime = abs(time[ind + 1] - time[ind]);

    if (s.weight < 4) { // not full
      dist = abs(pos[ind] - pos[ind + 1]);
      if (dist * s.weight <= limitTime) {
        pq.push(State(ind + 1, s.dist + dist, s.weight + 1));
      }
    }
    if (s.weight > 1) { // not empty
      if (pos[ind] * s.weight + pos[ind + 1] <= limitTime) {
        pq.push(State(s.index + 1, s.dist + pos[ind] + pos[ind + 1], 2));
      }
    }
  }

  cout << result << ' ' << output << endl;
  return true;
}

int main() {
  while (solve())
    ;
  return 0;
}