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

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

typedef pair<int, int> pint;

int N;
vector<vector<pint>> g;

vector<bool> used;
vector<int> pos;
bool dfs(const int cur) {
  for (int i = 0; i < g[cur].size(); i++) {
    int nxt = g[cur][i].first;
    int npos = (int)(g[cur][i].second) + pos[cur];
    if (used[nxt]) {
      if (pos[nxt] != npos)
        return false;
    } else {
      used[nxt] = true;
      pos[nxt] = npos;
      dfs(nxt);
    }
  }
  return true;
}

bool solve() {
  int M;
  cin >> N >> M;
  g = vector<vector<pint>>(N);
  vector<int> l(N), r(N), d(N);
  for (int i = 0; i < M; i++) {
    int L, R, D;
    cin >> L >> R >> D;
    L--;
    R--;
    g[L].push_back(make_pair(R, D));
    g[R].push_back(make_pair(L, -D));
    l[i] = L;
    r[i] = R;
    d[i] = D;
  }
  used = vector<bool>(N, false);
  pos = vector<int>(N, 0);
  for (int i = 0; i < N; i++) {
    if (used[i] == false) {
      used[i] = true;
      bool tmp = dfs(i);
      if (tmp == false)
        return false;
    }
  }
  for (int i = 0; i < M; i++) {
    if (d[i] != pos[r[i]] - pos[l[i]])
      return false;
  }
  return true;
}

int main() {
  cout << (solve() ? "Yes" : "No") << endl;
  return 0;
}


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

typedef pair<int, int> pint;

int N;
vector<vector<pint>> g;

vector<bool> used;
vector<int> pos;
bool dfs(const int cur) {
  for (int i = 0; i < g[cur].size(); i++) {
    int nxt = g[cur][i].first;
    int npos = (int)(g[cur][i].second) + pos[cur];
    if (used[nxt]) {
      if (pos[nxt] != npos)
        return false;
    } else {
      used[nxt] = true;
      pos[nxt] = npos;
      dfs(nxt);
    }
  }
  return true;
}

bool solve() {
  int M;
  cin >> N >> M;
  g = vector<vector<pint>>(N);
  vector<int> l(M), r(M), d(M);
  for (int i = 0; i < M; i++) {
    int L, R, D;
    cin >> L >> R >> D;
    L--;
    R--;
    g[L].push_back(make_pair(R, D));
    g[R].push_back(make_pair(L, -D));
    l[i] = L;
    r[i] = R;
    d[i] = D;
  }
  used = vector<bool>(N, false);
  pos = vector<int>(N, 0);
  for (int i = 0; i < N; i++) {
    if (used[i] == false) {
      used[i] = true;
      bool tmp = dfs(i);
      if (tmp == false)
        return false;
    }
  }
  for (int i = 0; i < M; i++) {
    if (d[i] != pos[r[i]] - pos[l[i]])
      return false;
  }
  return true;
}

int main() {
  cout << (solve() ? "Yes" : "No") << endl;
  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, m, b[10001] = {0}, d[10001]; //
int L, R, D;
struct node {
  int v, c, next;
};
node a[3000001];
int head[1000001], e;
void addnode(int from, int to, int c) {
  e++;
  a[e].c = c;             // 边权
  a[e].v = to;            // 下一个
  a[e].next = head[from]; //
  head[from] = e;         // 记下标
} // 喜闻乐见的建表

bool dfs(int x) {
  b[x] = 1; // 走过标记
  int v, c;
  for (int i = head[x]; i != 0; i = a[i].next) {
    v = a[i].v;
    c = a[i].c;
    if (d[v] > d[x] + c) {
      if (b[v] == 1)
        return 1; // 判断负环，走过即为负环
      d[v] = d[x] + c;
      if (dfs(v))
        return 1;
    }
  }
  b[x] = 0;
  return 0;
}

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

  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &L, &R, &D);
    addnode(R, L, D);  // 逆向建边
    addnode(L, R, -D); // 正向建边
  }
  for (int i = 1; i <= n; i++) {
    addnode(
        0, i,
        0); // 万一图不联通，分为两个部分，一边有负环一边没有，只搜了没有的那一边就GG
    // 所以把0和所有点连起来，每个点找一遍就ok了
  }

  // 于是我们从零开始dfs
  memset(d, 1000000, sizeof(d));
  d[0] = 0;
  if (dfs(0))
    printf("No");
  else
    printf("Yes");
  return 0;
}
// 思路：应该建一个图，题目相当于说如果本来一条连点A的边连到点B去了的话就no

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, b[1000001] = {0}, d[1000001]; //
int L, R, D;
struct node {
  int v, c, next;
};
node a[3000001];
int head[1000001], e;
void addnode(int from, int to, int c) {
  e++;
  a[e].c = c;             // 边权
  a[e].v = to;            // 下一个
  a[e].next = head[from]; //
  head[from] = e;         // 记下标
} // 喜闻乐见的建表

bool dfs(int x) {
  b[x] = 1; // 走过标记
  int v, c;
  for (int i = head[x]; i != 0; i = a[i].next) {
    v = a[i].v;
    c = a[i].c;
    if (d[v] > d[x] + c) {
      if (b[v] == 1)
        return 1; // 判断负环，走过即为负环
      d[v] = d[x] + c;
      if (dfs(v))
        return 1;
    }
  }
  b[x] = 0;
  return 0;
}

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

  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &L, &R, &D);
    addnode(R, L, D);  // 逆向建边
    addnode(L, R, -D); // 正向建边
  }
  for (int i = 1; i <= n; i++) {
    addnode(
        0, i,
        0); // 万一图不联通，分为两个部分，一边有负环一边没有，只搜了没有的那一边就GG
    // 所以把0和所有点连起来，每个点找一遍就ok了
  }

  // 于是我们从零开始dfs
  memset(d, 1000000, sizeof(d));
  d[0] = 0;
  if (dfs(0))
    printf("No");
  else
    printf("Yes");
  return 0;
}
// 思路：应该建一个图，题目相当于说如果本来一条连点A的边连到点B去了的话就no
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rr register
#define int long long
const int N = 100010, M = 200010;
struct Edge {
  int to, next, v;
} edge[M];
int head[N], sum;
int dis[N];
bool vst[N];
int n, m;
inline int read(void) {
  int res, f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      f = -1;
  res = c - 48;
  while ((c = getchar()) >= '0' && c <= '9')
    res = res * 10 + c - 48;
  return res * f;
}
void print(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
  return;
}
inline void add(int x, int y, int w) {
  edge[++sum].to = y;
  edge[sum].next = head[x];
  edge[sum].v = w;
  head[x] = sum;
  return;
}
bool spfa(int i, int w) {
  //	printf("now visit %d\n",i);
  if (vst[i] && dis[i] != w)
    return false;
  if (w > 1e9)
    return false;
  dis[i] = w;
  vst[i] = true;
  for (rr int j = head[i]; j; j = edge[j].next) {
    if (vst[edge[j].to] && dis[edge[j].to] != dis[i] + edge[j].v) {
      return false;
    }
    if (vst[edge[j].to])
      continue;
    if (!spfa(edge[j].to, dis[i] + edge[j].v))
      return false;
  }
  return true;
}
signed main(void) {
  n = read(), m = read();
  for (rr int i = 1, x, y, w; i <= m; ++i) {
    x = read(), y = read(), w = read();
    add(x, y, w), add(y, x, -w);
  }
  for (rr int i = 1; i <= n; ++i) {
    if (vst[i])
      continue;
    if (!spfa(i, 0)) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rr register
#define int long long
const int N = 400000, M = 800000;
struct Edge {
  int to, next, v;
} edge[M];
int head[N], sum;
int dis[N];
bool vst[N];
int n, m;
inline int read(void) {
  int res, f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-')
      f = -1;
  res = c - 48;
  while ((c = getchar()) >= '0' && c <= '9')
    res = res * 10 + c - 48;
  return res * f;
}
void print(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
  return;
}
inline void add(int x, int y, int w) {
  edge[++sum].to = y;
  edge[sum].next = head[x];
  edge[sum].v = w;
  head[x] = sum;
  return;
}
bool spfa(int i, int w) {
  //	printf("now visit %d\n",i);
  if (vst[i] && dis[i] != w)
    return false;
  if (w > 1e9)
    return false;
  dis[i] = w;
  vst[i] = true;
  for (rr int j = head[i]; j; j = edge[j].next) {
    if (vst[edge[j].to] && dis[edge[j].to] != dis[i] + edge[j].v) {
      return false;
    }
    if (vst[edge[j].to])
      continue;
    if (!spfa(edge[j].to, dis[i] + edge[j].v))
      return false;
  }
  return true;
}
signed main(void) {
  n = read(), m = read();
  for (rr int i = 1, x, y, w; i <= m; ++i) {
    x = read(), y = read(), w = read();
    add(x, y, w), add(y, x, -w);
  }
  for (rr int i = 1; i <= n; ++i) {
    if (vst[i])
      continue;
    if (!spfa(i, 0)) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  return 0;
}
Problem p03450 (C++)

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

using namespace std;

// 幅優先探索
string bfs(vector<pair<int, int>> *edge, int *x, int *visit, int N) {
  for (int i = 0; i < N; ++i) {
    if (visit[i] == 1) {
      continue;
    }
    visit[i] = 1;
    x[i] = 0;
    queue<int> que;
    que.push(i);
    while (!que.empty()) {
      int q = que.front(); // キューの先頭要素qを始点とする
      que.pop();
      for (auto &p : edge[q]) { // 頂点qを始点とする全ての枝について走査
        if (visit[p.first] == 1) { // 枝の終点が訪問済みの場合
          if (x[q] + p.second !=
              x[p.first]) { // x[始点]+枝の重みがx[終点]と不一致
            return "No";    // forループ脱出
          }
        } else { // 枝の終点が訪問済みでない場合
          x[p.first] =
              x[q] + p.second; // 終点のx座標を始点のx座標+枝の重みとする
          visit[p.first] = 1; // フラグを立て類
          que.push(p.first);  // 終点をキューに追加
        }
      }
    }
  }
  return "Yes";
}

int main() {
  int N, M, L, R, D = 0;
  cin >> N >> M;
  // 重み付き有向グラフ
  // N人の人がグラフの各頂点となる
  // L->Rの向きに重みDの枝，R->Lの向きに重み-Dの枝を持つ
  // Edge[始点]に{終点, 重み}を保持
  vector<pair<int, int>> *edge = new vector<pair<int, int>>[N];
  for (int i = 0; i < N; ++i) {
    cin >> L >> R >> D;
    --L;
    --R;
    edge[L].push_back({R, D});
    edge[R].push_back({L, -D});
  }

  int *visit = new int[N]; // 走査済みの頂点を表す
  int *x = new int[N];     // 頂点の持つ値(x座標)
  for (int i = 0; i < N; ++i) {
    visit[i] = 0;
    x[i] = 0;
  }

  cout << bfs(edge, x, visit, N) << endl;
  delete[] edge;
  delete[] x;
  delete[] visit;
  return 0;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

// 幅優先探索
string bfs(vector<pair<int, int>> *edge, int *x, int *visit, int N) {
  for (int i = 0; i < N; ++i) {
    if (visit[i] == 1) {
      continue;
    }
    visit[i] = 1;
    x[i] = 0;
    queue<int> que;
    que.push(i);
    while (!que.empty()) {
      int q = que.front(); // キューの先頭要素qを始点とする
      que.pop();
      for (auto &p : edge[q]) { // 頂点qを始点とする全ての枝について走査
        if (visit[p.first] == 1) { // 枝の終点が訪問済みの場合
          if (x[q] + p.second !=
              x[p.first]) { // x[始点]+枝の重みがx[終点]と不一致
            return "No";    // forループ脱出
          }
        } else { // 枝の終点が訪問済みでない場合
          x[p.first] =
              x[q] + p.second; // 終点のx座標を始点のx座標+枝の重みとする
          visit[p.first] = 1; // フラグを立て類
          que.push(p.first);  // 終点をキューに追加
        }
      }
    }
  }
  return "Yes";
}

int main() {
  int N, M, L, R, D = 0;
  cin >> N >> M;
  // 重み付き有向グラフ
  // N人の人がグラフの各頂点となる
  // L->Rの向きに重みDの枝，R->Lの向きに重み-Dの枝を持つ
  // Edge[始点]に{終点, 重み}を保持
  vector<pair<int, int>> *edge = new vector<pair<int, int>>[N];
  for (int i = 0; i < M; ++i) {
    cin >> L >> R >> D;
    --L;
    --R;
    edge[L].push_back({R, D});
    edge[R].push_back({L, -D});
  }

  int *visit = new int[N]; // 走査済みの頂点を表す
  int *x = new int[N];     // 頂点の持つ値(x座標)
  for (int i = 0; i < N; ++i) {
    visit[i] = 0;
    x[i] = 0;
  }

  cout << bfs(edge, x, visit, N) << endl;
  delete[] edge;
  delete[] x;
  delete[] visit;
  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e10;

template <typename Weight> class PotentializedUnionFind {
public:
  vector<int> par, rnk, sz;
  vector<Weight> difweight;
  PotentializedUnionFind(int n)
      : par(n, -1), rnk(n, 0), sz(n, 1), difweight(n, 0) {}

  int find(int x) {
    if (par[x] < 0) {
      return x;
    } else {
      int r = find(par[x]);
      difweight[x] += difweight[par[x]];
      return par[x] = r;
    }
  }
  Weight weight(int x) {
    find(x);
    return difweight[x];
  }
  bool same(int x, int y) { return find(x) == find(y); }

  bool unite(int x, int y, Weight w) {
    w += weight(x);
    w -= weight(y);
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (rnk[x] < rnk[y])
      swap(x, y), w = -w;
    if (rnk[x] == rnk[y])
      rnk[x]++;
    par[y] = x;
    difweight[y] = w;
    return true;
  }
  Weight diff(int x, int y) { return weight(y) - weight(x); }
};

signed main() {
  int n, m;
  cin >> n >> m;
  PotentializedUnionFind<int> puf(n);
  bool ans = true;
  rep(i, m) {
    int l, r, d;
    cin >> l >> r >> d;
    if (puf.same(l, r)) {
      if (puf.diff(l, r) != d)
        ans = false;
    } else
      puf.unite(l, r, d);
  }
  cout << (ans ? "Yes" : "No") << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e10;

template <typename Weight> class PotentializedUnionFind {
public:
  vector<int> par, rnk, sz;
  vector<Weight> difweight;
  PotentializedUnionFind(int n)
      : par(n, -1), rnk(n, 0), sz(n, 1), difweight(n, 0) {}

  int find(int x) {
    if (par[x] < 0) {
      return x;
    } else {
      int r = find(par[x]);
      difweight[x] += difweight[par[x]];
      return par[x] = r;
    }
  }
  Weight weight(int x) {
    find(x);
    return difweight[x];
  }
  bool same(int x, int y) { return find(x) == find(y); }

  bool unite(int x, int y, Weight w) {
    w += weight(x);
    w -= weight(y);
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (rnk[x] < rnk[y])
      swap(x, y), w = -w;
    if (rnk[x] == rnk[y])
      rnk[x]++;
    par[y] = x;
    difweight[y] = w;
    return true;
  }
  Weight diff(int x, int y) { return weight(y) - weight(x); }
};

signed main() {
  int n, m;
  cin >> n >> m;
  PotentializedUnionFind<int> puf(100005);
  bool ans = true;
  rep(i, m) {
    int l, r, d;
    cin >> l >> r >> d;
    if (puf.same(l, r)) {
      if (puf.diff(l, r) != d)
        ans = false;
    } else
      puf.unite(l, r, d);
  }
  cout << (ans ? "Yes" : "No") << endl;
  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9 + 100;
const ll MOD = 1e9 + 7;
const bool debug = 0;
//-----------------------------------------------//

int N, M;
int dist[112345];
vector<pii> g[112345];

int main() {
  cin >> N >> M;
  REP(i, M) {
    int a, b, d;
    scanf("%d %d %d", &a, &b, &d);
    a--;
    b--;
    g[a].push_back(pii(b, d));
    g[b].push_back(pii(a, -d));
  }

  fill(dist, dist + N, INF);
  REP(st, N) {
    if (dist[st] != INF)
      continue;

    queue<int> que;
    que.push(st);

    while (!que.empty()) {
      int u = que.front();
      que.pop();

      REP(i, g[u].size()) {
        int v = g[u][i].fi;
        int ncost = dist[u] + g[u][i].se;

        if (dist[v] == INF) {
          dist[v] = ncost;
          que.push(v);
        } else if (dist[v] != ncost) {
          puts("No");
          return 0;
        }
      }
    }
  }

  puts("Yes");

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e9 + 100;
const ll MOD = 1e9 + 7;
const bool debug = 0;
//-----------------------------------------------//

int N, M;
int dist[112345];
vector<pii> g[112345];

int main() {
  cin >> N >> M;
  REP(i, M) {
    int a, b, d;
    scanf("%d %d %d", &a, &b, &d);
    a--;
    b--;
    g[a].push_back(pii(b, d));
    g[b].push_back(pii(a, -d));
  }

  fill(dist, dist + N, INF);
  REP(st, N) {
    if (dist[st] != INF)
      continue;

    queue<int> que;
    que.push(st);
    dist[st] = 0;

    while (!que.empty()) {
      int u = que.front();
      que.pop();

      REP(i, g[u].size()) {
        int v = g[u][i].fi;
        int ncost = dist[u] + g[u][i].se;

        if (dist[v] == INF) {
          dist[v] = ncost;
          que.push(v);
        } else if (dist[v] != ncost) {
          puts("No");
          return 0;
        }
      }
    }
  }

  puts("Yes");

  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define lmid l, mid, rt << 1
#define rmid mid + 1, r, rt << 1 | 1
using namespace std;
struct Edge {
  int next;
  int v;
  int w;
} s[200005];
int head[100005], vis[100005], d[100005];
int cnt = 0;
void deal(int u, int v, int w) {
  s[cnt].v = v;
  s[cnt].w = w;
  s[cnt].next = head[u];
  head[u] = cnt++;
}
bool flag = false;
void dfs(int x) {
  if (d[x] != -2000000005) {
    if (abs(d[x]) > 1000000000) {
      flag = true;
      return;
    }
  }
  if (flag)
    return;
  for (int i = head[x]; ~i; i = s[i].next) {
    int v = s[i].v;
    if (d[v] != -2000000005) {
      if (d[x] + s[i].w != d[v])
        flag = true;
    } else {
      d[v] = d[x] + s[i].w;
      if (!vis[v]) {
        vis[v] = 1;
        dfs(v);
      }
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    d[i] = -2000000005;
  memset(head, -1, sizeof(head));
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    deal(a, b, c);
    deal(b, a, -c);
  }
  for (int i = 1; i <= n; i++) {
    if (flag)
      break;
    if (!vis[i]) {
      d[i] = 0;
      vis[i] = 1;
      dfs(i);
    }
  }
  if (flag)
    printf("No\n");
  else
    printf("Yes\n");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define lmid l, mid, rt << 1
#define rmid mid + 1, r, rt << 1 | 1
using namespace std;
struct Edge {
  int next;
  int v;
  int w;
} s[400005];
int head[100005], vis[100005], d[100005];
int cnt = 0;
void deal(int u, int v, int w) {
  s[cnt].v = v;
  s[cnt].w = w;
  s[cnt].next = head[u];
  head[u] = cnt++;
}
bool flag = false;
void dfs(int x) {
  if (d[x] != -2000000005) {
    if (abs(d[x]) > 1000000000) {
      flag = true;
      return;
    }
  }
  if (flag)
    return;
  for (int i = head[x]; ~i; i = s[i].next) {
    int v = s[i].v;
    if (d[v] != -2000000005) {
      if (d[x] + s[i].w != d[v])
        flag = true;
    } else {
      d[v] = d[x] + s[i].w;
      if (!vis[v]) {
        vis[v] = 1;
        dfs(v);
      }
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    d[i] = -2000000005;
  memset(head, -1, sizeof(head));
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    deal(a, b, c);
    deal(b, a, -c);
  }
  for (int i = 1; i <= n; i++) {
    if (flag)
      break;
    if (!vis[i]) {
      d[i] = 0;
      vis[i] = 1;
      dfs(i);
    }
  }
  if (flag)
    printf("No\n");
  else
    printf("Yes\n");
  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  struct edge {
    int to;
    int cost;
  };
  vector<vector<edge>> d(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    edge e;
    e.to = b;
    e.cost = c;
    d[a].push_back(e);
    e.to = a;
    e.cost = -c;
    d[b].push_back(e);
  }

  vector<int> ans(n + 1);
  int INF = 1000000000;
  for (int i = 0; i <= n; i++) {
    ans[i] = INF;
  }

  ans[1] = 0;
  vector<bool> former(n + 1);
  queue<int> que;
  for (int i = 1; i <= n; i++) {
    if (former[i])
      continue;
    que.push(i);
    while (!que.empty()) {
      int a = que.front();
      que.pop();
      for (int j = 0; j < d[a].size(); j++) {
        edge e = d[a][j];
        int to = e.to;
        int cost = e.cost;
        if (ans[to] == INF) {
          ans[to] = ans[a] + cost;
          que.push(to);
        }
        if (ans[to] != ans[a] + cost) {
          cout << "No" << endl;
          return 0;
        }
      }
      former[a] = true;
    }
  }
  cout << "Yes" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  struct edge {
    int to;
    int cost;
  };
  vector<vector<edge>> d(n + 1);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    edge e;
    e.to = b;
    e.cost = c;
    d[a].push_back(e);
    e.to = a;
    e.cost = -c;
    d[b].push_back(e);
  }

  vector<int> ans(n + 1);
  int INF = 1000000000;
  for (int i = 0; i <= n; i++) {
    ans[i] = INF;
  }

  ans[1] = 0;
  vector<bool> former(n + 1);
  queue<int> que;
  for (int i = 1; i <= n; i++) {
    if (former[i])
      continue;
    que.push(i);
    while (!que.empty()) {
      int a = que.front();
      que.pop();
      if (former[a])
        continue;
      for (int j = 0; j < d[a].size(); j++) {
        edge e = d[a][j];
        int to = e.to;
        int cost = e.cost;
        if (ans[to] == INF) {
          ans[to] = ans[a] + cost;
          que.push(to);
        }
        if (ans[to] != ans[a] + cost) {
          cout << "No" << endl;
          return 0;
        }
      }
      former[a] = true;
    }
  }
  cout << "Yes" << endl;
}
Problem p03450 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, long long>>> G(n);
  for (int i = 0; i < m; i++) {
    int l, r, d;
    cin >> l >> r >> d;
    G[l - 1].push_back(make_pair(r - 1, d));
    G[r - 1].push_back(make_pair(l - 1, -d));
  }

  long long number[n];
  bool visited[n];
  fill(visited, visited + n, false);
  set<int> todo;
  for (int i = 0; i < n; i++)
    todo.insert(i);
  queue<int> q;
  while (!todo.empty()) {
    q.push(*todo.begin());
    number[*todo.begin()] = 0;
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      todo.erase(v);
      visited[v] = true;
      for (auto &e : G[v]) {
        if (!visited[e.first]) {
          number[e.first] = number[v] + e.second;
          q.push(e.first);
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto &e : G[i]) {
      if (number[i] + e.second != number[e.first]) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, long long>>> G(n);
  for (int i = 0; i < m; i++) {
    int l, r, d;
    cin >> l >> r >> d;
    G[l - 1].push_back(make_pair(r - 1, d));
    G[r - 1].push_back(make_pair(l - 1, -d));
  }

  long long number[n];
  bool visited[n];
  fill(visited, visited + n, false);
  set<int> todo;
  for (int i = 0; i < n; i++)
    todo.insert(i);
  queue<int> q;
  while (!todo.empty()) {
    q.push(*todo.begin());
    number[*todo.begin()] = 0;
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      if (visited[v])
        continue;
      todo.erase(v);
      visited[v] = true;
      for (auto &e : G[v]) {
        if (!visited[e.first]) {
          number[e.first] = number[v] + e.second;
          q.push(e.first);
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto &e : G[i]) {
      if (number[i] + e.second != number[e.first]) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define maxn 100005
int n, m, fa[maxn], dist[maxn];
int findset(int x) {
  if (fa[x] == x)
    return x;
  int f = fa[x];
  fa[x] = findset(fa[x]);
  dist[x] += dist[f];
  return f;
}
int main() {
  bool f = false;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    if (f)
      continue;
    int r1 = findset(u), r2 = findset(v);
    if (r1 != r2) {
      fa[r2] = r1;
      dist[r2] = dist[r1] + w - dist[v] + dist[u];
    } else if (dist[v] - dist[u] != w)
      f = true;
  }
  printf("%s\n", f ? "No" : "Yes");
}

Fixed submission:
#include <cstdio>
#define maxn 100005
int n, m, fa[maxn], dist[maxn];
int findset(int x) {
  if (fa[x] == x)
    return x;
  int f = fa[x];
  fa[x] = findset(fa[x]);
  dist[x] += dist[f];
  return fa[x];
}
int main() {
  bool f = false;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    if (f)
      continue;
    int r1 = findset(u), r2 = findset(v);
    if (r1 != r2) {
      fa[r2] = r1;
      dist[r2] = dist[r1] + w - dist[v] + dist[u];
    } else if (dist[v] - dist[u] != w)
      f = true;
  }
  printf("%s\n", f ? "No" : "Yes");
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int N = 1e5 + 10;
using namespace std;

inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

struct node {
  int v, nxt, w;
} e[N << 1];
int head[N], cnt;
int dis[N], n, m, INF;
bool used[N], vis[N];

void add(int u, int v, int w) {
  e[++cnt].v = v;
  e[cnt].w = w;
  e[cnt].nxt = head[u];
  head[u] = cnt;
}

bool spfa(int s) {
  queue<int> q;
  q.push(s);
  vis[s] = true;
  dis[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = false;
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].v, w = e[i].w;
      if (dis[v] == INF) {
        dis[v] = dis[u] + w;
        used[v] = true;
        if (!vis[v])
          vis[v] = true, q.push(v);
      } else if (dis[v] != dis[u] + w)
        return false;
    }
  }
  return true;
}

int main() {
  memset(dis, 0x3f, sizeof(dis));
  INF = dis[1];
  n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read(), z = read();
    add(x, y, z);
    add(y, x, -z);
  }
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      used[i] = true;
      if (!spfa(i)) {
        printf("No");
        return 0;
      }
    }
  }
  printf("Yes");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
const int N = 1e6 + 10;
using namespace std;

inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

struct node {
  int v, nxt, w;
} e[N << 1];
int head[N], cnt;
int dis[N], n, m, INF;
bool used[N], vis[N];

void add(int u, int v, int w) {
  e[++cnt].v = v;
  e[cnt].w = w;
  e[cnt].nxt = head[u];
  head[u] = cnt;
}

bool spfa(int s) {
  queue<int> q;
  q.push(s);
  vis[s] = true;
  dis[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = false;
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].v, w = e[i].w;
      if (dis[v] == INF) {
        dis[v] = dis[u] + w;
        used[v] = true;
        if (!vis[v])
          vis[v] = true, q.push(v);
      } else if (dis[v] != dis[u] + w)
        return false;
    }
  }
  return true;
}

int main() {
  memset(dis, 0x3f, sizeof(dis));
  INF = dis[1];
  n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read(), z = read();
    add(x, y, z);
    add(y, x, -z);
  }
  for (int i = 1; i <= n; i++) {
    if (!used[i]) {
      used[i] = true;
      if (!spfa(i)) {
        printf("No");
        return 0;
      }
    }
  }
  printf("Yes");
  return 0;
}
Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 200200;
int head[maxn], nxt[maxn], to[maxn], w[maxn];
int flag[maxn];
int sz;
int n, m;
int ans = 1;
void init() {
  for (int i = 1; i <= n; i++)
    head[i] = -1, flag[i] = 0;
  sz = 0;
}
void add(int u, int v, int ww) {
  nxt[++sz] = head[u];
  to[sz] = v;
  w[sz] = ww;
  head[u] = sz;
}
int dis[maxn];
void dfs(int u, int pre) {
  if (!ans)
    return;
  for (int i = head[u]; ~i; i = nxt[i]) {
    int v = to[i];
    if (v == pre)
      continue;
    if (flag[v]) {
      if (dis[v] - dis[u] != w[i]) {
        ans = 0;
        return;
      }
    } else {
      flag[v] = 1;
      dis[v] = dis[u] + w[i];
      dfs(v, u);
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  init();
  for (int i = 1; i <= m; i++) {
    int l, r, d;
    scanf("%d %d %d", &l, &r, &d);
    add(l, r, d);
    add(r, l, -d);
  }
  for (int i = 1; i <= n; i++) {
    if (flag[i] == 0) {
      flag[i] = 1;
      dis[i] = 0;
      dfs(i, -1);
    }
  }
  if (ans) {
    printf("Yes\n");
  } else
    printf("No\n");

  return 0;
}


Fixed submission:
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 400200;
int head[maxn], nxt[maxn], to[maxn], w[maxn];
int flag[maxn];
int sz;
int n, m;
int ans = 1;
void init() {
  for (int i = 1; i <= n; i++)
    head[i] = -1, flag[i] = 0;
  sz = 0;
}
void add(int u, int v, int ww) {
  nxt[++sz] = head[u];
  to[sz] = v;
  w[sz] = ww;
  head[u] = sz;
}
int dis[maxn];
void dfs(int u, int pre) {
  if (!ans)
    return;
  for (int i = head[u]; ~i; i = nxt[i]) {
    int v = to[i];
    if (v == pre)
      continue;
    if (flag[v]) {
      if (dis[v] - dis[u] != w[i]) {
        ans = 0;
        return;
      }
    } else {
      flag[v] = 1;
      dis[v] = dis[u] + w[i];
      dfs(v, u);
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  init();
  for (int i = 1; i <= m; i++) {
    int l, r, d;
    scanf("%d %d %d", &l, &r, &d);
    add(l, r, d);
    add(r, l, -d);
  }
  for (int i = 1; i <= n; i++) {
    if (flag[i] == 0) {
      flag[i] = 1;
      dis[i] = 0;
      dfs(i, -1);
    }
  }
  if (ans) {
    printf("Yes\n");
  } else
    printf("No\n");

  return 0;
}
Problem p03450 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<array<int64_t, 2>>> E(n);

  for (int i = 0; i < m; i++) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    E[l].push_back({r, d});
    E[r].push_back({l, -d});
  }

  vector<bool> visited(n);
  map<int, int64_t> D;
  auto dfs = [&](auto i, auto self) -> bool {
    if (visited[i])
      return true;
    if (D.empty())
      D[i] = 0;

    bool res = true;
    for (const auto &e : E[i]) {
      int r = e[0];
      int64_t d = e[1];
      if (D.count(r) != 0 and D[r] != D[i] + d)
        return false;
      D[r] = D[i] + d;
      res &= self(r, self);
    }
    return res;
  };

  for (int i = 0; i < n; i++) {
    D.clear();
    if (!dfs(i, dfs)) {
      puts("No");
      return 0;
    }
  }

  puts("Yes");
}


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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<array<int64_t, 2>>> E(n);

  for (int i = 0; i < m; i++) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    E[l].push_back({r, d});
    E[r].push_back({l, -d});
  }

  vector<bool> visited(n);
  map<int, int64_t> D;
  auto dfs = [&](auto i, auto self) -> bool {
    if (visited[i])
      return true;
    if (D.empty())
      D[i] = 0;

    visited[i] = true;
    bool res = true;
    for (const auto &e : E[i]) {
      int r = e[0];
      int64_t d = e[1];
      if (D.count(r) != 0 and D[r] != D[i] + d)
        return false;
      D[r] = D[i] + d;
      res &= self(r, self);
    }
    return res;
  };

  for (int i = 0; i < n; i++) {
    D.clear();
    if (!dfs(i, dfs)) {
      puts("No");
      return 0;
    }
  }

  puts("Yes");
}
Problem p03450 (C++)

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

class WeightedUnionFind {
  vector<int> parent, rank, diff_weight;

public:
  WeightedUnionFind(int size) {
    parent = rank = diff_weight = vector<int>(size, 0);
    rep(i, size) parent[i] = i;
  }

  int root(int x) {
    if (parent[x] == x)
      return x;
    int r = root(parent[x]);
    diff_weight[x] += diff_weight[parent[x]];
    parent[x] = r;
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int weight(int x) {
    root(x);
    return diff_weight[x];
  }

  int diff(int x, int y) { return weight(y) - weight(x); }

  void unite(int x, int y, int w) {
    w += weight(x) - weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      swap(x, y);
      w = -w;
    }
    if (rank[x] == rank[y])
      rank[x]++;
    parent[y] = x;
    diff_weight[y] = w;
  }
};

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

  int n, m;
  cin >> n >> m;
  WeightedUnionFind uf(n + 1);
  rep(i, m) {
    int l, r, d;
    cin >> l >> r >> d;
    if (uf.same(l, r)) {
      if (d != uf.diff(l, r)) {
        cout << "No\n";
        return 0;
      }
    } else
      uf.unite(l, r, d);
  }
  cout << "Yes\n";
  return 0;
}

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

class WeightedUnionFind {
  vector<int> parent, rank, diff_weight;

public:
  WeightedUnionFind(int size) {
    parent = rank = diff_weight = vector<int>(size, 0);
    rep(i, size) parent[i] = i;
  }

  int root(int x) {
    if (parent[x] == x)
      return x;
    int r = root(parent[x]);
    diff_weight[x] += diff_weight[parent[x]];
    return parent[x] = r;
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int weight(int x) {
    root(x);
    return diff_weight[x];
  }

  int diff(int x, int y) { return weight(y) - weight(x); }

  void unite(int x, int y, int w) {
    w += weight(x) - weight(y);
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y]) {
      swap(x, y);
      w = -w;
    }
    if (rank[x] == rank[y])
      rank[x]++;
    parent[y] = x;
    diff_weight[y] = w;
  }
};

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

  int n, m;
  cin >> n >> m;
  WeightedUnionFind uf(n + 1);
  rep(i, m) {
    int l, r, d;
    cin >> l >> r >> d;
    if (uf.same(l, r)) {
      if (d != uf.diff(l, r)) {
        cout << "No\n";
        return 0;
      }
    } else
      uf.unite(l, r, d);
  }
  cout << "Yes\n";
  return 0;
}
Problem p03451 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repR(i, n) for (ll i = n; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout << (x) << endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, vector<ll>, greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define mp make_pair
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
int main() {
  ll n;
  cin >> n;
  vvll a(n, vll(2)), b(n, vll(2));
  rep(i, 2) {
    rep(j, n) { cin >> a[i][j]; }
  }
  ll ans = 0;
  ll cu = a[0][0];
  rep(i, n) cu += a[1][i];
  ans = cu;
  rep(i, n - 1) {
    cu -= a[1][i];
    cu += a[0][i + 1];
    ans = max(ans, cu);
  }
  COUT(ans);
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repR(i, n) for (ll i = n; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout << (x) << endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, vector<ll>, greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define mp make_pair
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
int main() {
  ll n;
  cin >> n;
  vvll a(2, vll(n));
  rep(i, 2) {
    rep(j, n) { cin >> a[i][j]; }
  }
  ll ans = 0;
  ll cu = a[0][0];
  rep(i, n) cu += a[1][i];
  ans = cu;
  rep(i, n - 1) {
    cu -= a[1][i];
    cu += a[0][i + 1];
    ans = max(ans, cu);
  }
  COUT(ans);
}
Problem p03451 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define Fast_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define int long long

int32_t main() {
  Fast_IO
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<int> a(n + 1, 0);
  vector<int> b(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int j = 1; j <= n; j++)
    cin >> b[j];
  vector<int> prefixa(n + 2, 0);
  vector<int> suffixb(n + 2, 0);
  for (int i = 1; i <= n; i++) {
    prefixa[i] = prefixa[i - 1] + a[i];
  }
  for (int i = n; i >= 1; i--) {
    suffixb[i] = suffixb[i + 1] + b[i];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, prefixa[i] + suffixb[i]);
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define Fast_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define int long long

int32_t main() {
  Fast_IO
      // #ifndef ONLINE_JUDGE
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w",stdout);
      // #endif
      int n;
  cin >> n;
  vector<int> a(n + 1, 0);
  vector<int> b(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int j = 1; j <= n; j++)
    cin >> b[j];
  vector<int> prefixa(n + 2, 0);
  vector<int> suffixb(n + 2, 0);
  for (int i = 1; i <= n; i++) {
    prefixa[i] = prefixa[i - 1] + a[i];
  }
  for (int i = n; i >= 1; i--) {
    suffixb[i] = suffixb[i + 1] + b[i];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, prefixa[i] + suffixb[i]);
  }
  cout << ans;
}
Problem p03451 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(2, vector<int>(n));
  for (int i = 0; i < 2; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 2; j <= n; j++) {
      a[i][j] += a[i][j - 1];
    }
  }
  int m = 0;
  for (int i = 1; i <= n; i++) {
    m = max(m, a[0][i] + a[1][n] - a[1][i - 1]);
  }
  cout << m << endl;
  return 0;
}


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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(2, vector<int>(n + 1));
  for (int i = 0; i < 2; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 2; j <= n; j++) {
      a[i][j] += a[i][j - 1];
    }
  }
  int m = 0;
  for (int i = 1; i <= n; i++) {
    m = max(m, a[0][i] + a[1][n] - a[1][i - 1]);
  }
  cout << m << endl;
  return 0;
}
Problem p03451 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define endl "\n"
#define zoom                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define PB push_back
#define MP make_pair
#define F first
#define S second
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  zoom;

  ll n;
  cin >> n;
  ll a[n], b[n];
  forn(i, n) { cin >> a[i]; }
  forn(i, n) { cin >> b[i]; }

  ll pa[n + 1];
  ll pb[n + 1];

  pa[0] = 0;
  pb[0] = 0;
  forn(i, n) {
    pa[i + 1] = pa[i] + a[i];
    pb[i + 1] = pb[i] + b[i];
  }
  ll maks = 0;
  for (ll i = 1; i <= n; i++) {

    maks = max(maks, pb[n] - pb[i - 1] + pa[i]);
    // cout << pb[n] - pb[i-1] + pa[i] << endl ;
  }

  cout << maks;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define endl "\n"
#define zoom                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define PB push_back
#define MP make_pair
#define F first
#define S second
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}

int main() {

  zoom;

  ll n;
  cin >> n;
  ll a[n], b[n];
  forn(i, n) { cin >> a[i]; }
  forn(i, n) { cin >> b[i]; }

  ll pa[n + 1];
  ll pb[n + 1];

  pa[0] = 0;
  pb[0] = 0;
  forn(i, n) {
    pa[i + 1] = pa[i] + a[i];
    pb[i + 1] = pb[i] + b[i];
  }
  ll maks = 0;
  for (ll i = 1; i <= n; i++) {

    maks = max(maks, pb[n] - pb[i - 1] + pa[i]);
    // cout << pb[n] - pb[i-1] + pa[i] << endl ;
  }

  cout << maks;
}
Problem p03451 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<vector<int>> v(n + 1, vector<int>(3));
  vector<vector<int>> dp(n + 1, vector<int>(3, 0));
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < n; j++) {
      cin >> v[i][j];
    }
  }
  dp[0][0] = v[0][0];
  dp[1][0] = v[1][0] + dp[0][0];
  for (int i = 1; i < n; i++) {
    dp[0][i] = dp[0][i - 1] + v[0][i];
  }
  for (int i = 1; i < n; i++) {
    dp[1][i] = max(dp[1][i - 1], dp[0][i]) + v[1][i];
  }
  cout << dp[1][n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<vector<int>> v(3, vector<int>(n + 1));
  vector<vector<int>> dp(3, vector<int>(n + 1, 0));
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < n; j++) {
      cin >> v[i][j];
    }
  }
  dp[0][0] = v[0][0];
  dp[1][0] = v[1][0] + dp[0][0];
  for (int i = 1; i < n; i++) {
    dp[0][i] = dp[0][i - 1] + v[0][i];
  }
  for (int i = 1; i < n; i++) {
    dp[1][i] = max(dp[1][i - 1], dp[0][i]) + v[1][i];
  }
  cout << dp[1][n - 1] << endl;
}
Problem p03451 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n;
  cin >> n;
  vi a1(n), a2(n);
  rep(i, n) cin >> a1[i];
  rep(i, n) cin >> a2[i];
  vi aa1(n), aa2(n);
  aa1[0] = 0;
  aa2[0] = 0;
  repA(i, 1, n) aa1[i] = aa1[i - 1] + a1[i - 1];
  repA(i, 1, n) aa2[i] = aa2[i - 1] + a2[n - i];
  int ans = 0;
  repA(i, 1, n) ans = max(ans, aa1[i] + aa2[n + 1 - i]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n;
  cin >> n;
  vi a1(n), a2(n);
  rep(i, n) cin >> a1[i];
  rep(i, n) cin >> a2[i];
  vi aa1(n + 1), aa2(n + 1);
  aa1[0] = 0;
  aa2[0] = 0;
  repA(i, 1, n) aa1[i] = aa1[i - 1] + a1[i - 1];
  repA(i, 1, n) aa2[i] = aa2[i - 1] + a2[n - i];
  int ans = 0;
  repA(i, 1, n) ans = max(ans, aa1[i] + aa2[n + 1 - i]);
  cout << ans << endl;
  return 0;
}