alexjercan/bugnet huggingface dataset:
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

// 立っているbitの数を数える関数
int pcnt(int x) { return __builtin_popcount(x); }

// f(x)を求める関数
int f(int x) {
  if (x == 0)
    return 0;
  return f(x % pcnt(x)) + 1;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  rep(i, n) x[i] = s[i] - '0'; // 配列xに、一要素に一桁ずつsを格納

  int pc = 0; // 最初のxのポップカウント
  rep(i, n) pc += x[i];

  vector<int> ans(n);

  rep(b, 2) { // ポップカウントが1増える場合と1減る場合について
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    if (npc <= 0)
      continue;

    int r0 =
        0; // 元の、割った余りを求める。2進数から受信数に直して余りを取っている（余りを取ることでオーバーフロー回避）
    rep(i, n) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    // i桁目をフリップした時の数の、割った余り(r)を求め
    // その時の答えを計算する
    int k = 1;
    for (int i = n - 1; i >= 0; --i) {
      if (x[i] == b) { // xのビット1/0 が bと等しい時
        int r = r0;
        if (b == 0)
          r = (r + k) % npc;
        else
          r = (r - k + npc) % npc; // 負の数の割り算に注意
        ans[i] = f(r) + 1;
      }
      k = (k * 2) % npc;
    }
  }

  rep(i, n) cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

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

ll func(ll n) {
  if (n == 0)
    return 0;
  ll num = __builtin_popcount(n);
  n %= num;
  return 1 + func(n);
}

ll strbin2i(const std::string &s) {
  ll out = 0;
  for (ll i = 0, size = s.size(); i < size; ++i) {
    out *= 2;
    out += ((ll)s[i] == 49) ? 1 : 0;
  }
  return out;
}

int main() {
  ll n;
  string x;
  cin >> n >> x;

  // ll n = 200000;
  // string x;
  // for(ll i=0;i<n;++i) x.push_back('1');

  vector<ll> m(2000000, -1);

  for (ll i = 0; i < n; ++i) {
    string s = x;
    if (s.at(i) == '0')
      s.at(i) = '1';
    else
      s.at(i) = '0';
    ll x_num = strbin2i(s);

    if (x_num < 200000) {
      if (m.at(x_num) != -1) {
        cout << m.at(x_num) << endl;
      } else {
        m.at(x_num) = func(x_num);
        cout << m.at(x_num) << endl;
      }
    } else {
      cout << func(x_num) << endl;
    }
  }

  return 0;
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define k first
#define v second
using namespace std;

const int N = 200005;
int n, T;

long long score(vector<pair<int, int>> &x) {
  if (x.empty())
    return 0;
  long long res = 0;
  sort(x.begin(), x.end());
  priority_queue<int> q;
  int idx = x.size() - 1;
  for (int i = x[idx].k; i; --i) {
    while (idx >= 0 && x[idx].k == i)
      q.push(x[idx--].v);
    if (!q.empty()) {
      res += q.top();
      q.pop();
    }
  }
  return res;
}

int main() {
  cin >> T;
  while (T--) {
    cin >> n;
    long long ans = 0;
    vector<pair<int, int>> L, R;
    for (int i = 0, k, l, r; i < n; ++i) {
      cin >> k >> l >> r;
      if (l <= r) {
        ans += l;
        R.push_back({n - k, r - l});
      } else {
        ans += r;
        L.push_back({k, l - r});
      }
    }
    ans += score(L);
    ans += score(R);
    printf("%lld\\n", ans);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define k first
#define v second
using namespace std;

const int N = 200005;
int n, T;

long long score(vector<pair<int, int>> &x) {
  long long res = 0;
  sort(x.begin(), x.end());
  priority_queue<int> q;
  int idx = x.size() - 1;
  for (int i = max(0, x[idx].k); i; --i) {
    while (idx >= 0 && x[idx].k == i)
      q.push(x[idx--].v);
    if (!q.empty()) {
      res += q.top();
      q.pop();
    }
  }
  return res;
}

int main() {
  cin >> T;
  while (T--) {
    cin >> n;
    long long ans = 0;
    vector<pair<int, int>> L, R;
    for (int i = 0, k, l, r; i < n; ++i) {
      cin >> k >> l >> r;
      if (l <= r) {
        ans += l;
        R.push_back({n - k, r - l});
      } else {
        ans += r;
        L.push_back({k, l - r});
      }
    }
    ans += score(L);
    ans += score(R);
    printf("%lld\\n", ans);
  }
  return 0;
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;

int main() {
  llong T;
  cin >> T;
  while (T--) {
    llong N;
    vector<vector<lpair>> l, r;
    cin >> N;
    l.resize(N + 1);
    r.resize(N + 1);
    for (int i = 0; i < N; i++) {
      llong K, L, R;
      cin >> K >> L >> R;
      if (K < N && L < R) {
        r[K].push_back(lpair(L, R));
      } else {
        l[K].push_back(lpair(L, R));
      }
    }

    llong ans = 0;
    priority_queue<llong> left, right;
    for (int i = 1; i <= N; i++) {
      for (auto vv : l[i]) {
        ans += vv.first;
        left.push(vv.second - vv.first);
      }
      while (left.size() > i) {
        ans += left.top();
        left.pop();
      }
    }
    for (int i = N - 1; i >= 1; i--) {
      for (auto vv : r[i]) {
        ans += vv.second;
        right.push(vv.first - vv.second);
      }
      while (right.size() > N - i) {
        ans += right.top();
        right.pop();
      }
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;
using llpair = pair<llong, lpair>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;

int main() {
  llong T;
  cin >> T;
  while (T--) {
    llong N;
    vector<vector<lpair>> l, r;
    priority_queue<llong> lque, rque;

    cin >> N;
    l.resize(N + 1);
    r.resize(N + 1);
    for (int i = 0; i < N; ++i) {
      llong K, L, R;
      cin >> K >> L >> R;
      if (L > R) {
        l[K].push_back(lpair(L, R));
      } else {
        r[K].push_back(lpair(L, R));
      }
    }

    llong ans = 0;
    llong lid = 0, rid = 0;
    for (int i = 1; i <= N; i++) {
      for (auto vv : l[i]) {
        ans += vv.first;
        lque.push(vv.second - vv.first);
      }
      lid += l[i].size();
      for (int j = i + 1; j <= lid; j++) {
        ans += lque.top();
        lque.pop();
      }
    }
    for (int i = N; i >= 0; i--) {
      for (auto vv : r[i]) {
        ans += vv.second;
        rque.push(vv.first - vv.second);
      }
      rid += r[i].size();
      for (int j = N - i + 1; j <= rid; j++) {
        ans += rque.top();
        rque.pop();
      }
    }
    cout << ans << endl;
  }

  return 0;
}

/*

前から埋めて、後から交換したい

j に牛1 を入れ、j 以前の 牛2 と交換しても牛1 のスコアには影響がない

前が全て埋まっている & j に入れたい牛が複数いるとき、swap できない = R
に固定される牛がでてくる
-> L から R に変えたときの減少分 L - R が大きい牛を選ぶ

*/
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
constexpr int inf = 1e9 + 7;
constexpr ll longinf = 1LL << 60;
constexpr ll mod = 1e9 + 7;

void solve() {
  int n;
  cin >> n;
  ll ans = 0;
  vector<pair<int, int>> l, r;
  rep(i, n) {
    int k, x, y;
    cin >> k >> x >> y;
    if (x >= y) {
      ans += y;
      l.emplace_back(x - y, k - 1);
    } else {
      ans += x;
      r.emplace_back(y - x, k - 1);
    }
  }
  set<int> L, R;
  rep(i, l.size()) L.insert(i);
  rep(i, r.size()) R.insert(i + l.size());
  sort(l.rbegin(), l.rend());
  sort(r.rbegin(), r.rend());
  for (auto e : l) {
    auto itr = L.upper_bound(e.second);
    if (itr != L.begin()) {
      --itr;
      ans += e.first;
      L.erase(itr);
    }
  }
  for (auto e : r) {
    auto itr = R.upper_bound(e.second);
    if (itr != R.end()) {
      ans += e.first;
      R.erase(itr);
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--)
    solve();
  return 0;
}

Error: -6
free(): double free detected in tcache 2


Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
constexpr int inf = 1e9 + 7;
constexpr ll longinf = 1LL << 60;
constexpr ll mod = 1e9 + 7;

void solve() {
  int n;
  cin >> n;
  ll ans = 0;
  vector<pair<int, int>> l, r;
  rep(i, n) {
    int k, x, y;
    cin >> k >> x >> y;
    if (x >= y) {
      ans += y;
      l.emplace_back(x - y, k - 1);
    } else {
      ans += x;
      r.emplace_back(y - x, k - 1);
    }
  }
  set<int> L, R;
  rep(i, l.size()) L.insert(i);
  rep(i, r.size()) R.insert(i + l.size());
  sort(l.rbegin(), l.rend());
  sort(r.rbegin(), r.rend());
  for (auto e : l) {
    auto itr = L.upper_bound(e.second);
    if (itr != L.begin()) {
      --itr;
      ans += e.first;
      L.erase(itr);
    }
  }
  for (auto e : r) {
    auto itr = R.upper_bound(e.second);
    if (itr != R.end()) {
      ans += e.first;
      L.erase(itr);
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--)
    solve();
  return 0;
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<P> ls(0), rs(0);
    int k, l, r;
    ll ans = 0;
    REP(i, n) {
      cin >> k >> l >> r;
      ans += max(l, r);
      if (l > r)
        ls.push_back({k, abs(l - r)});
      else
        rs.push_back({n - k, abs(l - r)});
    }
    sort(ALL(ls));
    sort(ALL(rs));
    multiset<int> sel;
    REP(i, ls.size()) {
      if (ls[i].first <= sel.size()) {
        if (*sel.begin() < ls[i].second) {
          ans -= *sel.begin();
          sel.erase(sel.begin());
          sel.insert(ls[i].second);
        } else {
          ans -= ls[i].second;
        }
      } else {
        sel.insert(ls[i].second);
      }
    }
    multiset<int> ser;
    REP(i, rs.size()) {
      if (rs[i].first <= ser.size()) {
        if (rs[i].first == 0) {
          ans -= rs[i].second;
          continue;
        }
        if (*ser.begin() < rs[i].second) {
          ans -= *ser.begin();
          ser.erase(ser.begin());
          ser.insert(rs[i].second);
        } else {
          ans -= rs[i].second;
        }
      } else {
        ser.insert(rs[i].second);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

struct ra {
  int k;
  int l;
  int r;
  bool operator<(const ra &another) const {
    return max(l, r) < max(another.l, another.l);
  };
};

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    scanf("%d", n);
    vector<ra> a(n);
    REP(i, n)
    scanf("%d %d %d", a[i].k, a[i].l, a[i].r);
    sort(ALL(a));
    multiset<int> s;
    ll ans = 0;
    REP(i, n) {
      int p = distance(s.begin(), s.upper_bound(a[i].k));
      if (a[i].l > a[i].r) {
        if (p < a[i].k) {
          ans += a[i].l;
          s.insert(a[i].k);
        } else
          ans += a[i].r;
      } else {
        if (s.size() - p + 1 < n - a[i].k + 1) {
          ans += a[i].r;
          s.insert(a[i].k);
        } else
          ans += a[i].l;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define F first
#define S second

const int mxn = 2e5 + 5;
int n;
bool good[mxn];
array<int, 3> a[mxn];

void solve() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  }

  sort(a, a + n);

  set<int> ff, fb;
  for (int i = 0; i < n; ++i) {
    ff.insert(i);
    fb.insert(i);
  }

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      worstf, worstb;

  for (int i = 0; i < n; ++i) {
    int k = a[i][0], l = a[i][1], r = a[i][2];
    if (l > r) {
      auto lb = ff.upper_bound(k - 1);
      if (lb == ff.begin()) {
        ff.erase(--ff.end());

        if (!worstf.empty() && worstf.top().F < l - r) {
          good[worstf.top().S] = false;
          worstf.pop();
          worstf.push({l - r, i});
          good[i] = true;
        } else {
          good[i] = false;
        }
      } else {
        --lb;
        ff.erase(lb);
        worstf.push({l - r, i});
        good[i] = true;
      }
    }
  }

  for (int i = n - 1; i >= 0; --i) {
    int k = a[i][0], l = a[i][1], r = a[i][2];
    if (r > l) {
      auto lb = fb.lower_bound(k);
      if (lb == fb.end()) {
        fb.erase(fb.begin());

        if (!worstb.empty() && worstb.top().F < r - l) {
          good[worstb.top().S] = false;
          worstb.pop();
          worstb.push({r - l, i});
          good[i] = true;
        } else {
          good[i] = false;
        }
      } else {
        fb.erase(lb);
        worstb.push({r - l, i});
        good[i] = true;
      }
    }
  }

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int mn = min(a[i][1], a[i][2]), mx = max(a[i][1], a[i][2]);
    ans += good[i] ? mx : mn;
  }
  cout << ans << '\\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  for (int i = 0; i < T; ++i)
    solve();
  return 0;
}

Error: -6
free(): double free detected in tcache 2


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define F first
#define S second

const int mxn = 2e5 + 5;
int n;
array<int, 3> a[mxn];

void solve() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i][0] >> a[i][1] >> a[i][2];

  sort(a, a + n);

  set<int> ff, fb;
  for (int i = 0; i < n; ++i) {
    ff.insert(i);
    fb.insert(i);
  }

  // set<int> uf, ub;

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int k = a[i][0], l = a[i][1], r = a[i][2];
    if (l > r) {
      auto lb = ff.upper_bound(k);
      if (lb == ff.begin()) {
        ff.erase(--ff.end());
        ans += r;
      } else {
        --lb;
        ff.erase(lb);
        ans += l;
      }

    } else {
      auto lb = fb.upper_bound(n - 1 - k);
      if (lb == ff.begin()) {
        ff.erase(--ff.end());
        ans += r;
      } else {
        --lb;
        ff.erase(lb);
        ans += l;
      }
    }
  }

  cout << ans << '\\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  for (int i = 0; i < T; ++i)
    solve();
  return 0;
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

int main(void) {
  u64 T;
  cin >> T;

  vector<u64> ans(T);

  for (u64 t = 0; t < T; t++) {
    u64 N;
    cin >> N;

    priority_queue<pair<u64, u64>> lside;
    priority_queue<pair<u64, u64>> rside;
    u64 t_ans = 0;

    for (uint n = 0; n < N; n++) {
      u64 K;
      u64 L, R;
      cin >> K >> L >> R;
      t_ans += min(L, R);

      if (L > R) {
        lside.push(make_pair(K, L - R));
      } else {
        // HACK : sort order defalut is N to M (N > M)
        rside.push(make_pair(N - K, R - L));
      }
    }

    priority_queue<u64> lcdt;
    for (u64 l = lside.size(); l > 0; l--) {
      while (!lside.empty() && l <= lside.top().first) {
        lcdt.push(lside.top().second);
        lside.pop();
      }
      if (!lcdt.empty()) {
        t_ans += lcdt.top();
        lcdt.pop();
      }
    }

    priority_queue<u64> rcdt;
    for (u64 r = rside.size(); r > 0; r--) {
      while (!rside.empty() && r <= rside.top().first) {
        rcdt.push(rside.top().second);
        rside.pop();
      }
      if (!rcdt.empty()) {
        t_ans += rcdt.top();
        rcdt.pop();
      }
    }

    ans[t] = t_ans;
  }

  for (const auto a : ans)
    cout << a << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

int main(void) {
  u64 T;
  cin >> T;

  vector<u64> ans(T);

  for (u64 t = 0; t < T; t++) {
    u64 N;
    cin >> N;

    priority_queue<pair<u64, u64>> lside;
    priority_queue<pair<u64, u64>> rside;
    u64 t_ans = 0;

    for (uint n = 0; n < N; n++) {
      u64 K;
      u64 L, R;
      cin >> K >> L >> R;
      t_ans += min(L, R);

      if (L > R) {
        lside.push(make_pair(K, L - R));
      } else {
        // HACK : sort order defalut is N to M (N > M)
        rside.push(make_pair(N - K, R - L));
      }
    }

    priority_queue<u64> lcdt;
    for (u64 l = min(lside.size(), lside.top().first); l > 0; l--) {
      while (!lside.empty() && l <= lside.top().first) {
        lcdt.push(lside.top().second);
        lside.pop();
      }
      if (!lcdt.empty()) {
        t_ans += lcdt.top();
        lcdt.pop();
      }
    }

    priority_queue<u64> rcdt;
    for (u64 r = min(rside.size(), rside.top().first); r > 0; r--) {
      while (!rside.empty() && r <= rside.top().first) {
        rcdt.push(rside.top().second);
        rside.pop();
      }
      if (!rcdt.empty()) {
        t_ans += rcdt.top();
        rcdt.pop();
      }
    }

    ans[t] = t_ans;
  }

  for (const auto a : ans)
    cout << a << endl;
  return 0;
}
Problem p02610 (C++) — buggy submission, verdict: Time Limit Exceeded

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

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

ll happy(vector<P> c) {
  int cn = c.size();
  rep(i, cn) c[i].first = min(c[i].first, cn); // cnまでしかみないので同等

  vector<vector<int>> carray(cn + 1);
  rep(i, cn) carray[c[i].first].push_back(-c[i].second);

  priority_queue<int> ok;
  for (int i = 1; i <= cn; ++i) {
    for (int x : carray[i])
      ok.push(x);
    while (ok.size() > i)
      ok.pop();
  }
  ll h = 0;
  while (ok.size() > 0) {
    h += -ok.top();
    ok.pop();
  }
  return h;
}

void solve() {
  int N;
  cin >> N;
  // vector<int> K(N),L(N),R(N);
  vector<P> mae, ato;
  int Ki, Li, Ri;
  ll ans = 0;
  rep(i, N) {
    cin >> Ki >> Li >> Ri;
    if (Li > Ri) {
      mae.emplace_back(Ki, Li - Ri);
    } else {
      ato.emplace_back(N - Ki, Ri - Li);
    }
    ans += min(Li, Ri);
  }

  ans += happy(mae);
  ans += happy(ato);
  cout << ans << endl;
}

//---main---------------------------------------------
int main() {
  // main
  int T;
  cin >> T;
  rep(ti, T) solve();

  return 0;
}

Error: TLE

Fixed submission:

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

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

ll happy(vector<P> c) {
  int cn = c.size();
  rep(i, cn) c[i].first = min(c[i].first, cn); // cnまでしかみないので同等

  vector<vector<int>> carray(cn + 1);
  rep(i, cn) carray[c[i].first].push_back(c[i].second);

  vector<int> ok;
  for (int i = 1; i <= cn; ++i) {
    for (int x : carray[i])
      ok.push_back(x);
    sort(ok.begin(), ok.end(), greater<int>());
    while (ok.size() > i)
      ok.pop_back();
  }
  ll h = 0;
  for (int x : ok)
    h += x;
  return h;
}

void solve() {
  int N;
  cin >> N;
  // vector<int> K(N),L(N),R(N);
  vector<P> mae, ato;
  int Ki, Li, Ri;
  ll ans = 0;
  rep(i, N) {
    cin >> Ki >> Li >> Ri;
    if (Li > Ri) {
      mae.emplace_back(Ki, Li - Ri);
    } else {
      ato.emplace_back(N - Ki, Ri - Li);
    }
    ans += min(Li, Ri);
  }

  ans += happy(mae);
  ans += happy(ato);
  cout << ans << endl;
}

//---main---------------------------------------------
int main() {
  // main
  int T;
  cin >> T;
  rep(ti, T) solve();

  return 0;
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
#define repr(i, n) for (ll i = n - 1; i >= 0; --i)
using namespace std;
using P = pair<int, int>;

ll f(vector<P> p) {
  int n = p.size();
  rep(i, n) p[i].first = min(p[i].first, n);
  vector<vector<int>> s(n + 1);
  rep(i, n) s[p[i].first].push_back(p[i].second);
  ll res = 0;
  priority_queue<int> q;
  for (int i = n; i >= 1; --i) {
    for (int x : s[i])
      q.push(x);
    if (q.size() > 0) {
      res += q.top();
      q.pop();
    }
  }
  return res;
}

void solve() {
  int n;
  cin >> n;
  vector<P> pl, pr;
  ll ans = 0;
  rep(i, n) {
    int k, l, r;
    cin >> k >> l >> r;
    int m = min(l, r);
    ans += m;
    l -= m;
    r -= m;
    if (l > 0)
      pl.emplace_back(k, l);
    else
      pr.emplace_back(n - k, r);
  }
  ans += f(pl);
  ans += f(pr);
  cout << ans << endl;
}
int main(void) {
  int ts;
  cin >> ts;
  rep(ti, ts) solve();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
using namespace std;
using P = pair<int, int>;

ll f(vector<P> p) {
  int n = p.size();
  rep(i, n) p[i].first = min(p[i].first, n);
  vector<vector<int>> s(n + 1);
  rep(i, n) s[p[i].first].push_back(p[i].second);
  ll res = 0;
  priority_queue<int> q;
  for (int i = n - 1; i <= 1; ++i) {
    for (int x : s[i])
      q.push(x);
    if (q.size() > 0)
      res += q.top(), q.pop();
  }
  return res;
}

void solve() {
  int n;
  cin >> n;
  vector<P> pl, pr;
  ll ans = 0;
  rep(i, n) {
    int k, l, r;
    cin >> k >> l >> r;
    int m = min(l, r);
    ans += m;
    l -= m;
    r -= m;
    if (l > 0)
      pl.emplace_back(k, l);
    else
      pr.emplace_back(n - k, r);
  }
  ans += f(pl);
  ans += f(pr);
  cout << ans << endl;
}
int main(void) {
  int ts;
  cin >> ts;
  rep(ti, ts) solve();
  return 0;
}