Problem p02750 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 2e5 + 5;
const int logn = 45;

class Shop {
public:
  int a, b;
  bool operator<(const Shop &oth) const {
    return 1ll * a * (oth.b + 1) > 1ll * oth.a / (b + 1);
  }
} s[maxn];

int n, T, ans;
int sum[maxn];
int dp[maxn][logn];

inline int go(int t, Shop cur) {
  ll ret = min(1ll * T + 1, t + 1 + 1ll * cur.a * (t + 1) + cur.b);
  return ret;
}

int main() {
  scanf("%d%d", &n, &T);
  FOR(i, 0, n) { scanf("%d%d", &s[i].a, &s[i].b); }
  sort(s, s + n);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  FOR(i, 0, n) {
    if (!s[i].a) {
      FOR(j, i, n)
      sum[j] = min(T + 1, (j == i ? 0 : sum[j - 1]) + 1 + s[j].b);
      FOR(j, 0, logn) if (dp[i][j] <= T) {
        int pos = upper_bound(sum + i, sum + n, T - dp[i][j]) - sum - i;
        ans = max(ans, j + pos);
      }
      break;
    }
    FOR(j, 0, logn) if (dp[i][j] <= T) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (go(dp[i][j], s[i]) <= T)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], go(dp[i][j], s[i]));
    }
  }
  if (s[n - 1].a) {
    FOR(j, 0, logn) if (dp[n][j] <= T) ans = j;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, x, y) for (int i = (x); i < (y); ++i)
#define REP(i, x, y) for (int i = (x); i <= (y); ++i)
#define MP make_pair
#define PB push_back
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;

const int INF = 1e9 + 7;
const int maxn = 2e5 + 5;
const int logn = 45;

class Shop {
public:
  int a, b;
  bool operator<(const Shop &oth) const {
    if (!a && !oth.a)
      return b < oth.b;
    return 1ll * a * (oth.b + 1) > 1ll * oth.a * (b + 1);
  }
} s[maxn];

int n, T, ans;
int sum[maxn];
int dp[maxn][logn];

inline int go(int t, Shop cur) {
  ll ret = min(1ll * T + 1, t + 1 + 1ll * cur.a * (t + 1) + cur.b);
  return ret;
}

int main() {
  scanf("%d%d", &n, &T);
  FOR(i, 0, n) { scanf("%d%d", &s[i].a, &s[i].b); }
  sort(s, s + n);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  FOR(i, 0, n) {
    if (!s[i].a) {
      FOR(j, i, n)
      sum[j] = min(T + 1, (j == i ? 0 : sum[j - 1]) + 1 + s[j].b);
      FOR(j, 0, logn) if (dp[i][j] <= T) {
        int pos = upper_bound(sum + i, sum + n, T - dp[i][j]) - sum - i;
        ans = max(ans, j + pos);
      }
      break;
    }
    FOR(j, 0, logn) if (dp[i][j] <= T) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (go(dp[i][j], s[i]) <= T)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], go(dp[i][j], s[i]));
    }
  }
  if (s[n - 1].a) {
    FOR(j, 0, logn) if (dp[n][j] <= T) ans = j;
  }
  printf("%d\n", ans);
  return 0;
}