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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1000000007;
template <class T> void print(const T &t) { cout << t << endl; }
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;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vi l(n);
  rep(i, n) { cin >> l[i]; }
  sort(all(l));
  ll total = 0;
  for (ll i = n - 1; i > n - k; i++) {
    total += l[i];
  }
  print(total);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1000000007;
template <class T> void print(const T &t) { cout << t << endl; }
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;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vi l(n);
  rep(i, n) { cin >> l[i]; }
  sort(all(l));
  ll total = 0;
  for (ll i = n - 1; i >= n - k; i--) {
    total += l[i];
  }
  print(total);
}
Problem p03658 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int N, K;
  int sum = 0;
  cin >> N >> K;
  int i;
  int l[K];
  for (i = 0; i < N; i++) {
    cin >> l[i];
  }
  sort(l, l + N, greater<int>());
  for (i = 0; i < K; i++) {
    sum += l[i];
  }
  cout << sum;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int N, K;
  int sum = 0;
  cin >> N >> K;
  int i;
  int l[N];
  for (i = 0; i < N; i++) {
    cin >> l[i];
  }
  sort(l, l + N, greater<int>());
  for (i = 0; i < K; i++) {
    sum += l[i];
  }
  cout << sum;
}
Problem p03658 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

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

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

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

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

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


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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(N);
  for (int i = 0; i < N; i++) {
    cin >> L.at(i);
  }
  sort(L.begin(), L.end());
  int Ans = 0;
  for (int i = 0; i < K; i++) {
    Ans += L.at(N - i - 1);
  }
  cout << Ans << endl;
}
Problem p03658 (C++)

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

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<int> l(N);
  for (int i = 0; i < N; i++) {
    cin >> l[i];
  }
  sort(l.begin(), l.end());
  int ans = 0;
  for (int i = N - 1; i >= N - K + 1; i++) {
    ans += l[i];
  }
  cout << ans << endl;
  return 0;
}


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

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<int> l(N);
  for (int i = 0; i < N; i++) {
    cin >> l[i];
  }
  sort(l.begin(), l.end());
  int ans = 0;
  for (int i = N - 1; i >= N - K; i--) {
    ans += l[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03658 (C++)

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

using namespace std;

int main() {
  int n, k, *l;
  cin >> n >> k;
  int i;
  for (i = 0; i < n; i++)
    cin >> l[i];

  sort(l, l + n, greater<int>());

  int ans = 0;

  for (i = 0; i < k; i++) {
    ans += l[i];
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;

int main() {
  int n, k, *l;
  cin >> n >> k;
  int i;
  l = new int[n];
  for (i = 0; i < n; i++)
    cin >> l[i];

  sort(l, l + n, greater<int>());

  int ans = 0;

  for (i = 0; i < k; i++) {
    ans += l[i];
  }

  cout << ans << endl;

  return 0;
}
Problem p03658 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  sort(l.begin(), l.end(), greater<int>());
  int ans = 0;
  for (int i = 0; i < k; i++) {
    ans += l[i];
  }
  cout << ans << endl;
  return 9;
}

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

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

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(N);

  for (int i = 0; i < N; i++) {
    cin >> L.at(N);
  }
  sort(L.rbegin(), L.rend());

  int answer = 0;
  for (int i = 0; i < K; i++) {
    answer += L.at(i);
  }

  cout << answer << endl;
}

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(N);

  for (int i = 0; i < N; i++) {
    cin >> L.at(i);
  }
  sort(L.rbegin(), L.rend());

  int answer = 0;
  for (int i = 0; i < K; i++) {
    answer += L.at(i);
  }

  cout << answer << endl;
}
Problem p03658 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  cin >> a >> b;
  vector<int> A(a);
  for (int i = 0; i < a; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());
  int sum = 0;
  for (int i = 0; i < b; i++) {
    sum += A.at(b - i);
  }
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  cin >> a >> b;
  vector<int> A(a);
  for (int i = 0; i < a; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());
  int sum = 0;
  for (int i = 0; i < b; i++) {
    sum += A.at(a - i - 1);
  }
  cout << sum << endl;
}
Problem p03658 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  int l[k];
  rep(i, n) cin >> l[i];

  sort(l, l + n, greater<int>());
  ll sum = 0;
  rep(i, k) sum += l[i];

  cout << sum << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  int l[n];
  rep(i, n) cin >> l[i];

  sort(l, l + n, greater<int>());
  ll sum = 0;
  rep(i, k) sum += l[i];

  cout << sum << endl;

  return 0;
}
Problem p03658 (C++)

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

using namespace std;

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

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

  for (int i = n - 1; i >= n - k; i--) {
    ans += a[i];
  }

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

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

using namespace std;

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

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

  for (int i = n - 1; i >= n - k; i--) {
    ans += a[i];
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int N, K, a[50], ans = 0;
  cin >> N >> K;
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  sort(a, a + N);
  for (int i = N - 1; i = N - K + 1; --i)
    ans += a[i];
  cout << ans << endl;
  return 0;
}

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

int main() {
  int N, K, a[50], ans = 0;
  cin >> N >> K;
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  sort(a, a + N);
  for (int i = N - 1; i >= N - K; --i)
    ans += a[i];
  cout << ans << endl;
  return 0;
}
Problem p03658 (C++)

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

using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()
#define all2(a, b) (a).begin(), (a).begin() + (b)
#define debug(vari) cerr << #vari << " = " << (vari) << endl;

int wa(int n) {
  int s = 0;
  while (n) {
    s += n % 10;
    n /= 10;
  }

  return s;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(n);
  rep(i, n) cin >> l[n];

  sort(all(l));
  reverse(all(l));

  int ans = 0;
  rep(i, k) { ans += l[i]; }
  cout << ans << endl;

  return 0;
}


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

using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()
#define all2(a, b) (a).begin(), (a).begin() + (b)
#define debug(vari) cerr << #vari << " = " << (vari) << endl;

int wa(int n) {
  int s = 0;
  while (n) {
    s += n % 10;
    n /= 10;
  }

  return s;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(n);
  rep(i, n) cin >> l[i];

  sort(all(l));
  reverse(all(l));

  int ans = 0;
  rep(i, k) { ans += l[i]; }
  cout << ans << endl;

  return 0;
}
Problem p03658 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> l(N);
  for (int i = 0; i < N; i++)
    cin >> l.at(i);
  sort(l.begin(), l.end(), [&](int x, int y) { return x > y; });
  int ans = 0;
  for (int i = 0; i < K; i++) {
    ans += l.at(K);
  }
  cout << ans << endl;
}


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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> l(N);
  for (int i = 0; i < N; i++)
    cin >> l.at(i);
  sort(l.begin(), l.end(), [&](int x, int y) { return x > y; });
  int ans = 0;
  for (int i = 0; i < K; i++) {
    ans += l.at(i);
  }
  cout << ans << endl;
}
Problem p03658 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K, S = 0;
  cin >> N >> K;
  vector<int> l(N);
  for (int i = 0; i < N; i++)
    cin >> l.at(N);
  sort(l.begin(), l.end());
  reverse(l.begin(), l.end());
  for (int i = 0; i < K; i++)
    S += l.at(i);
  cout << S << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K, S = 0;
  cin >> N >> K;
  vector<int> l(N);
  for (int i = 0; i < N; i++)
    cin >> l.at(i);
  sort(l.begin(), l.end());
  reverse(l.begin(), l.end());
  for (int i = 0; i < K; i++)
    S += l.at(i);
  cout << S << endl;
}
Problem p03658 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
template <typename T> bool PN(T x) {
  if (x <= 1)
    return false;
  if (x == 2)
    return true;
  for (int i = 2; i < sqrt(x) + 1; i++)
    if (x % i == 0)
      return false;
  return true;
}
const ll MOD = 1e9 + 7;
long long Comb(int n, int i) {
  long long ans = 1;
  if (i > n || i < 0)
    return 0;
  if (i == 0 || i == n)
    return 1;
  else {
    for (int j = 1; j <= i; ++j) {
      ans *= (n + 1 - j);
      ans /= j;
      ans %= MOD;
    }
  }
  return ans;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> snake;
  for (int i = 0; i < n; ++i) {
    int tmp;
    cin >> tmp;
    snake.push_back(tmp);
  }
  sort(snake.rbegin(), snake.rend());
  int ans = 0;
  for (int i = 0; i < k; ++k) {
    ans += snake[i];
  }
  cout << ans << endl;
}

int main(void) {
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
template <typename T> bool PN(T x) {
  if (x <= 1)
    return false;
  if (x == 2)
    return true;
  for (int i = 2; i < sqrt(x) + 1; i++)
    if (x % i == 0)
      return false;
  return true;
}
const ll MOD = 1e9 + 7;
long long Comb(int n, int i) {
  long long ans = 1;
  if (i > n || i < 0)
    return 0;
  if (i == 0 || i == n)
    return 1;
  else {
    for (int j = 1; j <= i; ++j) {
      ans *= (n + 1 - j);
      ans /= j;
      ans %= MOD;
    }
  }
  return ans;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> snake;
  for (int i = 0; i < n; ++i) {
    int tmp;
    cin >> tmp;
    snake.push_back(tmp);
  }
  sort(snake.rbegin(), snake.rend());
  int ans = 0;
  for (int i = 0; i < k; ++i) {
    ans += snake[i];
  }
  cout << ans << endl;
}

int main(void) {
  solve();
  return 0;
}
Problem p03658 (Python)

Buggy submission (status: Runtime Error):
K, N = map(int, input().split())
L = list(map(int, input().split())).sort(reverse=True)
print(sum(L[:K]))


Fixed submission:
N, K = map(int, input().split())
L = list(map(int, input().split()))
L.sort(reverse=True)
print(sum(L[:K]))
Problem p03658 (Python)

Buggy submission (status: Time Limit Exceeded):
from itertools import combinations

N, K = map(int, input().split())
L = list(map(int, input().split()))

ans = max([sum(combi) for combi in combinations(L, K)])
print(ans)


Fixed submission:
from itertools import combinations

N, K = map(int, input().split())
L = sorted(list(map(int, input().split())), reverse=True)
ans = sum(L[:K])
print(ans)
Problem p03658 (C++)

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

using namespace std;

int main(void) {

  /* Variable_Proclamation */
  int N;   // input  (棒の数)
  int K;   // input  (選択する数)
  int sum; // output

  int i; // subscript

  /* Input, Array_Proclamation */
  cin >> N >> K;
  vector<int> l(N);

  for (i = 0; i < N; i++) {
    cin >> l.at(i);
  }

  cout << "aaa\n";
  cout << l.at(800000) << endl;
  cout << "bbb\n";

  /* Initialize */
  sum = 0;

  /* Calculation */
  sort(l.begin(), l.end());
  reverse(l.begin(), l.end());

  for (i = 0; i < K; i++) {
    sum += l.at(i);
  }

  /* Output */
  cout << sum << endl;

  return 0;
}


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

using namespace std;

int main(void) {

  /* Variable_Proclamation */
  int N;   // input  (棒の数)
  int K;   // input  (選択する数)
  int sum; // output

  int i; // subscript

  /* Input, Array_Proclamation */
  cin >> N >> K;
  vector<int> l(N);

  for (i = 0; i < N; i++) {
    cin >> l.at(i);
  }

  /* Initialize */
  sum = 0;

  /* Calculation */
  sort(l.begin(), l.end());
  reverse(l.begin(), l.end());

  for (i = 0; i < K; i++) {
    sum += l.at(i);
  }

  /* Output */
  cout << sum << endl;

  return 0;
}
Problem p03658 (C++)

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

int main(int argc, char const *argv[]) {
  int sum = 0, n, k;
  cin >> n >> k;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    cin >> l.at(i);
  }
  sort(l.begin(), l.end());
  for (int i = k - 1; i >= 0; i++) {
    sum += l.at(i);
  }
  cout << sum << endl;
  return 0;
}


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

int main(int argc, char const *argv[]) {
  int sum = 0, n, k;
  cin >> n >> k;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    cin >> l.at(i);
  }
  sort(l.begin(), l.end(), greater<int>());
  for (int i = 0; i < k; i++) {
    sum += l.at(i);
  }
  cout << sum << endl;
  return 0;
}
Problem p03659 (C++)

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

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

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
// #include<cmath>
using namespace std;

long long a[100101], ans = 1e17, v1, v2, n, sum;

long long abss(long long x, long long y) {
  if (v1 > v2)
    return v1 - v2;
  return v2 - v1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  v1 = a[1];
  v2 = sum - a[1];
  ans = min(ans, abss(v2, v1));
  for (int i = 2; i < n; i++) {
    v1 += a[i];
    v2 -= a[i];
    ans = min(ans, abss(v2, v1));
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
// #include<cmath>
using namespace std;

long long a[200101], ans = 1e17, v1, v2, n, sum;

long long abss(long long x, long long y) {
  if (v1 > v2)
    return v1 - v2;
  return v2 - v1;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  v1 = a[1];
  v2 = sum - a[1];
  ans = min(ans, abss(v2, v1));
  for (int i = 2; i < n; i++) {
    v1 += a[i];
    v2 -= a[i];
    ans = min(ans, abss(v2, v1));
  }
  cout << ans << endl;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
typedef long long ll;
ll a[N], sum[N];
int main() {
  int n;
  scanf("%d", &n);
  sum[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sum[i] = sum[i - 1] + a[i];
  }
  ll ans = 1e18 + 7;
  for (int i = 1; i <= n - 1; ++i) {
    ans = min(ans, abs(sum[i] - sum[n] + sum[i]));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
typedef long long ll;
ll a[N], sum[N];
int main() {
  int n;
  scanf("%d", &n);
  sum[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    sum[i] = sum[i - 1] + a[i];
  }
  ll ans = 1e18 + 7;
  for (int i = 1; i <= n - 1; ++i) {
    ans = min(ans, abs(sum[i] - sum[n] + sum[i]));
  }
  cout << ans << endl;
  return 0;
}
Problem p03659 (C++)

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

using namespace std;

int main() {
  int N;
  vector<int> vec(N);
  vector<long long> ruiseki(N);
  vector<long long> araiguma(N - 1);
  vector<long long> balance(N - 1);

  for (int i = 0; i < N; i++) {
    cin >> vec[i];
    if (i == 0) {
      ruiseki[i] = vec[i];
    } else {
      ruiseki[i] = ruiseki[i - 1] + vec[i];
    }
  }

  long long max = ruiseki[N - 1];

  ruiseki.pop_back();

  for (int i = 0; i < ruiseki.size(); i++) {
    araiguma[i] = max - ruiseki[i];
  }

  for (int i = 0; i < N - 1; i++) {
    balance[i] = abs(araiguma[i] - ruiseki[i]);
  }

  cout << *min_element(balance.begin(), balance.end()) << endl;
}

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  vector<long long> ruiseki(N);
  vector<long long> araiguma(N - 1);
  vector<long long> balance(N - 1);

  for (int i = 0; i < N; i++) {
    cin >> vec[i];
    if (i == 0) {
      ruiseki[i] = vec[i];
    } else {
      ruiseki[i] = ruiseki[i - 1] + vec[i];
    }
  }

  long long max = ruiseki[N - 1];

  ruiseki.pop_back();

  for (int i = 0; i < ruiseki.size(); i++) {
    araiguma[i] = max - ruiseki[i];
  }

  for (int i = 0; i < N - 1; i++) {
    balance[i] = abs(araiguma[i] - ruiseki[i]);
  }

  cout << *min_element(balance.begin(), balance.end()) << endl;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define reset(a, b) memset(a, b, sizeof(a))
typedef long long LL;
int main() {
  LL n, ans, s;
  LL a[100001], sum[100001];
  LL i, j, k;
  cin >> n;
  sum[0] = 0;
  s = 0;
  ans = LL(1e18);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i];
    s += a[i];
  }
  for (i = 1; i < n; i++) {
    ans = min(ans, abs(s - 2 * sum[i]));
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define reset(a, b) memset(a, b, sizeof(a))
typedef long long LL;
int main() {
  LL n, ans, s;
  LL a[200001], sum[100001];
  LL i, j, k;
  cin >> n;
  sum[0] = 0;
  s = 0;
  ans = LL(1e18);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i];
    s += a[i];
  }
  for (i = 1; i < n; i++) {
    ans = min(ans, abs(s - 2 * sum[i]));
  }
  cout << ans << endl;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
long long a[maxn];
int main() {
  int n;
  long long sum1 = 0, sum2 = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]), sum2 += a[i];
  sum1 = a[0], sum2 -= a[0];
  long long Min = abs(sum1 - sum2);
  for (int i = 1; i < n - 1; i++) {
    Min = min(Min, abs(sum1 - sum2));
    sum1 += a[i];
    sum2 -= a[i];
  }
  cout << Min << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 1e5 + 5;
long long a[maxn];
int main() {
  int n;
  long long sum1 = 0, sum2 = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]), sum2 += a[i];
  sum1 = a[0], sum2 -= a[0];
  long long Min = abs(sum1 - sum2);
  for (int i = 1; i < n - 1; i++) {
    Min = min(Min, abs(sum1 - sum2));
    sum1 += a[i];
    sum2 -= a[i];
  }
  cout << Min << endl;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, A[100005];
int main() {
  scanf("%d", &N);
  ll ss = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%d", A + i);
    ss += 1LL * A[i];
  }
  ll s = 0;
  ll ans = 1e18;
  for (int i = 1; i < N; i++) {
    s += A[i];
    ll s2 = ss - s;
    ans = min(ans, abs(s2 - s));
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, A[200005];
int main() {
  scanf("%d", &N);
  ll ss = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%d", A + i);
    ss += 1LL * A[i];
  }
  ll s = 0;
  ll ans = 1e18;
  for (int i = 1; i < N; i++) {
    s += A[i];
    ll s2 = ss - s;
    ans = min(ans, abs(s2 - s));
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i < (int)(n); i++)

typedef long long ll;
using namespace std;
int main() {
  ll n, a[100010];
  cin >> n;
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }

  // cout<<sum<<endl;
  ll div = 0, min = 1e15;
  rep(i, n - 1) {
    sum -= a[i];
    div += a[i];
    if (abs(sum - div) < min)
      min = abs(sum - div);
  }
  cout << min << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i < (int)(n); i++)

typedef long long ll;
using namespace std;
int main() {
  ll n, a[200010];
  cin >> n;
  ll sum = 0;
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }

  // cout<<sum<<endl;
  ll div = 0, min = 1e15;
  rep(i, n - 1) {
    sum -= a[i];
    div += a[i];
    if (abs(sum - div) < min)
      min = abs(sum - div);
  }
  cout << min << endl;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long i = 0, n, m, x = 0, y = 0, a[114514];
  for (cin >> n; i < n; cin >> a[i], x += a[i++])
    ;
  for (i = 0; i < n - 1; i++) {
    y += a[i];
    x -= a[i];
    if (i == 0)
      m = abs(x - y);
    else
      m = min(m, abs(x - y));
  }
  cout << m << endl;
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  long i = 0, n, m, x = 0, y = 0, a[214514];
  for (cin >> n; i < n; cin >> a[i], x += a[i++])
    ;
  for (i = 0; i < n - 1; i++) {
    y += a[i];
    x -= a[i];
    if (i == 0)
      m = abs(x - y);
    else
      m = min(m, abs(x - y));
  }
  cout << m << endl;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) v.erase(remove(v.begin(), v.end(), a), v.end())
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

int a[100000];

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

  int x = a[0];
  int y = 0;
  for (int i = 1; i < N; i++) {
    y += a[i];
  }

  int min = abs(x - y);

  for (int i = 1; i < N - 1; i++) {
    x += a[i];
    y -= a[i];
    int n = abs(x - y);
    if (min > n) {
      min = n;
    }
  }

  cout << min << endl;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define int long long
#define CONTAINS(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v, a) v.erase(remove(v.begin(), v.end(), a), v.end())
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

int a[200000];

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

  int x = a[0];
  int y = 0;
  for (int i = 1; i < N; i++) {
    y += a[i];
  }

  int min = abs(x - y);

  for (int i = 1; i < N - 1; i++) {
    x += a[i];
    y -= a[i];
    int n = abs(x - y);
    if (min > n) {
      min = n;
    }
  }

  cout << min << endl;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
// 17.07.2017

#include <algorithm>
#include <iostream>

using namespace std;

#define lint long long int

// # define __TRR__

const int N = 100100;

lint a[N], n, k, m, mi;

int main() {
#ifdef __TRR__
  freopen("C.TXT", "r", stdin);
  freopen("C.OUT", "w", stdout);
#endif

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m += a[i];
  }

  k = a[0], m -= k;
  mi = abs(m - k);
  for (int i = 1; i < n - 1; i++) {
    k += a[i], m -= a[i];
    if (abs(m - k) < mi)
      mi = abs(m - k);
  }

  cout << mi;

  return 0;
}


Fixed submission:
// 17.07.2017

#include <algorithm>
#include <iostream>

using namespace std;

#define lint long long int

// # define __TRR__

const int N = 200100;

lint a[N], n, k, m, mi;

int main() {
#ifdef __TRR__
  freopen("C.TXT", "r", stdin);
  freopen("C.OUT", "w", stdout);
#endif

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m += a[i];
  }

  k = a[0], m -= k;
  mi = abs(m - k);
  for (int i = 1; i < n - 1; i++) {
    k += a[i], m -= a[i];
    if (abs(m - k) < mi)
      mi = abs(m - k);
  }

  cout << mi;

  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdlib>
#include <iostream>
using namespace std;

int main() {
  long N;
  long long sum = 0;
  cin >> N;
  long long a[200001] = {0};
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    sum += a[i];
  }
  long long ans = 10000000000;
  for (int k = 1; k < N; k++) {
    long long tmp = 0;
    for (int u = 0; u < k; u++) {
      tmp += a[u];
    }
    ans = min(abs(sum - 2 * tmp), ans);
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  long N;
  long long sum = 0;
  cin >> N;
  long long a[200001] = {0};
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    sum += a[i];
  }
  long long ans = 10000000000;
  long long tmp = 0;
  for (int k = 0; k < N - 1; k++) {
    tmp += a[k];
    ans = min(abs(sum - 2 * tmp), ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p03659 (C++)

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

typedef long long ll;
int main() {
  int n;
  cin >> n;
  ll a[110000];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  ll x = a[0], y = 0;
  for (int i = 1; i < n; i++) {
    y += a[i];
  }
  ll ans = abs(x - y);
  for (int i = 1; i < n - 1; i++) {
    x += a[i];
    y -= a[i];
    ans = min(ans, abs(x - y));
  }
  cout << ans << endl;
  return 0;
}

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

typedef long long ll;
int main() {
  int n;
  cin >> n;
  ll a[210000];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  ll x = a[0], y = 0;
  for (int i = 1; i < n; i++) {
    y += a[i];
  }
  ll ans = abs(x - y);
  for (int i = 1; i < n - 1; i++) {
    x += a[i];
    y -= a[i];
    ans = min(ans, abs(x - y));
  }
  cout << ans << endl;
  return 0;
}
Problem p03659 (C++)

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

int main() {
  int n;
  long long a[55];
  while (cin >> n) {
    long long sum = 0, lsum = 0, rsum, Min = 10000000000;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      sum += a[i];
    }

    for (int i = 0; i < n - 1; i++) {
      lsum += a[i];
      rsum = sum - lsum;
      if (Min > abs(lsum - rsum))
        Min = abs(lsum - rsum);
    }
    cout << Min << endl;
  }

  return 0;
}


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

int main() {
  int n;
  long long a[200005];
  while (cin >> n) {
    long long sum = 0, lsum = 0, rsum, Min = 10000000000;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      sum += a[i];
    }

    for (int i = 0; i < n - 1; i++) {
      lsum += a[i];
      rsum = sum - lsum;
      if (Min > abs(lsum - rsum))
        Min = abs(lsum - rsum);
    }
    cout << Min << endl;
  }

  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;
int n;
ll arr[100001];
ll prefSum[100001];
int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
    prefSum[i] = sum;
  }
  ll mnn = (1LL << 60LL);
  ll A = 0, B;
  for (int i = 0; i + 1 < n; i++) {
    A += arr[i];
    B = prefSum[n - 1] - A;
    mnn = min(mnn, abs(A - B));
  }
  cout << mnn << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
int n;
ll arr[200001];
ll prefSum[200001];
int main(int argc, const char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin >> n;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
    prefSum[i] = sum;
  }
  ll mnn = (1LL << 60LL);
  ll A = 0, B;
  for (int i = 0; i + 1 < n; i++) {
    A += arr[i];
    B = prefSum[n - 1] - A;
    mnn = min(mnn, abs(A - B));
  }
  cout << mnn << endl;
  return 0;
}
Problem p03659 (C++)

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

long long a[100010];

int main(void) {
  int n;
  long long ans = 0, x = 0, s = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    x += a[i];
    if (i == 0 || ans > abs(2 * x - s))
      ans = abs(2 * x - s);
  }
  cout << ans << endl;
  return 0;
}

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

long long a[200010];

int main(void) {
  int n;
  long long ans = 0, x = 0, s = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    x += a[i];
    if (i == 0 || ans > abs(2 * x - s))
      ans = abs(2 * x - s);
  }
  cout << ans << endl;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
const int INF = 1 << 30;
const ll LLINF = 2000000000;
#define rep(i, n) for (ll i = (ll)0; i < (ll)n; ++i)
#define ALL(x) x.begin(), x.end()
int main() {
  ll n, sums[20020] = {}, ans = LLINF;
  cin >> n;
  rep(i, n) {
    int nn;
    cin >> nn;
    if (i == 0)
      sums[i] = nn;
    else
      sums[i] = nn + sums[i - 1];
  }
  rep(i, n - 1) { ans = min(ans, abs((sums[n - 1] - sums[i]) - sums[i])); }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
const int INF = 1 << 30;
const ll LLINF = 2000000000;
#define rep(i, n) for (ll i = (ll)0; i < (ll)n; ++i)
#define ALL(x) x.begin(), x.end()
int main() {
  ll n, sums[200020] = {}, ans = LLINF;
  cin >> n;
  rep(i, n) {
    int nn;
    cin >> nn;
    if (i == 0)
      sums[i] = nn;
    else
      sums[i] = nn + sums[i - 1];
  }
  rep(i, n - 1) { ans = min(ans, abs((sums[n - 1] - sums[i]) - sums[i])); }
  cout << ans << endl;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
const ll INF = 100000000000000LL;

int main() {
  int n;
  ll a[100001];
  cin >> n;
  ll t = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    t += a[i];
  }
  ll t1 = 0;
  ll r = INF;
  for (int i = 0; i < n - 1; ++i) {
    t -= a[i];
    t1 += a[i];
    r = min(r, abs(t1 - t));
  }
  cout << r << endl;
  return 0;
}


Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
const ll INF = 100000000000000LL;

int main() {
  int n;
  ll a[200001];
  cin >> n;
  ll t = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    t += a[i];
  }
  ll t1 = 0;
  ll r = INF;
  for (int i = 0; i < n - 1; ++i) {
    t -= a[i];
    t1 += a[i];
    r = min(r, abs(t1 - t));
  }
  cout << r << endl;
  return 0;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>

using namespace std;
typedef long long ll;

ll a[100010];

int main() {
  ll n;
  scanf("%lld", &n);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  ll csum = 0;
  ll ans = 1000000000000;
  for (int i = 0; i < n - 1; i++) {
    csum += a[i];
    sum -= a[i];
    ll def = abs(csum - sum);
    ans = min(ans, def);
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>

using namespace std;
typedef long long ll;

ll a[200010];

int main() {
  ll n;
  scanf("%lld", &n);
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    sum += a[i];
  }
  ll csum = 0;
  ll ans = 1000000000000;
  for (int i = 0; i < n - 1; i++) {
    csum += a[i];
    sum -= a[i];
    ll def = abs(csum - sum);
    ans = min(ans, def);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03659 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))
ans = 10000000000
total = sum(a)
for i in range(1, N):
    sunuke = a[:i]
    Sum = sum(sunuke)
    if abs(Sum - (total - Sum)) <= ans:
        ans = abs(Sum - (total - Sum))
print(ans)


Fixed submission:
N = int(input())
a = list(map(int, input().split()))
ans = 10000000000
total = sum(a)
sunuke = 0
for i in range(N - 1):
    sunuke += a[i]
    dif = abs(2 * sunuke - total)
    if dif <= ans:
        ans = dif
print(ans)
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, b) for (ll i = 0; i < b; i++)

int main() {
  ll S, n, Min = 100000000000;
  vector<ll> x(100000);
  cin >> n;
  rep(i, n) {
    cin >> x[i];
    S += x[i];
  }
  rep(i, n - 1) Min = min(Min, abs(S -= 2 * x[i]));
  cout << Min << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, b) for (ll i = 0; i < b; i++)

int main() {
  ll S, n, Min = 100000000000;
  vector<ll> x(200000);
  cin >> n;
  rep(i, n) {
    cin >> x[i];
    S += x[i];
  }
  rep(i, n - 1) Min = min(Min, abs(S -= 2 * x[i]));
  cout << Min << endl;
}
Problem p03659 (Python)

Buggy submission (status: Time Limit Exceeded):
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    ans = 10**10
    for i in range(1, n):
        g1 = a[0:i]
        g2 = a[i:n]
        tmp = abs(sum(g1) - sum(g2))
        if tmp < ans:
            ans = tmp

    print(ans)


Fixed submission:
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    g1 = a[0]
    g2 = sum(a[1:n])
    ans = abs(g1 - g2)
    if n > 2:
        for i in range(1, n - 1):
            g1 += a[i]
            g2 -= a[i]
            tmp = abs(g1 - g2)
            if tmp < ans:
                ans = tmp

    print(ans)
Problem p03659 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
a_list = list(map(int, input().split()))

min_diff = abs(sum(a_list[:1]) - sum(a_list[1:]))
for i in range(2, N):
    diff = abs(sum(a_list[:i]) - sum(a_list[i:]))
    min_diff = min(min_diff, diff)
print(min_diff)


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

a_sum = sum(a_list)
l_a_sum = a_list[0]
min_diff = abs(a_sum - 2 * l_a_sum)
for i in range(1, N - 1):
    l_a_sum += a_list[i]
    min_diff = min(min_diff, abs(a_sum - 2 * l_a_sum))
print(min_diff)
Problem p03659 (C++)

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

const int N = 100005;

long long a[N], b[N], c[N];

int main() {

  long long n, minval = 9999999999999;

  cin >> n;

  for (int i = 0; i < n; ++i)
    cin >> a[i];

  for (int i = 1; i <= n; ++i)
    b[i] = b[i - 1] + a[i - 1];

  for (int i = n; i >= 0; --i)
    c[i] = c[i + 1] + a[i];

  for (int i = 1; i < n; ++i) {

    // cout<<c[i]<<" "<<b[i]<<"\n";

    minval = min(minval, abs(c[i] - b[i]));
  }

  cout << minval << '\n';
}

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

const int N = 300005;

long long a[N], b[N], c[N];

int main() {

  long long n, minval = 9999999999999;

  cin >> n;

  for (int i = 0; i < n; ++i)
    cin >> a[i];

  for (int i = 1; i <= n; ++i)
    b[i] = b[i - 1] + a[i - 1];

  for (int i = n; i >= 0; --i)
    c[i] = c[i + 1] + a[i];

  for (int i = 1; i < n; ++i) {

    // cout<<c[i]<<" "<<b[i]<<"\n";

    minval = min(minval, abs(c[i] - b[i]));
  }

  cout << minval << '\n';
}
Problem p03659 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define ll long long
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
const int MAX = 510000;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];
  vector<ll> ans(N - 1); // すぬけくんがi+1枚のカードをとった時のabs(x-y)の値
  ll sum = accumulate(a.begin(), a.end(), 0LL);
  for (int i = 0; i < N - 1; i++) {
    ll x = accumulate(a.begin(), a.begin() + i + 1, 0LL);
    ans[i] = abs(2 * x - sum);
  }
  sort(ans.begin(), ans.end());
  cout << ans[0] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define ll long long
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
const int MAX = 510000;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];
  vector<ll> ans(N - 1); // すぬけくんがi+1枚のカードをとった時のabs(x-y)の値
  ll sum = accumulate(a.begin(), a.end(), 0LL);
  vector<ll> memo(N); // 累積和
  memo[0] = a[0];
  for (int i = 1; i < N; i++)
    memo[i] = memo[i - 1] + a[i];
  for (int i = 0; i < N - 1; i++)
    ans[i] = abs(2 * memo[i] - sum);
  sort(ans.begin(), ans.end());
  cout << ans[0] << endl;
}
Problem p03659 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
typedef long long ll;

int c[maxn];
ll p[maxn];
int n;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", c + i);
    p[i] = p[i - 1] + c[i];
  }

  ll res = (ll)1e18;
  for (int i = 1; i < n; ++i) {
    ll l = p[i];
    ll r = p[n] - p[i];
    ll nw = abs(l - r);
    res = min(res, nw);
  }
  printf("%lld\n", res);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
typedef long long ll;

int c[maxn];
ll p[maxn];
int n;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", c + i);
    p[i] = p[i - 1] + c[i];
  }

  ll res = (ll)1e18;
  for (int i = 1; i < n; ++i) {
    ll l = p[i];
    ll r = p[n] - p[i];
    ll nw = abs(l - r);
    res = min(res, nw);
  }
  printf("%lld\n", res);
}
Problem p03659 (C++)

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

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define ces(d) cout << " " << endl;

int main() {

  int N;
  vector<long long> A(100001);
  vector<long long> B(100001);
  cin >> N;

  fx(N) {
    cin >> A.at(x);
    if (x > 0) {
      B.at(x) = B.at(x - 1) + A.at(x);
    } else {
      B.at(x) = A.at(x);
    }
  }
  long long min = 10000000000000;
  long long MAX = B.at(N - 1);
  fx(N - 1) {
    if (abs(MAX - B.at(x) - B.at(x)) < min) {
      min = abs(MAX - B.at(x) - B.at(x));
    }
  }

  ce(min);
}

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

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define ces(d) cout << " " << endl;

int main() {

  long long N;
  vector<long long> A(1000001);
  vector<long long> B(1000001);
  cin >> N;

  fx(N) {
    cin >> A.at(x);
    if (x > 0) {
      B.at(x) = B.at(x - 1) + A.at(x);
    } else {
      B.at(x) = A.at(x);
    }
  }
  long long min = 10000000000000;
  long long MAX = B.at(N - 1);
  fx(N - 1) {
    if (abs(MAX - B.at(x) - B.at(x)) < min) {
      min = abs(MAX - B.at(x) - B.at(x));
    }
  }

  ce(min);
}
Problem p03659 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);

  long long int X = 0, Y = 0;
  for (int i = 0; i < N; i++)
    X += A.at(i);

  long long int ans = 1000000000000000;
  for (int i = N - 1; i > 0; i++) {
    X -= A.at(i);
    Y += A.at(i);
    ans = min(ans, abs(X - Y));
  }

  cout << ans << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);

  long long int X = 0, Y = 0;
  for (int i = 0; i < N; i++)
    X += A.at(i);

  long long int ans = 1000000000000000;
  for (int i = N - 1; i > 0; i--) {
    X -= A.at(i);
    Y += A.at(i);
    ans = min(ans, abs(X - Y));
  }

  cout << ans << endl;
}
Problem p03659 (C++)

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

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000000;
const int maxN = 100010;
int a[maxN];
ll abs_(ll x, ll y) {
  ll z = x - y;
  if (z < 0)
    z = -z;
  return z;
}
int main() {
  int n;
  scanf("%d", &n);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ans += a[i];
  }
  ll left = 0, right = ans;
  ll minn = (ll)1 << 50;
  ll cnt;
  for (int i = 1; i < n; i++) {
    left += a[i];
    right -= a[i];
    cnt = abs_(left, right);
    if (cnt < minn)
      minn = cnt;
  }
  printf("%lld\n", minn);
  return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000000;
const int maxN = 100010;
int a[maxN * 2];
ll abs_(ll x, ll y) {
  ll z = x - y;
  if (z < 0)
    z = -z;
  return z;
}
int main() {
  int n;
  scanf("%d", &n);
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ans += a[i];
  }
  ll left = 0, right = ans;
  ll minn = (ll)1 << 50;
  ll cnt;
  for (int i = 1; i < n; i++) {
    left += a[i];
    right -= a[i];
    cnt = abs_(left, right);
    if (cnt < minn)
      minn = cnt;
  }
  printf("%lld\n", minn);
  return 0;
}