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

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

vector<int> tree;

int gcd(int a, int b) {
  if (a == 0 || b == 0) {
    return max(a, b);
  }
  int c;
  c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

int solve(int a, int b, int i, int l, int r) {
  if (r <= a || b <= l) {
    return 0;
  }
  if (a <= l && r <= b) {
    return tree.at(i);
  }
  int x = solve(a, b, 2 * i + 1, l, (l + r) / 2);
  int y = solve(a, b, 2 * i + 2, (l + r) / 2, r);
  return gcd(x, y);
}

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

  int n = 1;
  while (n < N) {
    n >>= 1;
  }
  tree.resize(2 * n - 1, 0);
  for (int i = 0; i < N; i++) {
    tree.at(i + n - 1) = A.at(i);
  }
  for (int i = n - 2; i >= 0; i--) {
    tree.at(i) = gcd(tree.at(2 * i + 1), tree.at(2 * i + 2));
  }

  int ans = 1;
  for (int i = 0; i < N; i++) {
    int x1 = solve(0, i, 0, 0, n);
    int x2 = solve(i + 1, n, 0, 0, n);
    ans = max(ans, gcd(x1, x2));
  }
  cout << ans << endl;
}


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

vector<int> tree;

int gcd(int a, int b) {
  if (a == 0 || b == 0) {
    return max(a, b);
  }
  int c;
  c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

int solve(int a, int b, int i, int l, int r) {
  if (r <= a || b <= l) {
    return 0;
  }
  if (a <= l && r <= b) {
    return tree.at(i);
  }
  int x = solve(a, b, 2 * i + 1, l, (l + r) / 2);
  int y = solve(a, b, 2 * i + 2, (l + r) / 2, r);
  return gcd(x, y);
}

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

  int n = 1;
  while (n < N) {
    n <<= 1;
  }
  tree.resize(2 * n - 1, 0);
  for (int i = 0; i < N; i++) {
    tree.at(i + n - 1) = A.at(i);
  }
  for (int i = n - 2; i >= 0; i--) {
    tree.at(i) = gcd(tree.at(2 * i + 1), tree.at(2 * i + 2));
  }

  int ans = 1;
  for (int i = 0; i < N; i++) {
    int x1 = solve(0, i, 0, 0, n);
    int x2 = solve(i + 1, n, 0, 0, n);
    ans = max(ans, gcd(x1, x2));
  }
  cout << ans << endl;
}
Problem p03061 (C++)

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

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  vector<int> l(n), r(n);
  for (int i = 0; i < n; i++)
    l[i + 1] = gcd(a[i], l[i]);
  for (int i = n - 1; i > 0; i--)
    r[i - 1] = gcd(a[i], r[i]);
  int ans = 1;
  for (int i = 0; i < n; i++) {
    int tmp = gcd(l[i], r[i]);
    ans = max(ans, tmp);
  }
  printf("%d\n", ans);
}

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

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  vector<int> l(n), r(n);
  for (int i = 0; i < n - 1; i++)
    l[i + 1] = gcd(a[i], l[i]);
  for (int i = n - 1; i > 0; i--)
    r[i - 1] = gcd(a[i], r[i]);
  int ans = 1;
  for (int i = 0; i < n; i++) {
    int tmp = gcd(l[i], r[i]);
    ans = max(ans, tmp);
  }
  printf("%d\n", ans);
}
Problem p03061 (C++)

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

int gcd(int x, int y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  vector<int> l(N), r(N);
  for (int i = 0; i < N - 1; ++i) {
    l[i + 1] = gcd(l[i], a[i]);
  }
  for (int i = N - 1; i >= 1; ++i) {
    r[i - 1] = gcd(r[i], a[i]);
  }
  int ans = 1;
  for (int i = 0; i < N; ++i) {
    ans = max(ans, gcd(l[i], r[i]));
  }
  cout << ans << endl;
}


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

int gcd(int x, int y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
  }
  vector<int> l(N), r(N);
  for (int i = 0; i < N - 1; ++i) {
    l[i + 1] = gcd(l[i], a[i]);
  }
  for (int i = N - 1; i >= 1; --i) {
    r[i - 1] = gcd(r[i], a[i]);
  }
  int ans = 1;
  for (int i = 0; i < N; ++i) {
    ans = max(ans, gcd(l[i], r[i]));
  }
  cout << ans << endl;
}
Problem p03061 (C++)

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

int main() {
  i64 n;
  cin >> n;
  vector<i64> a(n);
  for (i64 i = 0; i < n; i++)
    cin >> a[i];
  i64 ans = 0;
  for (i64 i = 0; i < n; i++) {
    i64 tmp = a[(i == 0 ? 1 : 0)];
    for (i64 j = 0; j < n; j++)
      if (i != j)
        tmp = __gcd(tmp, a[j]);
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  i64 n;
  cin >> n;
  vector<i64> a(n);
  for (i64 i = 0; i < n; i++)
    cin >> a[i];
  vector<i64> suml(n), sumr(n);
  suml[0] = a[0];
  for (i64 i = 1; i < n; i++)
    suml[i] = __gcd(suml[i - 1], a[i]);
  sumr[n - 1] = a[n - 1];
  for (i64 i = n - 2; 0 <= i; i--)
    sumr[i] = __gcd(sumr[i + 1], a[i]);
  i64 ans = max(suml[n - 2], sumr[1]);
  for (i64 i = 1; i < n - 1; i++)
    ans = max(ans, __gcd(suml[i - 1], sumr[i + 1]));
  cout << ans << endl;
  return 0;
}
Problem p03061 (C++)

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

int gcd(int x, int y) {
  if (y % x == 0)
    return x;
  else
    return gcd(y % x, x);
}

int main() {
  int n, m, k;
  m = 0;
  cin >> n;
  vector<int> li(n), l(n + 1, 0), r(n + 1, 0);
  for (int i = 0; i < n; i++)
    cin >> li[i];
  for (int i = 0; i < n; i++) {
    l.at(i + 1) = gcd(li[i], l[i]);
    r.at(n - i - 1) = gcd(li[n - 1 - i], r[n - i]);
  }
  for (int i = 0; i < n; i++) {
    k = gcd(l[i], r[i + 1]);
    if (k > m)
      m = k;
  }
  cout << m << endl;
  return 0;
}

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

int gcd(int x, int y) {
  if (x == 0)
    return y;
  else
    return gcd(y % x, x);
}

int main() {
  int n, m, k;
  m = 0;
  cin >> n;
  vector<int> li(n), l(n + 1, 0), r(n + 1, 0);
  for (int i = 0; i < n; i++)
    cin >> li[i];
  for (int i = 0; i < n; i++) {
    l.at(i + 1) = gcd(li[i], l[i]);
    r.at(n - i - 1) = gcd(li[n - 1 - i], r[n - i]);
  }
  for (int i = 0; i < n; i++) {
    k = gcd(l[i], r[i + 1]);
    if (k > m)
      m = k;
  }
  cout << m << endl;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n;
int a[10005];
int gcd1[10005];
int gcd2[10005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);

  gcd1[1] = a[1];
  gcd2[n] = a[n];
  for (int i = 2; i <= n; ++i)
    gcd1[i] = __gcd(gcd1[i - 1], a[i]); // cout<<gcd1[i]<<" ";
  // cout<<endl;
  for (int i = n - 1; i >= 1; --i)
    gcd2[i] = __gcd(gcd2[i + 1], a[i]); // cout<<gcd2[i]<<" ";
  // cout<<endl;
  gcd1[0] = gcd2[2];
  gcd2[n + 1] = gcd1[n - 1];
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, __gcd(gcd1[i - 1], gcd2[i + 1]));
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100005];
int gcd1[100005];
int gcd2[100005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);

  gcd1[1] = a[1];
  gcd2[n] = a[n];
  for (int i = 2; i <= n; ++i)
    gcd1[i] = __gcd(gcd1[i - 1], a[i]); // cout<<gcd1[i]<<" ";
  // cout<<endl;
  for (int i = n - 1; i >= 1; --i)
    gcd2[i] = __gcd(gcd2[i + 1], a[i]); // cout<<gcd2[i]<<" ";
  // cout<<endl;
  gcd1[0] = gcd2[2];
  gcd2[n + 1] = gcd1[n - 1];
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, __gcd(gcd1[i - 1], gcd2[i + 1]));
  printf("%d\n", ans);
  return 0;
}
Problem p03062 (C++)

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

long long dp[10010][2];
long long INF = -100000000000000;

long long max(long long a, long long b) { return a > b ? a : b; }

int main() {
  int N;
  cin >> N;
  vector<long long> a(N);

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

  dp[0][0] = 0;
  dp[0][1] = INF;

  for (int i = 0; i < N; i++) {
    dp[i + 1][0] = max(dp[i][0] + a[i], dp[i][1] - a[i]);
    dp[i + 1][1] = max(dp[i][0] - a[i], dp[i][1] + a[i]);
  }

  cout << dp[N][0] << endl;

  return 0;
}

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

long long dp[100010][2];
long long INF = -100000000000000;

long long max(long long a, long long b) { return a > b ? a : b; }

int main() {
  int N;
  cin >> N;
  vector<long long> a(N);

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

  dp[0][0] = 0;
  dp[0][1] = INF;

  for (int i = 0; i < N; i++) {
    dp[i + 1][0] = max(dp[i][0] + a[i], dp[i][1] - a[i]);
    dp[i + 1][1] = max(dp[i][0] - a[i], dp[i][1] + a[i]);
  }

  cout << dp[N][0] << endl;

  return 0;
}
Problem p03062 (C++)

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

int main() {
  //
  int num;
  int cnt = 0;
  long long kekka = 0;
  cin >> num;

  vector<long long> vec(num);
  for (int i = 0; i < num; i++) {
    long long tmp;
    cin >> tmp;

    if (tmp < 0)
      cnt++;
    vec.at(i) = tmp;
  }

  sort(vec.begin(), vec.end());
  if (cnt % 2 == 0) {
    for (int i = 0; i < num; i++) {
      kekka += abs(vec.at(i));
    }
  } else {
    for (int i = 0; i < cnt - 1; i++) {
      kekka += abs(vec.at(i));
    }
    if (abs(vec.at(cnt - 1)) > abs(vec.at(cnt))) {
      vec.at(cnt - 1) *= -1;
      vec.at(cnt) *= -1;
    }
    for (int i = cnt - 1; i < num; i++) {
      kekka += vec.at(i);
    }
  }

  cout << kekka << endl;

  //
}


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

int main() {
  //
  int num;
  int cnt = 0;
  long long kekka = 0;
  cin >> num;

  vector<long long> vec(num);
  for (int i = 0; i < num; i++) {
    long long tmp;
    cin >> tmp;

    if (tmp < 0)
      cnt++;
    vec.at(i) = tmp;
  }

  sort(vec.begin(), vec.end());
  if (cnt % 2 == 0) {
    for (int i = 0; i < num; i++) {
      kekka += abs(vec.at(i));
    }
  } else {
    for (int i = 0; i < cnt - 1; i++) {
      kekka += abs(vec.at(i));
    }

    if (cnt == num) {
      kekka += vec.at(num - 1);
    } else {
      if (abs(vec.at(cnt - 1)) > abs(vec.at(cnt))) {
        vec.at(cnt - 1) *= -1;
        vec.at(cnt) *= -1;
      }
      for (int i = cnt - 1; i < num; i++) {
        kekka += vec.at(i);
      }
    }
  }

  cout << kekka << endl;

  //
}
Problem p03062 (C++)

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

#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

using namespace std;

const int maxn = 1e5 + 10;

ll dp[maxn][2];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vi vs(n);
  dp[0][0] = 0;
  dp[0][1] = -(1LL << 60);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    dp[i + 1][0] = max(dp[i][0] + x, dp[i][1] - x);
    dp[i + 1][1] = max(dp[i][0] - x, dp[i][1] + x);
  }
  cout << dp[n][0] << endl;
  return 0;
}

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

#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

using namespace std;

const int maxn = 1e5 + 10;

ll dp[maxn][2];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vi vs(n);
  dp[0][0] = 0;
  dp[0][1] = -(1LL << 60);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    dp[i + 1][0] = max(dp[i][0] + x, dp[i][1] - x);
    dp[i + 1][1] = max(dp[i][0] - x, dp[i][1] + x);
  }
  cout << dp[n][0] << endl;
  return 0;
}
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9 + 7;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
ll dp[110][110];
int main() {
  int n;
  cin >> n;
  vector<ll> v(n);
  rep(i, n) cin >> v[i];
  rep(i, 2) { rep(j, n) dp[i][j] = -inf; }
  dp[0][0] = v[0] + v[1];
  dp[1][0] = -v[0] - v[1];
  rep(j, n - 2) {
    dp[0][j + 1] = max(dp[0][j] + v[j + 2], dp[0][j + 1]);
    dp[1][j + 1] = max(dp[0][j] - 2 * v[j + 1] - v[j + 2], dp[1][j + 1]);
    dp[0][j + 1] = max(dp[0][j + 1], dp[1][j] + v[j + 2]);
    dp[1][j + 1] = max(dp[1][j + 1], dp[1][j] + 2 * v[j + 1] - v[j + 2]);
  }
  /*
  rep(i,2){
      rep(j,n-1) cout<<dp[i][j]<<" ";
      cout<<endl;
  }*/
  cout << max(dp[0][n - 2], dp[1][n - 2]) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9 + 7;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
ll dp[5][100010];
int main() {
  int n;
  cin >> n;
  vector<ll> v(n);
  rep(i, n) cin >> v[i];
  rep(i, 2) { rep(j, n) dp[i][j] = -inf; }
  dp[0][0] = v[0] + v[1];
  dp[1][0] = -v[0] - v[1];
  rep(j, n - 2) {
    dp[0][j + 1] = max(dp[0][j] + v[j + 2], dp[0][j + 1]);
    dp[1][j + 1] = max(dp[0][j] - 2 * v[j + 1] - v[j + 2], dp[1][j + 1]);
    dp[0][j + 1] = max(dp[0][j + 1], dp[1][j] + v[j + 2]);
    dp[1][j + 1] = max(dp[1][j + 1], dp[1][j] + 2 * v[j + 1] - v[j + 2]);
  }
  /*
  rep(i,2){
      rep(j,n-1) cout<<dp[i][j]<<" ";
      cout<<endl;
  }*/
  cout << max(dp[0][n - 2], dp[1][n - 2]) << endl;
  return 0;
}
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
long long a[105];
long long xabs(long long x) {
  if (x >= 0)
    return x;
  else
    return x * -1;
}
int main() {
  int n, i, t = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      t++;
  }

  long long ans = 0;
  if (t % 2 == 0) {
    for (i = 1; i <= n; i++)
      ans += xabs(a[i]);
    cout << ans;
  } else {
    long long minv = 0x7ffffffff;
    for (i = 1; i <= n; i++) {
      ans += xabs(a[i]);
      minv = min(minv, xabs(a[i]));
    }
    cout << ans - minv * 2;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
long long a[100005];
long long xabs(long long x) {
  if (x >= 0)
    return x;
  else
    return x * -1;
}
int main() {
  int n, i, t = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] < 0)
      t++;
  }

  long long ans = 0;
  if (t % 2 == 0) {
    for (i = 1; i <= n; i++)
      ans += xabs(a[i]);
    cout << ans;
  } else {
    long long minv = 0x7ffffffff;
    for (i = 1; i <= n; i++) {
      ans += xabs(a[i]);
      minv = min(minv, xabs(a[i]));
    }
    cout << ans - minv * 2;
  }
  return 0;
}
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;
//__int128
const int maxn = 2e5;
int a[maxn];

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  int n;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  LL res = 0;
  int c = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] < 0) {
      ++c;
      a[i] = -a[i];
    }
    res += a[i];
  }
  if (c & 1) {
    sort(a, a + n);
    res -= 2 * a[0];
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;
//__int128
const int maxn = 2e5;
int a[maxn];

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  //  freopen("out.txt", "w", stdout);
#endif
  int n;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  LL res = 0;
  int c = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] < 0) {
      ++c;
      a[i] = -a[i];
    }
    res += a[i];
  }
  if (c & 1) {
    sort(a, a + n);
    res -= 2 * a[0];
  }
  cout << res << endl;
  return 0;
}
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define forr(a, b, v) for (int v = a; v <= b; v++)
#define ford(a, b, v) for (int v = a; v >= b; v--)
const int N = 100005;
int main() {
  int n;
  int A[N];
  // store the sum of the result in a long long pair
  ll s = 0;
  cin >> n;
  forr(1, n, i) { cin >> A[i]; }
  forr(1, n - 2, i) {
    if (A[i] < 0) {
      A[i] *= -1;
      A[i + 1] *= -1;
    }
    s += A[i];
  }
  // add the last two pairs
  if ((A[n] > 0 and A[n - 1] > 0) or (A[n] < 0 and A[n - 1] < 0)) {
    s += max(A[n - 1] + A[n], -A[n - 1] - A[n]);
  } else {
    int *mni = min_element(A + 1, A + n - 2);
    int mnv = *mni;

    if (mnv > min(abs(A[n - 1]), abs(A[n]))) {
      s += max(A[n - 1] + A[n], -A[n - 1] - A[n]);
    } else {
      s -= mnv * 2;
      s += abs(A[n - 1]);
      s += abs(A[n]);
    }
  }

  cout << s << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define forr(a, b, v) for (int v = a; v <= b; v++)
#define ford(a, b, v) for (int v = a; v >= b; v--)
const int N = 100005;
int main() {
  int n;
  int A[N];
  // store the sum of the result in a long long pair
  ll s = 0;
  cin >> n;
  forr(1, n, i) { cin >> A[i]; }

  if (n == 2) {
    int s = max(A[n - 1] + A[n], -A[n - 1] - A[n]);
    cout << s << endl;
    return 0;
  }
  forr(1, n - 2, i) {
    if (A[i] < 0) {
      A[i] *= -1;
      A[i + 1] *= -1;
    }
    s += A[i];
  }
  // add the last two pairs
  if ((A[n] > 0 and A[n - 1] > 0) or (A[n] < 0 and A[n - 1] < 0)) {
    s += max(A[n - 1] + A[n], -A[n - 1] - A[n]);
  } else {
    int *mni = min_element(A + 1, A + n - 2);
    int mnv = *mni;

    if (mnv > min(abs(A[n - 1]), abs(A[n]))) {
      s += max(A[n - 1] + A[n], -A[n - 1] - A[n]);
    } else {
      s -= mnv * 2;
      s += abs(A[n - 1]);
      s += abs(A[n]);
    }
  }

  cout << s << endl;
  return 0;
}
Problem p03062 (C++)

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

#define INF 100000

using namespace std;

void chmin(int &a, int b) {
  if (a > b)
    a = b;
}
void chmax(int &a, int b) {
  if (a < b)
    a = b;
}

int N;
vector<int> A;

vector<int> L, R;

int rec(int a, int b) {
  if (a == 0)
    return b;
  if (b == 0)
    return a;
  return rec(b, a % b);
}

int main() {
  cin >> N;
  A.resize(N);

  int min = 1000000000;
  int pm = 1;
  long long sum = 0;

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    pm = (pm * tmp) / abs(pm * tmp);
    sum += abs(tmp);
    chmin(min, abs(tmp));
  }

  if (pm >= 0)
    cout << sum << endl;
  else
    cout << sum - 2 * min << endl;
}

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

#define INF 100000

using namespace std;

void chmin(int &a, int b) {
  if (a > b)
    a = b;
}
void chmax(int &a, int b) {
  if (a < b)
    a = b;
}

int N;
vector<int> A;

vector<int> L, R;

int rec(int a, int b) {
  if (a == 0)
    return b;
  if (b == 0)
    return a;
  return rec(b, a % b);
}

int main() {
  cin >> N;
  A.resize(N);

  int min = 1000000000;
  int pm = 1;
  long long sum = 0;

  for (int i = 0; i < N; i++) {
    int tmp;
    cin >> tmp;
    if (tmp > 0) {

    } else if (tmp < 0) {
      pm *= -1;
    } else {
      pm = 0;
    }
    sum += abs(tmp);
    chmin(min, abs(tmp));
  }

  if (pm >= 0)
    cout << sum << endl;
  else
    cout << sum - 2 * min << endl;
}
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long a[1000];
  int mainasu = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0) {
      mainasu++;
    }
  }
  long long ans = 0;
  long long mins = abs(a[0]);
  for (int i = 0; i < n; i++) {
    ans = ans + abs(a[i]);
    mins = min(mins, abs(a[i]));
  }
  if (mainasu % 2 == 1) {
    ans = ans - mins * 2;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long a[100000];
  int mainasu = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0) {
      mainasu++;
    }
  }
  long long ans = 0;
  long long mins = abs(a[0]);
  for (int i = 0; i < n; i++) {
    ans = ans + abs(a[i]);
    mins = min(mins, abs(a[i]));
  }
  if (mainasu % 2 == 1) {
    ans = ans - mins * 2;
  }
  cout << ans << endl;
}
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
#define M 1000000007
#define endl "\n"
#define int long long
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }

  int mi = 1e9;
  int ind;
  for (int i = 0; i < n; i++) {
    if (abs(ar[i]) < mi) {
      mi = abs(ar[i]);
      ind = i;
    }
  }
  for (int i = 0; i < ind; i++) {
    if (ar[i] < 0) {
      ar[i] = -ar[i];
      ar[i + 1] = -ar[i + 1];
    }
  }
  for (int j = n - 1; j > ind; j--) {
    if (ar[j] < 0) {
      ar[j] = -ar[j];
      ar[j - 1] = -ar[j - 1];
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += ar[i];
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define M 1000000007
#define endl "\n"
#define int long long
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }

  int mi = 2 * M;
  int ind;
  for (int i = 0; i < n; i++) {
    if (abs(ar[i]) < mi) {
      mi = abs(ar[i]);
      ind = i;
    }
  }
  for (int i = 0; i < ind; i++) {
    if (ar[i] < 0) {
      ar[i] = -ar[i];
      ar[i + 1] = -ar[i + 1];
    }
  }
  for (int j = n - 1; j > ind; j--) {
    if (ar[j] < 0) {
      ar[j] = -ar[j];
      ar[j - 1] = -ar[j - 1];
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += ar[i];
  }
  cout << ans;
}
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int a[10001];
  cin >> N;
  long long ans = 0;
  int mini = INT_MAX, cnt = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    if (a[i] < 0) {
      cnt++;
    }
    ans += abs(a[i]);
    mini = min(abs(a[i]), mini);
  }
  if (cnt % 2 == 1) {
    ans -= 2 * mini;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int a[100001];
  cin >> N;
  long long ans = 0;
  int mini = INT_MAX, cnt = 0;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    if (a[i] < 0) {
      cnt++;
    }
    ans += abs(a[i]);
    mini = min(abs(a[i]), mini);
  }
  if (cnt % 2 == 1) {
    ans -= 2 * mini;
  }
  cout << ans << endl;
  return 0;
}
Problem p03062 (C++)

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

int main() {
  long n;
  cin >> n;
  vector<long long int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());

  while ((a.at(0) + a.at(1)) < 0) {
    a.at(0) *= -1;
    a.at(1) *= -1;
    sort(a.begin(), a.end());
  }

  long long int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
}

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

int main() {
  long n;
  cin >> n;
  vector<long long int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());

  rep(i, n / 2) {
    if (a[2 * i] + a[2 * i + 1] < 0) {
      a[2 * i] *= -1;
      a[2 * i + 1] *= -1;
    } else
      break;
  }

  long long int ans = 0;
  rep(i, n) ans += a[i];
  cout << ans << endl;
}
Problem p03062 (Python)

Buggy submission (status: Runtime Error):
import math  # noqa
import bisect  # noqa
import queue  # noqa


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))

    cnt_neg = 0
    abs_min = 1_000_000_005
    for a in A:
        abs_min = min(abs_min, abs(a))
        if a < 0:
            cnt_neg += 1

    ans = 0
    for a in A:
        ans += abs(a)

    if cnt_neg % 2 != 0:
        ans -= 2 * abs_min

    print(ans)


Fixed submission:
import math  # noqa
import bisect  # noqa
import queue  # noqa


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))

    cnt_neg = 0
    abs_min = 1000000005
    for a in A:
        abs_min = min(abs_min, abs(a))
        if a < 0:
            cnt_neg += 1

    ans = 0
    for a in A:
        ans += abs(a)

    if cnt_neg % 2 != 0:
        ans -= 2 * abs_min

    print(ans)
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (n); ++(i))
#define loop(i, a, n) for (int i = (a); (i) < (n); ++(i))
#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF (int)1e9 + 1
#define MOD 0
// #define int ll
using namespace std;
typedef vector<long long> vl;

// int gcd(int a,int b) {
//   if(b == 0) {
//     return b;
//   }
//   return gcd(b, a%b);
// }

signed main() {
  int n, ch = 0;
  ll sum = 0;
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vector<ll> a(10005, INF);
  vector<ll> b(10005, INF);
  rep(i, n) {
    cin >> a[i];
    b[i] = a[i];
    a[i] = llabs(a[i]);
    sum += a[i];
    if (a[i] == 0) {
      ch = 1;
    }
  }
  if (ch == 1) {
    cout << sum << endl;
    return 0;
  } else {
    rep(i, n - 1) {
      if (b[i] < 0) {
        b[i + 1] = b[i + 1] * (-1);
      }
    }
  }
  if (b[n - 1] > 0) {
    cout << sum << endl;
  } else {
    sort(a.begin(), a.end());
    sum -= 2 * a[0];
    cout << sum << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (n); ++(i))
#define loop(i, a, n) for (int i = (a); (i) < (n); ++(i))
#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF (int)1e9 + 1
#define MOD 0
// #define int ll
using namespace std;
typedef vector<long long> vl;

// int gcd(int a,int b) {
//   if(b == 0) {
//     return b;
//   }
//   return gcd(b, a%b);
// }

signed main() {
  int n, ch = 0;
  ll sum = 0;
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  vector<ll> a(100050, INF);
  vector<ll> b(100050, INF);
  rep(i, n) {
    cin >> a[i];
    b[i] = a[i];
    a[i] = llabs(a[i]);
    sum += a[i];
    if (a[i] == 0) {
      ch = 1;
    }
  }
  if (ch == 1) {
    cout << sum << endl;
    return 0;
  } else {
    rep(i, n - 1) {
      if (b[i] < 0) {
        b[i + 1] = b[i + 1] * (-1);
      }
    }
  }
  if (b[n - 1] > 0) {
    cout << sum << endl;
  } else {
    sort(a.begin(), a.end());
    sum -= 2 * a[0];
    cout << sum << endl;
  }
  return 0;
}
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll, ll>
#define mod 1000000007 // 998244353
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define PI 3.141592653589793238
#define pq priority_queue<ll>
#define pqr priority_queue<ll, vector<ll>, greater<ll>()>
using namespace std;
ll pow_mod(ll a, ll b) {
  ll res = 1;
  while (b != 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
void solve() {
  ll n;
  cin >> n;

  ll a[n + 1];

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

  ll sum = 0;
  ll mn = INF;

  vector<ll> v;

  for (ll i = 1; i <= n; i++) {
    if (a[i] > 0) {
      sum += a[i];
      mn = min(mn, a[i]);
    } else
      v.pb(abs(a[i]));
  }

  if (!v.empty())
    sort(v.begin(), v.end());

  for (ll i = 1; i < (ll)v.size(); i++)
    sum += v[i];

  ll sz = (ll)v.size();
  if (sz % 2 == 0)
    sum += v[0];

  if (sz % 2 == 1 && !v.empty()) {
    ll x = v[0];

    if (x > mn) {
      sum -= 2 * mn;
      sum += x;
    } else
      sum -= x;
  }

  cout << sum << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // Start from Here.

  ll t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();

  // Good Bye!
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll, ll>
#define mod 1000000007 // 998244353
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define PI 3.141592653589793238
#define pq priority_queue<ll>
#define pqr priority_queue<ll, vector<ll>, greater<ll>()>
using namespace std;
ll pow_mod(ll a, ll b) {
  ll res = 1;
  while (b != 0) {
    if (b & 1) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}
void solve() {
  ll n;
  cin >> n;

  ll a[n + 1];

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

  ll sum = 0;
  ll mn = INF;

  vector<ll> v;

  for (ll i = 1; i <= n; i++) {
    if (a[i] > 0) {
      sum += a[i];
      mn = min(mn, a[i]);
    } else
      v.pb(abs(a[i]));
  }

  if (!v.empty())
    sort(v.begin(), v.end());

  for (ll i = 1; i < (ll)v.size(); i++)
    sum += v[i];

  ll sz = (ll)v.size();
  if (sz % 2 == 0 && !v.empty())
    sum += v[0];

  if (sz % 2 == 1 && !v.empty()) {
    ll x = v[0];

    if (x > mn) {
      sum -= 2 * mn;
      sum += x;
    } else
      sum -= x;
  }

  cout << sum << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // Start from Here.

  ll t;
  t = 1;
  // cin>>t;
  while (t--)
    solve();

  // Good Bye!
  return 0;
}
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define print(x) cerr << (#x) << "is " << x << "\n"
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

void test();

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<int> a(n);
  ll sum_p = 0, sum_n = 0, c = 0;
  for (auto &i : a) {
    cin >> i;
    if (i >= 0) {
      sum_p += i;
    } else {
      c++;
      sum_n -= i;
    }
    i = abs(i);
  }
  sort(all(a));
  ll sum = sum_p + sum_n;
  if (c & 1) {
    sum -= 2 * a[0];
  }
  cout << sum << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define print(x) cerr << (#x) << "is " << x << "\n"
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

void test();

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif
  int n;
  cin >> n;
  vector<int> a(n);
  ll sum_p = 0, sum_n = 0, c = 0;
  for (auto &i : a) {
    cin >> i;
    if (i >= 0) {
      sum_p += i;
    } else {
      c++;
      sum_n -= i;
    }
    i = abs(i);
  }
  sort(all(a));
  ll sum = sum_p + sum_n;
  if (c & 1) {
    sum -= 2 * a[0];
  }
  cout << sum << "\n";
}
Problem p03062 (C++)

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

#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;

LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }
LL powmod(LL a, LL b, LL MOD) {
  LL ans = 1;
  while (b) {
    if (b % 2)
      ans = ans * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return ans;
}
const int N = 2e5 + 11;
LL n, a[N];
LL ans;
int sta;
vector<LL> l, r;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  LL x = 0, sta = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    if (a[i] < 0)
      l.pb(a[i]);
    else if (a[i] >= 0)
      ans += a[i], r.pb(a[i]);
  }
  sort(l.begin(), l.end());
  sort(r.begin(), r.end());
  for (int i = 2; i <= l.size(); i += 2) {
    ans -= l[i - 1];
    ans -= l[i - 2];
  }
  if (l.size() % 2 == 1) {
    if (r.size() && r[0] + l[l.size() - 1] < 0) {
      ans -= l[l.size() - 1];
      sta = 1;
    }
  }
  if (sta)
    ans -= 2 * r[0];
  else
    ans += l[l.size() - 1];
  cout << ans << endl;
  return 0;
}


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

#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;

LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }
LL powmod(LL a, LL b, LL MOD) {
  LL ans = 1;
  while (b) {
    if (b % 2)
      ans = ans * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return ans;
}
const int N = 2e5 + 11;
LL n, a[N];
LL ans;
int sta;
vector<LL> l, r;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  LL x = 0, sta = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    if (a[i] < 0)
      l.pb(a[i]);
    else if (a[i] >= 0)
      ans += a[i], r.pb(a[i]);
  }
  sort(l.begin(), l.end());
  sort(r.begin(), r.end());
  for (int i = 2; i <= l.size(); i += 2) {
    ans -= l[i - 1];
    ans -= l[i - 2];
  }
  if (l.size() % 2 == 1) {
    if (r.size() && r[0] + l[l.size() - 1] < 0) {
      ans -= l[l.size() - 1];
      sta = 1;
    }
  }
  if (sta)
    ans -= 2 * r[0];
  else if (l.size() && l.size() % 2 == 1 && !sta)
    ans += l[l.size() - 1];
  cout << ans << endl;
  return 0;
}
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  long long sum = 0;
  int m = 1e9 + 1;
  int count = 0;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    sum += abs(A[i]);
    m = min(abs(A[i]), m);
    if (A[i] < 0) {
      count++;
    }
  }
  cout << sum - (count % 2 == 0 ? 0 : 2 * m) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  long long sum = 0;
  int m = 1e9 + 1;
  int count = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += abs(A[i]);
    m = min(abs(A[i]), m);
    if (A[i] < 0) {
      count++;
    }
  }
  cout << sum - (count % 2 == 0 ? 0 : 2 * m) << endl;
  return 0;
}
Problem p03062 (C++)

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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define sc1(a) scanf("%d", &a)

int main() {
  bool f = 0;
  long long n, a, b = 0ll, x = 2e9;
  sc1(n);
  rep(i, n) {
    scanf("%lld", &a);
    if (a < 0)
      f = f ^ 1;
    b += abs(a);
    x = min(x, abs(a));
  }
  printf("%lld\n", f ? b - x * 2 : b);
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < n; ++i)
#define sc1(a) scanf("%d", &a)

int main() {
  bool f = 0;
  int n;
  long long a, b = 0ll, x = 2e9;
  sc1(n);
  rep(i, n) {
    scanf("%lld", &a);
    if (a < 0)
      f = f ^ 1;
    b += abs(a);
    x = min(x, abs(a));
  }
  printf("%lld\n", f ? b - x * 2 : b);
  return 0;
}
Problem p03062 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  sort(A.begin(), A.end());
  int64_t ans = 0;
  for (int i = 0; i < N; i++)
    ans += abs(A.at(i));
  auto itr = upper_bound(A.begin(), A.end(), 0);
  int k = distance(A.begin(), itr);
  if (k % 2 == 0)
    cout << ans << endl;
  else {
    int64_t Min = 2 * min(abs(A.at(k - 1)), A.at(k));
    ans -= Min;
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  sort(A.begin(), A.end());
  int64_t ans = 0;
  for (int i = 0; i < N; i++)
    ans += abs(A.at(i));
  auto itr = upper_bound(A.begin(), A.end(), 0);
  int k = distance(A.begin(), itr);
  if (k % 2 == 0)
    cout << ans << endl;
  else if (k == N) {
    ans += 2 * A.at(N - 1);
    cout << ans << endl;
  } else {
    int64_t Min = 2 * min(abs(A.at(k - 1)), A.at(k));
    ans -= Min;
    cout << ans << endl;
  }
  return 0;
}
Problem p03063 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  // 1
  vector<int> v(N + 1, 0);
  for (int i = 1; i <= N; i++)
    v[i] = s[i - 1] == '.' ? v[i - 1] + 1 : v[i - 1];
  v[N + 1] = v[N];
  // 2
  int ans = N;
  for (int i = 0; i <= N; i++)
    ans = min(ans, i - v[i] + v[N] - v[i]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  // 1
  vector<int> v(N + 1, 0);
  for (int i = 1; i <= N; i++)
    v[i] = s[i - 1] == '.' ? v[i - 1] + 1 : v[i - 1];
  // 2
  int ans = N;
  for (int i = 0; i <= N; i++)
    ans = min(ans, i - v[i] + v[N] - v[i]);
  cout << ans << endl;
  return 0;
}
Problem p03063 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, l[100005], r[100005];
char s[100005];
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    l[i] = l[i - 1];
    if (s[i] == '#')
      l[i]++;
  }
  for (int i = n; i >= 1; i--) {
    r[i] = r[i + 1];
    if (s[i] == '.')
      r[i]++;
  }
  int ans = 1e9;
  for (int i = 1; i < n; i++)
    ans = min(ans, l[i] + r[i + 1]);
  ans = min(ans, l[n]);
  ans = min(ans, r[1]);
  printf("%d\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, l[200005], r[200005];
char s[200005];
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    l[i] = l[i - 1];
    if (s[i] == '#')
      l[i]++;
  }
  for (int i = n; i >= 1; i--) {
    r[i] = r[i + 1];
    if (s[i] == '.')
      r[i]++;
  }
  int ans = 1e9;
  for (int i = 1; i < n; i++)
    ans = min(ans, l[i] + r[i + 1]);
  ans = min(ans, l[n]);
  ans = min(ans, r[1]);
  printf("%d\n", ans);
}
Problem p03063 (C++)

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

using namespace std;

int U[50500], V[50500];

int main(void) {

  int N;

  cin >> N;

  string S, tmp;
  cin >> tmp;

  S = "*" + tmp + "*";

  int ans = 300000;

  for (int i = 1; i <= N; i++) {
    if (S[i] != '.') {
      U[i] = U[i - 1] + 1;
    } else {
      U[i] = U[i - 1];
    }
  }

  for (int i = N; i >= 1; i--) {
    if (S[i] != '#') {
      V[i] = V[i + 1] + 1;
    } else {
      V[i] = V[i + 1];
    }
  }

  for (int i = 0; i <= N; i++) {
    // cout << "U[" << i << "]= " << U[i] << "  V[" << i + 1 << "]= " << V[i +
    // 1] << endl;
    ans = min(U[i] + V[i + 1], ans);
  }

  cout << ans << endl;

  // system("pause");
  return 0;
}

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

using namespace std;

int U[505000], V[505000];

int main(void) {

  int N;

  cin >> N;

  string S, tmp;
  cin >> tmp;

  S = "*" + tmp + "*";

  int ans = 300000;

  for (int i = 1; i <= N; i++) {
    if (S[i] != '.') {
      U[i] = U[i - 1] + 1;
    } else {
      U[i] = U[i - 1];
    }
  }

  for (int i = N; i >= 1; i--) {
    if (S[i] != '#') {
      V[i] = V[i + 1] + 1;
    } else {
      V[i] = V[i + 1];
    }
  }

  for (int i = 0; i <= N; i++) {
    // cout << "U[" << i << "]= " << U[i] << "  V[" << i + 1 << "]= " << V[i +
    // 1] << endl;
    ans = min(U[i] + V[i + 1], ans);
  }

  cout << ans << endl;

  // system("pause");
  return 0;
}
Problem p03063 (C++)

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

using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  int cnt_b[100005] = {0};
  int cnt_w[100005] = {0};

  for (int i = 0; i < n; i++) {
    if (s[i] == '#') {
      cnt_b[i + 1] = cnt_b[i] + 1;
      cnt_w[i + 1] = cnt_w[i];
    } else {
      cnt_b[i + 1] = cnt_b[i];
      cnt_w[i + 1] = cnt_w[i] + 1;
    }
  }

  int ans = n;
  for (int i = 0; i <= n; i++) {
    int left_black = cnt_b[i];             // (0, i) に含まれる黒の個数
    int right_white = cnt_w[n] - cnt_w[i]; // [i, n+1) に含まれる黒の個数
    ans = min(ans, left_black + right_white);
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  int cnt_b[200005] = {0};
  int cnt_w[200005] = {0};

  for (int i = 0; i < n; i++) {
    if (s[i] == '#') {
      cnt_b[i + 1] = cnt_b[i] + 1;
      cnt_w[i + 1] = cnt_w[i];
    } else {
      cnt_b[i + 1] = cnt_b[i];
      cnt_w[i + 1] = cnt_w[i] + 1;
    }
  }

  int ans = n;
  for (int i = 0; i <= n; i++) {
    int left_black = cnt_b[i];             // (0, i) に含まれる黒の個数
    int right_white = cnt_w[n] - cnt_w[i]; // [i, n+1) に含まれる黒の個数
    ans = min(ans, left_black + right_white);
  }
  cout << ans << endl;
  return 0;
}
Problem p03063 (C++)

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

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long ll;

const int mod = 1e9 + 7;
const ll inf = 3e18 + 5;

int n;
string s;
int dp[200050][2];
bool c[200050];

int f(int a, bool b) {
  if (a == n)
    return 0;
  if (dp[a][b] != -1)
    return dp[a][b];

  return dp[a][b] =
             min(!c[a] + f(a + 1, true), b ? (int)1e9 : c[a] + f(a + 1, false));
}

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

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

  cin >> n;
  cin >> s;
  memset(dp, -1, sizeof dp);
  bool frt;
  for (int i = 0; i < n; i++)
    if (s[i] == '#')
      c[i] = true;
    else
      c[i] = false;

  cout << f(0, false);
}

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

using namespace std;

#define all(x) x.begin(), x.end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long ll;

const int mod = 1e9 + 7;
const ll inf = 3e18 + 5;

int n;
string s;
int dp[200050][2];
bool c[200050];

int f(int a, bool b) {
  if (a == n)
    return 0;
  if (dp[a][b] != -1)
    return dp[a][b];

  return dp[a][b] =
             min(!c[a] + f(a + 1, true), b ? (int)1e9 : c[a] + f(a + 1, false));
}

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

  cin >> n;
  cin >> s;
  memset(dp, -1, sizeof dp);
  bool frt;
  for (int i = 0; i < n; i++)
    if (s[i] == '#')
      c[i] = true;
    else
      c[i] = false;

  cout << f(0, false);
}
Problem p03063 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
  int n, dpw[100001] = {}, dpb[100001] = {}, ans = 1000000;
  char s[100100];
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    dpw[i + 1] = dpw[i];
    if (s[i] == '#') {
      dpw[i + 1]++;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    dpb[n - i] = dpb[n - 1 - i];
    if (s[i] == '.') {
      dpb[n - i]++;
    }
  }
  for (int i = 0; i <= n; i++) {
    ans = min(ans, dpb[i] + dpw[n - i]);
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
  int n, dpw[200011] = {}, dpb[200011] = {}, ans = 2001000;
  char s[200100];
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    dpw[i + 1] = dpw[i];
    if (s[i] == '#') {
      dpw[i + 1]++;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    dpb[n - i] = dpb[n - 1 - i];
    if (s[i] == '.') {
      dpb[n - i]++;
    }
  }
  for (int i = 0; i <= n; i++) {
    ans = min(ans, dpb[i] + dpw[n - i]);
  }
  printf("%d\n", ans);
}
Problem p03063 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;

/*
黒を白にすることもある

*/

ll n, ru[100010] = {}, ru2[100010] = {}, ans = 1e18;
string s;
bool black = false;
vector<pair<char, int>> v;

void RLE(string &s, vector<pair<char, int>> &v) {
  v.push_back({s[0], 0});
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == v[v.size() - 1].first) {
      v[v.size() - 1].second++;
    } else {
      v.push_back({s[i], 1});
    }
  }
}

void init() {
  cin >> n >> s;
  RLE(s, v);
  ru[0] = 0;
  ru2[0] = 0;
  reg(i, 1, v.size()) {
    ru[i] = ru[i - 1] + (v[i - 1].first == '#' ? v[i - 1].second : 0);
    ru2[i] = ru2[i - 1] + (v[i - 1].first == '.' ? v[i - 1].second : 0);
    // cerr<<ru[i]<<","<<ru2[i]<<endl;
  }
  // rep(i,v.size())cerr<<v[i].first<<","<<v[i].second<<endl;
  cerr << "初期化終了" << endl;
}

int main(void) {
  init();
  reg(i, 0, v.size()) { ans = min(ans, ru[i] + ru2[v.size()] - ru2[i]); }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long ll;

/*
黒を白にすることもある

*/

ll n, ru[200010] = {}, ru2[200010] = {}, ans = 1e18;
string s;
bool black = false;
vector<pair<char, int>> v;

void RLE(string &s, vector<pair<char, int>> &v) {
  v.push_back({s[0], 0});
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == v[v.size() - 1].first) {
      v[v.size() - 1].second++;
    } else {
      v.push_back({s[i], 1});
    }
  }
}

void init() {
  cin >> n >> s;
  RLE(s, v);
  ru[0] = 0;
  ru2[0] = 0;
  reg(i, 1, v.size()) {
    ru[i] = ru[i - 1] + (v[i - 1].first == '#' ? v[i - 1].second : 0);
    ru2[i] = ru2[i - 1] + (v[i - 1].first == '.' ? v[i - 1].second : 0);
    // cerr<<ru[i]<<","<<ru2[i]<<endl;
  }
  // rep(i,v.size())cerr<<v[i].first<<","<<v[i].second<<endl;
  cerr << "初期化終了" << endl;
}

int main(void) {
  init();
  reg(i, 0, v.size()) { ans = min(ans, ru[i] + ru2[v.size()] - ru2[i]); }
  cout << ans << endl;
  return 0;
}