prithivMLmods/Coder-Stat huggingface dataset:
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define llong long long
#define INF (__INT32_MAX__ / 2)
#define EPS 10e-8
#define MAX_NM 300

using namespace std;
typedef pair<llong, llong> ipair;
llong modulo(llong left, llong right) { return (left % right + right) % right; }

int N, M;
list<int> sports[MAX_NM];
list<int>::iterator sports_pos[MAX_NM][MAX_NM];

int solve() {
  int res = INF;

  int sports_cnt[MAX_NM];
  for (int t = 0; t < M; ++t) {
    memset(sports_cnt, 0, sizeof(sports_cnt));

    for (int i = 0; i < M; ++i)
      sports_cnt[sports[i].front()]++;

    int max_val = -INF, max_sports;
    for (int i = 0; i < M; ++i)
      if (max_val < sports_cnt[i]) {
        max_val = sports_cnt[i];
        max_sports = i;
      }

    res = min(res, max_val);

    for (int i = 0; i < N; ++i)
      sports[i].erase(sports_pos[i][max_sports]);
  }

  return res;
}

int main(int argc, char **argv) {
  cin >> N >> M;

  for (int i = 0; i < N; ++i)
    for (int j = 0; j < M; ++j) {
      int A;
      cin >> A;
      sports[i].push_back(A - 1);
    }

  for (int i = 0; i < N; ++i)
    for (auto it = sports[i].begin(); it != sports[i].end(); ++it)
      sports_pos[i][*it] = it;

  cout << solve() << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define llong long long
#define INF (__INT32_MAX__ / 2)
#define EPS 10e-8
#define MAX_NM 300

using namespace std;
typedef pair<llong, llong> ipair;
llong modulo(llong left, llong right) { return (left % right + right) % right; }

int N, M;
list<int> sports[MAX_NM];
list<int>::iterator sports_pos[MAX_NM][MAX_NM];

int solve() {
  int res = INF;

  int sports_cnt[MAX_NM];
  for (int t = 0; t < M; ++t) {
    memset(sports_cnt, 0, sizeof(sports_cnt));

    for (int i = 0; i < N; ++i)
      sports_cnt[sports[i].front()]++;

    int max_val = -INF, max_sports;
    for (int i = 0; i < M; ++i)
      if (max_val < sports_cnt[i]) {
        max_val = sports_cnt[i];
        max_sports = i;
      }

    res = min(res, max_val);

    for (int i = 0; i < N; ++i)
      sports[i].erase(sports_pos[i][max_sports]);
  }

  return res;
}

int main(int argc, char **argv) {
  cin >> N >> M;

  for (int i = 0; i < N; ++i)
    for (int j = 0; j < M; ++j) {
      int A;
      cin >> A;
      sports[i].push_back(A - 1);
    }

  for (int i = 0; i < N; ++i)
    for (auto it = sports[i].begin(); it != sports[i].end(); ++it)
      sports_pos[i][*it] = it;

  cout << solve() << endl;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<deque<int>> a(n, deque<int>(m, 0));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> a[i][j];
    }
  }
  int ret = 1000;
  map<int, int> cnt;
  set<int> to_remove;
  for (int i = 0; i < m; ++i) {
    cnt.clear();
    for (int j = 0; j < n; ++j) {
      cnt[a[j].front()]++;
    }
    int maxidx = -1;
    int maxi = 0;
    for (auto &p : cnt) {
      if (p.second > maxi) {
        maxi = p.second;
        maxidx = p.first;
      }
    }
    chmin(ret, maxi);
    to_remove.insert(maxidx);
    for (int j = 0; j < n; ++j) {
      while (to_remove.count(a[i].front()) == 1)
        a[i].pop_front();
    }
  }
  cout << ret << "\n";
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}
template <class C> void print(const C &c, std::ostream &os = std::cout) {
  std::copy(std::begin(c), std::end(c),
            std::ostream_iterator<typename C::value_type>(os, " "));
  os << std::endl;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<deque<int>> a(n, deque<int>(m, 0));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> a[i][j];
    }
  }
  int ret = 1000;
  map<int, int> cnt;
  set<int> to_remove;
  for (int i = 0; i < m; ++i) {
    cnt.clear();
    for (int j = 0; j < n; ++j) {
      cnt[a[j].front()]++;
    }
    int maxidx = -1;
    int maxi = 0;
    for (auto &p : cnt) {
      if (p.second > maxi) {
        maxi = p.second;
        maxidx = p.first;
      }
    }
    chmin(ret, maxi);
    to_remove.insert(maxidx);
    for (int j = 0; j < n; ++j) {
      while (to_remove.count(a[j].front()) == 1)
        a[j].pop_front();
    }
  }
  cout << ret << "\n";
  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX

int N, M, ans;
deque<int> v[300];
map<int, int> m, b;

signed main() {
  cin >> N >> M;
  rep(i, N) rep(j, M) {
    int a;
    cin >> a;
    v[i].pb(a);
  }
  rep(i, N) m[v[i][0]]++;
  for (auto i : m)
    ans = max(ans, i.se);
  rep(k, N - 1) {
    int MAX = 0, num;
    for (auto i : m)
      if (i.se > MAX) {
        MAX = i.se;
        num = i.fi;
      }
    b[num] = 1;
    m.clear();
    rep(i, N) {
      while (b[v[i][0]])
        v[i].pop_front();
      m[v[i][0]]++;
    }
    int tmp = 0;
    for (auto i : m)
      tmp = max(tmp, i.se);
    ans = min(ans, tmp);
  }
  cout << ans << endl;
}


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

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX

int N, M, ans;
deque<int> v[300];
map<int, int> m, b;

signed main() {
  cin >> N >> M;
  rep(i, N) rep(j, M) {
    int a;
    cin >> a;
    v[i].pb(a);
  }
  rep(i, N) m[v[i][0]]++;
  for (auto i : m)
    ans = max(ans, i.se);
  rep(k, M - 1) {
    int MAX = 0, num;
    for (auto i : m)
      if (i.se > MAX) {
        MAX = i.se;
        num = i.fi;
      }
    b[num] = 1;
    m.clear();
    rep(i, N) {
      while (b[v[i][0]])
        v[i].pop_front();
      m[v[i][0]]++;
    }
    int tmp = 0;
    for (auto i : m)
      tmp = max(tmp, i.se);
    ans = min(ans, tmp);
  }
  cout << ans << endl;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(v, a, b) for (int v = a; v <= b; v++)
using namespace std;
const int maxn = 200 + 1;
int a[maxn][maxn];
bool mark[maxn];
int cnt[maxn];
int n, m;
void Count() {
  fo(i, 1, m) cnt[i] = 0;
  fo(i, 1, n) {
    fo(j, 1, m) {
      if (mark[a[i][j]]) {
        cnt[a[i][j]]++;
        break;
      }
    }
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  fo(i, 1, n) {
    fo(j, 1, m) { cin >> a[i][j]; }
  }
  fo(i, 1, m) mark[i] = true;
  int ans = 1e9;
  fo(i, 1, m) {
    Count();
    int cur = 0, id;
    fo(j, 1, m) {
      if (cnt[j] > cur) {
        cur = cnt[j];
        id = j;
      }
    }
    ans = min(ans, cur);
    mark[id] = false;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(v, a, b) for (int v = a; v <= b; v++)
using namespace std;
const int maxn = 300 + 5;
int a[maxn][maxn];
bool mark[maxn];
int cnt[maxn];
int n, m;
void Count() {
  fo(i, 1, m) cnt[i] = 0;
  fo(i, 1, n) {
    fo(j, 1, m) {
      if (mark[a[i][j]]) {
        cnt[a[i][j]]++;
        break;
      }
    }
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  fo(i, 1, n) {
    fo(j, 1, m) { cin >> a[i][j]; }
  }
  fo(i, 1, m) mark[i] = true;
  int ans = 1e9;
  fo(i, 1, m) {
    Count();
    int cur = 0, id;
    fo(j, 1, m) {
      if (cnt[j] > cur) {
        cur = cnt[j];
        id = j;
      }
    }
    ans = min(ans, cur);
    mark[id] = false;
  }
  cout << ans << endl;
  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>(M));
  rep(i, N) rep(j, M) {
    cin >> A[i][j];
    A[i][j]--;
  }
  vector<int> id(N);
  vector<vector<bool>> used(N, vector<bool>(M, false));
  int ans = INF;
  while (true) {
    vector<int> s(M);
    bool ok = true;
    rep(i, N) {
      while (used[i][id[i]]) {
        id[i]++;
        if (id[i] >= N)
          ok = false;
      }
      if (!ok)
        break;
      s[A[i][id[i]]]++;
    }
    if (!ok)
      break;
    int now = 0;
    int dame = 0;
    rep(i, M) {
      if (now < s[i]) {
        now = s[i];
        dame = i;
      }
    }
    rep(i, N) rep(j, M) {
      if (A[i][j] == dame)
        used[i][j] = true;
    }
    ans = min(ans, now);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>(M));
  rep(i, N) rep(j, M) {
    cin >> A[i][j];
    A[i][j]--;
  }
  vector<int> id(N);
  vector<vector<bool>> used(N, vector<bool>(M, false));
  int ans = INF;
  while (true) {
    vector<int> s(M);
    bool ok = true;
    rep(i, N) {
      while (used[i][id[i]]) {
        id[i]++;
        if (id[i] >= M)
          ok = false;
      }
      if (!ok)
        break;
      s[A[i][id[i]]]++;
    }
    if (!ok)
      break;
    int now = 0;
    int dame = 0;
    rep(i, M) {
      if (now < s[i]) {
        now = s[i];
        dame = i;
      }
    }
    rep(i, N) rep(j, M) {
      if (A[i][j] == dame)
        used[i][j] = true;
    }
    ans = min(ans, now);
  }
  cout << ans << endl;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, m;
int a[310][310];
int ans;
int vis[310];
stack<int> s[310];

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int o = 1; o <= m; o++)
      cin >> a[i][o];
  for (int i = 1; i <= n; i++)
    for (int o = m; o; o--)
      s[i].push(a[i][o]);

  ans = n;
  for (int i = 1; i <= n; i++) {
    int c[310];
    memset(c, 0, sizeof c);

    for (int o = 1; o <= n; o++)
      c[s[o].top()]++;
    int maxi = s[1].top();
    for (int o = 2; o <= n; o++)
      if (c[s[o].top()] > c[maxi])
        maxi = s[o].top();
    vis[maxi] = 1;
    ans = min(ans, c[maxi]);
    for (int o = 1; o <= n; o++)
      while (!s[o].empty() && vis[s[o].top()])
        s[o].pop();
  }
  cout << ans << endl;
}

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

int n, m;
int a[310][310];
int ans;
int vis[310];
stack<int> s[310];

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int o = 1; o <= m; o++)
      cin >> a[i][o];
  for (int i = 1; i <= n; i++)
    for (int o = m; o; o--)
      s[i].push(a[i][o]);

  ans = n;
  for (int i = 1; i <= m; i++) {
    int c[310];
    memset(c, 0, sizeof c);

    for (int o = 1; o <= n; o++)
      c[s[o].top()]++;
    int maxi = s[1].top();
    for (int o = 2; o <= n; o++)
      if (c[s[o].top()] > c[maxi])
        maxi = s[o].top();
    vis[maxi] = 1;
    ans = min(ans, c[maxi]);
    for (int o = 1; o <= n; o++)
      while (!s[o].empty() && vis[s[o].top()])
        s[o].pop();
  }
  cout << ans << endl;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int n, m;
bool removed[300];
int a[300][300];
int c[300];
int visible[300];
int res = 1e9;

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      cin >> a[i][j];
      a[i][j]--;
    }
  for (int i = 0; i < n; ++i)
    visible[a[i][0]]++;

  for (int i = 0; i < m; ++i) {
    int maxVisible = 0;
    for (int j = 0; j < m; ++j)
      if (visible[j] > visible[maxVisible])
        maxVisible = j;
    res = min(res, visible[maxVisible]);

    visible[maxVisible] = -1e9;
    removed[maxVisible] = true;
    for (int j = 0; j < n; ++j) {
      while (c[j] < m && removed[a[j][c[j]]]) {
        c[j]++;
        visible[a[j][c[j]]]++;
      }
    }
  }

  cout << res << "\n";
}


Fixed submission:
#include <iostream>

using namespace std;

int n, m;
bool removed[300];
int a[300][300];
int c[300];
int visible[300];
int res = 1e9;

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      cin >> a[i][j];
      a[i][j]--;
    }
  for (int i = 0; i < n; ++i)
    visible[a[i][0]]++;

  for (int i = 0; i < m; ++i) {
    int maxVisible = 0;
    for (int j = 0; j < m; ++j)
      if (visible[j] > visible[maxVisible])
        maxVisible = j;
    res = min(res, visible[maxVisible]);
    if (i == m - 1)
      break;

    visible[maxVisible] = -1e9;
    removed[maxVisible] = true;
    for (int j = 0; j < n; ++j) {
      while (c[j] < m && removed[a[j][c[j]]]) {
        c[j]++;
        visible[a[j][c[j]]]++;
      }
    }
  }

  cout << res << "\n";
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>

using namespace std;

int a[300][300];
int b[300];
int c[300];
int f[300];

int main() {
  int n, m, ans = 1e9, i, j;

  scanf("%d %d", &n, &m);

  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
      a[i][j]--;
    }
  }

  for (i = 0; i < m; i++) {
    int x = 0, y = 0;

    for (j = 0; j < m; j++)
      c[j] = 0;

    for (j = 0; j < n; j++)
      c[a[j][b[j]]]++;

    for (j = 0; j < m; j++) {
      if (c[j] > x) {
        x = c[j];
        y = j;
      }
    }

    ans = min(ans, x);

    f[y] = 1;

    for (j = 0; j < n; j++) {
      while (f[a[j][b[j]]] == 1)
        b[j]++;
    }
  }

  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>

using namespace std;

int a[300][300];
int b[300];
int c[300];
int f[300];

int main() {
  int n, m, ans = 1e9, i, j;

  scanf("%d %d", &n, &m);

  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
      a[i][j]--;
    }
  }

  for (i = 0; i < m; i++) {
    int x = 0, y = 0;

    for (j = 0; j < m; j++)
      c[j] = 0;

    for (j = 0; j < n; j++)
      c[a[j][b[j]]]++;

    for (j = 0; j < m; j++) {
      if (c[j] > x) {
        x = c[j];
        y = j;
      }
    }

    ans = min(ans, x);

    f[y] = 1;

    for (j = 0; j < n; j++) {
      while (b[j] < m && f[a[j][b[j]]] == 1)
        b[j]++;
    }
  }

  printf("%d\n", ans);

  return 0;
}
Problem p03652 (C++)

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

const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;

int n, m;
queue<int> b[300];
int cnt[300];

int main() {
  cin >> n >> m;
  rep(i, n) {
    int tmp;
    rep(j, m) {
      cin >> tmp;
      b[i].push(tmp - 1);
    }
    cnt[b[i].front()]++;
  }
  int ans = n;
  rep(i, n - 1) {
    int max_ind = 0, max_v = -1;
    rep(j, m) {
      if (cnt[j] > max_v) {
        max_v = cnt[j];
        max_ind = j;
      }
    }
    cnt[max_ind] = -1;
    ans = min(ans, max_v);
    rep(j, n) {
      if (cnt[b[j].front()] < 0) {
        while (cnt[b[j].front()] < 0)
          b[j].pop();
        cnt[b[j].front()]++;
      }
    }
  }
  cout << ans << endl;
}


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

const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;

int n, m;
queue<int> b[300];
int cnt[300];

int main() {
  cin >> n >> m;
  rep(i, n) {
    int tmp;
    rep(j, m) {
      cin >> tmp;
      b[i].push(tmp - 1);
    }
    cnt[b[i].front()]++;
  }
  int ans = n;
  rep(i, m - 1) {
    int max_ind = 0, max_v = -1;
    rep(j, m) {
      if (cnt[j] > max_v) {
        max_v = cnt[j];
        max_ind = j;
      }
    }
    cnt[max_ind] = -1;
    ans = min(ans, max_v);
    rep(j, n) {
      if (cnt[b[j].front()] < 0) {
        while (cnt[b[j].front()] < 0)
          b[j].pop();
        cnt[b[j].front()]++;
      }
    }
  }
  cout << ans << endl;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, m;
  scanf("%d %d", &n, &m);

  int l[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      scanf("%d", &l[i][j]);
      l[i][j]--;
    }

  for (int i = 1; i <= n; i++) {
    vector<int> cnt[m];
    int choice[n];
    fill(choice, choice + n, 0);
    bool lock[n];
    memset(lock, 0, sizeof(lock));

    queue<int> q;
    for (int j = 0; j < n; j++)
      q.push(j);

    bool ok = true;
    while (!q.empty()) {
      int cur = q.front();
      q.pop();

      while (choice[cur] < m) {
        int next = l[cur][choice[cur]];
        if (lock[next] == true)
          choice[cur]++;
        else
          break;
      }

      if (choice[cur] == m) {
        ok = false;
        break;
      }

      int next = l[cur][choice[cur]];
      cnt[next].push_back(cur);
      if ((int)cnt[next].size() > i) {
        for (auto j : cnt[next]) {
          q.push(j);
          lock[next] = true;
        }
      }
    }

    if (ok) {
      printf("%d\n", i);
      break;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, m;
  scanf("%d %d", &n, &m);

  int l[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      scanf("%d", &l[i][j]);
      l[i][j]--;
    }

  for (int i = 1; i <= n; i++) {
    vector<int> cnt[m];
    int choice[n];
    fill(choice, choice + n, 0);
    bool lock[m];
    memset(lock, 0, sizeof(lock));

    queue<int> q;
    for (int j = 0; j < n; j++)
      q.push(j);

    bool ok = true;
    while (!q.empty()) {
      int cur = q.front();
      q.pop();

      while (choice[cur] < m) {
        int next = l[cur][choice[cur]];
        if (lock[next] == true)
          choice[cur]++;
        else
          break;
      }

      if (choice[cur] == m) {
        ok = false;
        break;
      }

      int next = l[cur][choice[cur]];
      cnt[next].push_back(cur);
      if ((int)cnt[next].size() > i) {
        for (auto j : cnt[next]) {
          q.push(j);
          lock[next] = true;
        }
      }
    }

    if (ok) {
      printf("%d\n", i);
      break;
    }
  }

  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()

using namespace std;

template <class T, class U> void amin(T &x, U y) {
  if (x > y)
    x = y;
}

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

  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>(M));
  REP(i, N) REP(j, M) {
    cin >> A[i][j];
    --A[i][j];
  }

  int result = N;
  vector<bool> is_open(N, true);
  int n_opens = N;
  while (n_opens >= 2) {
    vector<int> cnt(M);
    REP(i, N) {
      REP(j, M) {
        if (is_open[A[i][j]]) {
          cnt[A[i][j]]++;
          break;
        }
      }
    }
    int k = max_element(all(cnt)) - cnt.begin();
    amin(result, cnt[k]);
    is_open[k] = false;
    --n_opens;
  }

  cout << result << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()

using namespace std;

template <class T, class U> void amin(T &x, U y) {
  if (x > y)
    x = y;
}

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

  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(N, vector<int>(M));
  REP(i, N) REP(j, M) {
    cin >> A[i][j];
    --A[i][j];
  }

  int result = N;
  vector<bool> is_open(M, true);
  int n_opens = M;
  while (n_opens >= 2) {
    vector<int> cnt(M);
    REP(i, N) {
      REP(j, M) {
        if (is_open[A[i][j]]) {
          cnt[A[i][j]]++;
          break;
        }
      }
    }
    int k = max_element(all(cnt)) - cnt.begin();
    amin(result, cnt[k]);
    is_open[k] = false;
    --n_opens;
  }

  cout << result << endl;

  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

int main() {
  ll n, m, i, j, k;
  cin >> n >> m;
  vector<vector<ll>> a(n, vector<ll>(m));

  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  vector<ll> used(m + 1, 0), sport(m + 1, 0);
  for (i = 0; i < n; i++) {
    sport[a[i][0]]++;
  }
  ll ret = 0, now;
  for (i = 0; i < m + 1; i++) {
    ret = max(ret, sport[i]);
    if (ret == sport[i])
      now = i;
  }
  used[now] = 1;
  for (k = 0; k < m - 2; k++) {
    for (i = 0; i < m + 1; i++) {
      sport[i] = 0;
    }
    for (i = 0; i < n; i++) {
      j = 0;
      while (used[sport[a[i][j]]] == 1)
        j++;
      sport[a[i][j]]++;
    }
    ll tret = 0, now = 0;
    for (i = 0; i < m + 1; i++) {
      tret = max(tret, sport[i]);
      if (tret == sport[i])
        now = i;
    }
    ret = min(ret, tret);
    used[now] = 1;
  }
  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

int main() {
  ll n, m, i, j, k;
  cin >> n >> m;
  vector<vector<ll>> a(n, vector<ll>(m));

  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  vector<ll> used(m + 1, 0), sport(m + 1, 0);
  for (i = 0; i < n; i++) {
    sport[a[i][0]]++;
  }
  ll ret = 0, now;
  for (i = 0; i < m + 1; i++) {
    ret = max(ret, sport[i]);
    if (ret == sport[i])
      now = i;
  }
  used[now] = 1;
  for (k = 0; k < m - 2; k++) {
    for (i = 0; i < m + 1; i++) {
      sport[i] = 0;
    }
    for (i = 0; i < n; i++) {
      j = 0;
      while (used[a[i][j]] == 1)
        j++;
      sport[a[i][j]]++;
    }
    ll tret = 0, now = 0;
    for (i = 0; i < m + 1; i++) {
      tret = max(tret, sport[i]);
      if (tret == sport[i])
        now = i;
    }
    ret = min(ret, tret);
    used[now] = 1;
  }
  cout << ret << endl;
  return 0;
}
Problem p03652 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int N = 3e2 + 10;

int n, m, a[N][N];
int des[N], h[N], co[N];

bool check(int len) {
  memset(des, 0, sizeof(des));
  memset(h, 0, sizeof(h));
  memset(co, 0, sizeof(co));
  int rem = m;
  for (int i = 1; i <= n; i++)
    co[a[i][++h[i]]]++;
  while (rem) {
    int old = rem;
    for (int i = 1; i <= m; i++)
      if (co[i] > len) {
        des[i] = 1;
        for (int j = 1; j <= n; j++) {
          co[a[j][h[j]]]--;
          while (h[j] <= n && des[a[j][h[j]]])
            h[j]++;
          co[a[j][h[j]]]++;
        }
        rem--;
      }
    if (old == rem) {
      //            cout<<len<<'\n';
      //            for(int i=1;i<=m;i++) cout<<des[i]<<" \n"[i==m];
      return 1;
    }
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", a[i] + j);
  int L = 1, R = n;
  while (L <= R) {
    int mid = (L + R) / 2;
    if (check(mid))
      R = mid - 1;
    else
      L = mid + 1;
  }
  cout << L;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 3e2 + 10;

int n, m, a[N][N];
int des[N], h[N], co[N];

bool check(int len) {
  memset(des, 0, sizeof(des));
  memset(h, 0, sizeof(h));
  memset(co, 0, sizeof(co));
  int rem = m;
  for (int i = 1; i <= n; i++)
    co[a[i][++h[i]]]++;
  while (rem) {
    int old = rem;
    for (int i = 1; i <= m; i++)
      if (co[i] > len) {
        des[i] = 1;
        for (int j = 1; j <= n; j++) {
          co[a[j][h[j]]]--;
          while (h[j] <= m && des[a[j][h[j]]])
            h[j]++;
          co[a[j][h[j]]]++;
        }
        rem--;
      }
    if (old == rem) {
      //            cout<<len<<'\n';
      //            for(int i=1;i<=m;i++) cout<<des[i]<<" \n"[i==m];
      return 1;
    }
  }
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      scanf("%d", a[i] + j);
  int L = 1, R = n;
  while (L <= R) {
    int mid = (L + R) / 2;
    if (check(mid))
      R = mid - 1;
    else
      L = mid + 1;
  }
  cout << L;
}
Problem p03653 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;

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

inline int Read() {
  int x = 0, f = 1, c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}

const int MAXN = 100005;

int a[MAXN], b[MAXN], p[MAXN], c, x, y, z, n;
LL ret, ans, cur, d[MAXN], e[MAXN];
priority_queue<int, vector<int>, greater<int>> q;

int main() {
#ifdef wxh010910
  freopen("data.in", "r", stdin);
#endif
  x = Read(), y = Read(), z = Read(), n = x + y + z, ans = -(1LL << 60);
  for (int i = 1; i <= n; i++)
    a[i] = Read(), b[i] = Read(), c = Read(), ret += c, a[i] -= c, b[i] -= c,
    p[i] = i;
  sort(p + 1, p + n + 1,
       [&](const int &x, const int &y) { return a[x] - b[x] <= a[y] - b[y]; });
  for (int i = 1; i <= n; i++) {
    cur += b[p[i]];
    q.push(b[p[i]]);
    if (q.size() > y)
      cur -= q.top(), q.pop();
    if (q.size() == y)
      d[i] = cur;
  }
  while (!q.empty())
    q.pop();
  cur = 0;
  for (int i = n; i; i--) {
    cur += a[p[i]];
    q.push(a[p[i]]);
    if (q.size() > x)
      cur -= q.top(), q.pop();
    if (q.size() == x)
      e[i] = cur;
  }
  for (int i = y; i <= n - x; i++)
    ans = max(ans, d[i] + e[i + 1]);
  return printf("%lld\n", ans + ret), 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;

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

inline int Read() {
  int x = 0, f = 1, c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  return x * f;
}

const int MAXN = 100005;

int a[MAXN], b[MAXN], p[MAXN], c, x, y, z, n;
LL ret, ans, cur, d[MAXN], e[MAXN];
priority_queue<int, vector<int>, greater<int>> q;

int main() {
#ifdef wxh010910
  freopen("data.in", "r", stdin);
#endif
  x = Read(), y = Read(), z = Read(), n = x + y + z, ans = -(1LL << 60);
  for (int i = 1; i <= n; i++)
    a[i] = Read(), b[i] = Read(), c = Read(), ret += c, a[i] -= c, b[i] -= c,
    p[i] = i;
  sort(p + 1, p + n + 1,
       [&](const int &x, const int &y) { return a[x] - b[x] < a[y] - b[y]; });
  for (int i = 1; i <= n; i++) {
    cur += b[p[i]];
    q.push(b[p[i]]);
    if (q.size() > y)
      cur -= q.top(), q.pop();
    if (q.size() == y)
      d[i] = cur;
  }
  while (!q.empty())
    q.pop();
  cur = 0;
  for (int i = n; i; i--) {
    cur += a[p[i]];
    q.push(a[p[i]]);
    if (q.size() > x)
      cur -= q.top(), q.pop();
    if (q.size() == x)
      e[i] = cur;
  }
  for (int i = y; i <= n - x; i++)
    ans = max(ans, d[i] + e[i + 1]);
  return printf("%lld\n", ans + ret), 0;
}
Problem p03653 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define N 100005
#define mk(x, y) make_pair(x, y)
#define ft first
#define sd second
using namespace std;
int x, y, z;
ll ans;
int a[N], b[N], c[N], q[N], p[N], v[N];
bool cmp1(int x, int y) {
  if (b[x] != b[y])
    return b[x] > b[y];
  return c[x] < c[y];
}
bool cmp2(int x, int y) { return c[x] > c[y]; }
priority_queue<pair<int, int>> q1;
multiset<int> s;
multiset<int>::iterator it;
int main() {
  scanf("%d%d%d", &x, &y, &z);
  int n = x + y + z;
  ll now = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
    p[i] = q[i] = i;
    now += a[i];
    b[i] -= a[i];
    c[i] -= a[i];
  }
  sort(q + 1, q + n + 1, cmp1);
  sort(p + 1, p + n + 1, cmp2);
  for (int i = 1; i <= y; i++) {
    now += b[q[i]];
    q1.push(mk(c[q[i]] - b[q[i]], q[i]));
    v[q[i]] = 1;
  }
  int point = 1;
  for (int i = 1; i <= z; i++) {
    while (v[p[point]])
      point++;
    now += c[p[point]];
    s.insert(c[p[point]]);
    v[p[point]] = 1;
    point++;
  }
  ans = max(ans, now);
  for (int i = y + 1; i <= y + z; i++) {
    now += b[q[i]];
    q1.push(mk(c[q[i]] - b[q[i]], q[i]));
    s.insert(q1.top().ft + b[q1.top().sd]);
    now += q1.top().ft;
    q1.pop();
    if (v[q[i]]) {
      s.erase(s.find(c[q[i]]));
      now -= c[q[i]];
    } else {
      it = s.begin();
      now -= (*it);
      s.erase(it);
    }
    ans = max(ans, now);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define N 100005
#define mk(x, y) make_pair(x, y)
#define ft first
#define sd second
using namespace std;
int x, y, z;
ll ans;
int a[N], b[N], c[N], q[N], p[N], v[N];
bool cmp1(int x, int y) {
  if (b[x] != b[y])
    return b[x] > b[y];
  return c[x] < c[y];
}
bool cmp2(int x, int y) { return c[x] > c[y]; }
priority_queue<pair<int, int>> q1;
multiset<int> s;
multiset<int>::iterator it;
int main() {
  scanf("%d%d%d", &x, &y, &z);
  int n = x + y + z;
  ll now = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
    p[i] = q[i] = i;
    now += a[i];
    b[i] -= a[i];
    c[i] -= a[i];
  }
  sort(q + 1, q + n + 1, cmp1);
  sort(p + 1, p + n + 1, cmp2);
  for (int i = 1; i <= y; i++) {
    now += b[q[i]];
    q1.push(mk(c[q[i]] - b[q[i]], q[i]));
    v[q[i]] = 1;
  }
  int point = 1;
  for (int i = 1; i <= z; i++) {
    while (v[p[point]])
      point++;
    now += c[p[point]];
    s.insert(c[p[point]]);
    v[p[point]] = 1;
    point++;
  }
  ans = max(ans, now);
  for (int i = y + 1; i <= y + z; i++) {
    now += b[q[i]];
    q1.push(mk(c[q[i]] - b[q[i]], q[i]));
    s.insert(q1.top().ft + b[q1.top().sd]);
    now += q1.top().ft;
    q1.pop();
    if (v[q[i]] && s.find(c[q[i]]) != s.end()) {
      s.erase(s.find(c[q[i]]));
      now -= c[q[i]];
    } else {
      it = s.begin();
      now -= (*it);
      s.erase(it);
    }
    ans = max(ans, now);
  }
  cout << ans << endl;
  return 0;
}
Problem p03653 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int x, y, z;
  cin >> x >> y >> z;
  int n = x + y + z;
  vector<long long> a(n), b(n), c(n);
  vector<int> v;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    sum += a[i];
    b[i] -= a[i], c[i] -= a[i];
    v.emplace_back(i);
  }
  auto cmp = [&b, &c](int i, int j) {
    if (b[i] - c[i] != b[j] - c[j])
      return b[i] - c[i] > b[j] - c[j];
    else
      b[i] > b[j];
  };
  sort(v.begin(), v.end(), cmp);
  vector<long long> pre_b(n, 0), suf_c(n, 0);
  auto calc = [&v](int st, int en, int inc, int sz, auto &val, auto &w) {
    multiset<long long> ms;
    long long sum = 0;
    for (int i = st; i != en; i += inc) {
      sum += val[v[i]];
      ms.insert(val[v[i]]);
      if (ms.size() > sz) {
        sum -= *ms.begin();
        ms.erase(ms.begin());
      }
      w[i] = sum;
    }
  };
  calc(0, n, 1, y, b, pre_b);
  calc(n - 1, -1, -1, z, c, suf_c);
  long long ans = 0;
  for (int i = y - 1; i < n - z; i++) {
    ans = max(ans, sum + pre_b[i] + suf_c[i + 1]);
  }
  cout << ans << "\n";
  return 0;
}

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

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int x, y, z;
  cin >> x >> y >> z;
  int n = x + y + z;
  vector<long long> a(n), b(n), c(n);
  vector<int> v;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    sum += a[i];
    b[i] -= a[i], c[i] -= a[i];
    v.emplace_back(i);
  }
  auto cmp = [&b, &c](int i, int j) { return b[i] - c[i] > b[j] - c[j]; };
  sort(v.begin(), v.end(), cmp);
  vector<long long> pre_b(n, 0), suf_c(n, 0);
  auto calc = [&v](int st, int en, int inc, int sz, auto &val, auto &w) {
    multiset<long long> ms;
    long long sum = 0;
    for (int i = st; i != en; i += inc) {
      sum += val[v[i]];
      ms.insert(val[v[i]]);
      if (ms.size() > sz) {
        sum -= *ms.begin();
        ms.erase(ms.begin());
      }
      w[i] = sum;
    }
  };
  calc(0, n, 1, y, b, pre_b);
  calc(n - 1, -1, -1, z, c, suf_c);
  long long ans = 0;
  for (int i = y - 1; i < n - z; i++) {
    ans = max(ans, sum + pre_b[i] + suf_c[i + 1]);
  }
  cout << ans << "\n";
  return 0;
}
Problem p03653 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 300010;

int X, Y, Z;
int A[N], B[N], C[N];
int n;
long long ans;
priority_queue<int, vector<int>, greater<int>> q;
vector<int> V;

bool cmp(int u, int v) { return A[u] + B[v] >= A[v] + B[u]; }

long long l[N], r[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> X >> Y >> Z;
  n = X + Y + Z;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i] >> B[i] >> C[i];
    ans += C[i];
    V.push_back(i);
  }

  sort(V.begin(), V.end(), cmp);
  for (int i = 1; i <= n; ++i)
    A[i] -= C[i], B[i] -= C[i];

  long long cur = 0;
  l[0] = -1e18;
  for (int i = 1; i <= n; ++i) {
    int p = V[i - 1];
    q.push(A[p]);
    cur += A[p];
    if ((int)q.size() > X) {
      int pa = q.top();
      q.pop();
      cur -= pa;
    }
    l[i] = (int)q.size() >= X ? cur : -1e18;
  }

  cur = 0;
  r[n + 1] = -1e18;
  q = priority_queue<int, vector<int>, greater<int>>();

  for (int i = n; i >= 1; --i) {
    int p = V[i - 1];
    q.push(B[p]);
    cur += B[p];
    if ((int)q.size() > Y) {
      int pb = q.top();
      q.pop();
      cur -= pb;
    }
    r[i] = (int)q.size() >= Y ? cur : -1e18;
  }

  long long res = -1e18;
  for (int i = 0; i <= n; ++i) {
    res = max(res, l[i] + r[i + 1]);
  }

  cout << ans + res << endl;
}

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

const int N = 300010;

int X, Y, Z;
int A[N], B[N], C[N];
int n;
long long ans;
priority_queue<int, vector<int>, greater<int>> q;
vector<int> V;

bool cmp(int u, int v) {
  return (A[u] + B[v] > A[v] + B[u] || (A[u] + B[v] == A[v] + B[u] && u < v));
}

long long l[N], r[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> X >> Y >> Z;
  n = X + Y + Z;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i] >> B[i] >> C[i];
    ans += C[i];
    V.push_back(i);
  }

  sort(V.begin(), V.end(), cmp);
  for (int i = 1; i <= n; ++i)
    A[i] -= C[i], B[i] -= C[i];

  long long cur = 0;
  l[0] = -1e18;
  for (int i = 1; i <= n; ++i) {
    int p = V[i - 1];
    q.push(A[p]);
    cur += A[p];
    if ((int)q.size() > X) {
      int pa = q.top();
      q.pop();
      cur -= pa;
    }
    l[i] = (int)q.size() >= X ? cur : -1e18;
  }

  cur = 0;
  r[n + 1] = -1e18;
  q = priority_queue<int, vector<int>, greater<int>>();

  for (int i = n; i >= 1; --i) {
    int p = V[i - 1];
    q.push(B[p]);
    cur += B[p];
    if ((int)q.size() > Y) {
      int pb = q.top();
      q.pop();
      cur -= pb;
    }
    r[i] = (int)q.size() >= Y ? cur : -1e18;
  }

  long long res = -1e18;
  for (int i = 0; i <= n; ++i) {
    res = max(res, l[i] + r[i + 1]);
  }

  cout << ans + res << endl;
}
Problem p03653 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back

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

int X, Y, Z;
const int maxn = 2e5 + 5;
int a[maxn], b[maxn], c[maxn];

bool cmp(int i, int j) { return a[i] - b[i] <= a[j] - b[j]; }

int id[maxn];
ll pre[maxn], suf[maxn];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> X >> Y >> Z;
  long long Ans = 0;
  for (int i = 1; i <= X + Y + Z; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i] -= c[i];
    b[i] -= c[i];
    Ans += c[i];
    id[i] = i;
  }
  int n = X + Y + Z;
  sort(id + 1, id + 1 + n, cmp);
  priority_queue<int, vector<int>, greater<int>> pq;
  ll sum = 0;
  pre[0] = -1e18;
  for (int i = 1; i <= n; i++) {
    int j = id[i];
    pq.push(b[j]);
    sum += b[j];
    if (pq.size() > Y) {
      sum -= pq.top();
      pq.pop();
    }
    if (pq.size() < Y) {
      pre[i] = -1e18;
    } else
      pre[i] = sum;
  }
  while (!pq.empty())
    pq.pop();
  sum = 0;
  suf[n + 1] = -1e18;
  for (int i = n; i >= 1; i--) {
    int j = id[i];
    pq.push(a[j]);
    sum += a[j];
    if (pq.size() > X) {
      sum -= pq.top();
      pq.pop();
    }
    if (pq.size() < X) {
      suf[i] = -1e18;
    } else
      suf[i] = sum;
  }
  ll ans = -2e18;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, pre[i] + suf[i + 1]);
  }
  cout << ans + Ans << endl;
  return 0;
}


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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back

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

int X, Y, Z;
const int maxn = 2e5 + 5;
int a[maxn], b[maxn], c[maxn];

bool cmp(int i, int j) { return a[i] - b[i] < a[j] - b[j]; }

int id[maxn];
ll pre[maxn], suf[maxn];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> X >> Y >> Z;
  long long Ans = 0;
  for (int i = 1; i <= X + Y + Z; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i] -= c[i];
    b[i] -= c[i];
    Ans += c[i];
    id[i] = i;
  }
  int n = X + Y + Z;
  sort(id + 1, id + 1 + n, cmp);
  priority_queue<int, vector<int>, greater<int>> pq;
  ll sum = 0;
  pre[0] = -1e18;
  for (int i = 1; i <= n; i++) {
    int j = id[i];
    pq.push(b[j]);
    sum += b[j];
    if (pq.size() > Y) {
      sum -= pq.top();
      pq.pop();
    }
    if (pq.size() < Y) {
      pre[i] = -1e18;
    } else
      pre[i] = sum;
  }
  while (!pq.empty())
    pq.pop();
  sum = 0;
  suf[n + 1] = -1e18;
  for (int i = n; i >= 1; i--) {
    int j = id[i];
    pq.push(a[j]);
    sum += a[j];
    if (pq.size() > X) {
      sum -= pq.top();
      pq.pop();
    }
    if (pq.size() < X) {
      suf[i] = -1e18;
    } else
      suf[i] = sum;
  }
  ll ans = -2e18;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, pre[i] + suf[i + 1]);
  }
  cout << ans + Ans << endl;
  return 0;
}
Problem p03653 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int x, y, z, n, i, c;
long long s, sum, dp[100010], ma;
priority_queue<int, vector<int>, greater<int>> q;
pair<int, int> v[100010];
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.first - a.second < b.first - b.second)
    return false;
  return true;
}
int main() {
  // freopen("a.in","r",stdin);
  //  freopen("a.out","w",stdout);
  scanf("%d%d%d", &x, &y, &z);
  n = x + y + z;
  for (i = 1; i <= n; ++i) {
    scanf("%d%d%d", &v[i].first, &v[i].second, &c);
    v[i].first -= c;
    v[i].second -= c;
    s += c;
  }
  sort(v + 1, v + n + 1, cmp);
  for (i = n; i >= n - y + 1; --i) {
    sum += v[i].second;
    q.push(v[i].second);
  }
  dp[n - y + 1] = sum;
  for (i = n - y; i >= 1; --i) {
    if (v[i].second > q.top()) {
      sum -= q.top();
      q.pop();
      q.push(v[i].second);
      sum += v[i].second;
    }
    dp[i] = sum;
  }
  while (!q.empty())
    q.pop();
  sum = 0;
  for (i = 1; i <= x; ++i) {
    sum += v[i].first;
    q.push(v[i].first);
  }
  ma = sum + dp[x + 1];
  for (i = x + 1; i <= n - y; ++i) {
    if (v[i].first > q.top()) {
      sum -= q.top();
      q.pop();
      q.push(v[i].first);
      sum += v[i].first;
    }
    ma = max(ma, sum + dp[i + 1]);
  }
  printf("%lld\n", ma + s);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int x, y, z, n, i, c;
long long s, sum, dp[100010], ma;
priority_queue<int, vector<int>, greater<int>> q;
pair<int, int> v[100010];
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.first - a.second < b.first - b.second)
    return false;
  else if (a.first - a.second == b.first - b.second)
    return (a.first > a.second);
  return true;
}
int main() {
  // freopen("a.in","r",stdin);
  //  freopen("a.out","w",stdout);
  scanf("%d%d%d", &x, &y, &z);
  n = x + y + z;
  for (i = 1; i <= n; ++i) {
    scanf("%d%d%d", &v[i].first, &v[i].second, &c);
    v[i].first -= c;
    v[i].second -= c;
    s += c;
  }
  sort(v + 1, v + n + 1, cmp);
  for (i = n; i >= n - y + 1; --i) {
    sum += v[i].second;
    q.push(v[i].second);
  }
  dp[n - y + 1] = sum;
  for (i = n - y; i >= 1; --i) {
    if (v[i].second > q.top()) {
      sum -= q.top();
      q.pop();
      q.push(v[i].second);
      sum += v[i].second;
    }
    dp[i] = sum;
  }
  while (!q.empty())
    q.pop();
  sum = 0;
  for (i = 1; i <= x; ++i) {
    sum += v[i].first;
    q.push(v[i].first);
  }
  ma = sum + dp[x + 1];
  for (i = x + 1; i <= n - y; ++i) {
    if (v[i].first > q.top()) {
      sum -= q.top();
      q.pop();
      q.push(v[i].first);
      sum += v[i].first;
    }
    ma = max(ma, sum + dp[i + 1]);
  }
  printf("%lld\n", ma + s);
  return 0;
}
Problem p03653 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int mn = 1e5 + 10;
typedef long long ll;
int x, y, z;
struct node {
  ll a, b, c;
} d[mn];
ll sc, ans, l[mn], r[mn];
struct cmp {
  bool operator()(ll x, ll y) { return x > y; }
};
priority_queue<ll, vector<ll>, cmp> q, p;
bool cmp(node x, node y) { return x.a - x.b <= y.a - y.b; }
int main() {
  scanf("%d %d %d", &x, &y, &z);
  int n = x + y + z;
  for (int i = 1; i <= n; i++)
    scanf("%lld %lld %lld", &d[i].a, &d[i].b, &d[i].c);
  sort(d + 1, d + (n + 1), cmp);
  sc = 0;
  for (int i = 1; i <= n; i++) {
    d[i].a -= d[i].c;
    d[i].b -= d[i].c;
    sc += d[i].c;
  }
  l[0] = 0;
  int t;
  for (int i = 1; i <= y; i++) {
    t = d[i].b;
    q.push(t);
    l[i] = l[i - 1] + t;
  }
  for (int i = y + 1; i <= n; i++) {
    if (d[i].b <= q.top()) {
      l[i] = l[i - 1];
      continue;
    }
    t = d[i].b;
    q.push(t);
    l[i] = l[i - 1] + t;
    l[i] -= q.top();
    q.pop();
  }
  r[x + y + z + 1] = 0;
  for (int i = n; i > y + z; i--) {
    t = d[i].a;
    p.push(t);
    r[i] = r[i + 1] + t;
  }
  for (int i = y + z; i >= 1; i--) {
    if (d[i].a <= p.top()) {
      r[i] = r[i + 1];
      continue;
    }
    t = d[i].a;
    p.push(t);
    r[i] = r[i + 1] + t;
    r[i] -= p.top();
    p.pop();
  }
  for (int i = y; i <= n - x; i++)
    if (i == y)
      ans = l[i] + r[i + 1];
    else
      ans = max(ans, l[i] + r[i + 1]);
  printf("%lld\n", ans + sc);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int mn = 1e5 + 10;
typedef long long ll;
int x, y, z;
struct node {
  ll a, b, c;
} d[mn];
ll sc, ans, l[mn], r[mn];
struct cmp {
  bool operator()(ll x, ll y) { return x > y; }
};
priority_queue<ll, vector<ll>, cmp> q, p;
bool cmp(node x, node y) { return x.a - x.b < y.a - y.b; }
int main() {
  scanf("%d %d %d", &x, &y, &z);
  int n = x + y + z;
  for (int i = 1; i <= n; i++)
    scanf("%lld %lld %lld", &d[i].a, &d[i].b, &d[i].c);
  sort(d + 1, d + (n + 1), cmp);
  sc = 0;
  for (int i = 1; i <= n; i++) {
    d[i].a -= d[i].c;
    d[i].b -= d[i].c;
    sc += d[i].c;
  }
  l[0] = 0;
  int t;
  for (int i = 1; i <= y; i++) {
    t = d[i].b;
    q.push(t);
    l[i] = l[i - 1] + t;
  }
  for (int i = y + 1; i <= n; i++) {
    if (d[i].b <= q.top()) {
      l[i] = l[i - 1];
      continue;
    }
    t = d[i].b;
    q.push(t);
    l[i] = l[i - 1] + t;
    l[i] -= q.top();
    q.pop();
  }
  r[x + y + z + 1] = 0;
  for (int i = n; i > y + z; i--) {
    t = d[i].a;
    p.push(t);
    r[i] = r[i + 1] + t;
  }
  for (int i = y + z; i >= 1; i--) {
    if (d[i].a <= p.top()) {
      r[i] = r[i + 1];
      continue;
    }
    t = d[i].a;
    p.push(t);
    r[i] = r[i + 1] + t;
    r[i] -= p.top();
    p.pop();
  }
  for (int i = y; i <= n - x; i++)
    if (i == y)
      ans = l[i] + r[i + 1];
    else
      ans = max(ans, l[i] + r[i + 1]);
  printf("%lld\n", ans + sc);
  return 0;
}