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

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

// UnionFind
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {} // each node has "minus-size"
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (-d[x] < -d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
  int component() {
    set<int> S;
    rep(x, d.size()) S.insert(find(x));
    return S.size();
  }
};

ll comb2(ll n) { return n * (n - 1) / 2; }

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> v;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    v.push_back(make_pair(a, b));
  }
  ll S[m + 1];
  rep(i, m + 1) S[i] = 0;
  S[m] = comb2(n);

  UnionFind un(n);
  for (int i = m - 1; i >= 0; i--) {
    int a = v[i].first;
    int b = v[i].second;
    if (un.same(a, b)) {
      S[i] = S[i + 1];
      continue;
    }

    int x = un.size(a);
    int y = un.size(b);
    un.unite(a, b);
    int z = un.size(a);
    S[i] = S[i + 1] + comb2(x) + comb2(y) - comb2(z);
  }

  for (int i = 1; i <= m; i++) {
    cout << S[i] << endl;
  }

  return 0;
}


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

// UnionFind
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {} // each node has "minus-size"
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (-d[x] < -d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
  int component() {
    set<int> S;
    rep(x, d.size()) S.insert(find(x));
    return S.size();
  }
};

ll comb2(ll n) { return n * (n - 1) / 2; }

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> v;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v.push_back(make_pair(a, b));
  }
  ll S[m + 1];
  rep(i, m + 1) S[i] = 0;
  S[m] = comb2(n);

  UnionFind un(n);
  for (int i = m - 1; i >= 0; i--) {
    int a = v[i].first;
    int b = v[i].second;
    if (un.same(a, b)) {
      S[i] = S[i + 1];
      continue;
    }

    int x = un.size(a);
    int y = un.size(b);
    un.unite(a, b);
    int z = un.size(a);
    S[i] = S[i + 1] + comb2(x) + comb2(y) - comb2(z);
  }

  for (int i = 1; i <= m; i++) {
    cout << S[i] << endl;
  }

  return 0;
}
Problem p03108 (Python)

Buggy submission (status: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import sys

sys.setrecursionlimit(10**7)


class UnionFind:
    def __init__(self, n):
        # parent[x] < 0 means x is root and abs(parent[x]) == size[x]
        self.parent, self.depth = [-1] * (n + 1), [0] * (n + 1)

    def find(self, x):
        if self.parent[x] < 0:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def isSame(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x, y = self.find(x), self.find(y)

        if self.depth[x] > self.depth[y]:
            self.parent[x] += self.parent[y]
            self.parent[y] = x
        else:
            self.parent[y] += self.parent[x]
            self.parent[x] = y
            if self.depth[x] == self.depth[y]:
                self.depth[y] += 1

    def count(self, x):
        return -self.parent[self.find(x)]


N, M = map(int, input().split())

AB = [tuple(map(int, input().split())) for _ in range(M)]
AB.reverse()

groups = UnionFind(N)
ans = [N * (N - 1) // 2]
for i in range(M):
    if groups.isSame(AB[i][0], AB[i][1]):
        ans.append(ans[-1])
    else:
        ans.append(ans[-1] - groups.count(AB[i][0]) * groups.count(AB[i][1]))
        groups.unite(AB[i][0], AB[i][1])

for i in range(M):
    print(ans[::-1][i + 1])


Fixed submission:
# -*- coding: utf-8 -*-

import sys

sys.setrecursionlimit(10**7)


class UnionFind:
    def __init__(self, n):
        # parent[x] < 0 means x is root and abs(parent[x]) == size[x]
        self.parent, self.depth = [-1] * (n + 1), [0] * (n + 1)

    def find(self, x):
        if self.parent[x] < 0:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def isSame(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x, y = self.find(x), self.find(y)

        if self.depth[x] > self.depth[y]:
            self.parent[x] += self.parent[y]
            self.parent[y] = x
        else:
            self.parent[y] += self.parent[x]
            self.parent[x] = y
            if self.depth[x] == self.depth[y]:
                self.depth[y] += 1

    def count(self, x):
        return -self.parent[self.find(x)]


N, M = map(int, input().split())

AB = [tuple(map(int, input().split())) for _ in range(M)]
AB.reverse()

groups = UnionFind(N)
ans = [N * (N - 1) // 2]
for i in range(M):
    if groups.isSame(AB[i][0], AB[i][1]):
        ans.append(ans[-1])
    else:
        ans.append(ans[-1] - groups.count(AB[i][0]) * groups.count(AB[i][1]))
        groups.unite(AB[i][0], AB[i][1])

for i in range(M):
    print(ans[M - 1 - i])
Problem p03108 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<long long> dsu(100005), cnt(100005, 1);
long long parent(long long x) {
  while (dsu[x] != x) {
    x = dsu[x];
  }
  return x;
}
int main() {

  srand(42);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  long long n, m, a1, r1, r2;
  cin >> n >> m;
  vector<pair<long long, long long>> edges;
  for (a1 = 1; a1 <= m; ++a1) {
    dsu[a1] = a1;
    cin >> r1 >> r2;
    edges.push_back(make_pair(r1, r2));
  }
  long long ans = (n * (n - 1)) / 2;
  vector<long long> res;
  for (a1 = m - 1; a1 > -1; --a1) {
    res.push_back(ans);
    long long p1 = parent(edges[a1].first), p2 = parent(edges[a1].second);
    if (p1 != p2 && rand() & 1) {
      ans -= (cnt[p1] * cnt[p2]);
      dsu[p1] = p2;
      cnt[p2] += cnt[p1];
    } else if (p1 != p2) {
      ans -= (cnt[p1] * cnt[p2]);
      dsu[p2] = p1;
      cnt[p1] += cnt[p2];
    }
    // cout<<ans<<"\n";
  }
  for (a1 = m - 1; a1 > -1; --a1)
    cout << res[a1] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<long long> dsu(100005), cnt(100005, 1);
long long parent(long long x) {
  while (dsu[x] != x) {
    dsu[x] = dsu[dsu[x]];
    x = dsu[x];
  }
  return x;
}
int main() {

  srand(42);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  long long n, m, a1, r1, r2;
  cin >> n >> m;
  vector<pair<long long, long long>> edges;
  for (a1 = 1; a1 <= m; ++a1) {
    dsu[a1] = a1;
    cin >> r1 >> r2;
    edges.push_back(make_pair(r1, r2));
  }
  long long ans = (n * (n - 1)) / 2;
  vector<long long> res;
  for (a1 = m - 1; a1 > -1; --a1) {
    res.push_back(ans);
    long long p1 = parent(edges[a1].first), p2 = parent(edges[a1].second);
    if (p1 != p2 && rand() & 1) {
      ans -= (cnt[p1] * cnt[p2]);
      dsu[p1] = p2;
      cnt[p2] += cnt[p1];
    } else if (p1 != p2) {
      ans -= (cnt[p1] * cnt[p2]);
      dsu[p2] = p1;
      cnt[p1] += cnt[p2];
    }
    // cout<<ans<<"\n";
  }
  for (a1 = m - 1; a1 > -1; --a1)
    cout << res[a1] << "\n";
  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rp(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
class UnionFind {
public:
  // 親の番号を格納する。親だった場合は-(その集合のサイズ)
  vector<int> Parent;

  // 作るときはParentの値を全て−1にする
  // こうすると全てバラバラになる
  UnionFind(int N) { Parent = vector<int>(N, -1); }

  // Aがどのグループに属しているか調べる
  int root(int A) {
    if (Parent[A] < 0)
      return A;
    return Parent[A] = root(Parent[A]);
  }

  // 自分のいるグループの頂点数を調べる
  int size(int A) {
    return -Parent[root(A)]; // 親をとってきたい
  }

  // AとBをくっつける
  bool connect(int A, int B) {
    // AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
    A = root(A);
    B = root(B);
    if (A == B) {
      return false;
    }

    if (size(A) < size(B))
      swap(A, B);

    Parent[A] += Parent[B];
    Parent[B] = A;
    return true;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  rp(i, m) { cin >> a.at(i) >> b.at(i); }
  UnionFind uni(n);
  vector<ll> ans(m, 0ll);
  ans.at(m - 1) = (ll)n * (n - 1) / 2;
  for (int i = m - 1; i >= 1; i--) {
    ans[i - 1] = ans[i];
    int as = uni.size(a[i]), bs = uni.size(b[i]);
    if (uni.connect(a[i], b[i])) {
      // cout << "here" << endl;
      ans[i - 1] -= (ll)as * bs;
    }
  }
  rp(i, m) cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rp(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
class UnionFind {
public:
  // 親の番号を格納する。親だった場合は-(その集合のサイズ)
  vector<int> Parent;

  // 作るときはParentの値を全て−1にする
  // こうすると全てバラバラになる
  UnionFind(int N) { Parent = vector<int>(N, -1); }

  // Aがどのグループに属しているか調べる
  int root(int A) {
    if (Parent[A] < 0)
      return A;
    return Parent[A] = root(Parent[A]);
  }

  // 自分のいるグループの頂点数を調べる
  int size(int A) {
    return -Parent[root(A)]; // 親をとってきたい
  }

  // AとBをくっつける
  bool connect(int A, int B) {
    // AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
    A = root(A);
    B = root(B);
    if (A == B) {
      return false;
    }

    if (size(A) < size(B))
      swap(A, B);

    Parent[A] += Parent[B];
    Parent[B] = A;
    return true;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m);
  rp(i, m) {
    cin >> a.at(i) >> b.at(i);
    a.at(i)--;
    b.at(i)--;
  }
  UnionFind uni(n);
  vector<ll> ans(m, 0ll);
  ans.at(m - 1) = (ll)n * (n - 1) / 2;
  for (int i = m - 1; i >= 1; i--) {
    ans[i - 1] = ans[i];
    int as = uni.size(a[i]), bs = uni.size(b[i]);
    if (uni.connect(a[i], b[i])) {
      // cout << "here" << endl;
      ans[i - 1] -= (ll)as * bs;
    }
  }
  rp(i, m) cout << ans[i] << endl;
  return 0;
}
Problem p03108 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define pb(a) push_back(a)
using namespace std;
typedef long long LL;
#define int long long
const LL INF = 999999999999;

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

signed main() {
  LL n, m;
  cin >> n >> m;

  UnionFind UF(n);
  vector<LL> ans(m, 0);
  ans[m - 1] = n * (n - 1) / 2;
  vector<LL> a(m), b(m);
  rep(i, m) cin >> a[i] >> b[i];
  for (int i = m - 1; i > 0; i--) {
    if (UF.find(a[i]) == UF.find(b[i])) {
      ans[i - 1] = ans[i];
      continue;
    }
    LL tmp_a = UF.size(a[i]);
    tmp_a = tmp_a * (tmp_a - 1) / 2;
    LL tmp_b = UF.size(b[i]);
    tmp_b = tmp_b * (tmp_b - 1) / 2;
    UF.unite(a[i], b[i]);
    LL tmp_mg = UF.size(a[i]);
    tmp_mg = tmp_mg * (tmp_mg - 1) / 2;
    ans[i - 1] = max(0LL, ans[i] - (tmp_mg - tmp_a - tmp_b));
  }
  rep(i, m) cout << ans[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define pb(a) push_back(a)
using namespace std;
typedef long long LL;
#define int long long
const LL INF = 999999999999;

struct UnionFind {
  vector<int> data;

  UnionFind(int sz) { data.assign(sz, -1); }

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k) { return (-data[find(k)]); }
};

signed main() {
  LL n, m;
  cin >> n >> m;

  UnionFind UF(n + 1);
  vector<LL> ans(m, 0);
  ans[m - 1] = n * (n - 1) / 2;
  vector<LL> a(m), b(m);
  rep(i, m) cin >> a[i] >> b[i];
  for (int i = m - 1; i > 0; i--) {
    if (UF.find(a[i]) == UF.find(b[i])) {
      ans[i - 1] = ans[i];
      continue;
    }
    LL tmp_a = UF.size(a[i]);
    tmp_a = tmp_a * (tmp_a - 1) / 2;
    LL tmp_b = UF.size(b[i]);
    tmp_b = tmp_b * (tmp_b - 1) / 2;
    UF.unite(a[i], b[i]);
    LL tmp_mg = UF.size(a[i]);
    tmp_mg = tmp_mg * (tmp_mg - 1) / 2;
    ans[i - 1] = max(0LL, ans[i] - (tmp_mg - tmp_a - tmp_b));
  }
  rep(i, m) cout << ans[i] << endl;
}
Problem p03108 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python3
import sys

try:
    from typing import List
except ImportError:
    pass


def solve(N: int, M: int, A: "List[int]", B: "List[int]"):
    uf = list(range(N))
    n = [1] * N

    def getpar(a: int):
        if a == uf[a]:
            return a
        uf[a] = getpar(uf[a])
        return uf[a]

    def union(a: int, b: int):
        pa = getpar(a)
        pb = getpar(b)
        if pa == pb:
            return 0, 0
        ra = n[pa]
        rb = n[pb]
        n[pa] += n[pb]
        uf[pb] = uf[pa]
        return ra, rb

    k = N * (N - 1) // 2
    ans = []
    A.reverse()
    B.reverse()
    for Ai, Bi in zip(A, B):
        Ai -= 1
        Bi -= 1
        ra, rb = union(Ai, Bi)
        ans.append(k)
        k -= ra * rb

    assert k == 0, k
    for ansi in reversed(ans):
        print(ansi)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int()] * (M)  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, M, A, B)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
import sys

try:
    from typing import List
except ImportError:
    pass


sys.setrecursionlimit(1000000)


def solve(N: int, M: int, A: "List[int]", B: "List[int]"):
    uf = list(range(N))
    n = [1] * N

    def getpar(a: int):
        if a == uf[a]:
            return a
        uf[a] = getpar(uf[a])
        return uf[a]

    def union(a: int, b: int):
        pa = getpar(a)
        pb = getpar(b)
        if pa == pb:
            return 0, 0
        ra = n[pa]
        rb = n[pb]
        n[pa] += n[pb]
        uf[pb] = uf[pa]
        return ra, rb

    k = N * (N - 1) // 2
    ans = []
    A.reverse()
    B.reverse()
    for Ai, Bi in zip(A, B):
        Ai -= 1
        Bi -= 1
        ra, rb = union(Ai, Bi)
        ans.append(k)
        k -= ra * rb

    assert k == 0, k
    for ansi in reversed(ans):
        print(ansi)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int()] * (M)  # type: "List[int]"
    B = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, M, A, B)


if __name__ == "__main__":
    main()
Problem p03108 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-

from scipy.misc import comb
import sys

sys.setrecursionlimit(1000000000)


class UnionFind:
    def __init__(self, n):
        # parent[x] < 0 means x is root and abs(parent[x]) == size[x]
        self.parent, self.depth = [-1] * (n + 1), [0] * (n + 1)

    def find(self, x):
        if self.parent[x] < 0:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def isSame(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x, y = self.find(x), self.find(y)

        if self.depth[x] > self.depth[y]:
            self.parent[x] += self.parent[y]
            self.parent[y] = x
        else:
            self.parent[y] += self.parent[x]
            self.parent[x] = y
            if self.depth[x] == self.depth[y]:
                self.depth[y] += 1

    def count(self, x):
        return -self.parent[self.find(x)]


N, M = map(int, input().split())

AB = [tuple(map(int, input().split())) for _ in range(M)]
AB.reverse()

groups = UnionFind(N)
ans = [comb(N, 2, exact=True)]
for i in range(M):
    if groups.isSame(AB[i][0], AB[i][1]):
        ans.append(ans)
    else:
        ans.append(ans[-1] - groups.count(AB[i][0]) * groups.count(AB[i][1]))
        groups.unite(AB[i][0], AB[i][1])

for i in range(M):
    print(ans[-(i + 2)])


Fixed submission:
# -*- coding: utf-8 -*-

from scipy.misc import comb
import sys

sys.setrecursionlimit(1000000000)


class UnionFind:
    def __init__(self, n):
        # parent[x] < 0 means x is root and abs(parent[x]) == size[x]
        self.parent, self.depth = [-1] * (n + 1), [0] * (n + 1)

    def find(self, x):
        if self.parent[x] < 0:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def isSame(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x, y = self.find(x), self.find(y)

        if self.depth[x] > self.depth[y]:
            self.parent[x] += self.parent[y]
            self.parent[y] = x
        else:
            self.parent[y] += self.parent[x]
            self.parent[x] = y
            if self.depth[x] == self.depth[y]:
                self.depth[y] += 1

    def count(self, x):
        return -self.parent[self.find(x)]


N, M = map(int, input().split())

AB = [tuple(map(int, input().split())) for _ in range(M)]
AB.reverse()

groups = UnionFind(N)
ans = [comb(N, 2, exact=True)]
for i in range(M):
    if groups.isSame(AB[i][0], AB[i][1]):
        ans.append(ans[-1])
    else:
        ans.append(ans[-1] - groups.count(AB[i][0]) * groups.count(AB[i][1]))
        groups.unite(AB[i][0], AB[i][1])

for i in range(M):
    print(ans[-(i + 2)])
Problem p03108 (Python)

Buggy submission (status: Runtime Error):
from collections import deque, Counter


class UnionFind:
    def __init__(self, v):
        self.v = v
        self._tree = list(range(v + 1))
        self._counter = Counter(self._tree)

    def _root(self, a):
        queue = deque()
        while self._tree[a] != a:
            queue.append(a)
            a = self._tree[a]
        while queue:
            index = queue.popleft()
            self._tree[index] = a
        return a

    def union(self, a, b):
        root_a = self._root(a)
        root_b = self._root(b)
        if root_a == root_b:
            return

        self._tree[root_b] = root_a
        ca = self._counter[root_a]
        cb = self._counter[root_b]
        self._counter[root_a] += cb
        self._counter[root_b] = 0
        return ca * cb

    def find(self, a, b):
        return self._root(a) == self._root(b)


N, M = map(int, input().split(" "))
edges = [tuple(map(int, input().split(" "))) for _ in range(M)]

dp = [0] * (M + 1)
dp[-1] = N * (N - 1) // 2
union_find = UnionFind(N)

for i in range(M - 1, 0, -1):
    if dp[i + 1] == 0:
        break
    a, b = edges[i]
    minus = union_find.union(a, b)
    dp[i] = dp[i + 1] - minus

for i in range(M):
    print(dp[i + 1])


Fixed submission:
from collections import deque, Counter


class UnionFind:
    def __init__(self, v):
        self.v = v
        self._tree = list(range(v + 1))
        self._counter = Counter(self._tree)

    def _root(self, a):
        queue = deque()
        while self._tree[a] != a:
            queue.append(a)
            a = self._tree[a]
        while queue:
            index = queue.popleft()
            self._tree[index] = a
        return a

    def union(self, a, b):
        root_a = self._root(a)
        root_b = self._root(b)
        if root_a == root_b:
            return 0

        self._tree[root_b] = root_a
        ca = self._counter[root_a]
        cb = self._counter[root_b]
        self._counter[root_a] += cb
        self._counter[root_b] = 0
        return ca * cb

    def find(self, a, b):
        return self._root(a) == self._root(b)


N, M = map(int, input().split(" "))
edges = [tuple(map(int, input().split(" "))) for _ in range(M)]

dp = [0] * (M + 1)
dp[-1] = N * (N - 1) // 2
union_find = UnionFind(N)

for i in range(M - 1, 0, -1):
    if dp[i + 1] == 0:
        break
    a, b = edges[i]
    minus = union_find.union(a, b)
    dp[i] = dp[i + 1] - minus

for i in range(M):
    print(dp[i + 1])
Problem p03109 (Python)

Buggy submission (status: Runtime Error):
from datetime import date

dt = date.fromisoformat(input().replace("/", "-"))
th = date.fromisoformat("2019-04-30")
if dt <= th:
    print("Heisei")
else:
    print("TBD")


Fixed submission:
y, m, d = map(int, input().split("/"))
if y < 2019:
    print("Heisei")
elif y == 2019 and m < 5:
    print("Heisei")
else:
    print("TBD")
Problem p03109 (Python)

Buggy submission (status: Runtime Error):
A, B, C = map(int, input().split(" "))
ymd = tuple(map(int, input().split("/")))

if ymd <= (2019, 4, 30):
    print("Heisei")
else:
    print("TBD")


Fixed submission:
ymd = tuple(map(int, input().split("/")))

if ymd <= (2019, 4, 30):
    print("Heisei")
else:
    print("TBD")
Problem p03109 (Python)

Buggy submission (status: Runtime Error):
date = input()

date = date.replace("/", "")
if date <= 20190430:
    print("Heisei")
else:
    print("TBD")


Fixed submission:
date = input()

date = int(date.replace("/", ""))
if date <= 20190430:
    print("Heisei")
else:
    print("TBD")
Problem p03109 (Python)

Buggy submission (status: Runtime Error):
s = map(int, input().split("/"))
print("Heisei" if s <= 20190430 else "TBD")


Fixed submission:
s = int(input().replace("/", ""))
print("Heisei" if s <= 20190430 else "TBD")
Problem p03109 (C++)

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

int main() {
  string S;

  if (S.at(5) == '1') {
    cout << "TBD" << endl;
  }

  else if (S.at(6) == '1' || S.at(6) == '2' || S.at(6) == '3' ||
           S.at(6) == '4') {
    cout << "Heisei" << endl;
  }

  else {
    cout << "TBD" << endl;
  }
}


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

int main() {
  string S;
  cin >> S;

  if (S.at(5) == '1') {
    cout << "TBD" << endl;
  }

  else if (S.at(6) == '1' || S.at(6) == '2' || S.at(6) == '3' ||
           S.at(6) == '4') {
    cout << "Heisei" << endl;
  }

  else {
    cout << "TBD" << endl;
  }
}
Problem p03109 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d, e, f;
  while (scanf("%d/%d/%d", &a, &b, &c)) {
    if (b <= 4) {
      cout << "Heisei" << endl;
    } else {
      cout << "TBD" << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d, e, f;
  scanf("%d/%d/%d", &a, &b, &c);

  if (b <= 4) {
    cout << "Heisei" << endl;
  } else {
    cout << "TBD" << endl;
  }

  return 0;
}
Problem p03109 (C++)

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

int main() {
  string s;
  string year = s.substr(0, 4);
  string month = s.substr(5, 2);
  string day = s.substr(8, 2);

  int yearI = stoi(year);
  int monthI = stoi(month);
  int dayI = stoi(day);

  if (yearI > 2019) {
    cout << "TBD";
  } else {
    if (monthI > 4) {
      cout << "TBD";
    } else {
      if (dayI > 30) {
        cout << "TBD";
      } else {
        cout << "Heisei";
      }
    }
  }
}

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

int main() {
  string s;
  cin >> s;
  string year = s.substr(0, 4);
  string month = s.substr(5, 2);
  string day = s.substr(8, 2);

  int yearI = stoi(year);
  int monthI = stoi(month);
  int dayI = stoi(day);

  if (yearI > 2019) {
    cout << "TBD";
  } else {
    if (monthI > 4) {
      cout << "TBD";
    } else {
      if (dayI > 30) {
        cout << "TBD";
      } else {
        cout << "Heisei";
      }
    }
  }
}
Problem p03109 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int a, b, c, d;
  a = (int)(s.at(6) - '0');
  b = (int)(s.at(7) - '0');
  c = (int)(s.at(9) - '0');
  d = (int)(s.at(10) - '0');
  if (10 * a + b >= 5) {
    cout << "TBD";
  } else {
    cout << "Heisei";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int a, b, c, d;
  a = (int)(s.at(5) - '0');
  b = (int)(s.at(6) - '0');
  c = (int)(s.at(8) - '0');
  d = (int)(s.at(9) - '0');
  if (10 * a + b >= 5) {
    cout << "TBD";
  } else {
    cout << "Heisei";
  }
}
Problem p03109 (Python)

Buggy submission (status: Runtime Error):
a = int(input().split("/"))
if a[1] <= 4:
    print("Heisei")
else:
    print("TBD")


Fixed submission:
S = input().split("/")
if int(S[1]) < 5:
    print("Heisei")
else:
    print("TBD")
Problem p03109 (Python)

Buggy submission (status: Runtime Error):
s = sorted(input())
t = sorted(input())[::-1]
print("Yes" if s < t else "No")


Fixed submission:
S = input()
print("Heisei" if S <= "2019/04/30" else "TBD")
Problem p03109 (C++)

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

int main() {
  string s;
  cin >> s;
  int y = stoi(s.substr(0, 4));
  int m = stoi(s.substr(4, 2));
  int d = stoi(s.substr(7, 2));
  if (y > 2019 || (y == 2019 && m > 4))
    cout << "TBD";
  else
    cout << "Heisei";
}

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

int main() {
  string s;
  cin >> s;
  int y = stoi(s.substr(0, 4));
  int m = stoi(s.substr(5, 2));
  int d = stoi(s.substr(8, 2));
  if (y > 2019 || (y == 2019 && m > 4))
    cout << "TBD";
  else
    cout << "Heisei";
}
Problem p03109 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INT(a) scanf("%d", &a)
#define STR(a) scanf("%s", a)
#define DBL(a) scanf("%lf", &a)
#define LNG(a) scanf("%lld", &a)
#define PI acos(-1)

int main() {
  string s;
  cin >> s;
  int date = 0, year = 0;
  bool f = 0;
  for (int i = s.length() - 1; i >= 0; i++) {
    if (s[i] == '/') {
      if (f)
        break;
      f = 1;
    }
    if (!f)
      date = date * 10 + (s[i] - '0');
    else
      year = year * 10 + (s[i] - '0');
  }
  if (year > 4)
    cout << "TBD\n";
  else
    cout << "Heisei\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INT(a) scanf("%d", &a)
#define STR(a) scanf("%s", a)
#define DBL(a) scanf("%lf", &a)
#define LNG(a) scanf("%lld", &a)
#define PI acos(-1)

int main() {
  string s;
  cin >> s;
  int year = (s[5] - '0') * 10 + (s[6] - '0');

  if (year > 4)
    cout << "TBD\n";
  else
    cout << "Heisei\n";
  return 0;
}
Problem p03109 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
int main() {
  int a, b, c;
  scanf("%d/%d/%d", a, b, c);
  if (b > 4)
    cout << "TBD" << endl;
  else
    cout << "Heisei" << endl;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int main() {
  int a, b, c;
  scanf("%d/%d/%d", &a, &b, &c);
  if (b > 4)
    cout << "TBD" << endl;
  else
    cout << "Heisei" << endl;
}
Problem p03109 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
int main() {

  std::string S;

  std::cin >> S;

  std::string s1;
  std::string s2;
  std::string s3;

  s1 = S[0] + S[1] + S[2] + S[3];
  s2 = S[5] + S[6];
  s3 = S[8] + S[9];

  if (std::stoi(s1) <= 2019 && std::stoi(s2) <= 4 && std::stoi(s3) <= 30) {
    std::cout << "Heisei" << std::endl;
  } else {
    std::cout << "TBD" << std::endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
int main() {

  std::string S;

  std::cin >> S;

  std::string s1;
  std::string s2;
  std::string s3;

  s1 = S.substr(0, 4);
  s2 = S.substr(5, 2);
  s3 = S.substr(8, 2);

  if (std::stoi(s1) <= 2019 && std::stoi(s2) <= 4 && std::stoi(s3) <= 30) {
    std::cout << "Heisei" << std::endl;
  } else {
    std::cout << "TBD" << std::endl;
  }
  return 0;
}
Problem p03109 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  string t = "TBD";
  string u;
  int i;
  for (i = 0; i < 10; i++) {
    if (s.at(i) != '/') {
      u.at(i) = s.at(i);
    }
  }
  int a = stoi(u);
  if (a / 10000 < 2019) {
    t = "Heisei";
  }
  if (a / 10000 == 2019 && a - 20190000 <= 430) {
    t = "Heisei";
  }
  cout << t;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  string t = "TBD";
  if (s <= "2019/04/30") {
    t = "Heisei";
  }
  cout << t;
}
Problem p03109 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <stdio.h>
#include <string.h>
#include <time.h>

int main() {
  int y, m, d;
  scanf("%d/%d/%d", y, m, d);

  if (y > 2019 || y == 2019 && m >= 5) {
    printf("TBD");
  } else {
    printf("Heisei");
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <stdio.h>
#include <string.h>
#include <time.h>

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

  if (y > 2019 || y == 2019 && m >= 5) {
    printf("TBD");
  } else {
    printf("Heisei");
  }
  return 0;
}
Problem p03109 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define LL long long
#define MP(a, b) make_pair(a, b)
#define POWER9 1000000000
#define MOD POWER9 + 7
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define LL_MIN (LL) - 9223372036854775807
#define LL_MAX (LL)9223372036854775807
#define PI 3.14159265359

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(9);

  string S;
  cin >> S;
  if (stoi("" + S[5] + S[6]) > 4)
    cout << "TBD" << endl;
  else
    cout << "Heisei" << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
#define LL long long
#define MP(a, b) make_pair(a, b)
#define POWER9 1000000000
#define MOD POWER9 + 7
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define LL_MIN (LL) - 9223372036854775807
#define LL_MAX (LL)9223372036854775807
#define PI 3.14159265359

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(9);

  string S;
  cin >> S;
  if (S[5] == '1' || S[6] > '4')
    cout << "TBD" << endl;
  else
    cout << "Heisei" << endl;

  return 0;
}
Problem p03110 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
typedef priority_queue<ll> pq;

const int MOD = 1e9 + 7;
const int INF = 2e9;
const int MAX_N = 1;

int main() {
  double ans = 0;
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string s;
    double x;
    cin >> x >> s;
    if (s[0] == 'B')
      x *= 380000.0;
    ans += x;
  }
  printf("%llf\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pii;
typedef vector<pii> vii;
typedef priority_queue<ll> pq;

const int MOD = 1e9 + 7;
const int INF = 2e9;
const int MAX_N = 1;

int main() {
  double ans = 0;
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string s;
    double x;
    cin >> x >> s;
    if (s[0] == 'B')
      x *= 380000.0;
    ans += x;
  }
  cout << ans << endl;
  return 0;
}
Problem p03110 (C++)

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

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  cout << fixed << setprecision(10);
  double ans = 0;
  rep(i, n) {
    double mass = 0;
    string coin = 0;
    cin >> mass >> coin;
    if (coin == "JPY")
      ans += mass;
    if (coin == "BTC")
      ans += mass * 380000.0;
  }
  cout << ans << endl;
}

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

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  cout << fixed << setprecision(10);
  double ans = 0;
  rep(i, n) {
    double mass;
    string coin;
    cin >> mass >> coin;
    if (coin == "JPY")
      ans += mass;
    if (coin == "BTC")
      ans += mass * 380000.0;
  }
  cout << ans << endl;
}
Problem p03110 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {

  int n, i;
  double s;
  s = 0;
  cin >> n;
  double a[n];
  string u[n];
  for (i = 1; i <= n; i++) {
    cin >> a[i] >> u[i];
    if (u[i] == "JPY") {
      s = s + a[i];
    } else if (u[i] == "BTC") {
      s = s + a[i] * 380000;
    }
  }
  cout << s << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {

  int n, i;
  double s;
  s = 0;
  cin >> n;
  double a[n];
  string u[n];
  for (i = 0; i <= n - 1; i++) {
    cin >> a[i] >> u[i];
    if (u[i] == "JPY") {
      s = s + a[i];
    } else if (u[i] == "BTC") {
      s = s + a[i] * 380000;
    }
  }
  cout << s << endl;

  return 0;
}
Problem p03110 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  double x[n], s = 0;
  string t[n];
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> t[i];
    if (t[n] == "JPY")
      s += x[i];
    else
      s += x[i] * 380000;
  }
  cout << fixed << setprecision(15) << s;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  double x[n], s = 0;
  string t[n];
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> t[i];
    if (t[i] == "JPY")
      s += x[i];
    else
      s += x[i] * 380000;
  }
  cout << fixed << setprecision(15) << s;
}
Problem p03110 (Python)

Buggy submission (status: Runtime Error):
N = int(input())

sum = 0.0

for i in range(N):
    x = float(input())
    u = input()

    if u == "JPY":
        sum = sum + x
    else:
        sum = sum + x * 380000

print(sum)


Fixed submission:
N = int(input())

sum = 0.0

for i in range(N):
    S = input()
    S = S.split()

    x = float(S[0])
    u = S[1]

    if u == "JPY":
        sum = sum + x
    else:
        sum = sum + x * 380000

print(sum)
Problem p03110 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define all(collection) (collection).begin(), (collection).end()
#define loop(i, times) for (int i = 0; i < times; i++)
#define seloop(i, start_index, end_index)                                      \
  for (int i = start_index; i < end_index; i++)
#define eqseloop(i, start_index, end_index)                                    \
  for (int i = start_index; i <= end_index; i++)

int mp = 0;

int main() {
  int n;
  cin >> n;
  vector<pair<string, string>> s(n);

  double sum = 0;
  for (const auto &e : s) {
    if (get<1>(e) == "JPY") {
      sum += stod(get<0>(e));
    } else {
      sum += (stod(get<0>(e)) * 380000.0);
    }
  }

  cout << sum;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define all(collection) (collection).begin(), (collection).end()
#define loop(i, times) for (int i = 0; i < times; i++)
#define seloop(i, start_index, end_index)                                      \
  for (int i = start_index; i < end_index; i++)
#define eqseloop(i, start_index, end_index)                                    \
  for (int i = start_index; i <= end_index; i++)

int mp = 0;

int main() {
  int n;
  cin >> n;
  vector<pair<string, string>> s(n);

  loop(i, n) {
    cin >> get<0>(s[i]);
    cin >> get<1>(s[i]);
  }

  double sum = 0;
  for (const auto &e : s) {
    if (get<1>(e) == "JPY") {
      sum += stod(get<0>(e));
    } else {
      sum += (stod(get<0>(e)) * 380000.0);
    }
  }

  cout << sum;

  return 0;
}
Problem p03111 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repf(i, m, n) for (int(i) = m; (i) < n; (i)++)
#define all(v) (v).begin(), (v).end()
#define ll long long
#define vec(n, m, v) vector<long long> v((n), (m));
int ans = INT_MAX;
int n;
vec(8, 0, k);
int a[3];
vec(8, 0, v);
void init() {
  cin >> n >> a[0] >> a[1] >> a[2];

  rep(i, n) cin >> v[i];
}

int dfs(int i) {
  if (i == n) {
    int mp = 0;
    vec(4, 0, cnt);
    vec(3, 0, tmp);
    /* rep(kk,n){
         cout<<k[kk]<<" ";
     }
     cout<<endl;*/
    rep(jj, n) {
      cnt[k[jj]]++;
      tmp[k[jj]] += v[jj];
    }
    if (cnt[0] * cnt[1] * cnt[2] != 0) {
      rep(jj, 3) mp += abs(tmp[jj] - a[jj]);
      mp += ((cnt[0] + cnt[1] + cnt[2] - 3) * 10);
      return mp;
    }
    return INT_MAX;
  }
  rep(j, 4) {
    k[i] = j;
    // cout<<"dd"<<endl;
    ans = min(dfs(i + 1), ans);
  }
  return ans;
}

int main() {

  init();
  cout << dfs(0) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repf(i, m, n) for (int(i) = m; (i) < n; (i)++)
#define all(v) (v).begin(), (v).end()
#define ll long long
#define vec(n, m, v) vector<int> v((n), (m));
int ans = INT_MAX;
int n;
vec(8, 0, k);
int a[3];
vec(8, 0, v);
void init() {
  cin >> n >> a[0] >> a[1] >> a[2];

  rep(i, n) cin >> v[i];
}

int dfs(int i) {
  if (i == n) {
    int mp = 0;
    vec(4, 0, cnt);
    vec(3, 0, tmp);
    /* rep(kk,n){
         cout<<k[kk]<<" ";
     }
     cout<<endl;*/
    rep(jj, n) {
      cnt[k[jj]]++;
      tmp[k[jj]] += v[jj];
    }
    if (cnt[0] * cnt[1] * cnt[2] != 0) {
      rep(jj, 3) mp += abs(tmp[jj] - a[jj]);
      mp += ((cnt[0] + cnt[1] + cnt[2] - 3) * 10);
      return mp;
    }
    return INT_MAX;
  }
  rep(j, 4) {
    k[i] = j;
    // cout<<"dd"<<endl;
    ans = min(dfs(i + 1), ans);
  }
  return ans;
}

int main() {

  init();
  cout << dfs(0) << endl;
  return 0;
}
Problem p03111 (C++)

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

const int INF = 1000000000;
int n;
int target[3];
std::vector<int> l;

// ref : https://img.atcoder.jp/abc119/editorial.pdf
// ref : https://drken1215.hatenablog.com/entry/2019/02/24/224100
/*
延長、縮小は合成の前後どっちでやろうが同じ
->
合成魔法を一切これ以降使用しない場合に限り延長、縮小魔法を使用としても一般性は失われない
目的を達成するに当たって竹はA, B, Cの材料or一切使わないの4通り
-> N本あるので4^N通り
そこで合成するペアだけ全探索すればあとは目標の長さを得るためのコストを計算すればok
*/

int solve(int i = 0, int a = 0, int b = 0, int c = 0) {
  if (i == n) {
    // どれも目標の長さを作るのに使っていない場合はダメ
    if (!a || !b || !c)
      return INF;
    else
      return std::abs(a - target[0]) + std::abs(b - target[1]) +
             std::abs(c - target[2]);
  }

  int cost = INF;
  cost = std::min(cost, solve(i + 1, a, b, c)); // この竹を使わない
  // 以降使う, このとき最初の1本目は合成できていないので+10しないことに注意
  cost = std::min(cost, solve(i + 1, a + l[i], b, c) + (a ? 10 : 0));
  cost = std::min(cost, solve(i + 1, a, b + l[i], c) + (b ? 10 : 0));
  cost = std::min(cost, solve(i + 1, a, b, c + l[i]) + (c ? 10 : 0));
  return cost;
}

int main() {
  std::cin >> n;
  l.assign(3, 0);
  for (int i = 0; i < 3; i++)
    std::cin >> target[i];
  for (int i = 0; i < n; i++) {
    std::cin >> l[i];
  }

  std::cout << solve() << std::endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>

const int INF = 1000000000;
int n;
int target[3];
std::vector<int> l;

// ref : https://img.atcoder.jp/abc119/editorial.pdf
// ref : https://drken1215.hatenablog.com/entry/2019/02/24/224100
/*
延長、縮小は合成の前後どっちでやろうが同じ
->
合成魔法を一切これ以降使用しない場合に限り延長、縮小魔法を使用としても一般性は失われない
目的を達成するに当たって竹はA, B, Cの材料or一切使わないの4通り
-> N本あるので4^N通り
そこで合成するペアだけ全探索すればあとは目標の長さを得るためのコストを計算すればok
*/

int solve(int i = 0, int a = 0, int b = 0, int c = 0) {
  if (i == n) {
    // どれも目標の長さを作るのに使っていない場合はダメ
    if (!a || !b || !c)
      return INF;
    else
      return std::abs(a - target[0]) + std::abs(b - target[1]) +
             std::abs(c - target[2]);
  }

  int cost = INF;
  cost = std::min(cost, solve(i + 1, a, b, c)); // この竹を使わない
  // 以降使う, このとき最初の1本目は合成できていないので+10しないことに注意
  cost = std::min(cost, solve(i + 1, a + l[i], b, c) + (a ? 10 : 0));
  cost = std::min(cost, solve(i + 1, a, b + l[i], c) + (b ? 10 : 0));
  cost = std::min(cost, solve(i + 1, a, b, c + l[i]) + (c ? 10 : 0));
  return cost;
}

int main() {
  std::cin >> n;
  l.assign(n, 0);
  for (int i = 0; i < 3; i++)
    std::cin >> target[i];
  for (int i = 0; i < n; i++) {
    std::cin >> l[i];
  }

  std::cout << solve() << std::endl;

  return 0;
}
Problem p03111 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
// typedef pair<string,string> P;
// double dat[100][100];
// int dp[6][1010];//動的計画法
// int prime[100001];
// char str[1010][1010];
// vector<pair<ll,ll>> pc;
// int ABS(int a){return max(a,-a);}
// int a[100001];
const int INF = 100000009;
int N, A, B, C;
vector<int> l;
int dfs(int cur, int a, int b, int c) {
  if (cur == N) {
    if (min(a, min(b, c)) > 0)
      return abs(a - A) + abs(b - B) + abs(c - C) - 30;
    else
      return INF;
  }
  int ret0 = dfs(cur + 1, a, b, c);
  int ret1 = dfs(cur + 1, a + l[cur], b, c) + 10;
  int ret2 = dfs(cur + 1, a, b + l[cur], c) + 10;
  int ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10;

  return min(min(ret0, ret1), min(ret2, ret3));
}
int main() {
  cin >> N >> A >> B >> C;
  for (int i = 0; i > N; i++) {
    int a;
    cin >> a;
    l.push_back(a);
  }
  cout << dfs(0, 0, 0, 0) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
// typedef pair<string,string> P;
// double dat[100][100];
// int dp[6][1010];//動的計画法
// int prime[100001];
// char str[1010][1010];
// vector<pair<ll,ll>> pc;
// int ABS(int a){return max(a,-a);}
// int a[100001];
const int INF = 100000009;
int N, A, B, C;
vector<int> l;
int dfs(int cur, int a, int b, int c) {
  if (cur == N) {
    if (min(a, min(b, c)) > 0)
      return abs(a - A) + abs(b - B) + abs(c - C) - 30;
    else
      return INF;
  }
  int ret0 = dfs(cur + 1, a, b, c);
  int ret1 = dfs(cur + 1, a + l[cur], b, c) + 10;
  int ret2 = dfs(cur + 1, a, b + l[cur], c) + 10;
  int ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10;

  return min(min(ret0, ret1), min(ret2, ret3));
}
int main() {
  cin >> N >> A >> B >> C;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    l.push_back(a);
  }
  cout << dfs(0, 0, 0, 0) << endl;
  return 0;
}
Problem p03111 (C++)

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

int N, A, B, C;
int l[8];

int dfs(int cur, int a, int b, int c) {

  if (cur == N) {
    if (min(a, min(b, c)) > 0) {
      return abs(a - A) + abs(b - B) + abs(c - C) - 30;
    }
  }

  int ret0 = dfs(cur + 1, a, b, c);
  int ret1 = dfs(cur + 1, a + l[cur], b, c) + 10;
  int ret2 = dfs(cur + 1, a, b + l[cur], c) + 10;
  int ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10;

  return min(ret0, min(ret1, min(ret2, ret3)));
}

int main() {

  cin >> N >> A >> B >> C;
  for (int i = 0; i < N; i++) {
    cin >> l[i];
  }
  cout << dfs(0, 0, 0, 0) << endl;
}


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

int N, A, B, C;
int l[8];

int dfs(int cur, int a, int b, int c) {

  if (cur == N) {
    if (min(a, min(b, c)) > 0) {
      return abs(a - A) + abs(b - B) + abs(c - C) - 30;
    } else {
      return 1e9;
    }
  }

  int ret0 = dfs(cur + 1, a, b, c);
  int ret1 = dfs(cur + 1, a + l[cur], b, c) + 10;
  int ret2 = dfs(cur + 1, a, b + l[cur], c) + 10;
  int ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10;

  return min(ret0, min(ret1, min(ret2, ret3)));
}

int main() {

  cin >> N >> A >> B >> C;
  for (int i = 0; i < N; i++) {
    cin >> l[i];
  }
  cout << dfs(0, 0, 0, 0) << endl;
}
Problem p03111 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c, i, l[9], ans;
void dfs(int x, int A, int B, int C, int s) {
  if (x > n && A && B && C) {
    ans = min(ans, s + abs(a - A) + abs(b - B) + abs(c - C));
    return;
  }
  dfs(x + 1, A, B, C, s);
  dfs(x + 1, A + l[x], B, C, s + 10 * (A > 0));
  dfs(x + 1, A, B + l[x], C, s + 10 * (B > 0));
  dfs(x + 1, A, B, C + l[x], s + 10 * (C > 0));
}
int main() {
  ans = 1e9;
  cin >> n >> a >> b >> c;
  for (i = 1; i <= n; i++)
    cin >> l[i];
  dfs(1, 0, 0, 0, 0);
  printf("%d", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c, i, l[9], ans;
void dfs(int x, int A, int B, int C, int s) {
  if (x > n) {
    if (A && B && C)
      ans = min(ans, s + abs(a - A) + abs(b - B) + abs(c - C));
    return;
  }
  dfs(x + 1, A, B, C, s);
  dfs(x + 1, A + l[x], B, C, s + 10 * (A > 0));
  dfs(x + 1, A, B + l[x], C, s + 10 * (B > 0));
  dfs(x + 1, A, B, C + l[x], s + 10 * (C > 0));
}
int main() {
  ans = 1e9;
  cin >> n >> a >> b >> c;
  for (i = 1; i <= n; i++)
    cin >> l[i];
  dfs(1, 0, 0, 0, 0);
  printf("%d", ans);
  return 0;
}