Problem:
Problem E: Rabbit Plays Games!
うさぎがとあるロールプレイングゲームで遊んでいる. 城に入る直前で, 敵に待ち伏せされていた！
うさぎが操作する主人公1 人と,
n
体の敵との戦闘となった. 各キャラクターには4 つの能力値, 体力
h
i
, 攻撃力
a
i
, 防御力
d
i
, 敏捷
s
i
が定められている.
i
= 0 は主人公の情報, 1 ≤
i
≤
n
は各敵の情報を表す.
戦闘はターン制である. 各ターン, 生き残っているキャラクターが, 敏捷の値が高い順に攻撃を行う. 敵は必ず主人公を攻撃する. 主人公は敵1 体を攻撃するが, どの敵を攻撃するかは毎ターンごとに主人公が選べる.攻撃力
a
のキャラクターが防御力
d
のキャラクターに攻撃するとき, max{
a
−
d
, 0} のダメージが与えられる. 受けたダメージの合計が体力の値以上になったキャラクターは直ちに戦闘不能になってしまう. 主人公が戦闘不能になったとき, あるいは敵がすべて戦闘不能になったとき, 戦闘は終了する.
Input
1 ≤
n
≤ 40 000
1 ≤
h
i
,
a
i
,
d
i
,
s
i
≤ 1 000 000 000 (整数)
s
i
はすべて異なる.
Output
主人公が必ず戦闘不能になってしまうとき, −1 を出力せよ. そうでないとき, 主人公が受けるダメージの合計の最小値を一行に出力せよ.
Sample Input 1
2
10 3 1 2
2 4 1 3
2 2 1 1
Sample Output 1
4
Sample Input 2
1
1 1 1 1
10000 10000 10000 10000
Sample Output 2
-1

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
	ll n; cin >> n;
	vector<ll> h(n + 1), a(n + 1), d(n + 1), s(n + 1);
	rep(i, n + 1) {
		cin >> h[i] >> a[i] >> d[i] >> s[i];
	}
	vector<ll> numlife;
	vector<ll> A;
	ll sum = 0;
	for(int i = 1; i <= n; i ++) {
		ll x = max(0LL, a[i] - d[0]);
		if(x == 0LL) {
			continue;
		}
		
		if(s[i] > s[0]) sum += x;
		ll at = max(0LL, a[0] - d[i]);
		if(at == 0LL) {
			cout << -1 << endl;
			return 0;
		}
		A.pb(x);
		numlife.pb((h[i] + at - 1) / at);
		
	}
	n = numlife.size();
	// v, numlife[i], a[i]
	
	ll v = h[0];
	vector<int> ord(n);
	ll suma = accumulate(all(A), 0LL);
	iota(all(ord), 0);
	sort(all(ord), [&](int i, int j) {
		return numlife[i] * A[j] < numlife[j] * A[i]; 
	});
	foa(i, ord) {
		sum += numlife[i] * suma - A[i];
		suma -= A[i];
		if(sum >= v) {
			cout << -1 << endl;
			return 0;
		}
	}
	if(sum >= v) {
		cout << -1 << endl;
	} else {
		cout << sum << endl;
	}


    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5320
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Fraction {
    long long a, b;
};

struct Rabbit {
    long long hp, attack, defend, s, idx;
};

bool operator<(Rabbit &p1, Rabbit &p2) {
    if (p1.s > p2.s) return true;
    return false;
}

bool operator<(const Fraction &p1, const Fraction &p2) {
    if (p1.a * p2.b < p2.a * p1.b) return true;
    return false;
}

int main() {
    // Step 1. Input
    long long N; cin >> N;
    vector<Rabbit> R(N + 1);
    for (int i = 0; i <= N; i++) {
        cin >> R[i].hp >> R[i].attack >> R[i].defend >> R[i].s;
        R[i].idx = i;
    }

    // Step 2. Sorting
    long long damage = 0, Index = 0;
    sort(R.begin(), R.end());
    for (int i = 0; i <= N; i++) {
        if (R[i].idx == 0) Index = i;
    }
    for (int i = 0; i < Index; i++) {
        damage += max(0LL, R[i].attack - R[Index].defend);
    }

    // Step 3. Shuffle
    vector<Rabbit> List;
    Rabbit Mine = R[Index];
    for (int i = 1; i <= N; i++) {
        if (R[(Index + i) % (N + 1)].attack <= Mine.defend) continue;
        List.push_back(R[(Index + i) % (N + 1)]);
    }
    
    // Step 4. Decide Order
    vector<pair<Fraction, int>> Frac;
    for (int i = 0; i < List.size(); i++) {
        long long attack = max(0LL, Mine.attack - List[i].defend);
        if (attack == 0LL) {
            cout << "-1" << endl;
            return 0;
        }
        long long death = (List[i].hp + attack - 1) / attack;
        Frac.push_back(make_pair(Fraction{List[i].attack - Mine.defend, death}, i));
    }
    sort(Frac.begin(), Frac.end());
    reverse(Frac.begin(), Frac.end());

    // Step 5. Calculate Damage
    long long total_turn = -1;
    for (int i = 0; i < Frac.size(); i++) {
        int idx = Frac[i].second;
        total_turn += Frac[i].first.b;
        damage += total_turn * Frac[i].first.a;
        // cerr << idx << " " << total_turn << " " << damage << endl;
        if (damage >= Mine.hp) {
            cout << "-1" << endl;
            return 0;
        }
    }

    // Step 6. Output
    cout << (damage >= Mine.hp ? -1 : damage) << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 8276
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct Enemy {
    long long s, w, r;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    long long h0, a0, d0, s0;
    cin >> h0 >> a0 >> d0 >> s0;

    vector<Enemy> damaging;
    bool impossible = false;

    for (int i = 0; i < n; i++) {
        long long h_i, a_i, d_i, s_i;
        cin >> h_i >> a_i >> d_i >> s_i;

        long long x_i = max(a0 - d_i, 0LL);
        long long w_i = max(a_i - d0, 0LL);

        if (x_i == 0) impossible = true;

        if (x_i > 0 && w_i > 0) {
            long long r_i = (h_i + x_i - 1) / x_i;
            damaging.push_back({s_i, w_i, r_i});
        }
    }

    if (impossible) {
        cout << -1 << "\n";
        return 0;
    }

    if (damaging.empty()) {
        cout << 0 << "\n";
        return 0;
    }

    sort(damaging.begin(), damaging.end(), [](const Enemy &e1, const Enemy &e2) {
        return (__int128)e1.w * e2.r > (__int128)e2.w * e1.r;
    });

    long long totalDamage = 0, partialSum = 0;

    for (const auto &e : damaging) {
        partialSum += e.r;

        __int128 dmg = (__int128)e.w * (e.s > s0 ? partialSum : partialSum - 1);
        if (dmg >= h0) {
            cout << -1 << "\n";
            return 0;
        }

        totalDamage += (long long)dmg;
        if (totalDamage >= h0) {
            cout << -1 << "\n";
            return 0;
        }
    }

    cout << totalDamage << "\n";
    return 0;
}
accuracy: 0.6875
time_ms: 10
memory_kb: 4664
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,s,t) for(int i=s;i<t;i++)


int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N;
    cin>>N;
    vector<array<ll,4>> D(N+1);
    vector<array<ll,4>> DD;
    
    rep(i,N+1){
        rep(j,4)cin>>D[i][j];
        if(i!=0&&D[0][1]-D[i][2]<=0){
            if(D[i][1]-D[0][2]<=0)continue;
            else{
                cout<<-1<<endl;
                return 0;
            }
        }
        
        DD.push_back(D[i]);
    }
    swap(DD,D);
    N=D.size()-1;

    sort(D.begin()+1,D.end(),[&](auto const& lhs, auto const& rhs) {
        ll L1=max(lhs[1]-D[0][2],0ll);
        ll L2=max(D[0][1]-lhs[2],0ll);
        
        ll R1=max(rhs[1]-D[0][2],0ll);
        ll R2=max(D[0][1]-rhs[2],0ll);
        L2=(lhs[0]+L2-1)/L2;
        R2=(rhs[0]+R2-1)/R2;
        
        return L1*R2>L2*R1;
    });
    // rep(i,N+1){
    //     if(i==0)continue;
    //     ll L1=max(D[i][1]-D[0][2],0ll);
    //     ll L2=max(D[0][1]-D[i][2],0ll);
    //     L2=(D[i][0]+L2-1)/L2;
    //     cout<<double(L1)/double(L2)<<endl;
    // }
    ll rt=0;
    ll DM=0;
    rep(i,N+1){
        if(i==0)continue;
        ll L1=max(D[i][1]-D[0][2],0ll);
        ll L2=max(D[0][1]-D[i][2],0ll);
        L2=(D[i][0]+L2-1)/L2;
        rt+=L2;
        DM+=(rt-(D[i][3]<D[0][3]))*L1;

        if(D[0][0]<=DM){
            cout<<-1<<endl;
            return 0;
        }
    }
    cout<<DM<<endl;

}
accuracy: 1
time_ms: 20
memory_kb: 6240
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,s,t) for(int i=s;i<t;i++)


int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N;
    cin>>N;
    vector<array<ll,4>> D(N+1);

    rep(i,N+1){
        rep(j,4)cin>>D[i][j];
        if(i!=0&&D[0][1]-D[i][2]<=0){
            cout<<-1<<endl;
            return 0;
        }
    }
    

    sort(D.begin()+1,D.end(),[&](auto const& lhs, auto const& rhs) {
        ll L1=max(lhs[1]-D[0][2],0ll);
        ll L2=max(D[0][1]-lhs[2],0ll);
        
        ll R1=max(rhs[1]-D[0][2],0ll);
        ll R2=max(D[0][1]-rhs[2],0ll);
        L2=(lhs[0]+L2-1)/L2;
        R2=(rhs[0]+R2-1)/R2;
        
        return L1*R2>L2*R1;
    });
    // rep(i,N+1){
    //     if(i==0)continue;
    //     ll L1=max(D[i][1]-D[0][2],0ll);
    //     ll L2=max(D[0][1]-D[i][2],0ll);
    //     L2=(D[i][0]+L2-1)/L2;
    //     cout<<double(L1)/double(L2)<<endl;
    // }
    ll rt=0;
    ll DM=0;
    rep(i,N+1){
        if(i==0)continue;
        ll L1=max(D[i][1]-D[0][2],0ll);
        ll L2=max(D[0][1]-D[i][2],0ll);
        L2=(D[i][0]+L2-1)/L2;
        rt+=L2;
        DM+=(rt-(D[i][3]<D[0][3]))*L1;

        if(D[0][0]<=DM){
            cout<<-1<<endl;
            return 0;
        }
    }
    cout<<DM<<endl;

}
accuracy: 0.6875
time_ms: 20
memory_kb: 4232
final_rank: 16

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    int N;
    cin >> N;
    ll PH, PA, PD, PS;
    ll ANS = 0;
    cin >> PH >> PA >> PD >> PS;
    vector<ll> X, Y;
    rep(i,0,N) {
        ll H, A, D, S;
        cin >> H >> A >> D >> S;
        if (A-PD <= 0) continue;
        if (PS > S) ANS -= max(0LL, A-PD);
        X.push_back(max(0LL, A-PD));
        if (PA-D < 0) {
            cout << -1 << endl;
            return 0;
        }
        Y.push_back(ceil(H, PA-D));
    }
    N = X.size();
    vector<int> ord(N);
    iota(ALL(ord),0);
    sort(ALL(ord),[&](int i, int j){return X[i]*Y[j] > X[j]*Y[i];});
    ll Cur = 0;
    for (int i : ord) {
        Cur += Y[i];
        if (Cur > 2000000000 && X[i] >= 1) {
            cout << -1 << endl;
            return 0;
        }
        ANS += Cur * X[i];
        if (ANS >= PH) {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << ANS << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3648
final_rank: 7

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    int N;
    cin >> N;
    ll PH, PA, PD, PS;
    ll ANS = 0;
    cin >> PH >> PA >> PD >> PS;
    vector<ll> X(N), Y(N);
    rep(i,0,N) {
        ll H, A, D, S;
        cin >> H >> A >> D >> S;
        if (PS > S) ANS -= max(0LL, A-PD);
        X[i] = max(0LL, A-PD);
        if (PA-D < 0) {
            cout << -1 << endl;
            return 0;
        }
        Y[i] = ceil(H, PA-D);
    }
    vector<int> ord(N);
    iota(ALL(ord),0);
    sort(ALL(ord),[&](int i, int j){return X[i]*Y[j] > X[j]*Y[i];});
    ll Cur = 0;
    for (int i : ord) {
        Cur += Y[i];
        if (Cur > 1000000000 && X[i] >= 1) {
            cout << -1 << endl;
            return 0;
        }
        ANS += Cur * X[i];
        if (ANS >= PH) {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << ANS << endl;
}
accuracy: 0.6875
time_ms: 30
memory_kb: 3736
final_rank: 17

code_snippet: #define LOCAL
#include <bits/stdc++.h>
using namespace std;
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(), (x).end()

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T, typename U> ostream& operator<<(ostream& os, const unordered_map<T, U>& m) {
    os << '{';
    for (auto itr = m.begin(); itr != m.end();) {
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr != m.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const unordered_set<T>& s) {
    os << '{';
    for (auto itr = s.begin(); itr != s.end();) {
        os << *itr;
        if (++itr != s.end()) os << ',';
    }
    os << '}';
    return os;
}
template <typename T> ostream& operator<<(ostream& os, const deque<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}
template <typename T, size_t N> ostream& operator<<(ostream& os, const array<T, N>& v) {
    for (size_t i = 0; i < N; i++) {
        os << v[i] << (i + 1 == N ? "" : " ");
    }
    return os;
}

template <int i, typename T> void print_tuple(ostream&, const T&) {}
template <int i, typename T, typename H, class... Args> void print_tuple(ostream& os, const T& t) {
    if (i) os << ',';
    os << get<i>(t);
    print_tuple<i + 1, T, Args...>(os, t);
}
template <typename... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) {
    os << '{';
    print_tuple<0, tuple<Args...>, Args...>(os, t);
    return os << '}';
}

void debug_out() { cerr << '\n'; }
template <class Head, class... Tail> void debug_out(Head&& head, Tail&&... tail) {
    cerr << head;
    if (sizeof...(Tail) > 0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...)                                                                   \
    cerr << " ";                                                                     \
    cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n'; \
    cerr << " ";                                                                     \
    debug_out(__VA_ARGS__)
#else
#define debug(...) void(0)
#endif

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T> void mkuni(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
template <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }
#pragma endregion

const int INF = 1e9;
const long long IINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const char dir[4] = {'D', 'R', 'U', 'L'};
const long long MOD = 1000000007;
// const long long MOD = 998244353;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> h(n + 1), a(n + 1), d(n + 1), s(n + 1);
    for (int i = 0; i <= n; i++) cin >> h[i] >> a[i] >> d[i] >> s[i];

    vector<pair<int, int>> enemy;
    ll H = 0;
    for (int i = 1; i <= n; i++) {
        a[i] -= d[0];
        if (a[i] <= 0) continue;
        if (a[0] <= d[i]) {
            cout << -1 << '\n';
            return 0;
        }
        if (s[i] > s[0]) H += a[i];
        enemy.emplace_back((h[i] + (a[0] - d[i]) - 1) / (a[0] - d[i]), a[i]);
    }
    if (H >= h[0]) {
        cout << -1 << '\n';
        return 0;
    }

    sort(enemy.begin(), enemy.end(), [](const pair<int, int>& p, const pair<int, int>& q) {
        return 1LL * p.first * q.second < 1LL * p.second * q.first;
    });
    ll cur = -1;
    for (auto& p : enemy) {
        cur += p.first;
        H += cur * p.second;
        if (H >= h[0]) {
            cout << -1 << '\n';
            return 0;
        }
    }

    cout << H << '\n';
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4364
final_rank: 1

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=1050000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}

ll f(ll a,ll b){
	return (a+b-1)/b;
}

void solve();
// oddloop
int main() {
	solve();
}

void solve(){
	int N;
	cin>>N;
	ll H,A,D,S;
	cin>>H>>A>>D>>S;
	ll F=H;
	vector<ll> h(N),a(N),d(N),s(N);
	rep(i,N){
		cin>>h[i]>>a[i]>>d[i]>>s[i];
	}
	rep(i,N){
		if(A-d[i]<=0){
			if(a[i]>D){
				cout<<"-1\n";
				return ;
			}
			h[i]=INF,a[i]=0,d[i]=0;
		}
	}
	vector<int> order(N);
	rep(i,N) order[i]=i;
	sort(all(order),[&](int l,int r){
		return 	f(h[l],A-d[l])*max(0ll,a[r]-D)<f(h[r],A-d[r])*max(0ll,a[l]-D);
	});
	ll T=0;
	rep(i,N){
		if(H<=0) continue;
		T+=f(h[order[i]],A-d[order[i]]);
		ll tmp=T;
		if(S>s[order[i]]) tmp--;
		H-=max(0ll,a[order[i]]-D)*tmp;
	}
	if(H<=0) cout<<"-1\n";
	else cout<<F-H<<"\n";
}
accuracy: 1
time_ms: 40
memory_kb: 4456
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=1050000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}

ll f(ll a,ll b){
	return (a+b-1)/b;
}

void solve();
// oddloop
int main() {
	solve();
}

void solve(){
	int N;
	cin>>N;
	ll H,A,D,S;
	cin>>H>>A>>D>>S;
	ll F=H;
	vector<ll> h(N),a(N),d(N),s(N);
	rep(i,N){
		cin>>h[i]>>a[i]>>d[i]>>s[i];
	}
	rep(i,N){
		if(A-d[i]<=0){
			if(a[i]>D){
				cout<<"-1\n";
				return ;
			}
			h[i]=0,a[i]=0,d[i]=0;
		}
	}
	vector<int> order(N);
	rep(i,N) order[i]=i;
	sort(all(order),[&](int l,int r){
		return 	f(h[l],A-d[l])*max(0ll,a[r]-D)<f(h[r],A-d[r])*max(0ll,a[l]-D);
	});
	ll T=0;
	rep(i,N){
		if(H<=0) continue;
		T+=f(h[order[i]],A-d[order[i]]);
		ll tmp=T;
		if(S>s[order[i]]) tmp--;
		H-=max(0ll,a[order[i]]-D)*tmp;
	}
	if(H<=0) cout<<"-1\n";
	else cout<<F-H<<"\n";
}
accuracy: 0.9583333333333334
time_ms: 40
memory_kb: 4400
final_rank: 13

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < b ? a = b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return b < a ? a = b, true : false;
}
constexpr int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
const double PI = acos(-1.0);
#line 6 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
using ll = int64_t;
using ld = long double;
#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FOR(i, 1, n+1)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n+1, 1)
#define all(s) (s).begin(), (s).end()
template<class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os<<'('<<p.first<< ','<<p.second<<')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?"":" ")<<*it; } return os;
}
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cout<<head<<'\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);
}
template<typename T, typename... Args>
auto make_vector(T x, int arg, Args ...args) {
    if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));
}
void sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }
void setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }
void Yes(bool is_correct) { std::cout<<(is_correct?"Yes":"No")<<std::endl; }
void YES(bool is_correct) { std::cout<<(is_correct?"YES":"NO")<<std::endl; }
#line 3 "a.cpp"

struct S {
    ll h, a, s, d;
};

int main(void) {
    sonic();
    int n;
    cin >> n;
    ll h, a, s, d;
    cin >> h >> a >> d >> s;
    vector<S> v(n);
    rep(i, n) cin >> v[i].h >> v[i].a >> v[i].d >> v[i].s;
    ll ans = 0;
    rep(i, n) {
        if (v[i].d >= a && d < v[i].a) {
            co(-1);
            return 0;
        }
        if (v[i].s > s)
            ans += max(v[i].a - d, 0L);
    }

    sort(all(v), [&](auto x, auto y) {
        return max(x.a - d, 0L) * ((y.h - 1) / max(a - y.d, 1L) + 1) >
               max(y.a - d, 0L) * ((x.h - 1) / max(a - x.d, 1L) + 1);
    });

    ll sum = 0;
    rep(i, n) sum += max(v[i].a - d, 0L);

    rep(i, n) {
        ans += sum * ((v[i].h - 1) / max(a - v[i].d, 1L) + 1) - max(v[i].a - d, 0L);
        sum -= max(v[i].a - d, 0L);
        if (ans > h)
            break;
    }

    if (ans >= h)
        ans = -1;
    co(ans);

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4260
final_rank: 6

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < b ? a = b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return b < a ? a = b, true : false;
}
constexpr int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
const double PI = acos(-1.0);
#line 6 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
using ll = int64_t;
using ld = long double;
#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FOR(i, 1, n+1)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n+1, 1)
#define all(s) (s).begin(), (s).end()
template<class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os<<'('<<p.first<< ','<<p.second<<')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?"":" ")<<*it; } return os;
}
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cout<<head<<'\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);
}
template<typename T, typename... Args>
auto make_vector(T x, int arg, Args ...args) {
    if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));
}
void sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }
void setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }
void Yes(bool is_correct) { std::cout<<(is_correct?"Yes":"No")<<std::endl; }
void YES(bool is_correct) { std::cout<<(is_correct?"YES":"NO")<<std::endl; }
#line 3 "a.cpp"

struct S {
    ll h, a, s, d;
};

int main(void) {
    sonic();
    int n;
    cin >> n;
    ll h, a, s, d;
    cin >> h >> a >> d >> s;
    vector<S> v(n);
    rep(i, n) cin >> v[i].h >> v[i].a >> v[i].d >> v[i].s;
    ll ans = 0;
    rep(i, n) {
        if (v[i].d >= a) {
            co(-1);
            return 0;
        }
        if (v[i].s > s)
            ans += max(v[i].a - d, 0L);
    }

    sort(all(v), [&](auto x, auto y) {
        return max((x.a - d), 0L) * ((y.h - 1) / (a - y.d) + 1) >
               max((y.a - d), 0L) * ((x.h - 1) / (a - x.d) + 1);
    });

    ll sum = 0;
    rep(i, n) sum += max(v[i].a - d, 0L);

    rep(i, n) {
        ans += sum * ((v[i].h - 1) / (a - v[i].d) + 1) - max(v[i].a - d, 0L);
        sum -= max(v[i].a - d, 0L);
        if (ans > h)
            break;
    }

    if (ans >= h)
        ans = -1;
    co(ans);

    return 0;
}
accuracy: 0.6875
time_ms: 10
memory_kb: 4264
final_rank: 14

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#line 2 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < b ? a = b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return b < a ? a = b, true : false;
}
constexpr int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
const double PI = acos(-1.0);
#line 6 "/home/kuhaku/atcoder/github/atcoder-lib/lib/template/atcoder.hpp"
using ll = int64_t;
using ld = long double;
#define FOR(i, m, n) for(int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for(int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for(int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FOR(i, 1, n+1)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n+1, 1)
#define all(s) (s).begin(), (s).end()
template<class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) { is >> p.first >> p.second; return is; }
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &i : v) is>>i; return is; }
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os<<'('<<p.first<< ','<<p.second<<')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it=v.begin(); it!=v.end(); ++it) { os<<(it==v.begin()?"":" ")<<*it; } return os;
}
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cout<<head<<'\n'; else std::cout<<head<<' ',co(forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
    if constexpr(sizeof...(tail)==0) std::cerr<<head<<'\n'; else std::cerr<<head<<' ',ce(forward<Tail>(tail)...);
}
template<typename T, typename... Args>
auto make_vector(T x, int arg, Args ...args) {
    if constexpr(sizeof...(args)==0) return std::vector<T>(arg,x); else return std::vector(arg,make_vector<T>(x,args...));
}
void sonic() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); }
void setp(const int n) { std::cout<<std::fixed<<std::setprecision(n); }
void Yes(bool is_correct) { std::cout<<(is_correct?"Yes":"No")<<std::endl; }
void YES(bool is_correct) { std::cout<<(is_correct?"YES":"NO")<<std::endl; }
#line 3 "a.cpp"

struct S {
    ll h, a, s, d;
};

int main(void) {
    sonic();
    int n;
    cin >> n;
    ll h, a, s, d;
    cin >> h >> a >> d >> s;
    vector<S> v(n);
    rep(i, n) cin >> v[i].h >> v[i].a >> v[i].d >> v[i].s;
    ll ans = 0;
    rep(i, n) {
        if (v[i].d >= a) {
            co(-1);
            return 0;
        }
        if (v[i].s > s)
            ans += v[i].a - d;
    }

    sort(all(v), [&](auto x, auto y) {
        return (x.a - d) * ((y.h - 1) / (a - y.d) + 1) > (y.a - d) * ((x.h - 1) / (a - x.d) + 1);
    });

    ll sum = 0;
    rep(i, n) sum += v[i].a - d;

    rep(i, n) {
        ans += sum * ((v[i].h - 1) / (a - v[i].d) + 1) - v[i].a + d;
        sum -= v[i].a - d;
        if (ans > h)
            break;
    }

    if (ans >= h)
        ans = -1;
    co(ans);

    return 0;
}
accuracy: 0.6666666666666666
time_ms: 20
memory_kb: 4260
final_rank: 20

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    n++;
    vector<ll> h(n),a(n),d(n),s(n);
    for(int i=0;i<n;i++){
        cin >> h[i] >> a[i] >> d[i] >> s[i];
    }
    ll sum = 0;
    // プレイヤーを常に先手として考えられる(先のダメージを前計算)
    // {かかる時間,食らうダメージ}
    vector<pair<ll,ll>> v;
    for(int i=1;i<n;i++){
        if(a[i]-d[0] <= 0)continue; // 無視してOK
        if(a[0]-d[i] <= 0 and h[i] > 0){ // ダメージを与えられない
            cout << -1 << endl;
            return 0;
        }
        if(s[i] > s[0]){
            sum += (a[i]-d[0]);
        }
        v.push_back({(h[i]+(a[0]-d[i])-1)/(a[0]-d[i]), a[i]-d[0]});
    }
    if(sum >= h[0]){
        cout << -1 << endl;
        return 0;
    }
    sort(v.begin(), v.end(), [&](auto i,auto j){
        return i.first * j.second < i.second * j.first;
    });
    ll uo = 0;
    for(auto p:v){
        uo += p.second;
    }
    for(auto p:v){
        sum += (p.first-1) * uo;
        uo -= p.second;
        sum += uo;
        if(sum >= h[0]){
            cout << -1 << endl;
            return 0;
        }
    }
    cout << sum << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 5320
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)

struct Character {
    ll h, a, d, s, i;
    Character(int h, int a, int d, int s, int i) : h(h), a(a), d(d), s(s), i(i) {}
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n;
    cin >> n;
    ll h, a, d, s;
    cin >> h >> a >> d >> s;
    ll H = h;
    Character me(h, a, d, s, -1);
    ll fast = 0, slow = 0;
    vector<Character> enemies;
    bool ok = true;
    rep(i,0,n) {
        cin >> h >> a >> d >> s;
        if (d >= me.a) {
            if (me.d < a) ok = false;
            continue;
        }
        enemies.emplace_back(h, a, d, s, i);
        if (s > me.s) {
            fast += max(0LL, a - me.d);
        } else {
            slow += max(0LL, a - me.d);
        }
    }
    if (!ok) {
        cout << -1 << endl;
        return 0;
    }
    sort(enemies.begin(), enemies.end(), [&](auto& p, auto& q) {
        ll dp = me.a - p.d;
        ll ap = max(p.a - me.d, 0LL);
        ll tp = (p.h+dp-1)/dp;
        ll dq = me.a - q.d;
        ll aq = max(q.a - me.d, 0LL);
        ll tq = (q.h+dq-1)/dq;
        return tp*aq < tq*ap;
    });

    for (auto& e : enemies) {
        ll d = me.a - e.d;
        ll a = max(e.a - me.d, 0LL);
        ll t = (e.h+d-1)/d;
        me.h -= t*(fast+slow);
        if (me.s > e.s) {
            me.h += a;
            slow -= a;
        } else {
            fast -= a;
        }
        if (me.h <= 0) break;
    }
    cout << (me.h <= 0 ? -1 : H - me.h) << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 7072
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)

struct Character {
    ll h, a, d, s, i;
    Character(int h, int a, int d, int s, int i) : h(h), a(a), d(d), s(s), i(i) {}
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n;
    cin >> n;
    ll h, a, d, s;
    cin >> h >> a >> d >> s;
    ll H = h;
    Character me(h, a, d, s, -1);
    ll fast = 0, slow = 0;
    vector<Character> enemies;
    bool ok = true;
    rep(i,0,n) {
        cin >> h >> a >> d >> s;
        enemies.emplace_back(h, a, d, s, i);
        if (s > me.s) {
            fast += max(0LL, a - me.d);
        } else {
            slow += max(0LL, a - me.d);
        }
        if (d >= me.a) ok = false;
    }
    if (!ok) {
        cout << -1 << endl;
        return 0;
    }
    sort(enemies.begin(), enemies.end(), [&](auto& p, auto& q) {
        ll dp = me.a - p.d;
        ll ap = max(p.a - me.d, 0LL);
        ll tp = (p.h+dp-1)/dp;
        ll dq = me.a - q.d;
        ll aq = max(q.a - me.d, 0LL);
        ll tq = (q.h+dq-1)/dq;
        return tp*aq < tq*ap;
    });

    for (auto& e : enemies) {
        ll d = me.a - e.d;
        ll a = max(e.a - me.d, 0LL);
        ll t = (e.h+d-1)/d;
        me.h -= t*(fast+slow);
        if (me.s > e.s) {
            me.h += a;
            slow -= a;
        } else {
            fast -= a;
        }
        if (me.h <= 0) break;
    }
    cout << (me.h <= 0 ? -1 : H - me.h) << endl;
}
accuracy: 0.6875
time_ms: 20
memory_kb: 6840
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)

struct Character {
    ll h, a, d, s, i;
    Character(int h, int a, int d, int s, int i) : h(h), a(a), d(d), s(s), i(i) {}
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n;
    cin >> n;
    ll h, a, d, s;
    cin >> h >> a >> d >> s;
    ll H = h;
    Character me(h, a, d, s, -1);
    ll fast = 0, slow = 0;
    vector<Character> enemies;
    bool ok = true;
    rep(i,0,n) {
        cin >> h >> a >> d >> s;
        enemies.emplace_back(h, a, d, s, i);
        if (s > me.s) {
            fast += max(0LL, a - me.d);
        } else {
            slow += max(0LL, a - me.d);
        }
        if (d >= me.a) ok = false;
    }
    if (!ok) {
        cout << -1 << endl;
        return 0;
    }
    sort(enemies.begin(), enemies.end(), [&](auto& p, auto& q) {
        int dp = me.a - p.d;
        int ap = max(p.a - me.d, 0LL);
        int tp = (p.h+dp-1)/dp;
        int dq = me.a - q.d;
        int aq = max(q.a - me.d, 0LL);
        int tq = (q.h+dq-1)/dq;
        return tp*aq < tq*ap;
    });

    for (auto& e : enemies) {
        ll d = me.a - e.d;
        ll a = max(e.a - me.d, 0LL);
        ll t = (e.h+d-1)/d;
        me.h -= t*(fast+slow);
        if (me.s > e.s) {
            me.h += a;
            slow -= a;
        } else {
            fast -= a;
        }
        if (me.h <= 0) break;
    }
    cout << (me.h <= 0 ? -1 : H - me.h) << endl;
}
accuracy: 0.6875
time_ms: 20
memory_kb: 6808
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define POPCOUNT(x) __builtin_popcount(x)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }

const ll INF = 1LL << 60;

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
} fastiofastio;

const ll MOD = 1e9 + 7;

// BEGIN CUT
ll modpow(ll x, ll y, ll m) {
    ll a = 1, p = x;
    while (y > 0) {
        if (y % 2 == 0) {
            p = (p * p) % m;
            y /= 2;
        } else {
            a = (a * p) % m;
            y--;
        }
    }
    return a;
}
// END CUT

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
    int n;
    cin >> n;
    vector<ll> h(n + 1), a(n + 1), d(n + 1), s(n + 1);
    for (int i = 0; i <= n; i++) {
        cin >> h[i] >> a[i] >> d[i] >> s[i];
    }
    vector<PII> vec;
    ll ans = 0;
    ll tot = 0;
    for (int i = 1; i <= n; i++) {
        ll A_hero = max(a[0] - d[i], 0LL);
        ll A_enemy = max(a[i] - d[0], 0LL);
        if (A_hero == 0) {
            if (A_enemy > 0) {
                cout << -1 << endl;
                return 0;
            }
            continue;
        }
        tot += A_enemy;
        ll T = (h[i] + A_hero - 1) / A_hero;
        if (s[i] < s[0])
            ans -= A_enemy;
        vec.emplace_back(A_enemy, T);
    }
    sort(ALL(vec), [](PII x, PII y) {
        auto [ax, tx] = x;
        auto [ay, ty] = y;
        return ay * tx < ax * ty;
    });
    for (auto [a, t] : vec) {
        ans += tot * t;
        tot -= a;
        if (h[0] <= ans) {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 5224
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    typedef struct {
        ll h,a,d,s;
        ll atk, times;
    } Character;

    int n; cin >> n;
    Character p;
    vector< Character > e(n);
    cin >> p.h >> p.a >> p.d >> p.s;
    rep(i,n) cin >> e[i].h >> e[i].a >> e[i].d >> e[i].s;
    rep(i,n){
        ll dmg = max(p.a - e[i].d, 0LL);
        if(dmg == 0 && e[i].a > p.d){ cout << -1 << endl; return 0; }
        if(dmg > 0) e[i].times = (e[i].h + dmg - 1) / dmg; else e[i].times = 1e9;
        e[i].atk = max(e[i].a - p.d, 0LL);
    }

    sort(e.begin(), e.end(), [&](Character a, Character b) {
        return a.atk * b.times > a.times * b.atk;
    });

    ll t = 0, ans = 0;
    rep(i,n){
        if(e[i].atk == 0) continue;
        ans += (t + e[i].times - (ll)(e[i].s < p.s)) * e[i].atk;
        if(ans >= p.h){
            cout << -1 << endl;
            return 0;
        }
        t += e[i].times;
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4916
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    typedef struct {
        ll h,a,d,s;
        ll atk, times;
    } Character;

    int n; cin >> n;
    Character p;
    vector< Character > e(n);
    cin >> p.h >> p.a >> p.d >> p.s;
    rep(i,n) cin >> e[i].h >> e[i].a >> e[i].d >> e[i].s;
    rep(i,n){
        ll dmg = max(p.a - e[i].d, 0LL);
        if(dmg == 0 && e[i].a > p.d){ cout << -1 << endl; return 0; }
        if(dmg > 0) e[i].times = (e[i].h + dmg - 1) / dmg;
        e[i].atk = max(e[i].a - p.d, 0LL);
    }

    sort(e.begin(), e.end(), [&](Character a, Character b) {
        return a.atk * b.times > a.times * b.atk;
    });

    ll t = 0, ans = 0;
    rep(i,n){
        if(e[i].atk == 0) continue;
        ans += (t + e[i].times - (e[i].s < p.s)) * e[i].atk;
        if(ans >= p.h){
            cout << -1 << endl;
            return 0;
        }
        t += e[i].times;
    }

    cout << ans << endl;
}
accuracy: 0.9583333333333334
time_ms: 10
memory_kb: 4752
final_rank: 12