Language: C++

#include <bits/stdc++.h>
using namespace std;
#define reep(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) reep((i),0,(n))
typedef pair<int,int> pii;
typedef pair<double,pii> P;
typedef long long ll;
#define mkp make_pair
#define F first
#define S second
#define int long long
// struct P {
// 	double first;
// 	pii second;
// 	P(double f, pii s) {
// 		first = f;
// 		second = s;
// 	}

// 	bool operator<(const P & obj) const {
// 		return this->S.S * obj.S.F > this->S.F * obj.S.S;
// 	}
// };

signed main(){
	int n;
	cin>>n;
	long long ans=0;
	n++;
	vector<int> h(n),a(n),d(n),s(n);
	rep(i,n) cin>>h[i]>>a[i]>>d[i]>>s[i];
	reep(i,1,n){
		a[i]=max(a[i]-d[0],0);
	}
	vector<int> atack(n);
	reep(i,1,n){
		atack[i]=max(a[0]-d[i],0);
	}
	vector<P> v;
	reep(i,1,n){
		if(a[i]==0){
			continue;
		}
		if(s[i]>s[0]) ans+=a[i];
		if(atack[i]==0){
			puts("-1");
			return 0;
		}
		int tmp=h[i]/atack[i];
		// cout<<h[i]<<" "<<atack[i]<<endl;
		if(h[i]%atack[i]) tmp++;
		// cout<<tmp<<endl;
		// v.push_back(mkp( (double)a[i]/tmp, pii(a[i],tmp)) );
		v.push_back(mkp( (double)a[i]/tmp, pii(a[i],tmp)) );
	}
	sort(v.begin(),v.end());
	reverse(v.begin(),v.end());
	ll turn=0;
	// rep(i,v.size()){
		// cout<<v[i].F<<" "<<v[i].S.F<<" "<<v[i].S.S<<endl;
	// }
	// ll ans1=LLONG_MAX;
	// do{
	// 	ll tmp=0;
	// 	turn=0;
	// 	rep(i,v.size()){
	// 		turn+=v[i].S.S;
	// 		tmp+=(turn-1)*v[i].S.F;
	// 	}
	// 	ans1=min(ans1,tmp+ans);
	// }while(next_permutation(v.begin(),v.end()));
	rep(i,v.size()){
		turn+=v[i].S.S;
		ans+=(turn-1)*v[i].S.F;
	}
	// if(ans1!=ans){
		// cout<<"NO"<<endl;
	// }
	if(ans>=h[0]) puts("-1");
	else cout<<ans<<endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:33:25: error: no matching function for call to 'max(__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type, int)'
   33 |                 a[i]=max(a[i]-d[0],0);
      |                      ~~~^~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:33:25: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   33 |                 a[i]=max(a[i]-d[0],0);
      |                      ~~~^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:33:25: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   33 |                 a[i]=max(a[i]-d[0],0);
      |                      ~~~^~~~~~~~~~~~~
a.cc:37:29: error: no matching function for call to 'max(__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type, int)'
   37 |                 atack[i]=max(a[0]-d[i],0);
      |                          ~~~^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:37:29: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   37 |                 atack[i]=max(a[0]-d[i],0);
      |                          ~~~^~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:37:29: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   37 |                 atack[i]=max(a[0]-d[i],0);
      |                          ~~~^~~~~~~~~~~~~