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

Buggy submission (status: Runtime Error):
// {{{
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
// }}}

using ll = long long;

#define FOR(t, x, v) for (t x = 0, v_ = (v); x < v_; x++)
#define FORR(t, x, v) for (t x = (v); x >= 0; x--)

int N;
ll A[101010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  FOR(int, x, N) cin >> A[x];

  ll total = 0;
  FOR(int, x, N) total += A[x];

  ll ans = numeric_limits<ll>::max();
  ll acc = A[0];
  for (int j = 1; j < N; ++j) {
    ans = min(ans, abs(acc - (total - acc)));
    acc += A[j];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// {{{
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
// }}}

using ll = long long;

#define FOR(t, x, v) for (t x = 0, v_ = (v); x < v_; x++)
#define FORR(t, x, v) for (t x = (v); x >= 0; x--)

int N;
ll A[201010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  FOR(int, x, N) cin >> A[x];

  ll total = 0;
  FOR(int, x, N) total += A[x];

  ll ans = numeric_limits<ll>::max();
  ll acc = A[0];
  for (int j = 1; j < N; ++j) {
    ans = min(ans, abs(acc - (total - acc)));
    acc += A[j];
  }
  cout << ans << endl;
  return 0;
}
Problem p03659 (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;

ll n, a[100005], s[100005], res = longINF;

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 >> n >> a[0];
  s[0] = a[0];
  for (int i = 1; i < n; i++) {
    cin >> a[i];
    s[i] = s[i - 1] + a[i];
  }
  for (int i = 0; i < n - 1; i++)
    res = min(res, abs(s[i] - (s[n - 1] - s[i])));
  cout << res;
  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;

ll n, a[200005], s[200005], res = longINF;

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 >> n >> a[0];
  s[0] = a[0];
  for (int i = 1; i < n; i++) {
    cin >> a[i];
    s[i] = s[i - 1] + a[i];
  }
  for (int i = 0; i < n - 1; i++)
    res = min(res, abs(s[i] - (s[n - 1] - s[i])));
  cout << res;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
  long long int a[100000], mini = 100000000000, n, sum1 = 0, sum2 = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum1 += a[i];
  }
  for (int i = n - 1; 0 < i; i--) {
    sum1 -= a[i];
    sum2 += a[i];
    if (abs(sum1 - sum2) < mini) {
      mini = abs(sum1 - sum2);
    }
  }
  cout << mini << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
  long long int a[200000], mini = 100000000000, n, sum1 = 0, sum2 = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum1 += a[i];
  }
  for (int i = n - 1; 0 < i; i--) {
    sum1 -= a[i];
    sum2 += a[i];
    if (abs(sum1 - sum2) < mini) {
      mini = abs(sum1 - sum2);
    }
  }
  cout << mini << endl;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, init, a) for (int i = init; i < a; i++)
#define rep(i, a) FOR(i, 0, a)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a, n) memset(a, n, sizeof(a))
#define all(a) a.begin(), a.end()
#define chmin(a, b) a = min(a, b);
#define chmax(a, b) a = max(a, b);

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

int N;
ll nums[100009];

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

  cin >> N;
  rep(i, N) cin >> nums[i];
  rep(i, N - 1) nums[i + 1] += nums[i];

  ll mn = LLINF;
  rep(i, N - 1) {
    ll x = nums[i];
    ll y = nums[N - 1] - x;
    chmin(mn, abs(x - y));
  }
  cout1(mn)
}

Fixed submission:
#include "bits/stdc++.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i, init, a) for (int i = init; i < a; i++)
#define rep(i, a) FOR(i, 0, a)
#define rrep(i, a) for (int i = a; i >= 0; i--)
#define rep1(i, a) for (int i = 1; i <= a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a, b) cout << a << " " << b << endl;
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl;
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a, n) memset(a, n, sizeof(a))
#define all(a) a.begin(), a.end()
#define chmin(a, b) a = min(a, b);
#define chmax(a, b) a = max(a, b);

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

int N;
ll nums[200009];

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

  cin >> N;
  rep(i, N) cin >> nums[i];
  rep(i, N - 1) nums[i + 1] += nums[i];

  ll mn = LLINF;
  rep(i, N - 1) {
    ll x = nums[i];
    ll y = nums[N - 1] - x;
    chmin(mn, abs(x - y));
  }
  cout1(mn)
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define INF 1LL << 60
#define MAX 1000000000
#define mod 1000000007
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll c(ll n, ll k) {
  ll a = 1;
  rep(i, 0, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll k) {
  ll a = 1, b = 1;
  rep(i, 0, k) {
    a = a * (n - i + 1) % mod;
    b = b * i % mod;
  }
  return a * mpower(b, mod - 2) % mod;
}

const double PI = 3.14159265358979323846;
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
int main() {
  int N;
  scanf("%d", &N);
  vector<int> A(N);
  vector<vector<int>> dp(N, vector<int>(N));
  ll s = 0, t = 0, ans;
  rep(i, 0, N) scanf("%d", &A[i]), s += A[i];
  // abs(2 * t - s)
  ans = abs(2 * A[0] - s);
  rep(i, 0, N - 1) {
    t += A[i];
    if (i)
      ans = min(ans, abs(2 * t - s));
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define INF 1LL << 60
#define MAX 1000000000
#define mod 1000000007
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll c(ll n, ll k) {
  ll a = 1;
  rep(i, 0, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll k) {
  ll a = 1, b = 1;
  rep(i, 0, k) {
    a = a * (n - i + 1) % mod;
    b = b * i % mod;
  }
  return a * mpower(b, mod - 2) % mod;
}

const double PI = 3.14159265358979323846;
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
int main() {
  int N;
  scanf("%d", &N);
  vector<int> A(N);
  ll s = 0, t = 0, ans;
  rep(i, 0, N) scanf("%d", &A[i]), s += A[i];
  // abs(2 * t - s)
  ans = abs(2 * A[0] - s);
  rep(i, 0, N - 1) {
    t += A[i];
    if (i)
      ans = min(ans, abs(2 * t - s));
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long a[100000], s[100000];
  s[0] = 0;
  for (int i = 0; i < n; i++) {
    /* code */
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    /* code */
    s[i] = s[i - 1] + a[i - 1];
  }
  long long ans = 0, ans2 = 1000000000000000000LL;
  for (int i = 1; i < n; i++) {
    /* code */
    ans = abs(s[i] - (s[n] - s[i]));
    ans2 = min(ans, ans2);
  }
  cout << ans2 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long a[1000000], s[1000000];
  s[0] = 0;
  for (int i = 0; i < n; i++) {
    /* code */
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    /* code */
    s[i] = s[i - 1] + a[i - 1];
  }
  long long ans = 0, ans2 = 1000000000000000000LL;
  for (int i = 1; i < n; i++) {
    /* code */
    ans = abs(s[i] - (s[n] - s[i]));
    ans2 = min(ans, ans2);
  }
  cout << ans2 << endl;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
ll pr[100500];
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    pr[i + 1] = x + pr[i];
  }
  ll ans = LONG_MAX;
  for (int i = 1; i <= n - 1; i++) {
    ans = min(ans, abs(pr[i] - pr[n] + pr[i]));
  }
  cout << ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--)
    solve();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
ll pr[200500];
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    pr[i + 1] = x + pr[i];
  }
  ll ans = LONG_MAX;
  for (int i = 1; i <= n - 1; i++) {
    ans = min(ans, abs(pr[i] - pr[n] + pr[i]));
  }
  cout << ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--)
    solve();
}
Problem p03659 (C++)

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

using namespace std;

typedef long long int ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define mm 100005
#define nn 1005
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (int)(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define rep(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define cases                                                                  \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define check(a, n)                                                            \
  forn(iiii, int(n)) cout << int(a[iiii]) << " ";                              \
  cout << endl
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
ll a[mm], ans = 1e18;

int main() {
  int n;
  cin >> n;
  forn(i, n) cin >> a[i + 1], a[i + 1] += a[i];
  forn(i, n - 1) ans = min(ans, abs(2LL * a[i + 1] - a[n]));
  cout << ans;
  return 0;
}


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

using namespace std;

typedef long long int ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define mm 100005
#define nn 1005
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (int)(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define rep(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define cases                                                                  \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define check(a, n)                                                            \
  forn(iiii, int(n)) cout << int(a[iiii]) << " ";                              \
  cout << endl
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
ll a[2 * mm], ans = 1e18;

int main() {
  int n;
  cin >> n;
  forn(i, n) cin >> a[i + 1], a[i + 1] += a[i];
  forn(i, n - 1) ans = min(ans, abs(2LL * a[i + 1] - a[n]));
  cout << ans;
  return 0;
}
Problem p03659 (C++)

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

int main() {
  long long ans, n, array[100000], snuke, kuma = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> array[i];
    if (i) {
      kuma += array[i];
    }
  }
  snuke = array[0];
  ans = abs(snuke - kuma);
  for (int i = 1; i < n - 1; i++) {
    snuke += array[i];
    kuma -= array[i];
    ans = min(ans, abs(snuke - kuma));
  }
  cout << ans << endl;
}


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

int main() {
  long long ans, n, array[200000], snuke, kuma = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> array[i];
    if (i) {
      kuma += array[i];
    }
  }
  snuke = array[0];
  ans = abs(snuke - kuma);
  for (int i = 1; i < n - 1; i++) {
    snuke += array[i];
    kuma -= array[i];
    ans = min(ans, abs(snuke - kuma));
  }
  cout << ans << endl;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n;
  cin >> n;
  long long X = 0, x = 0, ans = 1000000000000000000LL;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    X += a[i];
  }
  for (int i = 0; i < n; i++) {
    x += a[i];
    if (i + 1 < n)
      ans = min(ans, abs(X - 2 * x));
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n;
  long long X = 0, x = 0, ans = 1000000000000000000LL;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    X += a[i];
  }
  for (int i = 0; i < n; i++) {
    x += a[i];
    if (i + 1 < n)
      ans = min(ans, abs(X - 2 * x));
  }
  cout << ans << endl;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long LL;
LL n;
LL a[100005];
LL l[100005], r[100005], ans = 10000000000;
int main() {
  scanf("%lld", &n);
  for (LL i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    l[i] = l[i - 1] + a[i];
  }
  for (LL i = n; i >= 1; i--) {
    r[i] = r[i + 1] + a[i];
  }
  for (LL i = 1; i < n; i++) {
    ans = min(ans, abs(l[i] - r[i + 1]));
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long LL;
LL n;
LL a[200005];
LL l[200005], r[200005], ans = 10000000000;
int main() {
  scanf("%lld", &n);
  for (LL i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    l[i] = l[i - 1] + a[i];
  }
  for (LL i = n; i >= 1; i--) {
    r[i] = r[i + 1] + a[i];
  }
  for (LL i = 1; i < n; i++) {
    ans = min(ans, abs(l[i] - r[i + 1]));
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
#define int long long
#define mod 1000000007
signed main() {
  int n;
  cin >> n;
  int a[114514];
  int sum = 0;
  int ans = 1000000000000000001;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  int s = 0;
  for (int i = 0; i < n - 1; i++) {
    s += a[i];
    ans = min(ans, abs(s - (sum - s)));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
#define int long long
#define mod 1000000007
signed main() {
  int n;
  cin >> n;
  int a[1145140];
  int sum = 0;
  int ans = 1000000000000000001;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  int s = 0;
  for (int i = 0; i < n - 1; i++) {
    s += a[i];
    ans = min(ans, abs(s - (sum - s)));
  }
  cout << ans << endl;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define pb push_back
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define INF (1 << 10)
#define LLINF (1 << 30)
#define MOD 1000000007
using ll = long long;
using namespace std;

int main() {
  ll n, a[20000], sum[20000] = {};
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    if (i != 0) {
      sum[i] = sum[i - 1] + a[i];
    } else {
      sum[i] = a[i];
    }
  }
  ll ans = abs(sum[0] - (sum[n - 1] - sum[0]));
  for (int i = 1; i < n - 1; i++) {
    ans = min(ans, abs(sum[i] - (sum[n - 1] - sum[i])));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define pb push_back
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define INF (1 << 10)
#define LLINF (1 << 30)
#define MOD 1000000007
using ll = long long;
using namespace std;

int main() {
  ll n, a[200000], sum[200000] = {};
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    if (i != 0) {
      sum[i] = sum[i - 1] + a[i];
    } else {
      sum[i] = a[i];
    }
  }
  ll ans = abs(sum[0] - (sum[n - 1] - sum[0]));
  for (int i = 1; i < n - 1; i++) {
    ans = min(ans, abs(sum[i] - (sum[n - 1] - sum[i])));
  }
  cout << ans << endl;
  return 0;
}
Problem p03659 (C++)

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

long long n;
long long a[100006] = {0};

long long solve();

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (i != 0)
      a[i] += a[i - 1];
  }
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long ans = 100000000000000000;
  for (int i = 0; i < n - 1; ++i) {
    long long l, r;
    l = a[i];
    r = a[n - 1] - l;
    ans = min(max(l - r, r - l), ans);
  }
  return ans;
}

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

long long n;
long long a[200006] = {0};

long long solve();

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (i != 0)
      a[i] += a[i - 1];
  }
  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long ans = 100000000000000000;
  for (int i = 0; i < n - 1; ++i) {
    long long l, r;
    l = a[i];
    r = a[n - 1] - l;
    ans = min(max(l - r, r - l), ans);
  }
  return ans;
}
Problem p03659 (C++)

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)

long long a[100010];

int main() {
  int N;
  cin >> N;
  rep(i, N) cin >> a[i];

  long long sum = accumulate(a, a + N, 0ll);
  long long sun = 0;
  long long mi = 200000000000000000;
  rep(i, N - 1) {
    sun += a[i];
    mi = min(mi, abs(sun - (sum - sun)));
  }
  cout << mi << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)

int a[200020];

int main() {
  int N;
  cin >> N;
  rep(i, N) cin >> a[i];

  long long sum = accumulate(a, a + N, 0ll);
  long long sun = 0;
  long long mi = 200000000000000000;
  rep(i, N - 1) {
    sun += a[i];
    mi = min(mi, abs(sun - (sum - sun)));
  }
  cout << mi << endl;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mne 10000000000
long long a[101010], n, b[101010], mn = mne, x;

int main() {
  cin >> n;
  b[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i] + b[i - 1];
  }
  for (int i = 1; i <= n - 1; i++) {
    x = abs(2 * b[i] - b[n]);
    mn = min(x, mn);
  }
  cout << mn << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mne 10000000000000000
long long a[201010], n, b[201010], mn = mne, x;

int main() {
  cin >> n;
  b[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i] + b[i - 1];
  }
  for (int i = 1; i <= n - 1; i++) {
    x = abs(2 * b[i] - b[n]);
    mn = min(x, mn);
  }
  cout << mn << endl;

  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define rep(n) for (int i = 0; i < (n); i++)
typedef pair<int, int> pii;

typedef long long ll;
typedef pair<ll, ll> pll;
const int MOD = 1000000007;
const int INF = 1 << 29;

int main() {
  int n, a[100001];
  ll b[100001];
  cin >> n;
  rep(n) { cin >> a[i]; }
  ll sum = 0;
  rep(n) {
    sum += a[i];
    b[i] = sum;
  }
  ll mi = llabs(b[n - 1] - b[0] - b[0]);
  rep(n - 2) { mi = min(llabs(b[i] - (b[n - 1] - b[i])), mi); }
  cout << mi << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define rep(n) for (int i = 0; i < (n); i++)
typedef pair<int, int> pii;

typedef long long ll;
typedef pair<ll, ll> pll;
const int MOD = 1000000007;
const int INF = 1 << 29;

int main() {
  int n, a[200001];
  ll b[200001];
  cin >> n;
  rep(n) { cin >> a[i]; }
  ll sum = 0;
  rep(n) {
    sum += a[i];
    b[i] = sum;
  }
  ll mi = llabs(b[n - 1] - b[0] - b[0]);
  rep(n - 2) { mi = min(llabs(b[i] - (b[n - 1] - b[i])), mi); }
  cout << mi << endl;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>
long long num[100000 + 50];
int main() {
  long long n;
  while (scanf("%lld", &n) != EOF) {
    long long i, sn = 0, k;
    for (i = 0; i < n; i++) {
      scanf("%lld", &num[i]);
      sn += num[i];
    }
    k = fabs(sn - 2 * num[0]);
    for (i = 0; i < n - 1; i++) {
      sn -= 2 * num[i];
      // printf("sn=%lld\n",sn);
      if (k > fabs(sn))
        k = fabs(sn);
      // printf("k=%lld\n",k);
    }
    if (k < 0)
      k *= -1;
    printf("%lld\n", k);
  }
}

Fixed submission:
#include <math.h>
#include <stdio.h>
long long num[200000 + 50];
int main() {
  long long n;
  while (scanf("%lld", &n) != EOF) {
    long long i, sn = 0, k;
    for (i = 0; i < n; i++) {
      scanf("%lld", &num[i]);
      sn += num[i];
    }
    k = fabs(sn - 2 * num[0]);
    for (i = 0; i < n - 1; i++) {
      sn -= 2 * num[i];
      // printf("sn=%lld\n",sn);
      if (k > fabs(sn))
        k = fabs(sn);
      // printf("k=%lld\n",k);
    }
    if (k < 0)
      k *= -1;
    printf("%lld\n", k);
  }
}
Problem p03660 (C++)

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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

vector<int> g[10000];
int d1[100000] = {};
int dn[100000] = {};

void dfs(int s, int d[]) {
  rep(i, g[s].size()) {
    int v = g[s][i];
    if (d[v] != -1)
      continue;

    d[v] = d[s] + 1;
    dfs(v, d);
  }
}

int main() {
  rep(i, 100000) {
    d1[i] = -1;
    dn[i] = -1;
  }
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  d1[0] = 0;
  dn[n - 1] = 0;
  dfs(0, d1);
  dfs(n - 1, dn);

  int cnt = 0;

  rep(i, n) if (d1[i] <= dn[i]) cnt++;

  if (cnt > n - cnt)
    OP("Fennec");
  else
    OP("Snuke");

  return 0;
}

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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

vector<int> g[100000];
int d1[100000] = {};
int dn[100000] = {};

void dfs(int s, int d[]) {
  rep(i, g[s].size()) {
    int v = g[s][i];
    if (d[v] != -1)
      continue;

    d[v] = d[s] + 1;
    dfs(v, d);
  }
}

int main() {
  rep(i, 100000) {
    d1[i] = -1;
    dn[i] = -1;
  }
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  d1[0] = 0;
  dn[n - 1] = 0;
  dfs(0, d1);
  dfs(n - 1, dn);

  int cnt = 0;

  rep(i, n) if (d1[i] <= dn[i]) cnt++;

  if (cnt > n - cnt)
    OP("Fennec");
  else
    OP("Snuke");

  return 0;
}
Problem p03660 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;

int n;
int dist[2][100005];
vector<int> v[100005];
queue<int> que;
bool visit[10005];

void bfs(int id, int s) {
  for (int i = 0; i < n; i++)
    visit[i] = false;
  que.push(s);
  visit[s] = true;
  dist[id][s] = 0;
  while (que.size() != 0) {
    int now = que.front();
    que.pop();
    for (int i = 0; i < v[now].size(); i++) {
      int next = v[now][i];
      if (!visit[next]) {
        dist[id][next] = dist[id][now] + 1;
        visit[next] = true;
        que.push(next);
      }
    }
  }
}
int main(void) {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    v[s].push_back(t);
    v[t].push_back(s);
  }

  bfs(0, 0);
  bfs(1, n - 1);

  int fe, sn;
  fe = sn = 0;
  for (int i = 0; i < n; i++) {
    if (dist[0][i] > dist[1][i])
      fe++;
    else
      sn++;
  }

  if (fe < sn)
    cout << "Fennec" << endl;
  else
    cout << "Snuke" << endl;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;

int n;
int dist[2][100005];
vector<int> v[100005];
queue<int> que;
bool visit[100005];

void bfs(int id, int s) {
  for (int i = 0; i < n; i++)
    visit[i] = false;
  que.push(s);
  visit[s] = true;
  dist[id][s] = 0;
  while (que.size() != 0) {
    int now = que.front();
    que.pop();
    for (int i = 0; i < v[now].size(); i++) {
      int next = v[now][i];
      if (!visit[next]) {
        dist[id][next] = dist[id][now] + 1;
        visit[next] = true;
        que.push(next);
      }
    }
  }
}
int main(void) {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    v[s].push_back(t);
    v[t].push_back(s);
  }

  bfs(0, 0);
  bfs(1, n - 1);

  int fe, sn;
  fe = sn = 0;
  for (int i = 0; i < n; i++) {
    if (dist[0][i] > dist[1][i])
      fe++;
    else
      sn++;
  }

  if (fe < sn)
    cout << "Fennec" << endl;
  else
    cout << "Snuke" << endl;
}
Problem p03660 (C++)

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

#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#include <algorithm>
#include <cmath>
#include <numeric>

using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

/*

両者からの距離が同じかそれよりフェネックに近い色のが多ければふぇね

*/

void dfs(int u, vector<int> &d, vector<vector<int>> G, int N) {
  for (int i = 0; i < G[u].size(); i++) {
    if (d[G[u][i]] == -1) {
      d[G[u][i]] = d[u] + 1;
      dfs(G[u][i], d, G, N);
    }
  }
}

int main() {
  int N;
  cin >> N;
  int res = 0;
  vector<vector<int>> G(N, vector<int>{});
  vector<int> d1(N, -1), d2(N, -1);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }

  d1[0] = d2[N - 1] = 0;
  dfs(0, d1, G, N);
  dfs(N - 1, d2, G, N);
  for (int i = 0; i < N; i++) {
    if (d1[i] > d2[i])
      res++;
  }
  if (N % 2 == 0) {
    if (res < N / 2) {
      cout << "Fennec\n";
    } else {
      cout << "Snuke\n";
    }
  } else {
    if (res <= N / 2) {
      cout << "Fennec\n";
    } else {
      cout << "Snuke\n";
    }
  }
}


Fixed submission:
#include <cstdio>
#include <iomanip>
#include <iostream>

#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#include <algorithm>
#include <cmath>
#include <numeric>

using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

/*

両者からの距離が同じかそれよりフェネックに近い色のが多ければふぇね

*/

void dfs(int u, vector<int> &d, vector<vector<int>> &G, int N) {
  for (int i = 0; i < G[u].size(); i++) {
    if (d[G[u][i]] == -1) {
      d[G[u][i]] = d[u] + 1;
      dfs(G[u][i], d, G, N);
    }
  }
}

int main() {
  int N;
  cin >> N;
  int res = 0;
  vector<vector<int>> G(N, vector<int>{});
  vector<int> d1(N, -1), d2(N, -1);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }

  d1[0] = d2[N - 1] = 0;
  dfs(0, d1, G, N);
  dfs(N - 1, d2, G, N);
  for (int i = 0; i < N; i++) {
    if (d1[i] > d2[i])
      res++;
  }
  if (N % 2 == 0) {
    if (res < N / 2) {
      cout << "Fennec\n";
    } else {
      cout << "Snuke\n";
    }
  } else {
    if (res <= N / 2) {
      cout << "Fennec\n";
    } else {
      cout << "Snuke\n";
    }
  }
}
Problem p03660 (C++)

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

const ll MAX = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;

ll N, ans = 0, tmp = 0;
vector<ll> db, de;
vector<vector<ll>> G;
/*
void dfsb(ll id, ll now) {
    db[id] = now;
    for(int i=0;i<G[id].size();i++) {
        if(now+1<db[G[id][i]]) {
            dfsb(G[id][i], now+1);
        }
    }
}

void dfse(ll id, ll now) {
    de[id] = now;
    for(int i=0;i<G[id].size();i++) {
        if(now+1<de[G[id][i]]) {
            dfse(G[id][i], now+1);
        }
    }
}
*/
void dfs(ll id, ll now, vector<ll> &d) {
  d[id] = now;
  for (int i = 0; i < G[id].size(); i++) {
    if (now + 1 < db[G[id][i]]) {
      dfs(G[id][i], now + 1, d);
    }
  }
}

signed main() {
  cin >> N;
  G.resize(N);
  for (int i = 0; i < N - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  db.resize(N, INF);
  de.resize(N, INF);
  /*
  dfsb(0, 0);
  dfse(N-1, 0);
  */
  dfs(0, 0, db);
  dfs(N - 1, 0, de);
  for (int i = 0; i < N; i++) {
    if (db[i] > de[i])
      tmp++;
  }
  // cout << N-tmp << " " << tmp << endl;
  if (N - tmp > tmp)
    cout << "Fennec" << endl;
  else
    cout << "Snuke" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

const ll MAX = 200000;
const ll INF = 1001001001;
const ll MOD = 1000000007;

ll N, ans = 0, tmp = 0;
vector<ll> db, de;
vector<vector<ll>> G;
/*
void dfsb(ll id, ll now) {
    db[id] = now;
    for(int i=0;i<G[id].size();i++) {
        if(now+1<db[G[id][i]]) {
            dfsb(G[id][i], now+1);
        }
    }
}

void dfse(ll id, ll now) {
    de[id] = now;
    for(int i=0;i<G[id].size();i++) {
        if(now+1<de[G[id][i]]) {
            dfse(G[id][i], now+1);
        }
    }
}
*/
void dfs(ll id, ll now, vector<ll> &d) {
  d[id] = now;
  for (int i = 0; i < G[id].size(); i++) {
    if (now + 1 < d[G[id][i]]) {
      dfs(G[id][i], now + 1, d);
    }
  }
}

signed main() {
  cin >> N;
  G.resize(N);
  for (int i = 0; i < N - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  db.resize(N, INF);
  de.resize(N, INF);
  /*
  dfsb(0, 0);
  dfse(N-1, 0);
  */
  dfs(0, 0, db);
  dfs(N - 1, 0, de);
  for (int i = 0; i < N; i++) {
    if (db[i] > de[i])
      tmp++;
  }
  // cout << N-tmp << " " << tmp << endl;
  if (N - tmp > tmp)
    cout << "Fennec" << endl;
  else
    cout << "Snuke" << endl;
  return 0;
}
Problem p03660 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;

int N;
const int MAX_N = 100010;
int color[MAX_N], par[MAX_N];
mat path;

void dfs(int v, int p) {
  par[v] = p;
  for (int u : path[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
}

void paint(int v, int p) {
  color[v] = color[p];
  for (int u : path[v]) {
    if (u == p)
      continue;
    if (color[u])
      continue;
    paint(u, v);
  }
}

bool solve() {
  dfs(1, 0);
  int cnt = 0;
  for (int v = N; v > 0; v = par[v]) {
    cnt++;
  }
  for (int v = N, i = 0; v > 0; v = par[v], i++) {
    if (i < cnt / 2)
      color[v] = 2;
    else
      color[v] = 1;
  }

  for (int i = 1; i <= N; i++) {
    paint(i, i);
  }

  cnt = 0;
  for (int i = 1; i <= N; i++) {
    cnt += (color[i] == 1);
  }
  return cnt > (N / 2);
}

int main() {

  scanf("%d", &N);
  path.assign(N + 1, vec());
  for (int i = 0, x, y; i < N - 1; i++) {
    scanf("%d%d", &x, &y);
    path[x].push_back(y);
    path[y].push_back(x);
  }

  if (solve()) {
    puts("Fennec");
  } else {
    puts("Snuke");
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;

int N;
const int MAX_N = 100010;
int color[MAX_N], par[MAX_N];
mat path;

void dfs(int v, int p) {
  par[v] = p;
  for (int u : path[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
}

void paint(int v, int p) {
  color[v] = color[p];
  for (int u : path[v]) {
    if (u == p)
      continue;
    if (color[u])
      continue;
    paint(u, v);
  }
}

bool solve() {
  dfs(1, 0);
  int cnt = 0;
  for (int v = N; v > 0; v = par[v]) {
    cnt++;
  }
  for (int v = N, i = 0; v > 0; v = par[v], i++) {
    if (i < cnt / 2)
      color[v] = 2;
    else
      color[v] = 1;
  }

  for (int v = N; v > 0; v = par[v]) {
    paint(v, v);
  }

  cnt = 0;
  for (int i = 1; i <= N; i++) {
    cnt += (color[i] == 1);
  }
  return cnt > (N / 2);
}

int main() {

  scanf("%d", &N);
  path.assign(N + 1, vec());
  for (int i = 0, x, y; i < N - 1; i++) {
    scanf("%d%d", &x, &y);
    path[x].push_back(y);
    path[y].push_back(x);
  }

  if (solve()) {
    puts("Fennec");
  } else {
    puts("Snuke");
  }

  return 0;
}
Problem p03660 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; // 10^18
const int inINF = 2147483647;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }

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

  stack<int> st;
  st.push(0);
  vector<int> par(N, -1);
  par[0] = -1;
  while (!st.empty()) {
    int v = st.top();
    st.pop();
    for (int w : G[v]) {
      if (par[w] == -1) {
        par[w] = v;
        st.push(w);
      }
    }
  }

  int d = 0;
  int now = N - 1;
  while (now != 0) {
    now = par[now];
    d++;
  }
  d = (d - 1) / 2;
  now = N - 1;
  while (d > 0) {
    now = par[now];
    d--;
  }
  int m = par[now];

  int cnt = 0;
  vector<bool> used(N, false);
  used[m] = true;
  st.push(now);
  while (!st.empty()) {
    int v = st.top();
    st.pop();
    cnt++;
    used[v] = true;
    for (int w : G[v]) {
      if (used[w])
        continue;
      st.push(w);
    }
  }
  if (N - cnt > cnt) {
    printf("Fennec\n");
  } else {
    printf("Snuke\n");
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; // 10^18
const int inINF = 2147483647;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }

int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N);
  int a, b;
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  stack<int> st;
  st.push(0);
  vector<int> par(N, -1);
  par[0] = -1;
  while (!st.empty()) {
    int v = st.top();
    st.pop();
    for (int w : G[v]) {
      if (par[w] == -1) {
        par[w] = v;
        st.push(w);
      }
    }
  }

  int d = 0;
  int now = N - 1;
  while (now != 0) {
    now = par[now];
    d++;
  }
  d = (d - 1) / 2;
  now = N - 1;
  while (d > 0) {
    now = par[now];
    d--;
  }
  int m = par[now];

  int cnt = 0;
  vector<bool> used(N, false);
  used[m] = true;
  st.push(now);
  while (!st.empty()) {
    int v = st.top();
    st.pop();
    cnt++;
    used[v] = true;
    for (int w : G[v]) {
      if (used[w])
        continue;
      st.push(w);
    }
  }
  if (N - cnt > cnt) {
    printf("Fennec\n");
  } else {
    printf("Snuke\n");
  }
}
Problem p03660 (C++)

Buggy submission (status: Runtime Error):
// code by 27.
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
vector<int> a[10000];
int f[100001];
queue<int> que;
int main() {
  int g = 0, g1 = 0, g2 = 0;
  int n, m;
  cin >> n;
  int x, y;
  for (int i = 1; i <= n - 1; i++) {
    cin >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  que.push(1);
  que.push(n);
  f[1] = 1;
  f[n] = -1;
  while (g < n) {
    for (int i = 0; i < a[que.front()].size(); i++) {
      // cout<<"*"<<a[que.front()][i]<<" "<<f[a[que.front()][i]]<<endl;
      if (f[a[que.front()][i]] == 0 && f[que.front()] == -1) {
        f[a[que.front()][i]] = -1;
        que.push(a[que.front()][i]);
      } else if (f[a[que.front()][i]] == 0 && f[que.front()] == 1) {
        f[a[que.front()][i]] = 1;
        que.push(a[que.front()][i]);
      }
    }
    if (f[que.front()] == 1)
      g1++;
    else if (f[que.front()] == -1)
      g2++;
    que.pop();
    g++;
  }
  if (g2 >= g1)
    cout << "Snuke";
  else
    cout << "Fennec";
  return 0;
}

Fixed submission:
// code by 27.
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
vector<int> a[1000001];
int f[1000001];
queue<int> que;
int main() {
  int g = 0, g1 = 0, g2 = 0;
  int n, m;
  cin >> n;
  int x, y;
  for (int i = 1; i <= n - 1; i++) {
    cin >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  que.push(1);
  que.push(n);
  f[1] = 1;
  f[n] = -1;
  while (g < n) {
    for (int i = 0; i < a[que.front()].size(); i++) {
      // cout<<"*"<<a[que.front()][i]<<" "<<f[a[que.front()][i]]<<endl;
      if (f[a[que.front()][i]] == 0 && f[que.front()] == -1) {
        f[a[que.front()][i]] = -1;
        que.push(a[que.front()][i]);
      } else if (f[a[que.front()][i]] == 0 && f[que.front()] == 1) {
        f[a[que.front()][i]] = 1;
        que.push(a[que.front()][i]);
      }
    }
    if (f[que.front()] == 1)
      g1++;
    else if (f[que.front()] == -1)
      g2++;
    que.pop();
    g++;
  }
  if (g2 >= g1)
    cout << "Snuke";
  else
    cout << "Fennec";
  return 0;
}
Problem p03660 (C++)

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

using namespace std;

const int MAXN = 1000 + 10;
vector<int> edge[MAXN];
int n, cnt[3];
int tree[MAXN];

void bfs() {
  queue<int> q;
  q.push(1), q.push(n);
  while (!q.empty()) {
    int temp = q.front();
    q.pop();
    cnt[tree[temp]]++;
    for (int i = 0; i < edge[temp].size(); ++i) {
      int v = edge[temp][i];
      // cnt[tree[v]]++;
      if (tree[v])
        continue;
      else
        tree[v] = tree[temp];
      q.push(v);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    edge[x].push_back(y);
    edge[y].push_back(x);
  }
  tree[1] = 1, tree[n] = 2;
  bfs();
  if (cnt[2] >= cnt[1])
    cout << "Snuke" << endl;
  else
    cout << "Fennec" << endl;
  return 0;
}

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

using namespace std;

const int MAXN = 100000 + 10;
vector<int> edge[MAXN];
int n, cnt[3];
int tree[MAXN];

void bfs() {
  queue<int> q;
  q.push(1), q.push(n);
  while (!q.empty()) {
    int temp = q.front();
    q.pop();
    cnt[tree[temp]]++;
    for (int i = 0; i < edge[temp].size(); ++i) {
      int v = edge[temp][i];
      // cnt[tree[v]]++;
      if (tree[v])
        continue;
      else
        tree[v] = tree[temp];
      q.push(v);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    edge[x].push_back(y);
    edge[y].push_back(x);
  }
  tree[1] = 1, tree[n] = 2;
  bfs();
  if (cnt[2] >= cnt[1])
    cout << "Snuke" << endl;
  else
    cout << "Fennec" << endl;
  return 0;
}
Problem p03660 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
vector<int> g[100000];
queue<int> fe;
queue<int> sn;
int n, u, v, t, k;
int d[100000];
int x, y;
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  fe.push(0);
  d[0] = 1;
  sn.push(n - 1);
  d[n - 1] = -1;
  t = 2;
  while (t < n) {
    k = fe.size();
    for (int i = 0; i < k; i++) {
      u = fe.front();
      fe.pop();
      for (int j = 0; j < g[u].size(); j++) {
        if (d[g[u][j]] == 0) {
          d[g[u][j]] = d[u] + 1;
          fe.push(g[u][j]);

          x++;
        }
      }
    }
    k = sn.size();
    for (int i = 0; i < k; i++) {
      u = sn.front();
      sn.pop();
      for (int j = 0; j < g[u].size(); j++) {
        if (d[g[u][j]] == 0) {
          d[g[u][j]] = d[u] - 1;
          sn.push(g[u][j]);
          y++;
        }
      }
    }
  }
  if (x > y)
    printf("Fennec\n");
  else
    printf("Snuke\n");
}

Fixed submission:
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
vector<int> g[100000];
queue<int> fe;
queue<int> sn;
int n, u, v, t, k;
int d[100000];
int x, y;
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  fe.push(0);
  d[0] = 1;
  sn.push(n - 1);
  d[n - 1] = -1;
  t = 2;
  x = 1;
  y = 1;
  while (x + y < n) {
    k = fe.size();
    for (int i = 0; i < k; i++) {
      u = fe.front();
      fe.pop();
      for (int j = 0; j < g[u].size(); j++) {
        if (d[g[u][j]] == 0) {
          d[g[u][j]] = d[u] + 1;
          fe.push(g[u][j]);

          x++;
        }
      }
    }
    k = sn.size();
    for (int i = 0; i < k; i++) {
      u = sn.front();
      sn.pop();
      for (int j = 0; j < g[u].size(); j++) {
        if (d[g[u][j]] == 0) {
          d[g[u][j]] = d[u] - 1;
          sn.push(g[u][j]);
          y++;
        }
      }
    }
  }
  if (x > y)
    printf("Fennec\n");
  else
    printf("Snuke\n");
}