Problem p02750 (C++)

Buggy submission (status: Runtime Error):
// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

void solve(long long n, long long t, std::vector<long long> a,
           std::vector<long long> b) {
  for (int i = 0; i < n; ++i)
    ++a[i];
  auto comp = [&](int i, int j) {
    i64 x = (b[i] + 1) * a[j] + b[j];
    i64 y = (b[j] + 1) * a[i] + b[i];
    return x < y;
  };
  vector<int> idxes(n);
  iota(idxes.begin(), idxes.end(), 0);
  sort(idxes.begin(), idxes.end(), comp);

  auto aa = a, bb = b;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (aa[idxes[i]] != 1) {
      a[cnt] = aa[idxes[i]];
      b[cnt] = bb[idxes[i]];
      ++cnt;
    }
  }
  int border = cnt;
  for (int i = 0; i < n; ++i) {
    if (aa[idxes[i]] == 1) {
      a[cnt] = aa[idxes[i]];
      b[cnt] = bb[idxes[i]];
      ++cnt;
    }
  }
  vector<vector<i64>> dp(border + 1, vector<i64>(33, INF));
  dp[0][0] = 0;
  for (int i = 0; i < border; ++i) {
    for (int j = 0; j <= 32; ++j) {
      chmin(dp[i + 1][j], dp[i][j]);
      if (j != 32) {
        i64 time = dp[i][j] + 1;
        if (time > t)
          continue;
        chmin(dp[i + 1][j + 1], a[i] * time + b[i]);
      }
    }
  }
  vector<i64> res = dp.back();
  auto comp2 = [&](int i, int j) {
    assert(a[i] == 1);
    assert(a[j] == 1);
    return b[i] < b[j];
  };
  sort(next(idxes.begin(), border), idxes.end(), comp2);
  bb = b;
  for (int i = border; i < n; ++i) {
    a[i] = 1;
    b[i] = bb[idxes[i]];
  }
  vector<i64> v(1, 0);
  for (int i = border; i < n; ++i)
    v.push_back(v.back() + b[i] + 1);
  int ans = 0;
  for (int i = 0; i <= 32; ++i) {
    if (res[i] > t)
      continue;
    auto it = upper_bound(v.begin(), v.end(), t - res[i]);
    int d = distance(v.begin(), it) - 1;
    chmax(ans, i + d);
  }
  cout << ans << endl;
}

signed main() {
  long long N;
  scanf("%lld", &N);
  long long T;
  scanf("%lld", &T);
  std::vector<long long> a(N);
  std::vector<long long> b(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
  }
  solve(N, T, std::move(a), std::move(b));
  return 0;
}


Fixed submission:
// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

void solve(long long n, long long t, std::vector<long long> a,
           std::vector<long long> b) {
  for (int i = 0; i < n; ++i)
    ++a[i];
  auto comp = [&](int i, int j) {
    i64 x = (b[i] + 1) * a[j] + b[j];
    i64 y = (b[j] + 1) * a[i] + b[i];
    return x < y;
  };
  vector<int> idxes(n);
  iota(idxes.begin(), idxes.end(), 0);
  sort(idxes.begin(), idxes.end(), comp);

  auto aa = a, bb = b;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (aa[idxes[i]] != 1) {
      a[cnt] = aa[idxes[i]];
      b[cnt] = bb[idxes[i]];
      ++cnt;
    }
  }
  int border = cnt;
  for (int i = 0; i < n; ++i) {
    if (aa[idxes[i]] == 1) {
      a[cnt] = aa[idxes[i]];
      b[cnt] = bb[idxes[i]];
      ++cnt;
    }
  }
  vector<vector<i64>> dp(border + 1, vector<i64>(33, INF));
  dp[0][0] = 0;
  for (int i = 0; i < border; ++i) {
    for (int j = 0; j <= 32; ++j) {
      chmin(dp[i + 1][j], dp[i][j]);
      if (j != 32) {
        i64 time = dp[i][j] + 1;
        if (time > t)
          continue;
        chmin(dp[i + 1][j + 1], a[i] * time + b[i]);
      }
    }
  }
  vector<i64> res = dp.back();
  auto comp2 = [&](int i, int j) {
    assert(a[i] == 1);
    assert(a[j] == 1);
    return b[i] < b[j];
  };
  iota(idxes.begin(), idxes.end(), 0);
  sort(next(idxes.begin(), border), idxes.end(), comp2);
  bb = b;
  for (int i = border; i < n; ++i) {
    a[i] = 1;
    b[i] = bb[idxes[i]];
  }
  vector<i64> v(1, 0);
  for (int i = border; i < n; ++i)
    v.push_back(v.back() + b[i] + 1);
  int ans = 0;
  for (int i = 0; i <= 32; ++i) {
    if (res[i] > t)
      continue;
    auto it = upper_bound(v.begin(), v.end(), t - res[i]);
    int d = distance(v.begin(), it) - 1;
    chmax(ans, i + d);
  }
  cout << ans << endl;
}

signed main() {
  long long N;
  scanf("%lld", &N);
  long long T;
  scanf("%lld", &T);
  std::vector<long long> a(N);
  std::vector<long long> b(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
  }
  solve(N, T, std::move(a), std::move(b));
  return 0;
}