Language: C++

//By TheOneYouWant

#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define memreset(a) memset(a,0,sizeof(a))
#define testcase(t) int t;cin>>t;while(t--)
#define forstl(i,v) for(auto &i: v)
#define forn(i,e) for(int i = 0; i < e;++i)
#define forsn(i,s,e) for(int i = s; i < e;++i)
#define rforn(i,s) for(int i = s; i >= 0;--i)
#define rforsn(i,s,e) for(int i = s; i >= e;--i)
#define bitcount(a) __builtin_popcount(a) // set bits (add ll)
#define ln '\n'
#define getcurrtime ((double)clock()/CLOCKS_PER_SEC)
#define dbgarr(v,s,e) cerr<<#v<<" = "; forsn(i,s,e) cerr<<v[i]<<", "; cerr<<endl
#define inputfile freopen("input.txt", "r", stdin)
#define outputfile freopen("output.txt", "w", stdout)
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) { cerr<<endl; }
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << "\t"; err(++it, args...);
}
template<typename T1,typename T2>
ostream& operator <<(ostream& c,pair<T1,T2> &v){
    c<<"("<<v.fi<<","<<v.se<<")"; return c;
}
template <template <class...> class TT, class ...T>
ostream& operator<<(ostream& out,TT<T...>& c){
    out<<"{ ";
    forstl(x,c) out<<x<<" ";
    out<<"}"; return out;
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> p64;
typedef pair<int,int> p32;
typedef pair<int,p32> p96;
typedef vector<ll> v64;
typedef vector<int> v32; 
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int,int> m32;

const int LIM=1e5+5, MOD=1e9+7;
const ld EPS = 1e-9;
// int t,n,m,k,x,y,w;
signed main(){
    fastio;
    
    ll n,x;
    cin>>n>>x;
    ll b[n], l[n], r[n];
    forn(i,n) cin>>b[i]>>l[i]>>r[i];
    vector<pair<ll,p64> > v;
    forn(i,n){
        v.pb(mp(LIM-b[i],mp(r[i],l[i])));
    }

    ll estimate = 0 ;
    forn(i,n) estimate += b[i]*l[i];
    ll curr = 0;
    ll hours = 0;
    // cout<<estimate<<ln;

    vector<pair<ld, ll> > minh;

    forn(i,n){
        ll h = b[i]*r[i];
        ld cover = (x-b[i])*r[i];
        // cover = max(cover, l[i]);
        minh.pb(mp(cover,i));
    }

    sort(minh.begin(),minh.end(),greater<pair<ld, ll> >());

    int chosen = 0;
    bool done[n] = {0};
    bool ok = 0;
    forn(j,n){
        int i = minh[j].se;
        v[i].fi = LIM - v[i].fi;
        // cout<<v[i]<<ln;
        estimate += (v[i].se.fi - v[i].se.se) * v[i].fi;
        if(x*v[i].se.fi + curr >= estimate){
            ll hadded = 1e9;
            estimate -= (v[i].se.fi - v[i].se.se) * v[i].fi;
            // cout<<estimate<<" "<<curr<<ln;
            
            forn(e,n){
                if(done[e]) continue;
                ll y = (estimate - curr + (v[e].se.fi-v[e].se.se)*v[e].fi +v[i].se.fi-1)/v[i].se.fi;
                if(y< hadded) {
                    chosen = e;
                    hadded = y;
                    ok = 0;
                }
                y = (estimate - curr + v[e].se.se-1)/ v[e].se.se;
                if(y< hadded) {
                    chosen = e;
                    hadded = y;
                    ok = 1;
                }
            }
            if(ok == 0) done[chosen] = 1;
            // cout<<hadded<<ln;
            hours += hadded;
            break;
        }
        else{
            curr += x*v[i].se.fi;
            hours += x;
            done[i] = 1;
        }
        // cout<<i<<" "<<hours<<" "<<curr<<" "<<estimate<<ln;
    }
    // cout<<hours<<ln;
    hours = 0;

    vector<pair<ll, pair<ll, p64> > > finalc;

    ll est = 0;

    forn(i,n){
        if(done[i]){
            finalc.pb(mp(r[i],mp(x,mp(r[i]-l[i],b[i]))));
        }
        else{
            finalc.pb(mp(l[i],mp(b[i],0)));
        }
        est += b[i]*l[i];
    }
    // if(ok){
    //     finalc.pb(mp(l[chosen],b[chosen]));
    // }
    sort(finalc.begin(),finalc.end(), greater<pair<ll, pair<ll, p64> > >());

    // cout<<finalc<<ln;

    // ll currcost[n] = {0};
    // forn(i,n){
    //     if(i==0) currcost[i] = (r[i]-l[i])*b[i];
    //     else currcost[i] = currcost[i-1] + (r[i]-l[i])*b[i];
    // }

    curr = 0;
    forstl(r,finalc){
        est += r.se.se*r.se.se.se;
        if(curr + r.se.fi*r.fi >= ){
            hours += (est-curr+r.fi-1)/r.fi;
            break;
        }
        else{
            curr += r.se.fi*r.fi;
            hours += r.se.fi;
        }
    }


    cout<<hours<<ln;

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:142:22: error: no matching function for call to 'std::vector<std::pair<long long int, std::pair<long long int, std::pair<long long int, long long int> > > >::push_back(std::pair<long long int, std::pair<long long int, int> >)'
  142 |             finalc.pb(mp(l[i],mp(b[i],0)));
      |             ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<long long int, std::pair<long long int, std::pair<long long int, long long int> > >; _Alloc = std::allocator<std::pair<long long int, std::pair<long long int, std::pair<long long int, long long int> > > >; value_type = std::pair<long long int, std::pair<long long int, std::pair<long long int, long long int> > >]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from 'std::pair<long long int, std::pair<long long int, int> >' to 'const std::vector<std::pair<long long int, std::pair<long long int, std::pair<long long int, long long int> > > >::value_type&' {aka 'const std::pair<long long int, std::pair<long long int, std::pair<long long int, long long int> > >&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<long long int, std::pair<long long int, std::pair<long long int, long long int> > >; _Alloc = std::allocator<std::pair<long long int, std::pair<long long int, std::pair<long long int, long long int> > > >; value_type = std::pair<long long int, std::pair<long long int, std::pair<long long int, long long int> > >]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from 'std::pair<long long int, std::pair<long long int, int> >' to 'std::vector<std::pair<long long int, std::pair<long long int, std::pair<long long int, long long int> > > >::value_type&&' {aka 'std::pair<long long int, std::pair<long long int, std::pair<long long int, long long int> > >&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
a.cc:161:23: error: no match for 'operator*' (operand types are 'std::pair<long long int, long long int>' and 'long long int')
  161 |         est += r.se.se*r.se.se.se;
      |                       ^
In file included from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127:
/usr/include/c++/14/complex:400:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const complex<_Tp>&, const complex<_Tp>&)'
  400 |     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:400:5: note:   template argument deduction/substitution failed:
a.cc:10:12: note:   'std::pair<long long int, long long int>' is not derived from 'const std::complex<_Tp>'
   10 | #define se second
      |            ^~~~~~
a.cc:161:32: note: in expansion of macro 'se'
  161 |         est += r.se.se*r.se.se.se;
      |                                ^~
/usr/include/c++/14/complex:409:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const complex<_Tp>&, const _Tp&)'
  409 |     operator*(const complex<_Tp>& __x, const _Tp& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:409:5: note:   template argument deduction/substitution failed:
a.cc:10:12: note:   'std::pair<long long int, long long int>' is not derived from 'const std::complex<_Tp>'
   10 | #define se second
      |            ^~~~~~
a.cc:161:32: note: in expansion of macro 'se'
  161 |         est += r.se.se*r.se.se.se;
      |                                ^~
/usr/include/c++/14/complex:418:5: note: candidate: 'template<class _Tp> std::complex<_Tp> std::operator*(const _Tp&, const complex<_Tp>&)'
  418 |     operator*(const _Tp& __x, const complex<_Tp>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/complex:418:5: note:   template argument deduction/substitution failed:
a.cc:10:12: note:   mismatched types 'const std::complex<_Tp>' and 'long long int'
   10 | #define se second
      |            ^~~~~~
a.cc:161:32: note: in expansion of macro 'se'
  161 |         est += r.se.se*r.se.se.se;
      |                                ^~
In file included from /usr/include/c++/14/valarray:605,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166:
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom1, class _Dom2> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Expr, std::_Expr, _Dom1, _Dom2>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const _Expr<_Dom1, typename _Dom1::value_type>&, const _Expr<_Dom2, typename _Dom2::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:10:12: note:   'std::pair<long long int, long long int>' is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
   10 | #define se second
      |            ^~~~~~
a.cc:161:32: note: in expansion of macro 'se'
  161 |         est += r.se.se*r.se.se.se;
      |                                ^~
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Expr, std::_Constant, _Dom, typename _Dom::value_type>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const _Expr<_Dom1, typename _Dom1::value_type>&, const typename _Dom::value_type&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:10:12: note:   'std::pair<long long int, long long int>' is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
   10 | #define se second
      |            ^~~~~~
a.cc:161:32: note: in expansion of macro 'se'
  161 |         est += r.se.se*r.se.se.se;
      |                                ^~
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Constant, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const typename _Dom::value_type&, const _Expr<_Dom1, typename _Dom1::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:10:12: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'long long int'
   10 | #define se second
      |            ^~~~~~
a.cc:161:32: note: in expansion of macro 'se'
  161 |         est += r.se.se*r.se.se.se;
      |                                ^~
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_Expr, std::_ValArray, _Dom, typename _Dom::value_type>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const _Expr<_Dom1, typename _Dom1::value_type>&, const valarray<typename _Dom::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:10:12: note:   'std::pair<long long int, long long int>' is not derived from 'const std::_Expr<_Dom1, typename _Dom1::value_type>'
   10 | #define se second
      |            ^~~~~~
a.cc:161:32: note: in expansion of macro 'se'
  161 |         est += r.se.se*r.se.se.se;
      |                                ^~
/usr/include/c++/14/bits/valarray_after.h:407:5: note: candidate: 'template<class _Dom> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_ValArray, std::_Expr, typename _Dom::value_type, _Dom>, typename std::__fun<std::__multiplies, typename _Dom1::value_type>::result_type> std::operator*(const valarray<typename _Dom::value_type>&, const _Expr<_Dom1, typename _Dom1::value_type>&)'
  407 |     _DEFINE_EXPR_BINARY_OPERATOR(*, struct std::__multiplies)
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/valarray_after.h:407:5: note:   template argument deduction/substitution failed:
a.cc:10:12: note:   mismatched types 'const std::_Expr<_Dom1, typename _Dom1::value_type>' and 'long long int'
   10 | #define se second
      |            ^~~~~~
a.cc:161:32: note: in expansion of macro 'se'
  161 |         est += r.se.se*r.se.se.se;
      |                                ^~
/usr/include/c++/14/valarray:1198:1: note: candidate: 'template<class _Tp> std::_Expr<std::__detail::_BinClos<std::__multiplies, std::_ValArray, std::_ValArray, _Tp, _Tp>, typename std::__fun<std::__multiplies, _Tp>::result_type> std::operator*(const valarray<_Tp>&, const valarray<_Tp>&)'
 1198 | _DEFINE_BINARY_OPERATOR(*, __multiplies)
      | ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/valarray:1198:1: note:   template argument deduction/substitution failed:
a.cc:10:12: note:   'std::pair<long long int, long long int>' is not derived from 'const std::valarray<_Tp>'
   10 | #define se second
      |            ^~~~~~
a.cc:161:32: note: in expansion of macro 'se'
  161 |         est += r.se.se*r.se.se.se;
      |                                ^~
/usr/include/c++/14/valarray:11