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

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

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

// 4-side
// int xx[] = {0,0,-1,1};
// int yy[] = {-1,1,0,0};
// 6-side hexagonal
// int xx[] = {2,-2,1,1,-1,-1};
// int yy[] = {0,0,1,-1,1,-1};

#define popc(a) (__builtin_popcount(a))
// ll bigmod(ll a,ll b,ll m){if(b == 0) return 1%m;ll x = bigmod(a,b/2,m);x = (x
// * x) % m;if(b % 2 == 1) x = (x * a) % m;return x;} ll BigMod(ll B,ll P,ll M){
// ll R=1%M; while(P>0) {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;} ///
// (B^P)%M

#define MX 100005
#define inf 100000000

const double pi = acos(-1.0);
const ll mod = 1000000000ll;

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ll x, y;
  scanf("%lld %lld", &x, &y);
  if (x == y) {
    puts("-1");
    return 0;
  }
  ll res = x + x;
  while (res % y == 0 && res <= (mod * mod)) {
    res += x;
  }
  if (res > (mod * mod)) {
    puts("-1");
  } else {
    printf("%lld\n", res);
  }
  return 0;
}


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

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

// 4-side
// int xx[] = {0,0,-1,1};
// int yy[] = {-1,1,0,0};
// 6-side hexagonal
// int xx[] = {2,-2,1,1,-1,-1};
// int yy[] = {0,0,1,-1,1,-1};

#define popc(a) (__builtin_popcount(a))
// ll bigmod(ll a,ll b,ll m){if(b == 0) return 1%m;ll x = bigmod(a,b/2,m);x = (x
// * x) % m;if(b % 2 == 1) x = (x * a) % m;return x;} ll BigMod(ll B,ll P,ll M){
// ll R=1%M; while(P>0) {if(P%2==1){R=(R*B)%M;}P/=2;B=(B*B)%M;} return R;} ///
// (B^P)%M

#define MX 100005
#define inf 100000000

const double pi = acos(-1.0);
const ll mod = 1000000000ll;

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ll x, y;
  scanf("%lld %lld", &x, &y);
  if (x % y == 0) {
    puts("-1");
    return 0;
  }
  ll res = x + x;
  while (res % y == 0 && res <= (mod * mod)) {
    res += x;
  }
  if (res > (mod * mod)) {
    puts("-1");
  } else {
    printf("%lld\n", res);
  }
  return 0;
}
Problem p03437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7;

int main() {

  ll x, y;
  cin >> x >> y;
  if (x == y) {
    cout << -1 << endl;
  } else {
    for (int i = 1;; i++) {
      if (x * i % y != 0) {
        cout << x * i << endl;
        return 0;
      }
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7;

int main() {

  ll x, y;
  cin >> x >> y;
  if (x == y || x % y == 0) {
    cout << -1 << endl;
  } else {
    for (int i = 1;; i++) {
      if (x * i % y != 0) {
        cout << x * i << endl;
        return 0;
      }
    }
  }

  return 0;
}
Problem p03437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  // unsigned long long int a, b, c, d, e, ans = 0,n,x[19],c[19],v[19];
  int a, b, c, d, e, f = 0, ans = 0, n;
  // 整数の入力

  unsigned long long x, y;
  // 文字列の入力
  //	string s;
  cin >> x >> y;
  if (x % y == 0 || x == y) {
    cout << -1 << endl;

  } else {
    for (a = x; a < 1000000000000000000; a++) {
      if (x % a == 0 && y % a != 0) {
        cout << a << endl;
      }
    }
  }
  // 出力
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  // unsigned long long int a, b, c, d, e, ans = 0,n,x[19],c[19],v[19];
  int a, b, c, d, e, f = 0, ans = 0, n;
  // 整数の入力

  unsigned long long x, y;
  // 文字列の入力
  //	string s;
  cin >> x >> y;
  if (x % y == 0 || x == y) {
    cout << -1 << endl;

  } else {
    for (a = 1; a * x < 1000000000000000000; a++) {
      if ((a * x) % y != 0) {
        cout << a * x << endl;
        break;
      }
    }
  }
  // 出力
  return 0;
}
Problem p03438 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

c = max(0, sum(B) - sum(A))
ca = 0
cb = 0

for a, b in zip(A, B):
    if a < b:
        ca += (b - a + 1) // 2
        cb += (b - a) % 2
    if a > b:
        cb += a - b

if c < max(ca + cb):
    print("No")
else:
    print("Yes")


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

c = max(0, sum(B) - sum(A))
ca = 0
cb = 0

for a, b in zip(A, B):
    if a < b:
        ca += (b - a + 1) // 2
        cb += (b - a) % 2
    if a > b:
        cb += a - b

if c < max(ca, cb):
    print("No")
else:
    print("Yes")
Problem p03438 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = 0
b = 0
for i in range(N):
    if B[i] >= A[i]:
        a += (B[i] - A[i] + 1) // 2
        b += (B[i] - A[i]) % 2
    else:
        b += A[i] - bytes[i]
print("Yes" if a >= b else "No")


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = 0
b = 0
for i in range(N):
    if B[i] >= A[i]:
        a += (B[i] - A[i] + 1) // 2
        b += (B[i] - A[i]) % 2
    else:
        b += A[i] - B[i]
print("Yes" if a >= b else "No")
Problem p03438 (C++)

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

using namespace std;

#define SPEED                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define fileio freopen("in.in", "r", stdin), freopen("out.out", "w", stdout);
#define ll long long int
#define FF first
#define SS second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\n", x)
#define plld(x) printf("%lld\n", x)
#define pss printf
#define MOD 1000000007
#define INF 1e18
#define eps 0.00001
#define endl '\n'
#define debug(n1) cout << n1 << endl
ll n, a[1005], b[1005];
int main() {
  slld(n);
  for (int i = 1; i <= n; i++)
    slld(a[i]);
  for (int i = 1; i <= n; i++)
    slld(b[i]);
  ll x = 0, y = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > b[i])
      x += a[i] - b[i];
    else
      y += (b[i] - a[i]) / 2;
  }
  if (x <= y)
    pss("Yes\n");
  else
    pss("No\n");
  return 0;
}


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

using namespace std;

#define SPEED                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define fileio freopen("in.in", "r", stdin), freopen("out.out", "w", stdout);
#define ll long long int
#define FF first
#define SS second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define pd(x) printf("%d\n", x)
#define plld(x) printf("%lld\n", x)
#define pss printf
#define MOD 1000000007
#define INF 1e18
#define eps 0.00001
#define endl '\n'
#define debug(n1) cout << n1 << endl
ll n, a[10005], b[10005];
int main() {
  slld(n);
  for (int i = 1; i <= n; i++)
    slld(a[i]);
  for (int i = 1; i <= n; i++)
    slld(b[i]);
  ll x = 0, y = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > b[i])
      x += a[i] - b[i];
    else
      y += (b[i] - a[i]) / 2;
  }
  if (x <= y)
    pss("Yes\n");
  else
    pss("No\n");
  return 0;
}
Problem p03438 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define F(i, s, t) for (int i = (s), _ = (t); i <= _; i++)
#define LL long long
int rd() {
  int x = 0, f = 1;
  char c;
  do {
    c = getchar();
  } while (!isdigit(c));
  do {
    x = x * 10 + c - '0', c = getchar();
  } while (isdigit(c));
  return x * f;
}
const int N = 10005;
int n, a[N], b[N];
LL A, B;
int main() {
#ifndef ONLINE_JUDGE
  freopen("f.in", "r", stdin);
#endif
  n = rd();
  F(i, 1, n) a[i] = rd();
  F(i, 1, n) b[i] = rd();
  F(i, 1, n)
  if (a[i] > b[i])
    A += a[i] - b[i];
  else
    B += b[i] - a[i] >> 1;
  puts(A <= B ? "Yes" : "No");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define F(i, s, t) for (int i = (s), _ = (t); i <= _; i++)
#define LL long long
int rd() {
  int x = 0, f = 1;
  char c;
  do {
    c = getchar();
  } while (!isdigit(c));
  do {
    x = x * 10 + c - '0', c = getchar();
  } while (isdigit(c));
  return x * f;
}
const int N = 10005;
int n, a[N], b[N];
LL A, B;
int main() {
  n = rd();
  F(i, 1, n) a[i] = rd();
  F(i, 1, n) b[i] = rd();
  F(i, 1, n)
  if (a[i] > b[i])
    A += a[i] - b[i];
  else
    B += b[i] - a[i] >> 1;
  puts(A <= B ? "Yes" : "No");
  return 0;
}
Problem p03438 (C++)

Buggy submission (status: Runtime Error):
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define dbg(x) cerr << #x << " = " << x << '\n'
#define sz(x) (int)x.size()
#define all(a) (a.begin(), a.end())
#define ff first
#define ss second
#define pii pair<int, int>
#define lcm(a, b) (a * b) / __gcd(a, b)
using namespace std;

inline void solve() {
  int n;
  cin >> n;
  vi a(n), b(n);
  for (int &i : a)
    cin >> i;
  for (int &i : b)
    cin >> i;
  int ok = 0, s = 0;
  for (int i = 0; i < n; i++) {
    s += max(0LL, (a[i] - b[i]) * 2);
    ok += max(0LL, (b[i] - a[i]) / 2);
  }
  if ((ok >= s / 2) or (s == 0))
    printf("Yes\n");
  else
    printf("No\n");
}

signed main() {
  int n = 1;
  cin >> n;
  while (n--)
    solve();
  return 0;
}


Fixed submission:
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define dbg(x) cerr << #x << " = " << x << '\n'
#define sz(x) (int)x.size()
#define all(a) (a.begin(), a.end())
#define ff first
#define ss second
#define pii pair<int, int>
#define lcm(a, b) (a * b) / __gcd(a, b)
using namespace std;

inline void solve() {
  int n;
  cin >> n;
  vi a(n), b(n);
  for (int &i : a)
    cin >> i;
  for (int &i : b)
    cin >> i;
  int ok = 0, s = 0;
  for (int i = 0; i < n; i++) {
    s += max(0LL, (a[i] - b[i]) * 2);
    ok += max(0LL, (b[i] - a[i]) / 2);
  }
  if ((ok >= s / 2) or (s == 0))
    printf("Yes\n");
  else
    printf("No\n");
}

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}
Problem p03438 (C++)

Buggy submission (status: Runtime Error):
// In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
const bool DEBUG = false;
#define cerr                                                                   \
  if (DEBUG)                                                                   \
  cerr
using namespace std;

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

const long long maxn = 1000 + 5;
const long long INF = 4e18;
const long long M = 1e9 + 7;
const int lg = 21;

ll n, mx, mn, a[maxn], b[maxn];
multiset<ll> s;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    int c = a[i] - b[i];
    if (c > 0)
      mx += c;
    if (c < 0)
      mn += ((-c) / 2);
  }
  if (mx <= mn)
    return cout << "Yes" << endl, 0;
  cout << "No" << endl;
  return 0;
}


Fixed submission:
// In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
const bool DEBUG = false;
#define cerr                                                                   \
  if (DEBUG)                                                                   \
  cerr
using namespace std;

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

const long long maxn = 10000 + 5;
const long long INF = 4e18;
const long long M = 1e9 + 7;
const int lg = 21;

ll n, mx, mn, a[maxn], b[maxn];
multiset<ll> s;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    int c = a[i] - b[i];
    if (c > 0)
      mx += c;
    if (c < 0)
      mn += ((-c) / 2);
  }
  if (mx <= mn)
    return cout << "Yes" << endl, 0;
  cout << "No" << endl;
  return 0;
}
Problem p03438 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define M 1000000007
#define P pair<int, int>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) FOR(i, 0, n)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

int n;
ll a[20000], b[20000];
ll suma, sumb;

int main() {
  cin >> n;

  rep(i, n) {
    cin >> a[i];
    suma += a[i];
  }
  rep(i, n) {
    cin >> b[i];
    sumb += b[i];
  }

  ll sa = 0;
  rep(i, n) {
    if (a[i] < b[i]) {
      sa += (b[i] - a[i] + (b[i] % a[i])) / 2;
    }
  }
  ll sumc = sumb - suma;
  if (sumc >= sa)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define M 1000000007
#define P pair<int, int>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) FOR(i, 0, n)
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

int n;
ll a[20000], b[20000];
ll suma, sumb;

int main() {
  cin >> n;

  rep(i, n) {
    cin >> a[i];
    suma += a[i];
  }
  rep(i, n) {
    cin >> b[i];
    sumb += b[i];
  }

  ll sa = 0;
  rep(i, n) {
    if (a[i] < b[i]) {
      sa += (b[i] - a[i] + ((b[i] - a[i]) % 2)) / 2;
    }
  }
  ll sumc = sumb - suma;
  if (sumc >= sa)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}
Problem p03438 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = []() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();

int main() {
  int n;
  cin >> n;
  vector<ll> a(n, 0);
  vector<ll> b(n, 0);
  for (ll &val : a)
    cin >> val;
  for (ll &val : b)
    cin >> val;
  ll sA = accumulate(a.begin(), a.end(), 0LL);
  ll sB = accumulate(b.begin(), b.end(), 0LL);
  if (sA > sB)
    cout << "No" << endl;
  else {
    ll quota = sB - sA;
    ll cast_b = 0;
    vector<ll> buffer_a;
    for (int i = 0; i < n; i++) {
      if (a[i] < b[i])
        buffer_a.push_back(b[i] - a[i]);
      if (a[i] > b[i])
        cast_b += (a[i] - b[i]);
    }
    if (cast_b > quota) {
      cout << "No" << endl;
    } else {
      ll remain = quota - cast_b;
      ll after = 0;
      for (ll val : buffer_a) {
        while (cast_b > 0 && val > 2) {
          cast_b--;
          val -= 2;
        }
        after += val;
      }
      if (cast_b > 0) {
        cout << "No" << endl;
      } else {
        if (after == remain) {
          cout << "Yes" << endl;
        } else {
          cout << "No" << endl;
        }
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = []() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();

int main() {
  int n;
  cin >> n;
  vector<ll> a(n, 0);
  vector<ll> b(n, 0);
  for (ll &val : a)
    cin >> val;
  for (ll &val : b)
    cin >> val;
  ll sA = accumulate(a.begin(), a.end(), 0LL);
  ll sB = accumulate(b.begin(), b.end(), 0LL);
  if (sA > sB)
    cout << "No" << endl;
  else {
    ll quota = sB - sA;
    ll cast_b = 0;
    vector<ll> buffer_a;
    for (int i = 0; i < n; i++) {
      if (a[i] < b[i])
        buffer_a.push_back(b[i] - a[i]);
      if (a[i] > b[i])
        cast_b += (a[i] - b[i]);
    }
    if (cast_b > quota) {
      cout << "No" << endl;
    } else {
      ll remain = quota - cast_b;
      ll after = 0;
      for (ll val : buffer_a) {
        if (cast_b > 0 && val > 1) {
          ll sub = min(cast_b, val / 2);
          cast_b -= sub;
          val -= sub * 2;
        } /*
         while(cast_b > 0 && val > 2) {
             cast_b--;
             val-=2;
         }*/
        after += val;
      }
      if (cast_b > 0) {
        cout << "No" << endl;
      } else {
        if (after == remain) {
          cout << "Yes" << endl;
        } else {
          cout << "No" << endl;
        }
      }
    }
  }
  return 0;
}
Problem p03438 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> a(N), b(N), c(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < N; i++) {
    cin >> b.at(i);
    c.at(i) = b.at(i) - a.at(i);
  }
  while (1) {
    sort(c.begin(), c.end());
    if (c.front() >= 0) {
      cout << "Yes" << endl;
      return 0;
    } else if (c.back() <= 0) {
      cout << "No" << endl;
      return 0;
    }
    c.front()++;
    c.back() -= 2;
  }
  return 0;
}


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

int main() {
  int N;
  cin >> N;
  vector<int64_t> a(N), b(N), c(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < N; i++) {
    cin >> b.at(i);
    c.at(i) = b.at(i) - a.at(i);
  }
  while (1) {
    sort(c.begin(), c.end());
    if (c.front() >= 0) {
      cout << "Yes" << endl;
      return 0;
    } else if (c.back() <= 0) {
      cout << "No" << endl;
      return 0;
    }
    int b = c.back();
    int f = c.front();
    if (c.back() >= -c.front() * 2) {
      c.back() += c.front() * 2;
      c.front() = 0;
    } else {
      int64_t x = c.back() + c.front() * 2;
      if (x % 2 == 0) {
        c.front() = x / 2;
        c.back() = 0;
      } else {
        c.front() = x / 2 - 1;
        c.back() = 1;
      }
    }
    if ((c.back() == b) && (c.front() == f)) {
      cout << "No" << endl;
      return 0;
    }
  }
  return 0;
}
Problem p03438 (C++)

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).bigin(), (c).end())

typedef long long ll;

int main() {
  int n, a[1100], b[1100];
  int one = 0, two = 0;

  cin >> n;
  REP(i, n)
  cin >> a[i];

  REP(i, n) {
    cin >> b[i];

    if (a[i] > b[i])
      one += a[i] - b[i];
    if (a[i] < b[i]) {
      two += (b[i] - a[i] + 2 - 1) / 2;
      one += (b[i] - a[i]) % 2;
    }
  }

  bool res = false;

  if (two >= one)
    res = true;

  cout << (res ? "Yes" : "No") << endl;

  return 0;
}

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).bigin(), (c).end())

typedef long long ll;

int main() {
  int n, a[11000], b[11000];
  ll one = 0, two = 0;

  cin >> n;
  REP(i, n)
  cin >> a[i];

  REP(i, n) {
    cin >> b[i];

    if (a[i] > b[i])
      one += a[i] - b[i];
    if (a[i] < b[i]) {
      two += (b[i] - a[i] + 2 - 1) / 2;
      one += (b[i] - a[i]) % 2;
    }
  }

  bool res = false;

  if (two >= one)
    res = true;

  cout << (res ? "Yes" : "No") << endl;

  return 0;
}
Problem p03440 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
int n, m;
int p[maxn], head[maxn];
int mn[maxn], bad[maxn];
deque<int> all;
int get(int x, int y) {
  if (p[x] < p[y])
    return x;
  return y;
}
int findhead(int x) {
  if (x == head[x])
    return x;
  return head[x] = findhead(head[x]);
}
int main() {
  int i, x, y;
  long long ans = 0;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (i = 1; i <= n; i++)
    head[i] = i;
  for (i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    head[findhead(x + 1)] = findhead(y + 1);
  }
  p[0] = 2e9;
  for (i = 1; i <= n; i++) {
    x = findhead(i);
    mn[x] = get(mn[x], i);
  }
  for (i = 1; i <= n; i++)
    bad[mn[i]] = 1;
  for (i = 1; i <= n; i++)
    if (!bad[i])
      all.push_back(p[i]);
  sort(all.begin(), all.end());
  for (i = 1; i <= n; i++)
    if (bad[i])
      all.push_front(p[i]);
  if ((n - m - 1) * 2 > all.size())
    return !printf("Impossible");
  for (i = 0; i < (n - m - 1) * 2; i++)
    ans += all[i];
  printf("%lld", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, m;
int p[maxn], head[maxn];
int mn[maxn], bad[maxn];
deque<int> all;
int get(int x, int y) {
  if (p[x] < p[y])
    return x;
  return y;
}
int findhead(int x) {
  if (x == head[x])
    return x;
  return head[x] = findhead(head[x]);
}
int main() {
  int i, x, y;
  long long ans = 0;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  for (i = 1; i <= n; i++)
    head[i] = i;
  for (i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    head[findhead(x + 1)] = findhead(y + 1);
  }
  p[0] = 2e9;
  for (i = 1; i <= n; i++) {
    x = findhead(i);
    mn[x] = get(mn[x], i);
  }
  for (i = 1; i <= n; i++)
    bad[mn[i]] = 1;
  for (i = 1; i <= n; i++)
    if (!bad[i])
      all.push_back(p[i]);
  sort(all.begin(), all.end());
  for (i = 1; i <= n; i++)
    if (bad[i])
      all.push_front(p[i]);
  if ((n - m - 1) * 2 > all.size())
    return !printf("Impossible");
  for (i = 0; i < (n - m - 1) * 2; i++)
    ans += all[i];
  printf("%lld", ans);
}
Problem p03440 (C++)

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

long long a[100000];
vector<int> edges[100000];

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    edges[x].push_back(y);
    edges[y].push_back(x);
  }

  if (M == N - 1) {
    cout << 0 << endl;
    return 0;
  }
  if (N < 2 * (N - M - 1)) {
    cout << "Impossible" << endl;
    return 0;
  }

  long long ans = 0;
  int conn_comp_cnt = 0;
  bool visited[100000] = {};
  for (int i = 0; i < N; i++) {
    if (!visited[i]) {
      int shortest_index = i;
      stack<int> st;
      st.push(i);
      while (!st.empty()) {
        int now = st.top();
        st.pop();
        visited[now] = true;
        if (a[shortest_index] > a[now]) {
          shortest_index = now;
        }
        for (int j = 0; j < edges[i].size(); j++) {
          if (!visited[edges[now][j]]) {
            st.push(edges[now][j]);
          }
        }
      }
      ans += a[shortest_index];
      a[shortest_index] += 1000000000;
      conn_comp_cnt++;
    }
  }
  sort(a, a + N);
  for (int i = 0; i < 2 * (N - M - 1) - conn_comp_cnt; i++) {
    ans += a[i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

long long a[100000];
vector<int> edges[100000];

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    edges[x].push_back(y);
    edges[y].push_back(x);
  }

  if (M == N - 1) {
    cout << 0 << endl;
    return 0;
  }
  if (N < 2 * (N - M - 1)) {
    cout << "Impossible" << endl;
    return 0;
  }

  long long ans = 0;
  int conn_comp_cnt = 0;
  bool visited[100000] = {};
  for (int i = 0; i < N; i++) {
    if (!visited[i]) {
      int shortest_index = i;
      stack<int> st;
      st.push(i);
      while (!st.empty()) {
        int now = st.top();
        st.pop();
        visited[now] = true;
        if (a[shortest_index] > a[now]) {
          shortest_index = now;
        }
        for (int j = 0; j < edges[now].size(); j++) {
          if (!visited[edges[now][j]]) {
            st.push(edges[now][j]);
          }
        }
      }
      ans += a[shortest_index];
      a[shortest_index] += 1000000000;
      conn_comp_cnt++;
    }
  }
  sort(a, a + N);
  for (int i = 0; i < 2 * (N - M - 1) - conn_comp_cnt; i++) {
    ans += a[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03440 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
struct UnionFind {
  vector<ll> data;
  UnionFind(ll size) : data(size, -1) {}
  bool unionSet(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(ll x, ll y) { return root(x) == root(y); }
  ll root(ll x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  ll size(ll x) { return -data[root(x)]; }
};
int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  UnionFind arr(n);
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    arr.unionSet(x, y);
  }
  set<ll> syu;
  set<ll> s[n];
  rep(i, n) syu.insert(arr.root(i));
  if ((syu.size() - 1) * 2 > n) {
    cout << "Impossible" << endl;
    return 0;
  }
  if (syu.size() == 1) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, n) { s[arr.root(i)].insert(a[i]); }
  vector<ll> v;
  rep(i, n) {
    bool ok = 1;
    for (auto r : s[i]) {
      if (ok) {
        ans += r;
        ok = 0;
      } else
        v.push_back(r);
    }
  }
  sort(v.begin(), v.end());
  rep(i, syu.size() - 2) { ans += v[i]; }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;
struct UnionFind {
  vector<ll> data;
  UnionFind(ll size) : data(size, -1) {}
  bool unionSet(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(ll x, ll y) { return root(x) == root(y); }
  ll root(ll x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  ll size(ll x) { return -data[root(x)]; }
};
int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  UnionFind arr(n);
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    arr.unionSet(x, y);
  }
  set<ll> syu;
  multiset<ll> s[n];
  rep(i, n) syu.insert(arr.root(i));
  if ((syu.size() - 1) * 2 > n) {
    cout << "Impossible" << endl;
    return 0;
  }
  if (syu.size() == 1) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, n) { s[arr.root(i)].insert(a[i]); }
  vector<ll> v;
  rep(i, n) {
    bool ok = 1;
    for (auto r : s[i]) {
      if (ok) {
        ans += r;
        ok = 0;
      } else
        v.push_back(r);
    }
  }
  sort(v.begin(), v.end());
  rep(i, syu.size() - 2) { ans += v[i]; }

  cout << ans << endl;
  return 0;
}
Problem p03440 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
using namespace std;
int par[100100], rk[100100];

int getroot(int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = getroot(par[x]);
}

void unite(int x, int y) {
  int rx = getroot(x), ry = getroot(y);
  if (rx != ry) {
    if (rk[rx] > rk[ry]) {
      par[ry] = rx;
    } else {
      par[rx] = ry;
      if (rk[rx] == rk[ry])
        rk[ry]++;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  if (n / 2 + m < n - 1) {
    cout << "Impossible\n";
    return 0;
  }
  if (m == n - 1) {
    cout << "0\n";
    return 0;
  }
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  map<int, int> costs;
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rk[i] = 0;
    costs[a[i]]++;
  }

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    unite(x, y);
  }
  int tmp[n];
  fill(tmp, tmp + n, 1 << 30);
  for (int i = 0; i < n; i++) {
    int r = getroot(i);
    tmp[r] = min(tmp[r], a[i]);
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == getroot(i)) {
      ans += tmp[i];
      costs[tmp[i]]--;
    }
  }
  int c = n - m - 2;
  auto it = costs.begin();
  while (c > 0) {
    while (it->second == 0)
      it++;
    ans += it->first;
    it->second--;
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <map>
using namespace std;
int par[100100], rk[100100];

int getroot(int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = getroot(par[x]);
}

void unite(int x, int y) {
  int rx = getroot(x), ry = getroot(y);
  if (rx != ry) {
    if (rk[rx] > rk[ry]) {
      par[ry] = rx;
    } else {
      par[rx] = ry;
      if (rk[rx] == rk[ry])
        rk[ry]++;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  if (n / 2 + m < n - 1) {
    cout << "Impossible\n";
    return 0;
  }
  if (m == n - 1) {
    cout << "0\n";
    return 0;
  }
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  map<int, int> costs;
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rk[i] = 0;
    costs[a[i]]++;
  }

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    unite(x, y);
  }
  int tmp[n];
  fill(tmp, tmp + n, 1 << 30);
  for (int i = 0; i < n; i++) {
    int r = getroot(i);
    tmp[r] = min(tmp[r], a[i]);
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == getroot(i)) {
      ans += tmp[i];
      costs[tmp[i]]--;
    }
  }
  int c = n - m - 2;
  auto it = costs.begin();
  while (c > 0) {
    while (it->second == 0)
      it++;
    ans += it->first;
    it->second--;
    c--;
  }
  cout << ans << endl;
}
Problem p03440 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define mp make_pair
#define inf 1000000007
using namespace std;
ll par[114514];
ll rankk[114514];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rankk[i] = 0;
  }
}
ll find(ll x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (rankk[x] < rankk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rankk[x] == rankk[y])
      rankk[x]++;
  }
}
vector<ll> tre[100009];
int main() {
  int n, m;
  vector<ll> a;
  cin >> n >> m;
  if (n - 1 == m) {
    cout << 0 << endl;
    return 0;
  }
  if (n < 2 * (n - m - 1)) {
    cout << "Impossible" << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.pb(x);
  }
  init(n);
  vector<ll> amari;
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    unite(x, y);
  }
  for (int i = 0; i < n; i++) {
    ll rt = find((ll)i);
    tre[rt].pb(a[i]);
  }
  ll cnt = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    if (!tre[i].empty()) {
      sort(tre[i].begin(), tre[i].end());
      cnt++;
      ans += tre[i][0];
      // cout<<tre[i][0]<<endl;
      for (int j = 1; j < tre[i].size(); i++) {
        amari.pb(tre[i][j]);
      }
    }
  }
  sort(amari.begin(), amari.end());
  for (int i = cnt; i < 2 * (n - m - 1); i++) {
    ans += amari[i - cnt];
    // cout<<amari[i-cnt]<<endl;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define mp make_pair
#define inf 1000000007
using namespace std;
ll par[114514];
ll rankk[114514];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rankk[i] = 0;
  }
}
ll find(ll x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (rankk[x] < rankk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rankk[x] == rankk[y])
      rankk[x]++;
  }
}
vector<ll> tre[100009];
int main() {
  int n, m;
  vector<ll> a;
  cin >> n >> m;
  if (n - 1 == m) {
    cout << 0 << endl;
    return 0;
  }
  if (n < 2 * (n - m - 1)) {
    cout << "Impossible" << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.pb(x);
  }
  init(n);
  vector<ll> amari;
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    unite(x, y);
  }
  for (int i = 0; i < n; i++) {
    ll rt = find((ll)i);
    tre[rt].pb(a[i]);
  }
  ll cnt = 0, ans = 0;
  for (int i = 0; i < n; i++) {
    if (!tre[i].empty()) {
      sort(tre[i].begin(), tre[i].end());
      cnt++;
      ans += tre[i][0];
      // cout<<tre[i][0]<<endl;
      for (int j = 1; j < tre[i].size(); j++) {
        amari.pb(tre[i][j]);
      }
    }
  }
  sort(amari.begin(), amari.end());
  for (int i = cnt; i < 2 * (n - m - 1); i++) {
    ans += amari[i - cnt];
    // cout<<amari[i-cnt]<<endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p03440 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int uli;
const int mx = 1e5 + 10;
vector<int> g[mx];
int a[mx];
multiset<int> s;
bool vis[mx];
void dfs(int u) {
  vis[u] = true;
  s.insert(a[u]);
  for (int v : g[u])
    if (!vis[v])
      dfs(v);
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vector<int> o;
  multiset<int> rem;
  uli ans = 0;
  uli minis = 0;
  int k = 0;
  vector<int> mini;
  for (int i = 0; i < n; i++)
    if (!vis[i]) {
      s.clear();
      dfs(i);
      if (s.size() == 1)
        o.push_back(*s.begin());
      else {
        minis += uli(*s.begin());
        mini.push_back(minis);
        for (int x : s)
          rem.insert(x);
        k++;
      }
    }
  if (k > 1) {
    ans += minis;
    for (int x : mini)
      rem.erase(rem.find(x));
  }
  for (int i = 0; i < k - 2; i++) {
    ans += *rem.begin();
    rem.erase(rem.begin());
  }
  if (!rem.empty()) {
    if (o.size() > rem.size()) {
      puts("Impossible");
      return 0;
    }
    auto it = rem.begin();
    for (int v : o) {
      ans += uli(v + *it);
      it++;
    }
  } else {
    if (o.size() > 2) {
      puts("Impossible");
      return 0;
    }
    if (o.size() == 2)
      for (int x : o)
        ans += uli(x);
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int uli;
const int mx = 1e5 + 10;
vector<int> g[mx];
int a[mx];
multiset<int> s;
bool vis[mx];
void dfs(int u) {
  vis[u] = true;
  s.insert(a[u]);
  for (int v : g[u])
    if (!vis[v])
      dfs(v);
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vector<int> o;
  multiset<int> rem;
  uli ans = 0;
  uli minis = 0;
  int k = 0;
  vector<int> mini;
  for (int i = 0; i < n; i++)
    if (!vis[i]) {
      s.clear();
      dfs(i);
      if (s.size() == 1)
        o.push_back(*s.begin());
      else {
        minis += uli(*s.begin());
        mini.push_back(*s.begin());
        for (int x : s)
          rem.insert(x);
        k++;
      }
    }
  if (k > 1) {
    ans += minis;
    for (int x : mini)
      rem.erase(rem.find(x));
  }
  for (int i = 0; i < k - 2; i++) {
    ans += *rem.begin();
    rem.erase(rem.begin());
  }
  if (!rem.empty()) {
    if (o.size() > rem.size()) {
      puts("Impossible");
      return 0;
    }
    auto it = rem.begin();
    for (int v : o) {
      ans += uli(v + *it);
      it++;
    }
  } else {
    if (o.size() > 2) {
      puts("Impossible");
      return 0;
    }
    if (o.size() == 2)
      for (int x : o)
        ans += uli(x);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03440 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD (ll)1000003
// 1000000007
#define P pair<ll, ll>

ll n, m, cnt, s[110000], ans;
bool used[110000];
vector<ll> list, g[110000], now;

void dfs(ll a) {
  used[a] = true;
  now.push_back(s[a]);
  REP(i, g[a].size()) {
    if (!used[g[a][i]])
      dfs(g[a][i]);
  }
}
int main() {
  cin >> n >> m;
  REP(i, n) cin >> s[i];
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  REP(i, n) {
    now.resize(0);
    if (!used[i]) {
      cnt++;
      dfs(i);
      sort(ALL(now));
      ans += now[0];
      FOR(j, 1, now.size()) { list.push_back(now[i]); }
    }
  }
  if (cnt == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (list.size() < cnt - 2) {
    cout << "Impossible" << endl;
    return 0;
  }
  sort(ALL(list));
  REP(i, cnt - 2) { ans += list[i]; }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD (ll)1000003
// 1000000007
#define P pair<ll, ll>

ll n, m, cnt, s[110000], ans;
bool used[110000];
vector<ll> list, g[110000], now;

void dfs(ll a) {
  used[a] = true;
  now.push_back(s[a]);
  REP(i, g[a].size()) {
    if (!used[g[a][i]])
      dfs(g[a][i]);
  }
}
int main() {
  cin >> n >> m;
  REP(i, n) cin >> s[i];
  REP(i, m) {
    ll a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  REP(i, n) {
    now.resize(0);
    if (!used[i]) {
      cnt++;
      dfs(i);
      sort(ALL(now));
      ans += now[0];
      FOR(j, 1, now.size()) { list.push_back(now[j]); }
    }
  }
  if (cnt == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (list.size() < cnt - 2) {
    cout << "Impossible" << endl;
    return 0;
  }
  sort(ALL(list));
  REP(i, cnt - 2) { ans += list[i]; }
  cout << ans << endl;
}
Problem p03440 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
class dsu {
public:
  vector<int> p;
  int n;
  dsu(int _n) : n(_n) {
    p.resize(n);
    iota(p.begin(), p.end(), 0);
  }
  inline int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); }
  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      return true;
    }
    return false;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  dsu d(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    d.unite(x, y);
  }
  vector<vector<int>> comp(n);
  for (int i = 0; i < n; i++) {
    comp[d.get(i)].push_back(a[i]);
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!comp[i].empty()) {
      cnt++;
    }
  }
  if (cnt == 1) {
    puts("0");
    return 0;
  }
  int choose = 2 * cnt - 2;
  long long ans = 0;
  vector<int> other;
  for (int i = 0; i < n; i++) {
    if (!comp[i].empty()) {
      sort(comp[i].begin(), comp[i].end());
      ans += comp[i][0];
      choose--;
      for (int j = 1; j < (int)comp.size(); j++) {
        other.push_back(comp[i][j]);
      }
    }
  }
  if (choose > (int)other.size()) {
    puts("Impossible");
    return 0;
  }
  sort(other.begin(), other.end());
  for (int i = 0; i < choose; i++) {
    ans += other[i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
class dsu {
public:
  vector<int> p;
  int n;
  dsu(int _n) : n(_n) {
    p.resize(n);
    iota(p.begin(), p.end(), 0);
  }
  inline int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); }
  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      return true;
    }
    return false;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  dsu d(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    d.unite(x, y);
  }
  vector<vector<int>> comp(n);
  for (int i = 0; i < n; i++) {
    comp[d.get(i)].push_back(a[i]);
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!comp[i].empty()) {
      cnt++;
    }
  }
  if (cnt == 1) {
    puts("0");
    return 0;
  }
  int choose = 2 * cnt - 2;
  long long ans = 0;
  vector<int> other;
  for (int i = 0; i < n; i++) {
    if (!comp[i].empty()) {
      sort(comp[i].begin(), comp[i].end());
      ans += comp[i][0];
      choose--;
      for (int j = 1; j < (int)comp[i].size(); j++) {
        other.push_back(comp[i][j]);
      }
    }
  }
  if (choose > (int)other.size()) {
    puts("Impossible");
    return 0;
  }
  sort(other.begin(), other.end());
  for (int i = 0; i < choose; i++) {
    ans += other[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03440 (C++)

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

const int N = 1e5 + 10;

int n, m, a[N];
vector<int> g[N];

vector<int> vals;
bool vis[N];
void dfs(int u) {
  if (vis[u])
    return;
  vis[u] = 1;
  vals.push_back(a[u]);
  for (auto v : g[u])
    dfs(v);
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int iter = m;
  while (iter--) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  if (n == 1)
    return cout << 0, 0;

  bool good = n >= 2 * (n - m - 1);
  if (!good)
    return cout << "Impossible\n", 0;

  vector<int> rest;
  int deg = 2 * (n - m - 1);
  long long ans = 0;
  for (int u = 0; u < n; u++)
    if (!vis[u]) {
      vals.clear();
      dfs(u);
      sort(vals.begin(), vals.end(), greater<int>());

      ans += vals.back();
      vals.pop_back();
      deg--;
      for (auto x : vals)
        rest.push_back(x);
    }

  sort(rest.begin(), rest.end(), greater<int>());
  while (deg--) {
    ans += rest.back();
    rest.pop_back();
  }
  cout << ans << endl;

  return 0;
}


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

const int N = 1e5 + 10;

int n, m, a[N];
vector<int> g[N];

vector<int> vals;
bool vis[N];
void dfs(int u) {
  if (vis[u])
    return;
  vis[u] = 1;
  vals.push_back(a[u]);
  for (auto v : g[u])
    dfs(v);
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int iter = m;
  while (iter--) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  if (n - m == 1)
    return cout << 0, 0;

  bool good = n >= 2 * (n - m - 1);
  if (!good)
    return cout << "Impossible\n", 0;

  vector<int> rest;
  int deg = 2 * (n - m - 1);
  long long ans = 0;
  for (int u = 0; u < n; u++)
    if (!vis[u]) {
      vals.clear();
      dfs(u);
      sort(vals.begin(), vals.end(), greater<int>());

      ans += vals.back();
      vals.pop_back();
      deg--;
      for (auto x : vals)
        rest.push_back(x);
    }

  sort(rest.begin(), rest.end(), greater<int>());
  while (deg--) {
    ans += rest.back();
    rest.pop_back();
  }
  cout << ans << endl;

  return 0;
}
Problem p03440 (C++)

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

int N, M;
int A[100000];
vector<int> G[100000];
bool used[100000];
multiset<int> ms[100000];

void DFS(int from, int prev, int start) {
  used[from] = true;
  ms[start].insert(A[from]);
  for (int to : G[from]) {
    if (to == prev || used[to])
      continue;
    DFS(to, from, start);
  }
}

int main() {
  cin >> N >> M;
  if (M + 1 == N) {
    cout << 0 << endl;
    return 0;
  }

  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> tr;
  for (int i = 0; i < N; ++i) {
    if (used[i])
      continue;
    tr.push_back(i);
    DFS(i, -1, i);
  }

  long long sum = 0;
  vector<int> v;
  for (int i = 0; i < tr.size(); ++i) {
    sum += *tr.begin();
    for (auto it = next(tr.begin()); it != tr.end(); ++it) {
      v.push_back(*it);
    }
  }

  sort(v.begin(), v.end());
  if (v.size() < tr.size() - 2) {
    cout << "Impossible" << endl;
    return 0;
  }

  for (int i = 0; i < tr.size() - 2; ++i) {
    sum += v[i];
  }
  cout << sum << endl;

  return 0;
}


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

int N, M;
int A[100000];
vector<int> G[100000];
bool used[100000];
multiset<int> ms[100000];

void DFS(int from, int prev, int start) {
  used[from] = true;
  ms[start].insert(A[from]);
  for (int to : G[from]) {
    if (to == prev || used[to])
      continue;
    DFS(to, from, start);
  }
}

int main() {
  cin >> N >> M;
  if (M + 1 == N) {
    cout << 0 << endl;
    return 0;
  }

  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < M; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<int> tr;
  for (int i = 0; i < N; ++i) {
    if (used[i])
      continue;
    tr.push_back(i);
    DFS(i, -1, i);
  }

  long long sum = 0;
  vector<int> v;
  for (int i = 0; i < tr.size(); ++i) {
    multiset<int> &z = ms[tr[i]];
    sum += *z.begin();
    for (auto it = next(z.begin()); it != z.end(); ++it) {
      v.push_back(*it);
    }
  }

  sort(v.begin(), v.end());
  if (v.size() < tr.size() - 2) {
    cout << "Impossible" << endl;
    return 0;
  }

  for (int i = 0; i < tr.size() - 2; ++i) {
    sum += v[i];
  }
  cout << sum << endl;

  return 0;
}