Problem p02750 (C++)

Buggy submission (status: Runtime Error):
// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #include "boost/multiprecision/cpp_int.hpp"
// typedef boost::multiprecision::cpp_int LL;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fi first
#define se second
#define endl "\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void mod_print(ll k) {
  ll P = 1000;
  rep(y, 1, P) {
    ll x = mod(y * k);
    if (x + P >= i_7) {
      x -= i_7;
    }
    if (abs(x) <= P) {
      cout << x << "/" << y << endl;
      return;
    }
  }
  cout << "nun" << endl;
}
//////////////////////////

struct shop {
  ll a, b;
  dd cmp;
};
bool comp(shop x, shop y) {
  // return x.a*(y.b+1)>=y.a*(x.b+1);
  return x.cmp >= y.cmp;
}

int main() {
  fastio ll n, t;
  cin >> n >> t;
  vector<shop> v;
  vector<ll> last;
  rep(i, 0, n - 1) {
    shop s;
    cin >> s.a >> s.b;
    s.cmp = (dd)s.a / (dd)(s.b + 1);
    if (s.a == 0) {
      last.pb(s.b);
    } else {
      v.pb(s);
    }
  }

  sort(v.begin(), v.end(), comp);
  // for(auto x:v)cout<<x.a<<" "<<x.b<<endl;
  ll M = 32;
  ll vs = v.size();
  ll dp[2][M];
  rep(i, 0, 1) rep(j, 0, M - 1) dp[i][j] = inf;
  dp[0][0] = 0;
  rep(i, 0, vs - 1) {
    rep(j, 0, M - 1) dp[(i + 1) & 1][j] = inf;
    shop s = v[i];
    rep(j, 0, M - 1) {
      if (dp[i & 1][j] == inf)
        continue;
      chmin(dp[(i + 1) & 1][j], dp[i & 1][j]);
      ll nxt = (dp[i & 1][j] + 1) * (s.a + 1) + s.b;
      if (nxt <= t && j + 1 <= M - 1)
        chmin(dp[(i + 1) & 1][j + 1], nxt);
    }
    // rep(j,0,M-1)cout<<dp[(i+1)&1][j]<<" ";cout<<endl;
  }
  ll ans = 0;
  SORT(last);
  ll sz = last.size();
  vector<ll> a(sz + 1);
  a[0] = 0;
  rep(i, 0, sz - 1) { a[i + 1] = a[i] + (last[i] + 1); }
  rep(j, 0, M - 1) {
    ll res = t - dp[vs & 1][j];
    if (res < 0)
      continue;

    ll p = POSU(a, res) - 1;
    chmax(ans, p + j);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #include "boost/multiprecision/cpp_int.hpp"
// typedef boost::multiprecision::cpp_int LL;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fi first
#define se second
#define endl "\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void mod_print(ll k) {
  ll P = 1000;
  rep(y, 1, P) {
    ll x = mod(y * k);
    if (x + P >= i_7) {
      x -= i_7;
    }
    if (abs(x) <= P) {
      cout << x << "/" << y << endl;
      return;
    }
  }
  cout << "nun" << endl;
}
//////////////////////////

struct shop {
  ll a, b;
  dd cmp;
};
bool comp(shop x, shop y) {
  // return x.a*(y.b+1)>=y.a*(x.b+1);
  return x.cmp > y.cmp;
}

int main() {
  fastio ll n, t;
  cin >> n >> t;
  vector<shop> v;
  vector<ll> last;
  rep(i, 0, n - 1) {
    shop s;
    cin >> s.a >> s.b;
    s.cmp = (dd)s.a / (dd)(s.b + 1);
    if (s.a == 0) {
      last.pb(s.b);
    } else {
      v.pb(s);
    }
  }

  sort(v.begin(), v.end(), comp);
  // for(auto x:v)cout<<x.a<<" "<<x.b<<endl;
  ll M = 32;
  ll vs = v.size();
  ll dp[2][M];
  rep(i, 0, 1) rep(j, 0, M - 1) dp[i][j] = inf;
  dp[0][0] = 0;
  rep(i, 0, vs - 1) {
    rep(j, 0, M - 1) dp[(i + 1) & 1][j] = inf;
    shop s = v[i];
    rep(j, 0, M - 1) {
      if (dp[i & 1][j] == inf)
        continue;
      chmin(dp[(i + 1) & 1][j], dp[i & 1][j]);
      ll nxt = (dp[i & 1][j] + 1) * (s.a + 1) + s.b;
      if (nxt <= t && j + 1 <= M - 1)
        chmin(dp[(i + 1) & 1][j + 1], nxt);
    }
    // rep(j,0,M-1)cout<<dp[(i+1)&1][j]<<" ";cout<<endl;
  }
  ll ans = 0;
  SORT(last);
  ll sz = last.size();
  vector<ll> a(sz + 1);
  a[0] = 0;
  rep(i, 0, sz - 1) { a[i + 1] = a[i] + (last[i] + 1); }
  rep(j, 0, M - 1) {
    ll res = t - dp[vs & 1][j];
    if (res < 0)
      continue;

    ll p = POSU(a, res) - 1;
    chmax(ans, p + j);
  }
  cout << ans << endl;

  return 0;
}