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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// c++11
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

#define mp make_pair
#define mt make_tuple
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

const int INF = 1 << 29;
const double EPS = 1e-9;
const ll MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int MAX_N = 100100;
int N;
vector<int> A;
ll sum[MAX_N];
int main() {
  cin >> N;
  A.resize(N);
  for (auto &val : A) {
    cin >> val;
  }
  for (int i = 0; i < N; i++) {
    sum[i + 1] = sum[i] + A[i];
  }
  ll res = 1LL << 60;
  for (int i = 1; i < N; i++) {
    ll x = sum[i];
    ll y = sum[N] - sum[i];
    res = min(res, abs(x - y));
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// c++11
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

#define mp make_pair
#define mt make_tuple
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

const int INF = 1 << 29;
const double EPS = 1e-9;
const ll MOD = 1000000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
const int MAX_N = 200100;
int N;
vector<int> A;
ll sum[MAX_N];
int main() {
  cin >> N;
  A.resize(N);
  for (auto &val : A) {
    cin >> val;
  }
  for (int i = 0; i < N; i++) {
    sum[i + 1] = sum[i] + A[i];
  }
  ll res = 1LL << 60;
  for (int i = 1; i < N; i++) {
    ll x = sum[i];
    ll y = sum[N] - sum[i];
    res = min(res, abs(x - y));
  }
  cout << res << endl;
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>

using namespace std;

int a[100000];

int main() {
  int n, i;
  long long ans = 1e18, sum1 = 0, sum2 = 0;

  scanf("%d", &n);

  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);

  for (i = 0; i < n; i++)
    sum2 += a[i];

  for (i = 0; i < n - 1; i++) {
    sum1 += a[i];
    sum2 -= a[i];

    ans = min(ans, abs(sum1 - sum2));
  }

  printf("%lld\n", ans);

  return 0;
}


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

using namespace std;

int a[200000];

int main() {
  int n, i;
  long long ans = 1e18, sum1 = 0, sum2 = 0;

  scanf("%d", &n);

  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);

  for (i = 0; i < n; i++)
    sum2 += a[i];

  for (i = 0; i < n - 1; i++) {
    sum1 += a[i];
    sum2 -= a[i];

    ans = min(ans, abs(sum1 - sum2));
  }

  printf("%lld\n", ans);

  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
typedef long long LL;
const int MaxN = 1e5;
using namespace std;
LL a[MaxN + 5], sum[MaxN + 5];
int n;
int main() {
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &a[i]);
      sum[i] = sum[i - 1] + a[i];
    }
    LL ans = 1LL << 60;
    for (int i = 1; i <= n - 1; i++) {
      LL f = sum[i];
      LL e = sum[n] - sum[i];
      ans = min(ans, abs(f - e));
    }
    printf("%lld\n", ans);
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
typedef long long LL;
const int MaxN = 1e6;
using namespace std;
LL a[MaxN + 5], sum[MaxN + 5];
int n;
int main() {
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &a[i]);
      sum[i] = sum[i - 1] + a[i];
    }
    LL ans = 1LL << 60;
    for (int i = 1; i <= n - 1; i++) {
      LL f = sum[i];
      LL e = sum[n] - sum[i];
      ans = min(ans, abs(f - e));
    }
    printf("%lld\n", ans);
  }
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
const long long INF = 1e16;
long long Abs(long long x) { return x < 0 ? -x : x; }
long long a[100100];
long long sum[100100];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  long long ans = INF;
  for (int i = 1; i < n; i++) {
    if (Abs(sum[n] - sum[i] - sum[i]) < ans)
      ans = Abs(sum[n] - sum[i] - sum[i]);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;
const long long INF = 1e16;
long long Abs(long long x) { return x < 0 ? -x : x; }
long long a[200100];
long long sum[200100];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  long long ans = INF;
  for (int i = 1; i < n; i++) {
    if (Abs(sum[n] - sum[i] - sum[i]) < ans)
      ans = Abs(sum[n] - sum[i] - sum[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
ll a[20001];
int main(void) {
  int n;
  scanf("%d", &n);
  ll sum1 = 0, sum2 = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (i != n)
      sum1 += a[i];
    else
      sum2 += a[i];
  }
  ll ans = abs(sum1 - sum2);
  for (int i = n - 1; i > 1; i--) {
    sum1 -= a[i];
    sum2 += a[i];
    ll v;
    if (sum1 >= sum2)
      v = sum1 - sum2;
    else
      v = sum2 - sum1;
    ans = min(v, ans);
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
ll a[200001];
int main(void) {
  int n;
  scanf("%d", &n);
  ll sum1 = 0, sum2 = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (i != n)
      sum1 += a[i];
    else
      sum2 += a[i];
  }
  ll ans = abs(sum1 - sum2);
  for (int i = n - 1; i > 1; i--) {
    sum1 -= a[i];
    sum2 += a[i];
    ll v;
    if (sum1 >= sum2)
      v = sum1 - sum2;
    else
      v = sum2 - sum1;
    ans = min(v, ans);
  }
  printf("%lld", ans);
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MN 100000
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  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;
}
int n, a[MN + 5];
long long ans = 1e18, s[MN + 5];
inline long long Abs(long long x) { return x < 0 ? -x : x; }
int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    s[i] = s[i - 1] + (a[i] = read());
  for (int i = 1; i < n; ++i)
    ans = min(ans, Abs(s[i] - (s[n] - s[i])));
  cout << ans;
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MN 200000
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  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;
}
int n, a[MN + 5];
long long ans = 1e18, s[MN + 5];
inline long long Abs(long long x) { return x < 0 ? -x : x; }
int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    s[i] = s[i - 1] + (a[i] = read());
  for (int i = 1; i < n; ++i)
    ans = min(ans, Abs(s[i] - (s[n] - s[i])));
  cout << ans;
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#define N 100005
typedef long long ll;
using namespace std;
int n;
ll a[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", a + i);
    a[i] += a[i - 1];
  }
  ll ans = 0x7fffffffffffffff;
  for (int i = 1; i < n; ++i)
    ans = min(abs(a[n] - a[i] * 2), ans);
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#define N 200005
typedef long long ll;
using namespace std;
int n;
ll a[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", a + i);
    a[i] += a[i - 1];
  }
  ll ans = 0x7fffffffffffffff;
  for (int i = 1; i < n; ++i)
    ans = min(abs(a[n] - a[i] * 2), ans);
  printf("%lld", ans);
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mst(a, b) memset(a, b, sizeof(a))
#define lowbit(x) ((x) & (-x))
#define X first
#define Y second
#define lc o << 1
#define rc o << 1 | 1
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long LL;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 100000 + 10;
const int maxm = 2000 + 10;
const int mod = 1e9 + 7;
const double eps = 1e-8;
int a[maxn];
int main() {
#ifdef local
  freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll ans = INF, s = 0;
  for (int i = 1; i < n; i++) {
    s += a[i];
    ans = min(ans, abs(sum - s - s));
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mst(a, b) memset(a, b, sizeof(a))
#define lowbit(x) ((x) & (-x))
#define X first
#define Y second
#define lc o << 1
#define rc o << 1 | 1
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long LL;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 200000 + 10;
const int maxm = 2000 + 10;
const int mod = 1e9 + 7;
const double eps = 1e-8;
int a[maxn];
int main() {
#ifdef local
  freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll ans = INF, s = 0;
  for (int i = 1; i < n; i++) {
    s += a[i];
    ans = min(ans, abs(sum - s - s));
  }
  cout << ans << "\n";
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; ++i)
#define pb push_back
#define mp make_pair
#define frab(i, a, b) for (int i = a; i < b; ++i)

using namespace std;

typedef long long ll;
typedef long double ld;

const ll MOD = 1e9 + 7;
const ld PI = acos(-1);
const ll MAX = 1e12;
const ll INF = 1e17;
const ld EPS = 1e-9;
const int M = 1e2 + 12;
const int N = 1e5 + 10;

void solve() {}

ll a[N];

int main() {
  // freopen("a.in", "r", stdin);
  // freopen("mines.in", "r", stdin);
  // freopen("mines.out", "w", stdout);
  int n;
  cin >> n;
  fr(i, n) cin >> a[i];
  ll s = accumulate(a, a + n, 0ll);
  ll ans = INF;
  ll p = 0;
  frab(i, 0, n - 1) {
    p += a[i];
    ans = min(ans, abs(p - (s - p)));
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; ++i)
#define pb push_back
#define mp make_pair
#define frab(i, a, b) for (int i = a; i < b; ++i)

using namespace std;

typedef long long ll;
typedef long double ld;

const ll MOD = 1e9 + 7;
const ld PI = acos(-1);
const ll MAX = 1e12;
const ll INF = 1e17;
const ld EPS = 1e-9;
const int M = 1e2 + 12;
const int N = 3e5 + 10;

void solve() {}

ll a[N];

int main() {
  // freopen("a.in", "r", stdin);
  // freopen("mines.in", "r", stdin);
  // freopen("mines.out", "w", stdout);
  int n;
  cin >> n;
  fr(i, n) cin >> a[i];
  ll s = accumulate(a, a + n, 0ll);
  ll ans = INF;
  ll p = 0;
  frab(i, 0, n - 1) {
    p += a[i];
    ans = min(ans, abs(p - (s - p)));
  }
  cout << ans;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#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 <tuple>
#include <utility>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PI 4 * atan(1.0)
typedef long long ll;
typedef long double ld;
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int main() {
  const int MAX = 20001;
  int N;
  cin >> N;
  ll a[MAX] = {};
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 1; i < N; i++) {
    a[i] += a[i - 1];
  }
  ll mini = 9223372036854775807;
  for (int i = 0; i < N - 1; i++) {
    mini = min(mini, llabs(a[i] - (sum - a[i])));
  }
  cout << mini << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#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 <tuple>
#include <utility>
#include <vector>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PI 4 * atan(1.0)
typedef long long ll;
typedef long double ld;
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int main() {
  const int MAX = 200001;
  int N;
  cin >> N;
  ll a[MAX] = {};
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 1; i < N; i++) {
    a[i] += a[i - 1];
  }
  ll mini = 9223372036854775807;
  for (int i = 0; i < N - 1; i++) {
    mini = min(mini, llabs(a[i] - (sum - a[i])));
  }
  cout << mini << endl;
  return 0;
}
Problem p03661 (C++)

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

using namespace std;
long long getabs(long long vl) {
  if (vl < 0)
    return -vl;
  return vl;
}
int main() {
  long long sum[100008];
  sum[0] = 0;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> sum[i];
    sum[i] += sum[i - 1];
  }
  long long sol = getabs(sum[n] - sum[1] * 2);
  for (int i = 2; i < n; i++)
    if (sol > getabs(sum[n] - sum[i] * 2))
      sol = getabs(sum[n] - sum[i] * 2);
  cout << sol << endl;
  return 0;
}


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

using namespace std;
long long getabs(long long vl) {
  if (vl < 0)
    return -vl;
  return vl;
}
int main() {
  long long sum[200008];
  sum[0] = 0;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> sum[i];
    sum[i] += sum[i - 1];
  }
  long long sol = getabs(sum[n] - sum[1] * 2);
  for (int i = 2; i < n; i++)
    if (sol > getabs(sum[n] - sum[i] * 2))
      sol = getabs(sum[n] - sum[i] * 2);
  cout << sol << endl;
  return 0;
}
Problem p03661 (C++)

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

using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];
int main() {
  int n, i, j;
  long long s2 = 0, s1 = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    s2 += a[i];
  }
  long long ans = 1e16;
  for (i = 0; i < n - 1; i++) {
    s1 += a[i];
    s2 -= a[i];
    ans = min(ans, abs(s1 - s2));
  }
  cout << ans << endl;
  return 0;
}


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

using namespace std;
const int maxn = 3e5 + 5;
int a[maxn];
int main() {
  int n, i, j;
  long long s2 = 0, s1 = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    s2 += a[i];
  }
  long long ans = 1e16;
  for (i = 0; i < n - 1; i++) {
    s1 += a[i];
    s2 -= a[i];
    ans = min(ans, abs(s1 - s2));
  }
  cout << ans << endl;
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
typedef long long LL;

const int MAXN = 1e5 + 5;

LL s[MAXN + 5], sum[MAXN + 5];

int N;
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%lld", &s[i]);
  for (int i = 1; i <= N; i++)
    sum[i] = sum[i - 1] + s[i];
  LL Min = abs(sum[N] - sum[1] - sum[1]);
  for (int i = 1; i <= N - 1; i++)
    Min = min(Min, abs(sum[N] - sum[i] - sum[i]));
  printf("%lld\n", Min);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
typedef long long LL;

const int MAXN = 2e5 + 5;

LL s[MAXN + 5], sum[MAXN + 5];

int N;
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%lld", &s[i]);
  for (int i = 1; i <= N; i++)
    sum[i] = sum[i - 1] + s[i];
  LL Min = abs(sum[N] - sum[1] - sum[1]);
  for (int i = 1; i <= N - 1; i++)
    Min = min(Min, abs(sum[N] - sum[i] - sum[i]));
  printf("%lld\n", Min);
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define gec getchar
#define puc putchar
#define File(S) (freopen(S ".in", "r", stdin), freopen(S ".out", "w", stdout))
using namespace std;
typedef long long ll;
template <typename T> inline void read(T &a) {
  static char ch;
  static bool sign;
  for (ch = gec(), sign = 0; ch < '0' || ch > '9'; ch = gec())
    sign ^= (ch == '-');
  for (a = 0; ch >= '0' && ch <= '9'; ch = gec())
    a = ((a + (a << 2)) << 1) + (ch ^ '0');
  if (sign)
    a = -a;
}
template <typename T> inline void print(T x) {
  static int Buf[30], PS = 0, BUF;
  if (x < 0)
    puc('-'), x = -x;
  for (; x >= 10; x = BUF) {
    BUF = (x / 5) >> 1;
    Buf[++PS] = x - ((BUF + (BUF << 2)) << 1);
  }
  Buf[++PS] = x;
  while (PS)
    puc('0' ^ Buf[PS--]);
}
const int N = 1e5 + 10;
ll Sum = 0, S = 0, Ans = 0x7ffffffffffffff;
int n, a[N];
int main() {
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]), Sum += a[i];
  for (int i = 1; i < n; i++) {
    S += a[i];
    if (abs((Sum - S) - S) < Ans)
      Ans = abs((Sum - S) - S);
  }
  printf("%lld", Ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define gec getchar
#define puc putchar
#define File(S) (freopen(S ".in", "r", stdin), freopen(S ".out", "w", stdout))
using namespace std;
typedef long long ll;
template <typename T> inline void read(T &a) {
  static char ch;
  static bool sign;
  for (ch = gec(), sign = 0; ch < '0' || ch > '9'; ch = gec())
    sign ^= (ch == '-');
  for (a = 0; ch >= '0' && ch <= '9'; ch = gec())
    a = ((a + (a << 2)) << 1) + (ch ^ '0');
  if (sign)
    a = -a;
}
template <typename T> inline void print(T x) {
  static int Buf[30], PS = 0, BUF;
  if (x < 0)
    puc('-'), x = -x;
  for (; x >= 10; x = BUF) {
    BUF = (x / 5) >> 1;
    Buf[++PS] = x - ((BUF + (BUF << 2)) << 1);
  }
  Buf[++PS] = x;
  while (PS)
    puc('0' ^ Buf[PS--]);
}
const int N = 2e5 + 10;
ll Sum = 0, S = 0, Ans = 0x7ffffffffffffff;
int n, a[N];
int main() {
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]), Sum += a[i];
  for (int i = 1; i < n; i++) {
    S += a[i];
    if (abs((Sum - S) - S) < Ans)
      Ans = abs((Sum - S) - S);
  }
  printf("%lld", Ans);
  return 0;
}
Problem p03661 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << "\n"
#define _ << " _ " <<
#define ll long long

using namespace std;

ll a[100010];

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

  ll x, y, sum = 0;
  int n;
  cin >> n;

  REP(i, n) {
    ll tr;
    cin >> tr;
    a[i] = tr;
    if (i)
      a[i] += a[i - 1];
    sum += tr;
  }
  ll out = 1e18;
  REP(i, n - 1) {
    x = a[i];
    y = (sum - a[i]);
    if (x > y)
      swap(x, y);
    if (x < 0 && y > 0)
      out = min(out, -x + y);
    else
      out = min(out, abs(y - x));
  }
  cout << out << endl;

  return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << "\n"
#define _ << " _ " <<
#define ll long long

using namespace std;

ll a[200010];

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

  ll x, y, sum = 0;
  int n;
  cin >> n;

  REP(i, n) {
    ll tr;
    cin >> tr;
    a[i] = tr;
    if (i)
      a[i] += a[i - 1];
    sum += tr;
  }
  ll out = 1e18;
  REP(i, n - 1) {
    x = a[i];
    y = (sum - a[i]);
    if (x > y)
      swap(x, y);
    if (x < 0 && y > 0)
      out = min(out, -x + y);
    else
      out = min(out, abs(y - x));
  }
  cout << out << endl;

  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;
typedef long long LL;
const int MAXN = 100000 + 10;
LL sum[MAXN];
int main() {
  //    freopen("test.txt", "r", stdin);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &sum[i]);
    sum[i] += sum[i - 1];
  }
  LL ret = LLONG_MAX;
  LL tmp;
  for (int i = 1; i < n; i++) {
    tmp = sum[n] - 2 * sum[i];
    ret = min(ret, abs(tmp));
  }
  cout << ret << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;
typedef long long LL;
const int MAXN = 200000 + 10;
LL sum[MAXN];
int main() {
  //    freopen("test.txt", "r", stdin);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &sum[i]);
    sum[i] += sum[i - 1];
  }
  LL ret = LLONG_MAX;
  LL tmp;
  for (int i = 1; i < n; i++) {
    tmp = sum[n] - 2 * sum[i];
    ret = min(ret, abs(tmp));
  }
  cout << ret << endl;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// typedef __int64 LL;
typedef long long int LL;

typedef vector<int> ivec;
typedef vector<string> svec;

LL a[100010];
int main() {
  int n = 0;
  LL ans = 10000000000000000;
  LL sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    LL x = 0;
    cin >> x;
    sum += x;
    a[i] = sum;
  }

  for (int i = 0; i < n - 1; i++) {
    LL y = sum - 2 * a[i];
    if (y < 0)
      y *= -1;
    // ans=min(ans,x);
    if (ans > y)
      ans = y;
  }

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// typedef __int64 LL;
typedef long long int LL;

typedef vector<int> ivec;
typedef vector<string> svec;

LL a[200010];
int main() {
  int n = 0;
  LL ans = 10000000000000000;
  LL sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    LL x = 0;
    cin >> x;
    sum += x;
    a[i] = sum;
  }

  for (int i = 0; i < n - 1; i++) {
    LL y = sum - 2 * a[i];
    if (y < 0)
      y *= -1;
    // ans=min(ans,x);
    if (ans > y)
      ans = y;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define vll vector<ll>
#define mll map<ll, ll>
#define sll set<ll>
#define fo(i, n) for (i = 0; i < n; i++)
#define MOD 1000000007
using namespace std;

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

#ifndef ONLINE_JUDGE
  freopen("000.txt", "r", stdin);
#endif

  ll n, i, j, k, t, a[200001];

  cin >> n;
  ll sum1 = 0, sum2 = 0;
  fo(i, n) {
    cin >> a[i];
    sum1 += a[i];
  }
  ll ma = 1000000000000;
  fo(i, n - 1) {
    sum2 += a[i];
    ma = min(ma, abs(sum1 - sum2 - sum2));
  }
  cout << ma;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define vll vector<ll>
#define mll map<ll, ll>
#define sll set<ll>
#define fo(i, n) for (i = 0; i < n; i++)
#define MOD 1000000007
using namespace std;

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

  ll n, i, j, k, t, a[200001];

  cin >> n;
  ll sum1 = 0, sum2 = 0;
  fo(i, n) {
    cin >> a[i];
    sum1 += a[i];
  }
  ll ma = 1000000000000;
  fo(i, n - 1) {
    sum2 += a[i];
    ma = min(ma, abs(sum1 - sum2 - sum2));
  }
  cout << ma;
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Time Limit Exceeded):
// 2017-7-15
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long LL;
const int MaxN = 2e5;
int n;
int a[MaxN + 5];
LL presum[MaxN + 5];
LL sum;

int main() {
  while (scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      presum[i] = presum[i - 1] + a[i];
      sum += a[i];
    }
    LL ans = 1LL << 60;
    for (int i = 1; i < n; i++) {
      LL a = presum[i];
      LL b = sum - presum[i];
      ans = min(ans, abs(a - b));
    }
    printf("%lld\n", ans);
  }
  return 0;
}

Fixed submission:
// 2017-7-15
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long LL;
const int MaxN = 2e5;
int n;
int a[MaxN + 5];
LL presum[MaxN + 5];
LL sum;

int main() {
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      presum[i] = presum[i - 1] + a[i];
      sum += a[i];
    }
    LL ans = 1LL << 60;
    for (int i = 1; i < n; i++) {
      LL a = presum[i];
      LL b = sum - presum[i];
      ans = min(ans, abs(a - b));
    }
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p03661 (C++)

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

using namespace std;

const int maxn = 1e5 + 5;

long long sum[maxn];

int main(int argc, char const *argv[]) {

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    long long x;
    scanf("%lld", &x);
    sum[i] = sum[i - 1] + x;
  }

  long long ans = 2e9;
  for (int i = 1; i <= n - 1; i++) {
    long long tmp = abs(sum[i] - (sum[n] - sum[i]));
    ans = min(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;

const int maxn = 2e5 + 5;

long long sum[maxn];

int main(int argc, char const *argv[]) {

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    long long x;
    scanf("%lld", &x);
    sum[i] = sum[i - 1] + x;
  }

  long long ans = 2e9;
  for (int i = 1; i <= n - 1; i++) {
    long long tmp = abs(sum[i] - (sum[n] - sum[i]));
    ans = min(ans, tmp);
  }

  cout << ans << endl;

  return 0;
}
Problem p03661 (C++)

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

const int N = 100005;

int i, n;
long long a[N], ans = 1e18 + 69;

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

  cin >> n;
  for (i = 1; i <= n; ++i)
    cin >> a[i], a[i] += a[i - 1];

  for (i = 1; i < n; ++i)
    ans = min(ans, abs(a[i] - (a[n] - a[i])));

  cout << ans << '\n';

  return 0;
}

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

const int N = 200005;

int i, n;
long long a[N], ans = 1e18 + 69;

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

  cin >> n;
  for (i = 1; i <= n; ++i)
    cin >> a[i], a[i] += a[i - 1];

  for (i = 1; i < n; ++i)
    ans = min(ans, abs(a[i] - (a[n] - a[i])));

  cout << ans << '\n';

  return 0;
}
Problem p03661 (C++)

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

using namespace std;

int rd() {
  int x = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0')
    f = c == '-' ? -1 : 1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - 48, c = getchar();
  return x * f;
}

long long n, a[100010], ans;

int main() {
  n = rd();
  for (int i = 1; i <= n; i++)
    a[i] = a[i - 1] + rd();
  ans = 1e18;
  for (int i = 1; i < n; i++)
    ans = min(ans, abs(a[n] - a[i] - a[i]));
  printf("%lld\n", ans);
  return 0;
}

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

using namespace std;

int rd() {
  int x = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0')
    f = c == '-' ? -1 : 1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - 48, c = getchar();
  return x * f;
}

long long n, a[200010], ans;

int main() {
  n = rd();
  for (int i = 1; i <= n; i++)
    a[i] = a[i - 1] + rd();
  ans = 1e18;
  for (int i = 1; i < n; i++)
    ans = min(ans, abs(a[n] - a[i] - a[i]));
  printf("%lld\n", ans);
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9 + 7;
ll S1, S2, a[100005];
ll ABS(ll v) { return v < 0 ? -v : v; }
int main() {
  int n;
  scanf("%d", &n);
  rep(k, n) scanf("%lld", &a[k]);
  rep(k, n) S2 += a[k];
  ll ans = INF;
  rep(k, n) {
    S1 += a[k];
    S2 -= a[k];
    if (k != n - 1)
      ans = min(ans, ABS(S1 - S2));
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define MP make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1LL << 60;
ll S1, S2, a[200005];
ll ABS(ll v) { return v < 0 ? -v : v; }
int main() {
  int n;
  scanf("%d", &n);
  rep(k, n) scanf("%lld", &a[k]);
  rep(k, n) S2 += a[k];
  ll ans = INF;
  rep(k, n) {
    S1 += a[k];
    S2 -= a[k];
    if (k != n - 1)
      ans = min(ans, ABS(S1 - S2));
  }
  printf("%lld", ans);
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 100005;

int n, a[maxn];

int main() {
#ifdef suiyuan2009
  freopen("in.cpp", "r", stdin);
#endif
  ll sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll ret = abs(sum - a[0] - a[0]);
  ll s = a[0];
  sum -= a[0];
  for (int i = 1; i + 1 < n; i++) {
    s += a[i];
    sum -= a[i];
    ret = min(ret, abs(s - sum));
  }
  cout << ret << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 200005;

int n, a[maxn];

int main() {
#ifdef suiyuan2009
  freopen("in.cpp", "r", stdin);
#endif
  ll sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ll ret = abs(sum - a[0] - a[0]);
  ll s = a[0];
  sum -= a[0];
  for (int i = 1; i + 1 < n; i++) {
    s += a[i];
    sum -= a[i];
    ret = min(ret, abs(s - sum));
  }
  cout << ret << endl;
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
/**
 *
 * Author: MARC
 * Lang: GNU C++14
 *
 **/

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

#define F first
#define S second
#define ii pair<int, int>
#define LS pair<ll, string>

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl pi = 3.1415926535897932385;
const ldbl E = 2.71828182845904523536;
const dbl eps = 1e-9;
const ll mod = 1e9 + 7;
const ll inf = 1e17;

/** /////////// ** Code ** /////////// **/

const int N = 100100;
ll a[N];

int main() {
  int n;
  scanf("%d", &n);
  ll mn = inf;

  ll sum1 = 0, sum2 = 0;

  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum1 += a[i];
  }

  for (int i = 1; i < n; i++) {
    sum1 -= a[i];
    sum2 += a[i];

    mn = min(mn, abs(sum1 - sum2));
  }

  printf("%lld", mn);

  return 0;
}


Fixed submission:
/**
 *
 * Author: MARC
 * Lang: GNU C++14
 *
 **/

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

#define F first
#define S second
#define ii pair<int, int>
#define LS pair<ll, string>

typedef unsigned long long ull;
typedef long double ldbl;
typedef long long ll;
typedef double dbl;

const ldbl pi = 3.1415926535897932385;
const ldbl E = 2.71828182845904523536;
const dbl eps = 1e-9;
const ll mod = 1e9 + 7;
const ll inf = 1e17;

/** /////////// ** Code ** /////////// **/

const int N = 200200;
ll a[N];

int main() {
  int n;
  scanf("%d", &n);
  ll mn = inf;

  ll sum1 = 0, sum2 = 0;

  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum1 += a[i];
  }

  for (int i = 1; i < n; i++) {
    sum1 -= a[i];
    sum2 += a[i];

    mn = min(mn, abs(sum1 - sum2));
  }

  printf("%lld", mn);

  return 0;
}
Problem p03661 (C++)

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

typedef long long LL;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb push_back
#define eb emplace_back
#define REP(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++i)
#define DREP(i, a, b) for (int i = (a - 1), i##end = (b); i >= i##end; --i)

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int oo = 0x3f3f3f3f;
const int maxn = 100000 + 10;

template <typename T> T read() {
  T n(0), f(1);
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    n = n * 10 + ch - 48;
  return n * f;
}

int n;
int a[maxn];
LL s[maxn];

int main() {
#ifdef Wearry
  freopen("data.txt", "r", stdin);
  freopen("ans.txt", "w", stdout);
#endif

  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], s[i] = s[i - 1] + a[i];

  LL ans = LLONG_MAX;

  for (int i = 1; i < n; ++i)
    ans = min(ans, abs(s[i] - (s[n] - s[i])));

  printf("%lld\n", ans);
  return 0;
}


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

typedef long long LL;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb push_back
#define eb emplace_back
#define REP(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++i)
#define DREP(i, a, b) for (int i = (a - 1), i##end = (b); i >= i##end; --i)

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int oo = 0x3f3f3f3f;
const int maxn = 200000 + 10;

template <typename T> T read() {
  T n(0), f(1);
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    n = n * 10 + ch - 48;
  return n * f;
}

int n;
int a[maxn];
LL s[maxn];

int main() {
#ifdef Wearry
  freopen("data.txt", "r", stdin);
  freopen("ans.txt", "w", stdout);
#endif

  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], s[i] = s[i - 1] + a[i];

  LL ans = LLONG_MAX;

  for (int i = 1; i < n; ++i)
    ans = min(ans, abs(s[i] - (s[n] - s[i])));

  printf("%lld\n", ans);
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 100010;
int a[N];
ll s[N];
ll ab(ll x) { return max(x, -x); }
int main() {
  int n, i, j;
  ll ans = 1ll << 55, all;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]), s[i] = s[i - 1] + a[i];
  all = s[n];
  for (i = 1; i < n; i++)
    ans = min(ans, ab(s[n] - 2 * s[i]));
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 200010;
int a[N];
ll s[N];
ll ab(ll x) { return max(x, -x); }
int main() {
  int n, i, j;
  ll ans = 1ll << 55, all;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]), s[i] = s[i - 1] + a[i];
  all = s[n];
  for (i = 1; i < n; i++)
    ans = min(ans, ab(s[n] - 2 * s[i]));
  printf("%lld\n", ans);
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iostream>
#include <locale>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
long long a[100001];
int main() {
  long n;
  cin >> n;
  long long z = 20000000000;
  long long s = 0;
  for (long i = 0; i < n; i++) {
    // long long x;
    scanf("%lld", &a[i]);
    // q.push(x);
    s += a[i];
  }
  long long x = 0;
  for (long i = 0; i < n - 1; i++) {
    x += a[i];
    z = min(z, abs(s - x * 2));
  }
  cout << z;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <ctime>
#include <fstream>
#include <iostream>
#include <locale>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
long long a[1000010];
int main() {
  long n;
  cin >> n;
  long long z = 20000000000;
  long long s = 0;
  for (long i = 0; i < n; i++) {
    // long long x;
    scanf("%lld", &a[i]);
    // q.push(x);
    s += a[i];
  }
  long long x = 0;
  for (long i = 0; i < n - 1; i++) {
    x += a[i];
    z = min(z, abs(s - x * 2));
  }
  cout << z;
  return 0;
}
Problem p03661 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <iostream>
using namespace std;
int n;
long long sum[100010], a;
int main() {
  cin >> n >> sum[0];
  for (int i = 1; i < n; i++) {
    cin >> a;
    sum[i] = sum[i - 1] + a;
  }
  long long ans = 1145141145141919;
  for (int i = 0; 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 <complex>
#include <iostream>
using namespace std;
int n;
long long sum[200010], a;
int main() {
  cin >> n >> sum[0];
  for (int i = 1; i < n; i++) {
    cin >> a;
    sum[i] = sum[i - 1] + a;
  }
  long long ans = 1145141145141919;
  for (int i = 0; i < n - 1; i++) {
    ans = min(ans, abs(sum[i] - (sum[n - 1] - sum[i])));
  }
  cout << ans << endl;
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

int n;
vector<int> g[100005];

void dfs(int cur, vector<bool> &used, vector<int> &depth) {
  used[cur] = true;
  for (auto edge : g[cur]) {
    if (not used[edge]) {
      depth[edge] = depth[cur] + 1;
      dfs(edge, used, depth);
    }
  }
}

int search(int k, int cur, vector<int> &depth) {
  if (k == 0)
    return cur;
  for (auto edge : g[cur]) {
    if (depth[cur] < depth[edge]) {
      return search(k - 1, edge, depth);
    }
  }
  return -1e8;
}

int countNode(int cur, vector<bool> &used, vector<int> &depth) {
  int res = 1;
  used[cur] = true;
  for (auto edge : g[cur]) {
    if (depth[cur] < depth[edge] && not used[edge]) {
      res += countNode(edge, used, depth);
    }
  }
  return res;
}

int main() {
  cin >> n;

  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  vector<bool> used(n, 0);
  vector<int> depth(n);
  depth[0] = 0;

  dfs(0, used, depth);
  used = vector<bool>(n, 0);

  // rep(i,n) show(depth[i])
  int start = search((depth[n - 1] - 1) / 2, n - 1, depth);
  // show(start)

  int node = countNode(start, used, depth);
  // show(node)
  cout << (n - node > node ? "Fennec" : "Snuke") << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
// const int INF = 1e8;
using namespace std;

int n;
vector<int> g[100005];

void dfs(int cur, vector<bool> &used, vector<int> &depth) {
  used[cur] = true;
  for (auto edge : g[cur]) {
    if (not used[edge]) {
      depth[edge] = depth[cur] + 1;
      dfs(edge, used, depth);
    }
  }
}

int search(int k, int cur, vector<int> &depth) {
  if (k == 0)
    return cur;
  for (auto edge : g[cur]) {
    if (depth[cur] > depth[edge]) {
      return search(k - 1, edge, depth);
    }
  }
  return -1e8;
}

int countNode(int cur, vector<bool> &used, vector<int> &depth) {
  int res = 1;
  used[cur] = true;
  for (auto edge : g[cur]) {
    if (depth[cur] < depth[edge] && not used[edge]) {
      res += countNode(edge, used, depth);
    }
  }
  return res;
}

int main() {
  cin >> n;

  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }

  vector<bool> used(n, 0);
  vector<int> depth(n);
  depth[0] = 0;

  dfs(0, used, depth);
  used = vector<bool>(n, 0);

  // rep(i,n) show(depth[i])
  int start = search((depth[n - 1] - 1) / 2, n - 1, depth);
  // show(start)

  int node = countNode(start, used, depth);
  // show(node)
  cout << (n - node > node ? "Fennec" : "Snuke") << endl;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>
#define pb push_back
#define mk make_pair
using namespace std;
const int INF = 1e9;

typedef long long ll;

#define MAX_V 10000

struct edge {
  int to;
  int cost;
};

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int E, r;
  cin >> V;
  E = V - 1;
  REP(i, E) {
    int f, t, c;
    cin >> f >> t;
    f--;
    t--;
    c = 1;
    edge e = {t, c};
    G[f].pb(e);
    e.to = f;
    G[t].pb(e);
  }
  int dis[2][V];
  dijkstra(0);
  REP(i, V) { dis[0][i] = d[i]; }
  dijkstra(V - 1);
  REP(i, V) { dis[1][i] = d[i]; }
  int black = 0, white = 0;
  REP(i, V) {
    // cout << dis[0][i] << "      " << dis[1][i] << endl;
    if (dis[0][i] <= dis[1][i])
      black++;
    else
      white++;
  }
  if (black > white)
    cout << "Fennec" << endl;
  else
    cout << "Snuke" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>
#define pb push_back
#define mk make_pair
using namespace std;
const int INF = 1e9;

typedef long long ll;

#define MAX_V 100010

struct edge {
  int to;
  int cost;
};

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int E, r;
  cin >> V;
  E = V - 1;
  REP(i, E) {
    int f, t, c;
    cin >> f >> t;
    f--;
    t--;
    c = 1;
    edge e = {t, c};
    G[f].pb(e);
    e.to = f;
    G[t].pb(e);
  }
  int dis[2][V];
  dijkstra(0);
  REP(i, V) { dis[0][i] = d[i]; }
  dijkstra(V - 1);
  REP(i, V) { dis[1][i] = d[i]; }
  int black = 0, white = 0;
  REP(i, V) {
    // cout << dis[0][i] << "      " << dis[1][i] << endl;
    if (dis[0][i] <= dis[1][i])
      black++;
    else
      white++;
  }
  if (black > white)
    cout << "Fennec" << endl;
  else
    cout << "Snuke" << endl;
  return 0;
}