Problem p01353 (C++) — buggy submission, verdict: Runtime Error

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

signed main() {
  int N;
  cin >> N;
  int H, A, D, S;
  cin >> H >> A >> D >> S;

  vector<tuple<int, int, int, int>> enemies;
  for (int i = 0; i < N; i++) {
    int h, a, d, s;
    cin >> h >> a >> d >> s;

    // 主人公が攻撃しても意味ない
    if (A - d <= 0) {
      // 敵がつよい
      if (a - D > 0) {
        cout << -1 << endl;
        return 0;
      }
      // 戦闘不能判定に関係ないからいらなさそう
      else {
        continue;
      }
    }
    // 敵が攻撃しても意味ない
    if (a - D <= 0) {
      continue;
    }

    enemies.emplace_back(h, a, d, s);
  }

  sort(enemies.begin(), enemies.end(), [&](auto x, auto y) {
    int xh, xa, xd, xs;
    tie(xh, xa, xd, xs) = x;
    int yh, ya, yd, ys;
    tie(yh, ya, yd, ys) = y;

    // 何ターンで死ぬか
    int xdam = A - xd;
    int xturn = (xh + xdam - 1) / xdam;
    int ydam = A - yd;
    int yturn = (yh + ydam - 1) / ydam;

    // 敵の攻撃はいくつ？
    int xinj = xa - D, yinj = ya - D;

    // 何回攻撃するか (x -> y)
    int xatkl = (xs > S ? xturn : xturn - 1);
    int yatkl = (ys > S ? xturn + yturn : xturn + yturn - 1);

    // 何回攻撃するか (y -> x)
    int yatkr = (ys > S ? yturn : yturn - 1);
    int xatkr = (xs > S ? xturn + yturn : xturn + yturn - 1);

    int suml = xinj * xatkl + yinj * yatkl;
    int sumr = xinj * xatkr + yinj * yatkr;
    return suml < sumr;
  });

  int turn_sum = 0, mi = 0;
  for (auto e : enemies) {
    int h, a, d, s;
    tie(h, a, d, s) = e;
    int dam = A - d, turn = (h + dam - 1) / dam;
    int inj = a - D;

    // fprintf(stderr, "dam = %lld, turn = %lld, inj = %lld\\n", dam, turn, inj);
    turn_sum += turn;
    int atk = (s > S ? turn_sum : turn_sum - 1);

    mi += atk * inj;
    if (mi >= H) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << mi << endl;
  return 0;
}


Error: 0
dam = 2, turn = 1, inj = 3
dam = 2, turn = 1, inj = 1


Fixed submission:

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

signed main() {
  int N;
  cin >> N;
  int H, A, D, S;
  cin >> H >> A >> D >> S;

  vector<tuple<int, int, int, int>> enemies;
  for (int i = 0; i < N; i++) {
    int h, a, d, s;
    cin >> h >> a >> d >> s;

    // 主人公が攻撃しても意味ない
    if (A - d <= 0) {
      // 敵がつよい
      if (a - D > 0) {
        cout << -1 << endl;
        return 0;
      }
      // 戦闘不能判定に関係ないからいらなさそう
      else {
        continue;
      }
    }
    // 敵が攻撃しても意味ない
    if (a - D <= 0) {
      continue;
    }

    enemies.emplace_back(h, a, d, s);
  }

  sort(enemies.begin(), enemies.end(), [&](auto x, auto y) {
    int xh, xa, xd, xs;
    tie(xh, xa, xd, xs) = x;
    int yh, ya, yd, ys;
    tie(yh, ya, yd, ys) = y;

    // 何ターンで死ぬか
    int xdam = A - xd;
    int xturn = (xh + xdam - 1) / xdam;
    int ydam = A - yd;
    int yturn = (yh + ydam - 1) / ydam;

    // 敵の攻撃はいくつ？
    int xinj = xa - D, yinj = ya - D;

    // 何回攻撃するか (x -> y)
    int xatkl = (xs > S ? xturn : xturn - 1);
    int yatkl = (ys > S ? xturn + yturn : xturn + yturn - 1);

    // 何回攻撃するか (y -> x)
    int yatkr = (ys > S ? yturn : yturn - 1);
    int xatkr = (xs > S ? xturn + yturn : xturn + yturn - 1);

    int suml = xinj * xatkl + yinj * yatkl;
    int sumr = xinj * xatkr + yinj * yatkr;
    return suml < sumr;
  });

  int turn_sum = 0, mi = 0;
  for (auto e : enemies) {
    int h, a, d, s;
    tie(h, a, d, s) = e;
    int dam = A - d, turn = (h + dam - 1) / dam;
    int inj = a - D;

    fprintf(stderr, "dam = %lld, turn = %lld, inj = %lld\\n", dam, turn, inj);
    turn_sum += turn;
    int atk = (s > S ? turn_sum : turn_sum - 1);

    mi += atk * inj;
  }

  if (mi >= H)
    cout << -1 << endl;
  else
    cout << mi << endl;
  return 0;
}