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

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

const int N = 2e5 + 5;
const int M = 5e5 + 5;
int n, u, v, x, sz[N], gra, gra2;
int E, ot[M], ne[M], g[N], w[M];
long long ans;

void add(int u, int v, int x) {
  ot[++E] = v;
  ne[E] = g[u];
  g[u] = E;
  w[E] = x;
}

void init() {
  scanf("%d", &n);
  int i;
  for (i = 1; i < n; ++i) {
    scanf("%d%d%d", &u, &v, &x);
    add(u, v, x), add(v, u, x);
  }
}

void dfs(int x, int fath) {
  int p, to;
  sz[x] = 1;
  int fl = 0;
  for (p = g[x]; p; p = ne[p]) {
    to = ot[p];
    if (to == fath)
      continue;
    dfs(to, x);
    ans += 2LL * min(sz[to], n - sz[to]) * w[p];
    if (sz[to] > n / 2)
      fl = 1;
    sz[x] += sz[to];
  }
  if (n - sz[x] > n / 2)
    fl = 1;
  if (!fl) {
    if (!gra)
      gra = x;
    else
      gra2 = x;
  }
}

void work() {
  if (gra && gra2) {
    int p, to;
    for (p = g[gra]; p; p = ne[p]) {
      to = ot[p];
      if (to == gra2)
        ans -= w[p], printf("%lld\n", ans), exit(0);
    }
  } else {
    int p, to, mi = 1e9 + 7;
    for (p = g[x]; p; p = ne[p]) {
      to = ot[p];
      mi = min(mi, w[p]);
    }
    printf("%lld\n", ans - mi);
  }
}

int main() {
  init();
  dfs(1, 0);
  work();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

const int N = 2e5 + 5;
const int M = 5e5 + 5;
int n, u, v, x, sz[N], gra, gra2;
int E, ot[M], ne[M], g[N], w[M];
long long ans;

void add(int u, int v, int x) {
  ot[++E] = v;
  ne[E] = g[u];
  g[u] = E;
  w[E] = x;
}

void init() {
  scanf("%d", &n);
  int i;
  for (i = 1; i < n; ++i) {
    scanf("%d%d%d", &u, &v, &x);
    add(u, v, x), add(v, u, x);
  }
}

void dfs(int x, int fath) {
  int p, to;
  sz[x] = 1;
  int fl = 0;
  for (p = g[x]; p; p = ne[p]) {
    to = ot[p];
    if (to == fath)
      continue;
    dfs(to, x);
    ans += 2LL * min(sz[to], n - sz[to]) * w[p];
    if (sz[to] > n / 2)
      fl = 1;
    sz[x] += sz[to];
  }
  if (n - sz[x] > n / 2)
    fl = 1;
  if (!fl) {
    if (!gra)
      gra = x;
    else
      gra2 = x;
  }
}

void work() {
  if (gra && gra2) {
    int p, to;
    for (p = g[gra]; p; p = ne[p]) {
      to = ot[p];
      if (to == gra2)
        ans -= w[p], printf("%lld\n", ans), exit(0);
    }
  } else {
    int p, to, mi = 1e9 + 7;
    for (p = g[gra]; p; p = ne[p]) {
      to = ot[p];
      mi = min(mi, w[p]);
    }
    printf("%lld\n", ans - mi);
  }
}

int main() {
  init();
  dfs(1, 0);
  work();
  return 0;
}
Problem p03654 (C++)

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

typedef pair<int, int> P;

struct Centroid {
  vector<int> centroid;
  vector<int> sz;
  vector<int> esub;
  int N;

  void dfs(int i, int prev, const vector<P> G[]) {
    sz[i] = 1;
    bool is_centroid = true;
    for (auto p : G[i]) {
      int j = p.first, e = p.second;
      if (j == prev)
        continue;
      dfs(j, i, G);
      sz[i] += sz[j];
      if (sz[j] > N / 2)
        is_centroid = false;
      esub[e] = min(sz[j], N - sz[j]);
    }
    if (N - sz[i] > N / 2)
      is_centroid = false;
    if (is_centroid)
      centroid.push_back(i);
  }

  vector<int> calculate(int Nin, const vector<P> G[]) {
    N = Nin;
    sz.resize(N);
    esub.resize(N - 1);
    dfs(0, -1, G);
    return centroid;
  }
};

int main() {
  const int M = 1000;
  int N, A[M], B[M];
  int64_t C[M];
  vector<P> edges[M];
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> A[i] >> B[i] >> C[i];
    A[i]--;
    B[i]--;
    edges[A[i]].push_back({B[i], i});
    edges[B[i]].push_back({A[i], i});
  }

  Centroid CT;
  auto ct = CT.calculate(N, edges);

  int64_t ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ans += min(2 * CT.esub[i], N - 1) * C[i];
  }
  if (ct.size() == 1) {
    int64_t mn = 1e9;
    for (int i = 0; i < N - 1; i++)
      if (A[i] == ct[0] || B[i] == ct[0])
        mn = min(mn, C[i]);
    ans -= mn;
  }
  cout << ans << endl;
  return 0;
}

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

typedef pair<int, int> P;

struct Centroid {
  vector<int> centroid;
  vector<int> sz;
  vector<int> esub;
  int N;

  void dfs(int i, int prev, const vector<P> G[]) {
    sz[i] = 1;
    bool is_centroid = true;
    for (auto p : G[i]) {
      int j = p.first, e = p.second;
      if (j == prev)
        continue;
      dfs(j, i, G);
      sz[i] += sz[j];
      if (sz[j] > N / 2)
        is_centroid = false;
      esub[e] = min(sz[j], N - sz[j]);
    }
    if (N - sz[i] > N / 2)
      is_centroid = false;
    if (is_centroid)
      centroid.push_back(i);
  }

  vector<int> calculate(int Nin, const vector<P> G[]) {
    N = Nin;
    sz.resize(N);
    esub.resize(N - 1);
    dfs(0, -1, G);
    return centroid;
  }
};

int main() {
  const int M = 100000;
  int N, A[M], B[M];
  int64_t C[M];
  vector<P> edges[M];
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> A[i] >> B[i] >> C[i];
    A[i]--;
    B[i]--;
    edges[A[i]].push_back({B[i], i});
    edges[B[i]].push_back({A[i], i});
  }

  Centroid CT;
  auto ct = CT.calculate(N, edges);

  int64_t ans = 0;
  for (int i = 0; i < N - 1; i++) {
    ans += min(2 * CT.esub[i], N - 1) * C[i];
  }
  if (ct.size() == 1) {
    int64_t mn = 1e9;
    for (int i = 0; i < N - 1; i++)
      if (A[i] == ct[0] || B[i] == ct[0])
        mn = min(mn, C[i]);
    ans -= mn;
  }
  cout << ans << endl;
  return 0;
}
Problem p03654 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define LL long long
#define fo(i, j, k) for (int i = j; i <= k; i++)
#define fr(i, j) for (int i = beg[j]; i; i = nex[i])
using namespace std;
int const mn = 1e2 + 3, mm = 2e5 + 3, inf = 1e9 + 7;
int n, rt, gra, beg[mn], to[mm], len[mm], nex[mm], size[mn], f[mn];
LL ans;
void ins(int u, int v, int w) {
  to[++gra] = v;
  len[gra] = w;
  nex[gra] = beg[u];
  beg[u] = gra;
}
void dfs(int p, int q) {
  size[p] = 1;
  fr(i, p) if (to[i] != q) {
    dfs(to[i], p);
    ans += 2ll * len[i] * min(size[to[i]], n - size[to[i]]);
    size[p] += size[to[i]];
    f[p] = max(f[p], size[to[i]]);
  }
  f[p] = max(f[p], n - size[p]);
}
int main() {
  scanf("%d", &n);
  fo(i, 1, n - 1) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    ins(u, v, w);
    ins(v, u, w);
  }
  rt = 1;
  dfs(1, 0);
  fo(i, 2, n) if (f[i] < f[rt]) rt = i;
  int mi = inf;
  fr(i, rt) if (f[to[i]] == f[rt]) {
    printf("%lld", ans - len[i]);
    return 0;
  }
  else mi = min(mi, len[i]);
  printf("%lld", ans - mi);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define LL long long
#define fo(i, j, k) for (int i = j; i <= k; i++)
#define fr(i, j) for (int i = beg[j]; i; i = nex[i])
using namespace std;
int const mn = 1e5 + 3, mm = 2e5 + 3, inf = 1e9 + 7;
int n, rt, gra, beg[mn], to[mm], len[mm], nex[mm], size[mn], f[mn];
LL ans;
void ins(int u, int v, int w) {
  to[++gra] = v;
  len[gra] = w;
  nex[gra] = beg[u];
  beg[u] = gra;
}
void dfs(int p, int q) {
  size[p] = 1;
  fr(i, p) if (to[i] != q) {
    dfs(to[i], p);
    ans += 2ll * len[i] * min(size[to[i]], n - size[to[i]]);
    size[p] += size[to[i]];
    f[p] = max(f[p], size[to[i]]);
  }
  f[p] = max(f[p], n - size[p]);
}
int main() {
  scanf("%d", &n);
  fo(i, 1, n - 1) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    ins(u, v, w);
    ins(v, u, w);
  }
  rt = 1;
  dfs(1, 0);
  fo(i, 2, n) if (f[i] < f[rt]) rt = i;
  int mi = inf;
  fr(i, rt) if (f[to[i]] == f[rt]) {
    printf("%lld", ans - len[i]);
    return 0;
  }
  else mi = min(mi, len[i]);
  printf("%lld", ans - mi);
  return 0;
}
Problem p03654 (C++)

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

using namespace std;

L n, ans;

vector<L> v[100010], l[100010];

L chk[100010], sz[100010], up[100010], bu[100010];

void dfs(L x) {
  sz[x] = 1;
  L i;
  for (i = 0; i < v[x].size(); i++) {
    if (!chk[v[x][i]]) {
      chk[v[x][i]] = 1;
      up[v[x][i]] = l[x][i];
      bu[v[x][i]] = x;
      dfs(v[x][i]);
      sz[x] += sz[v[x][i]];
    }
  }
}

int main() {
  scanf("%lld", &n);
  L i;
  for (i = 1; i < n; i++) {
    L s, e, d;
    scanf("%lld %lld %lld", &s, &e, &d);
    v[s].push_back(e);
    v[e].push_back(s);
    l[s].push_back(d);
    l[e].push_back(d);
  }
  chk[1] = 1;
  dfs(1);
  L temp = 1;
  while (1) {
    L ma = 0, mad;
    // puts("1");
    for (i = 0; i < v[temp].size(); i++) {
      // printf("%lld %lld %lld %lld %lld
      // %lld\n",temp,i,v[temp][i],bu[v[temp][i]],sz[v[temp][i]],ma);
      if (bu[temp] != v[temp][i] && sz[v[temp][i]] > ma) {
        // puts("123");
        ma = sz[v[temp][i]];
        mad = v[temp][i];
      }
    }
    temp = mad;
    // puts("123");
    if (sz[temp] <= n / 2)
      break;
  }
  // puts("123");
  for (i = 1; i <= n; i++) {
    ans += up[i] * 2 * min(sz[i], n - sz[i]);
  }
  if (n % 2 == 0) {
    while (1)
      ;
    ans -= up[temp];
  } else {
    temp = bu[temp];
    L mi = up[temp];
    for (i = 0; i < v[temp].size(); i++) {
      if (l[temp][i] < mi) {
        mi = l[temp][i];
      }
    }
    ans -= mi;
  }
  printf("%lld", ans);
}

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

using namespace std;

L n, ans;

vector<L> v[100010], l[100010];

L chk[100010], sz[100010], up[100010], bu[100010];

void dfs(L x) {
  sz[x] = 1;
  L i;
  for (i = 0; i < v[x].size(); i++) {
    if (!chk[v[x][i]]) {
      chk[v[x][i]] = 1;
      up[v[x][i]] = l[x][i];
      bu[v[x][i]] = x;
      dfs(v[x][i]);
      sz[x] += sz[v[x][i]];
    }
  }
}

int main() {
  scanf("%lld", &n);
  L i;
  for (i = 1; i < n; i++) {
    L s, e, d;
    scanf("%lld %lld %lld", &s, &e, &d);
    v[s].push_back(e);
    v[e].push_back(s);
    l[s].push_back(d);
    l[e].push_back(d);
  }
  chk[1] = 1;
  dfs(1);
  L temp = 1;
  while (1) {
    L ma = 0, mad;
    // puts("1");
    for (i = 0; i < v[temp].size(); i++) {
      // printf("%lld %lld %lld %lld %lld
      // %lld\n",temp,i,v[temp][i],bu[v[temp][i]],sz[v[temp][i]],ma);
      if (bu[temp] != v[temp][i] && sz[v[temp][i]] > ma) {
        // puts("123");
        ma = sz[v[temp][i]];
        mad = v[temp][i];
      }
    }
    temp = mad;
    // puts("123");
    if (sz[temp] <= n / 2)
      break;
  }
  // puts("123");
  for (i = 1; i <= n; i++) {
    ans += up[i] * 2 * min(sz[i], n - sz[i]);
  }
  if (n % 2 == 0 && sz[temp] == n / 2) {
    ans -= up[temp];
  } else {
    temp = bu[temp];
    L mi = up[temp];
    for (i = 0; i < v[temp].size(); i++) {
      if (l[temp][i] < mi) {
        mi = l[temp][i];
      }
    }
    ans -= mi;
  }
  printf("%lld", ans);
}
Problem p03656 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
const int N = 2e5 + 5, M = 3 * N;
using namespace std;

bool vis[M], f[M];
int n, e, ter[M], nxt[M], lnk[N], rec[M], sza[N], szb[N], pos[N];

void add(int x, int y) {
  ter[++e] = y, nxt[e] = lnk[x], lnk[x] = e, rec[e] = e + 1;
  ter[++e] = x, nxt[e] = lnk[y], lnk[y] = e, rec[e] = e - 1;
}

void dfs(int p) {
  for (int i = lnk[p]; i; i = nxt[i])
    if (vis[i]) {
      int u = ter[i];
      lnk[p] = nxt[i];
      vis[i] = vis[rec[i]] = false, f[i] = true;
      dfs(u);
    } else
      lnk[p] = nxt[i];
}

int main() {
  scanf("%d", &n);
  int x;
  rep(i, 1, n) {
    scanf("%d", &x);
    if (x == -1)
      add(2 * n + 1, i);
    else
      add(x, i), ++sza[x];
  }
  rep(i, 1, n) {
    scanf("%d", &x);
    if (x == -1)
      add(2 * n + 1, n + i);
    else
      add(x + n, i + n), ++szb[x];
  }
  rep(i, 1, n) if ((sza[i] & 1) != (szb[i] & 1)) {
    puts("IMPOSSIBLE");
    return 0;
  }
  rep(i, 1, n) if (sza[i] & 1);
  else add(i, i + n), pos[i] = e;
  puts("POSSIBLE");
  rep(i, 1, e) vis[i] = true;
  dfs(2 * n + 1);
  rep(i, 1, n) if (sza[i] & 1) printf("0 ");
  else {
    if (f[pos[i]])
      printf("1 ");
    else
      printf("-1 ");
  }
  puts("");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
const int N = 2e5 + 5, M = 3 * N;
using namespace std;

bool vis[M], f[M];
int n, e, ter[M], nxt[M], lnk[N], rec[M], sza[N], szb[N], pos[N];

void add(int x, int y) {
  ter[++e] = y, nxt[e] = lnk[x], lnk[x] = e, rec[e] = e + 1;
  ter[++e] = x, nxt[e] = lnk[y], lnk[y] = e, rec[e] = e - 1;
}

void dfs(int p) {
  for (int i = lnk[p]; i; i = lnk[p])
    if (vis[i]) {
      int u = ter[i];
      lnk[p] = nxt[i];
      vis[i] = vis[rec[i]] = false, f[i] = true;
      dfs(u);
    } else
      lnk[p] = nxt[i];
}

int main() {
  scanf("%d", &n);
  int x;
  rep(i, 1, n) {
    scanf("%d", &x);
    if (x == -1)
      add(2 * n + 1, i);
    else
      add(x, i), ++sza[x];
  }
  rep(i, 1, n) {
    scanf("%d", &x);
    if (x == -1)
      add(2 * n + 1, n + i);
    else
      add(x + n, i + n), ++szb[x];
  }
  rep(i, 1, n) if ((sza[i] & 1) != (szb[i] & 1)) {
    puts("IMPOSSIBLE");
    return 0;
  }
  rep(i, 1, n) if (sza[i] & 1);
  else add(i, i + n), pos[i] = e;
  puts("POSSIBLE");
  rep(i, 1, e) vis[i] = true;
  dfs(2 * n + 1);
  rep(i, 1, n) if (sza[i] & 1) printf("0 ");
  else {
    if (f[pos[i]])
      printf("1 ");
    else
      printf("-1 ");
  }
  puts("");
  return 0;
}
Problem p03656 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back

struct edge {
  int to, ab;
};
int N, cnt, t1 = -1, t2, f, p;
vector<edge> g[100000];
int X[100000];

void dfs(int u) {
  while (!g[u].empty()) {
    edge e = g[u].back();
    int v = e.to;
    if (e.ab) {
      X[u]--;
      X[v]++;
    } else {
      if (u == t2)
        p++;
      if (v == t2)
        p--;
    }
    g[u].pop_back();
    rep(i, g[v].size()) {
      if (g[v][i].to == u && g[v][i].ab == e.ab) {
        g[v].erase(g[v].begin() + i);
        break;
      }
    }
    dfs(v);
  }
}

int main() {
  cin >> N;
  rep(i, 2 * N) {
    int v;
    cin >> v;
    v--;
    if (v != -2) {
      g[i % N].pb({v, (i / N)});
      g[v].pb({i % N, (i / N)});
    } else {
      if (t1 == -1)
        t1 = i % N;
      else
        t2 = i % N;
    }
  }
  if (t1 == t2) {
    rep(i, N) {
      if (g[i].size() % 2 == 1)
        f = 1;
    }
  } else {
    rep(i, N) {
      if (i == t1 || i == t2) {
        if (g[i].size() % 2 == 0)
          f = 1;
      } else if (g[i].size() % 2 == 1)
        f = 1;
    }
  }
  if (f)
    cout << "IMPOSSIBLE"
         << "\n";
  else {
    dfs(t1);
    cout << "POSSIBLE"
         << "\n";
    if (t1 == t2)
      X[t1]++;
    else
      X[t2] = p;
    rep(i, N) cout << X[i] << " ";
    cout << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#pragma GCC optimize("Ofast")
struct edge {
  int to, ab;
};
int N, cnt, t1 = -1, t2, f, p;
vector<edge> g[100000];
int X[100000];

void dfs(int u) {
  while (!g[u].empty()) {
    edge e = g[u].back();
    int v = e.to;
    if (e.ab) {
      X[u]--;
      X[v]++;
    } else {
      if (u == t2)
        p++;
      if (v == t2)
        p--;
    }
    g[u].pop_back();
    rep(i, g[v].size()) {
      if (g[v][i].to == u && g[v][i].ab == e.ab) {
        g[v].erase(g[v].begin() + i);
        break;
      }
    }
    dfs(v);
  }
}

int main() {
  cin >> N;
  rep(i, 2 * N) {
    int v;
    cin >> v;
    v--;
    if (v != -2) {
      g[i % N].pb({v, (i / N)});
      g[v].pb({i % N, (i / N)});
    } else {
      if (t1 == -1)
        t1 = i % N;
      else
        t2 = i % N;
    }
  }
  if (t1 == t2) {
    rep(i, N) {
      if (g[i].size() % 2 == 1)
        f = 1;
    }
  } else {
    rep(i, N) {
      if (i == t1 || i == t2) {
        if (g[i].size() % 2 == 0)
          f = 1;
      } else if (g[i].size() % 2 == 1)
        f = 1;
    }
  }
  if (f)
    cout << "IMPOSSIBLE"
         << "\n";
  else {
    dfs(t1);
    cout << "POSSIBLE"
         << "\n";
    if (t1 == t2)
      X[t1]++;
    else
      X[t2] = p;
    rep(i, N) cout << X[i] << " ";
    cout << "\n";
  }
  return 0;
}
Problem p03656 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const int N = 200010, P = 998244353;
inline int read() {
  int x = 0, f = 0, c = getchar();
  for (; c > '9' || c < '0'; f = c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  return f ? -x : x;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int L = 1, h[N], ne[N], to[N], f[N], sz[N], n, g[N];
void addl(int x, int y) {
  ne[++L] = h[x];
  h[x] = L;
  to[L] = y;
  ne[++L] = h[y];
  h[y] = L;
  to[L] = x;
}
int tp, st[N];
void dfs(int x) {
  int k;
  for (k = h[x], h[x] = ne[h[x]]; k; k = h[x], h[x] = ne[h[x]])
    if (!f[k / 2])
      f[k / 2] = k % 2 + 1, dfs(to[k]);
  st[++tp] = x;
}
int main() {
  // freopen("1.out","w",stdout);
  int i;
  n = read();
  for (i = 1; i <= n + n; i++) {
    int x = read();
    if (x < 0)
      x = n + n + 1;
    else if (i > n)
      x += n;
    sz[x]++;
    addl(x, i);
  }
  for (i = 1; i <= n; i++)
    if (sz[i] % 2 != sz[i + n] % 2) {
      puts("IMPOSSIBLE");
      return 0;
    } else {
      if (sz[i] % 2 == 0)
        addl(i, n + i), g[i] = L / 2;
    }
  dfs(n + n + 1);
  for (i = 1; i <= n; i++)
    if (sz[i] % 2 == 0) {
      if (f[g[i]] == 2)
        g[i] = -1;
      else
        g[i] = 1;
      g[i + n] = -g[i];
    }
  puts("POSSIBLE");
  for (i = 1; i <= n; i++)
    printf("%d ", g[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
using namespace std;
const int N = 1000010, P = 998244353;
inline int read() {
  int x = 0, f = 0, c = getchar();
  for (; c > '9' || c < '0'; f = c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    x = (x << 1) + (x << 3) + c - '0';
  return f ? -x : x;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int L = 1, h[N], ne[N], to[N], f[N], sz[N], n, g[N];
void addl(int x, int y) {
  ne[++L] = h[x];
  h[x] = L;
  to[L] = y;
  ne[++L] = h[y];
  h[y] = L;
  to[L] = x;
}
int tp, st[N];
void dfs(int x) {
  int k;
  for (k = h[x], h[x] = ne[h[x]]; k; k = h[x], h[x] = ne[h[x]])
    if (!f[k / 2])
      f[k / 2] = k % 2 + 1, dfs(to[k]);
  st[++tp] = x;
}
int main() {
  // freopen("1.out","w",stdout);
  int i;
  n = read();
  for (i = 1; i <= n + n; i++) {
    int x = read();
    if (x < 0)
      x = n + n + 1;
    else if (i > n)
      x += n;
    sz[x]++;
    addl(x, i);
  }
  for (i = 1; i <= n; i++)
    if (sz[i] % 2 != sz[i + n] % 2) {
      puts("IMPOSSIBLE");
      return 0;
    } else {
      if (sz[i] % 2 == 0)
        addl(i, n + i), g[i] = L / 2;
    }
  dfs(n + n + 1);
  for (i = 1; i <= n; i++)
    if (sz[i] % 2 == 0) {
      if (f[g[i]] == 2)
        g[i] = -1;
      else
        g[i] = 1;
      g[i + n] = -g[i];
    }
  puts("POSSIBLE");
  for (i = 1; i <= n; i++)
    printf("%d ", g[i]);
  return 0;
}
Problem p03656 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define N 400010
using namespace std;
int nxt[N << 2], to[N << 2], head[N], cnt = 1;
int in[N];
void add(int u, int v) {
  nxt[++cnt] = head[u];
  to[cnt] = v;
  head[u] = cnt;
  in[u]++;
  nxt[++cnt] = head[v];
  to[cnt] = u;
  head[v] = cnt;
  in[v]++;
}
int num[N], n;
bool cut[N << 2], in2[N];
void dfs(int u, int pre) {
  for (int i = head[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == pre || cut[i])
      continue;
    cut[i] = cut[i ^ 1] = true;
    if (in2[u]) {
      if (u <= n && v > n)
        num[u] = num[v] = 1;
      if (u > n && v <= n)
        num[u] = num[v] = -1;
    }
    dfs(v, u);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int u;
    scanf("%d", &u);
    if (u == -1)
      u = 0;
    add(u, i);
  }
  for (int i = 1; i <= n; i++) {
    int u;
    scanf("%d", &u);
    if (u == -1)
      u = 0;
    else
      u = u + n;
    add(u, i + n);
  }
  for (int i = 1; i <= 2 * n; i++)
    in2[i] = in[i] & 1;
  for (int i = 1; i <= n; i++)
    if (in2[i] != in2[i + n]) {
      puts("IMPOSSIBLE");
      return 0;
    }
  puts("POSSIBLE");
  for (int i = 1; i <= n; i++)
    if (in2[i])
      add(i, i + n);
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d ", num[i]);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define N 400010
using namespace std;
int nxt[N << 2], to[N << 2], head[N], cnt = 1;
int in[N];
void add(int u, int v) {
  nxt[++cnt] = head[u];
  to[cnt] = v;
  head[u] = cnt;
  in[u]++;
  nxt[++cnt] = head[v];
  to[cnt] = u;
  head[v] = cnt;
  in[v]++;
}
int num[N], n;
bool cut[N << 2], in2[N];
void dfs(int u, int pre) {
  for (int &i = head[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == pre || cut[i])
      continue;
    cut[i] = cut[i ^ 1] = true;
    if (in2[u]) {
      if (u <= n && v > n)
        num[u] = num[v] = 1;
      if (u > n && v <= n)
        num[u] = num[v] = -1;
    }
    dfs(v, u);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int u;
    scanf("%d", &u);
    if (u == -1)
      u = 0;
    add(u, i);
  }
  for (int i = 1; i <= n; i++) {
    int u;
    scanf("%d", &u);
    if (u == -1)
      u = 0;
    else
      u = u + n;
    add(u, i + n);
  }
  for (int i = 1; i <= 2 * n; i++)
    in2[i] = in[i] & 1;
  for (int i = 1; i <= n; i++)
    if (in2[i] != in2[i + n]) {
      puts("IMPOSSIBLE");
      return 0;
    }
  puts("POSSIBLE");
  for (int i = 1; i <= n; i++)
    if (in2[i])
      add(i, i + n);
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d ", num[i]);
  return 0;
}
Problem p03656 (C++)

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

const int N = 100010;
struct edge {
  int s, t, p, n;
} e[4 * N];
int n, tot = 1, a[N], b[N], h[N], d[N], ans[N];

void link(int u, int v) {
  e[++tot] = (edge){u, v, 0, h[u]}, h[u] = tot, d[u]++;
  e[++tot] = (edge){v, u, 0, h[v]}, h[v] = tot, d[v]++;
}

void dfs(int x) {
  for (int i = h[x], y; y = e[i].t, h[x] = e[h[x]].n, i; i = e[i].n)
    if (!e[i].p) {
      e[i].p = e[i ^ 1].p = 1, dfs(y);
      if (i & 2)
        ans[a[x] == e[i].t ? x : e[i].t] = (i & 1 ? -1 : 1);
    }
}

void calc(int x) {
  for (int i = h[x]; i; i = e[i].n)
    ans[x] -= ans[i], calc(i);
}

void work() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i] = (a[i] == -1 ? n + 1 : a[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &b[i]), b[i] = (b[i] == -1 ? n + 1 : b[i]);
  for (int i = 1; i <= n; i++)
    link(a[i], i), link(b[i], i);
  for (int i = 1; i <= n; i++)
    if (d[i] & 1)
      puts("IMPOSSIBLE"), exit(0);
  puts("POSSIBLE");
  dfs(1);
  for (int i = 1; i <= n + 1; i++)
    h[i] = 0;
  for (int i = 1; i <= n; i++)
    e[i] = (edge){a[i], i, 0, h[a[i]]}, h[a[i]] = i;
  calc(n + 1);
  for (int i = 1; i <= n; i++)
    printf("%d ", ans[i]);
}

int main() {
  work();
  return 0;
}


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

const int N = 100010;
struct edge {
  int s, t, p, n;
} e[4 * N];
int n, tot = 1, a[N], b[N], h[N], d[N], ans[N];

void link(int u, int v) {
  e[++tot] = (edge){u, v, 0, h[u]}, h[u] = tot, d[u]++;
  e[++tot] = (edge){v, u, 0, h[v]}, h[v] = tot, d[v]++;
}

void dfs(int x) {
  for (int i = h[x], y; y = e[i].t, h[x] = e[h[x]].n, i; i = h[x])
    if (!e[i].p) {
      e[i].p = e[i ^ 1].p = 1, dfs(y);
      if (i & 2)
        ans[a[x] == e[i].t ? x : e[i].t] = (i & 1 ? -1 : 1);
    }
}

void calc(int x) {
  for (int i = h[x]; i; i = e[i].n)
    ans[x] -= ans[i], calc(i);
}

void work() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i] = (a[i] == -1 ? n + 1 : a[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &b[i]), b[i] = (b[i] == -1 ? n + 1 : b[i]);
  for (int i = 1; i <= n; i++)
    link(a[i], i), link(b[i], i);
  for (int i = 1; i <= n; i++)
    if (d[i] & 1)
      puts("IMPOSSIBLE"), exit(0);
  puts("POSSIBLE");
  dfs(1);
  for (int i = 1; i <= n + 1; i++)
    h[i] = 0;
  for (int i = 1; i <= n; i++)
    e[i] = (edge){a[i], i, 0, h[a[i]]}, h[a[i]] = i;
  calc(n + 1);
  for (int i = 1; i <= n; i++)
    printf("%d ", ans[i]);
}

int main() {
  work();
  return 0;
}
Problem p03656 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
typedef long long LL;
const int N = 600050;

int head[N], to[N], nex[N], tot = 1, vis[N], ans[N], oe[N], oe2[N], n;
void addedge(int u, int v) {
  tot++;
  to[tot] = v, nex[tot] = head[u], head[u] = tot;
  tot++;
  to[tot] = u, nex[tot] = head[v], head[v] = tot;
}
void dfs(int x) {
  for (int i = head[x]; i; i = head[x] = nex[i]) {
    if (vis[i] == 0) {
      vis[i] = 1;
      vis[i ^ 1] = 1;
      if (to[i] == x + n)
        ans[x] = 1;
      else if (to[i] == x - n)
        ans[x - n] = -1;
      dfs(to[i]);
    }
  }
}

int main() {

  int i, j, k, ra, rb;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &j), oe[j] ^= 1;
    if (j != -1)
      addedge(i, j);
    else
      ra = i;
  }
  for (i = 1; i <= n; i++) {
    scanf("%d", &k), oe2[k] ^= 1;
    if (k != -1)
      addedge(i + n, k + n);
    else
      rb = i + n;
  }
  for (i = 1; i <= n; i++) {
    if (oe2[i] != oe[i]) {
      puts("IMPOSSIBLE");
      return 0;
    } else if (oe[i] == 0)
      addedge(i, i + n);
  }
  addedge(ra, 2 * n + 1);
  addedge(rb, 2 * n + 1);
  puts("POSSIBLE"), dfs(1);
  for (i = 1; i <= n; i++)
    printf("%d ", ans[i]);

  return 0;
}

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

using namespace std;
typedef long long LL;
const int N = 600050;

int head[N], to[N], nex[N], tot = 1, vis[N], ans[N], oe[N], oe2[N], n;
void addedge(int u, int v) {
  tot++;
  to[tot] = v, nex[tot] = head[u], head[u] = tot;
  tot++;
  to[tot] = u, nex[tot] = head[v], head[v] = tot;
}
void dfs(int x) {
  for (int i = head[x]; i; i = head[x]) {
    head[x] = nex[i];
    if (vis[i] == 0) {
      vis[i] = 1;
      vis[i ^ 1] = 1;
      if (to[i] == x + n)
        ans[x] = 1;
      else if (to[i] == x - n)
        ans[x - n] = -1;
      dfs(to[i]);
    }
  }
}

int main() {

  int i, j, k, ra, rb;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &j), oe[j] ^= 1;
    if (j != -1)
      addedge(i, j);
    else
      ra = i;
  }
  for (i = 1; i <= n; i++) {
    scanf("%d", &k), oe2[k] ^= 1;
    if (k != -1)
      addedge(i + n, k + n);
    else
      rb = i + n;
  }
  for (i = 1; i <= n; i++) {
    if (oe2[i] != oe[i]) {
      puts("IMPOSSIBLE");
      return 0;
    } else if (oe[i] == 0)
      addedge(i, i + n);
  }
  addedge(ra, 2 * n + 1);
  addedge(rb, 2 * n + 1);
  puts("POSSIBLE"), dfs(1);
  for (i = 1; i <= n; i++)
    printf("%d ", ans[i]);

  return 0;
}
Problem p03657 (Python)

Buggy submission (status: Runtime Error):
print("Impossible" if sum(map(int, input.split())) % 3 else "Possible")


Fixed submission:
A, B = map(int, input().split())
if A % 3 == 0 or B % 3 == 0 or (A + B) % 3 == 0:
    print("Possible")
else:
    print("Impossible")
Problem p03657 (C++)

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

int main() {
  int n, k;
  int sum = 0;
  cin >> n >> k;
  vector<int> vec(n);
  for (int i = 0; i < n; i++) {
    cin >> vec.at(i);
  }
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
  for (int i = 0; i < k; i++) {
    sum += vec.at(i);
  }
  cout << sum << endl;
}


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

int main() {
  int a, b;
  cin >> a >> b;
  int wa = a + b;
  bool judge = false;
  if (a % 3 == 0 || b % 3 == 0 || wa % 3 == 0)
    judge = true;
  if (judge)
    cout << "Possible" << endl;
  else
    cout << "Impossible" << endl;
}
Problem p03657 (C++)

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

int main() {
  int a, b;
  cin >> a >> b;
  if (a % 3 == 0 || b % 3 == 0 || (a + b) % 3 == 0)
    return cout << "Possible" << endl, 1;
  return cout << "Impossible" << endl, 1;

  return 0;
}

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

int main() {
  int a, b;
  cin >> a >> b;
  if (a % 3 == 0 || b % 3 == 0 || (a + b) % 3 == 0)
    return cout << "Possible" << endl, 0;
  return cout << "Impossible" << endl, 0;

  return 0;
}
Problem p03657 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
const ll longINF = 3e18;
const double eps = 1e-8;
const double dinf = 190.0;
const double pi = acos(-1);
const int INF = 2e9 + 1;

int a, b;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  cin >> a >> b;
  if (a % 3 == 0 || b % 3 == 0 || (a + b) % 3 == 0)
    cout << "Possible";
  else
    cout << "Impossible";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
const ll longINF = 3e18;
const double eps = 1e-8;
const double dinf = 190.0;
const double pi = acos(-1);
const int INF = 2e9 + 1;

int a, b;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> a >> b;
  if (a % 3 == 0 || b % 3 == 0 || (a + b) % 3 == 0)
    cout << "Possible";
  else
    cout << "Impossible";
  return 0;
}
Problem p03657 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  if (a % 3 == 0 || b % 3 == 0 || (a + b) % 3 == 0)
    return printf("Possible");
  return printf("Impossible");
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  if (a % 3 == 0 || b % 3 == 0 || (a + b) % 3 == 0) {
    printf("Possible");
    return 0;
  }
  printf("Impossible");
  return 0;
}
Problem p03658 (C++)

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

#define LL long long
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ALL(x) (x).begin(), (x).end()
const int IINF = 1e9;
const LL LINF = 1e18;
const LL mod = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> v;
  REP(i, N) { cin >> v[i]; }
  sort(ALL(v), greater<int>());
  int res = 0;
  REP(i, K) { res += v[i]; }
  cout << res << endl;
  return 0;
}


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

#define LL long long
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ALL(x) (x).begin(), (x).end()
const int IINF = 1e9;
const LL LINF = 1e18;
const LL mod = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> v(N);
  REP(i, N) { cin >> v[i]; }
  sort(ALL(v), greater<int>());
  int res = 0;
  REP(i, K) { res += v[i]; }
  cout << res << endl;
  return 0;
}
Problem p03658 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int len = 0;
  sort(a.begin(), a.end());
  for (int i = n - 1; i >= n - k; i--) {
    len += a[i];
  }
  cout << len << endl;
}


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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int len = 0;
  sort(a.begin(), a.end());
  for (int i = n - 1; i >= n - k; i--) {
    len += a[i];
  }
  cout << len << endl;
}
Problem p03658 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  vector<int> vec;
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> vec[i];
  sort(vec.rbegin(), vec.rend());
  int length = 0;
  for (int i = 0; i < K; i++) {
    length += vec[i];
  }
  cout << length << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  vector<int> vec(51);
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> vec[i];
  sort(vec.rbegin(), vec.rend());
  int length = 0;
  for (int i = 0; i < K; i++) {
    length += vec[i];
  }
  cout << length << endl;
}
Problem p03658 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }

  sort(v.rbegin(), v.rend());

  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum += v[i];
  }
  cout << sum << endl;
}


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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }

  sort(v.rbegin(), v.rend());

  int sum = 0;
  for (int i = 0; i < k; i++) {
    sum += v[i];
  }
  cout << sum << endl;
}
Problem p03658 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> a(N);
  int sum = 0;
  for (int i = 0; i < N; i++)
    cin >> a[N];
  sort(a.begin(), a.end(), greater<int>());

  for (int i = 0; i < K; i++) {
    sum += a[i];
  }
  cout << sum << endl;
}

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> a(N);
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  sort(a.begin(), a.end(), greater<int>());

  for (int i = 0; i < K; i++) {
    sum += a[i];
  }
  cout << sum << endl;
}
Problem p03658 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  sort(v.begin(), v.end());
  int sum = 0;
  for (int i = v.size() - 1; i >= v.size() - k; i--) {
    sum += v[i];
  }
  cout << sum;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  sort(v.begin(), v.end());
  int sum = 0;
  for (int i = v.size() - 1; i >= n - k; i--) {
    sum += v[i];
  }
  cout << sum;
}
Problem p03658 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
const int MOD = (int)1e9 + 7;
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> vec(k);
  REP(i, n) cin >> vec[i];
  sort(vec.begin(), vec.end());
  int ans = 0;
  REP(i, k) { ans += vec[n - 1 - i]; }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
const int MOD = (int)1e9 + 7;
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> vec(n);
  REP(i, n) cin >> vec[i];
  sort(vec.begin(), vec.end());
  int ans = 0;
  REP(i, k) { ans += vec[n - 1 - i]; }
  cout << ans << endl;
}
Problem p03658 (C++)

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

int main() {
  ios_base::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> sticks;

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sticks.push_back(x);
  }

  sort(sticks.begin(), sticks.end());

  int sum = 0;
  for (unsigned i = sticks.size() - 1; i >= sticks.size() - k; i--) {
    sum += sticks.at(i);
  }

  cout << sum << endl;

  return 0;
}

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

int main() {
  ios_base::sync_with_stdio(false);

  int n, k;
  cin >> n >> k;
  vector<int> sticks;

  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sticks.push_back(x);
  }

  sort(sticks.begin(), sticks.end());

  int sum = 0;
  for (int i = n - 1; i >= n - k; i--) {

    sum += sticks.at(i);
  }

  cout << sum << endl;

  return 0;
}
Problem p03658 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << endl;
#define CFYN(n) cout << ((n) ? "YES" : "NO") << endl;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

int main(void) {
  IOS int n, k;
  cin >> n >> k;
  vector<int> l(n);
  REP(i, n) cin >> l[n];
  sort(RALL(l));
  int ans = 0;
  REP(i, k) { ans += l[i]; }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << endl;
#define CFYN(n) cout << ((n) ? "YES" : "NO") << endl;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

int main(void) {
  IOS int n, k;
  cin >> n >> k;
  vector<int> l(n);
  REP(i, n) cin >> l[i];
  sort(RALL(l));
  int ans = 0;
  REP(i, k) { ans += l[i]; }
  cout << ans << endl;

  return 0;
}
Problem p03658 (C++)

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

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;

  vector<int> L;
  for (int i = 0; i < N; i++) {
    int l;
    cin >> l;
  }

  sort(L.begin(), L.end());
  reverse(L.begin(), L.end());

  int sum = 0;
  for (int i = 0; i < K; i++) {
    sum += L[i];
  }
  cout << sum << endl;
}


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

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;

  vector<int> L;
  for (int i = 0; i < N; i++) {
    int l;
    cin >> l;
    L.push_back(l);
  }

  sort(L.begin(), L.end());
  reverse(L.begin(), L.end());

  int sum = 0;
  for (int i = 0; i < K; i++) {
    sum += L[i];
  }
  cout << sum << endl;
}
Problem p03658 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)

const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int sum = 0;

int32_t main() {
  IOS int n, k;
  cin >> n >> k;
  vector<int> v;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  for (int i = v.size() - 1; i >= v.size() - k; --i) {
    sum += v[i];
  }
  cout << sum;
}

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)

const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int sum = 0;

int32_t main() {
  IOS int n, k;
  cin >> n >> k;
  vector<int> v;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  for (int i = 0; i < k; ++i) {
    sum += v[i];
  }
  cout << sum;
}
Problem p03658 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  int d = 0;
  for (int i = n - 1; i > n - k; i++) {
    d += a[i];
  }
  cout << d;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  int d = 0;
  for (int i = 0; i < k; i++) {
    d += a[n - i - 1];
  }
  cout << d;
}
Problem p03658 (C++)

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

int main() {
  int n, k;
  cin >> k >> n;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    cin >> l.at(i);
  }
  sort(l.begin(), l.end());
  reverse(l.begin(), l.end());
  int a = 0;
  for (int i = 0; i < k; i++) {
    a += l.at(i);
  }
  cout << a << endl;
  return 0;
}


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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    cin >> l.at(i);
  }
  sort(l.begin(), l.end());
  reverse(l.begin(), l.end());
  int a = 0;
  for (int i = 0; i < k; i++) {
    a += l.at(i);
  }
  cout << a << endl;
  return 0;
}
Problem p03658 (C++)

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

int main() {

  int N, K, sum = 0;
  cin >> N >> K;
  vector<int> l(N);

  for (int i = 0; i < N; i++) {
    cin >> l.at(i);
  }
  sort(l.begin(), l.end());
  for (int i = N; i > N - K; i--) {
    sum += l.at(i);
  }
  cout << sum << endl;
}


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

int main() {

  int N, K, sum = 0;
  cin >> N >> K;
  vector<int> l(N);

  for (int i = 0; i < N; i++) {
    cin >> l.at(i);
  }
  sort(l.begin(), l.end());
  for (int i = N - 1; i > N - K - 1; i--) {
    sum += l.at(i);
  }
  cout << sum << endl;
}