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

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

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define mem(x, val) memset((x), (val), sizeof(x))
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define PI acos(-1.0)
#define N 111111

const int INF = 1 << 30;
typedef long long ll;
typedef pair<int, int> pii;

int a[N];

int main() {
  int n;
  ll sum = 0LL;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= (i + 1);
  }

  sort(a, a + n);
  if (n & 1) {
    int median = a[n >> 1];
    ll sum = 0LL;
    for (int i = 0; i < n; i++)
      sum += abs(a[i] - median);
    cout << sum << endl;
  } else {
    int median_first = a[n / 2 - 1];
    int median_second = a[n / 2];
    ll sum_first = 0LL, sum_second = 0LL;
    for (int i = 0; i < n; i++) {
      sum_first += abs(a[i] - median_first);
      sum_second += abs(a[i] - median_second);
    }
    cout << min(sum_second, sum_first) << endl;
  }

  return 0;
}

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

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define mem(x, val) memset((x), (val), sizeof(x))
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define PI acos(-1.0)
#define N 211111

const int INF = 1 << 30;
typedef long long ll;
typedef pair<int, int> pii;

int a[N];

int main() {
  int n;
  ll sum = 0LL;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= (i + 1);
  }

  sort(a, a + n);
  if (n & 1) {
    int median = a[n >> 1];
    ll sum = 0LL;
    for (int i = 0; i < n; i++)
      sum += abs(a[i] - median);
    cout << sum << endl;
  } else {
    int median_first = a[n / 2 - 1];
    int median_second = a[n / 2];
    ll sum_first = 0LL, sum_second = 0LL;
    for (int i = 0; i < n; i++) {
      sum_first += abs(a[i] - median_first);
      sum_second += abs(a[i] - median_second);
    }
    cout << min(sum_second, sum_first) << endl;
  }

  return 0;
}
Problem p03309 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  long long M = -1001001001, m = 1001001001;
  for (int i = 1; i <= n; i++) {
    cin >> a.at(i - 1);
    a.at(i - 1) -= i;
  }
  sort(a.begin(), a.end());
  long long ans1 = 0, ans2 = 0;
  long long tmp1 = a.at(n / 2 - 1);
  long long tmp2 = a.at(n / 2);
  for (int i = 0; i < n; i++) {
    ans1 += abs(a.at(i) - tmp1);
    ans2 += abs(a.at(i) - tmp2);
  }
  cout << min(ans1, ans2) << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  long long M = -1001001001, m = 1001001001;
  for (int i = 1; i <= n; i++) {
    cin >> a.at(i - 1);
    a.at(i - 1) -= i;
  }
  sort(a.begin(), a.end());
  long long ans1 = 0, ans2 = 0;
  long long tmp1;
  if (n >= 2)
    tmp1 = a.at(n / 2 - 1);
  else
    tmp1 = 0;
  long long tmp2 = a.at(n / 2);
  for (int i = 0; i < n; i++) {
    ans1 += abs(a.at(i) - tmp1);
    ans2 += abs(a.at(i) - tmp2);
  }
  cout << min(ans1, ans2) << endl;
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] -= (i + 1);
  }
  sort(A.begin(), A.end());
  int ans = 0;
  int B = (N % 2 == 1 ? A[N / 2] : A[(A[N / 2] + A[N / 2 - 1]) / 2]);
  for (int Ai : A) {
    ans += std::abs(Ai - B);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] -= (i + 1);
  }
  sort(A.begin(), A.end());
  long long int ans = 0;
  int B = (N % 2 == 1 ? A[N / 2] : (A[N / 2] + A[N / 2 - 1]) / 2);
  for (int Ai : A) {
    ans += std::abs(Ai - B);
  }
  cout << ans << endl;
}
Problem p03309 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    a.at(i) -= i - 1;
  }

  sort(a.begin(), a.end());
  int l = a.size();
  long long b;
  if (l % 2)
    b = a.at(l / 2 - 1);
  else
    b = a.at(l / 2 - 1) + (a.at(l / 2) - a.at(l / 2 - 1)) / 2;

  long long ans = 0;
  for (int i = 0; i < l; i++) {
    ans += abs(a.at(i) - b);
  }

  cout << ans << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    a.at(i) -= i - 1;
  }

  sort(a.begin(), a.end());
  int l = a.size();
  long long b;
  if (l == 1)
    b = a.at(0);
  else if (l % 2)
    b = a.at(l / 2);
  else
    b = a.at(l / 2 - 1) + (a.at(l / 2) - a.at(l / 2 - 1)) / 2;

  long long ans = 0;
  for (int i = 0; i < l; i++) {
    ans += abs(a.at(i) - b);
  }

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

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/abc102/tasks/arc100_a

#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++)
#define INF 1e9
#define ALL(v) (v).begin(), (v).end()
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

ll N, A[20], t, med, c;

int main() {
  c = 0;
  cin >> N;
  REP(i, N) {
    cin >> t;
    A[i] = t - i - 1;
  }

  sort(A, A + N);
  med = N % 2 ? A[(N - 1) / 2] : (A[N / 2 - 1] + A[N / 2]) / 2;

  REP(i, N) { c += abs(A[i] - med); }

  cout << c << '\n';

  return 0;
}


Fixed submission:
// https://atcoder.jp/contests/abc102/tasks/arc100_a

#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++)
#define INF 1e9
#define ALL(v) (v).begin(), (v).end()
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

ll N, A[200000], t, med, c;

int main() {
  c = 0;
  cin >> N;
  REP(i, N) {
    cin >> t;
    A[i] = t - i - 1;
  }

  sort(A, A + N);
  med = N % 2 ? A[(N - 1) / 2] : (A[N / 2 - 1] + A[N / 2]) / 2;

  REP(i, N) { c += abs(A[i] - med); }

  cout << c << '\n';

  return 0;
}
Problem p03309 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;
const ll INF = 1e12;

ll snuke(std::vector<ll> &va, ll b) {
  std::vector<ll>::iterator it;
  ll ans = 0;
  for (it = va.begin(); it != va.end(); it++) {
    ll idx = std::distance(va.begin(), it);
    ans += (ll)std::fabs(va[idx] - (b + idx));
  }
  return ans;
}

int main(int argc, char *argv[]) {
  ll n;
  std::cin >> n;

  std::vector<ll> va(n, 0);
  for (ll i = 0; i < n; i++) {
    std::cin >> va[i];
  }

  ll ans = INF;
  ll max_val = *std::max_element(va.begin(), va.end());
  for (ll i = -max_val; i < max_val; i++) {
    // std::cout << "s: " << snuke(va, i) << std::endl;
    ans = std::min(ans, snuke(va, i));
  }

  std::cout << ans << std::endl;

  return 0;
}


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

typedef long long ll;
const ll INF = 1e12;

ll snuke(std::vector<ll> &va, ll b) {
  std::vector<ll>::iterator it;
  ll ans = 0;
  for (it = va.begin(); it != va.end(); it++) {
    ll idx = std::distance(va.begin(), it);
    ans += (ll)std::fabs(va[idx] - (b + idx));
  }
  return ans;
}

int main(int argc, char *argv[]) {
  ll n;
  std::cin >> n;

  std::vector<ll> va(n, 0);
  for (ll i = 0; i < n; i++) {
    std::cin >> va[i];
  }

  ll ans = INF;
  ll max_val = *std::max_element(va.begin(), va.end());

  ll l = -max_val * 100;
  ll r = +max_val * 100;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    ll l_val = snuke(va, mid - 1);
    ll m_val = snuke(va, mid + 0);
    ll r_val = snuke(va, mid + 1);
    // std::cout << l << " " << r << " " << mid << " " << ans << std::endl;
    // std::cout << "val: " << l_val << " " << m_val << " " << r_val <<
    // std::endl;
    if (m_val <= r_val && m_val <= l_val) {
      ans = m_val;
      break;
    }
    if (l_val <= m_val && m_val <= r_val) {
      r = mid;
      ans = l_val;
    } else {
      l = mid;
      ans = r_val;
    }
  }

  std::cout << ans << std::endl;

  return 0;
}
Problem p03309 (C++)

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;

int main() {
  int n, a[112345];

  cin >> n;
  REP(i, n) {
    cin >> a[i];
    a[i] -= i + 1;
  }

  sort(a, a + n);

  int b;

  if (n % 2)
    b = a[n / 2];
  else
    b = (a[n / 2 - 1] + a[n / 2]) / 2;

  ll res = 0;

  REP(i, n)
  res += abs(a[i] - b);

  cout << res << endl;

  return 0;
}

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;

int main() {
  int n, a[212345];

  cin >> n;
  REP(i, n) {
    cin >> a[i];
    a[i] -= i + 1;
  }

  sort(a, a + n);

  int b;

  if (n % 2)
    b = a[n / 2];
  else
    b = (a[n / 2 - 1] + a[n / 2]) / 2;

  ll res = 0;

  REP(i, n)
  res += abs(a[i] - b);

  cout << res << endl;

  return 0;
}
Problem p03309 (C++)

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

long long N, a[111], b, A;

int main() {
  scanf("%lld", &N);
  for (int i = 0; i < N; a[i] -= i, ++i)
    scanf("%lld", a + i);
  sort(a, a + N);
  b = a[N / 2];
  for (int i = 0; i < N; ++i)
    A += abs(a[i] - b);
  cout << A << endl;
}

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

long long N, a[202020], b, A;

int main() {
  scanf("%lld", &N);
  for (int i = 0; i < N; a[i] -= i, ++i)
    scanf("%lld", a + i);
  sort(a, a + N);
  b = a[N / 2];
  for (int i = 0; i < N; ++i)
    A += abs(a[i] - b);
  cout << A << endl;
}
Problem p03310 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

long diff(long l, long m, long r, vector<long> b) {
  long a = b[m] - b[l];
  long c = b[r] - b[m];
  return abs(a - c);
}

int main() {
  long N;
  cin >> N;
  vector<long> A(N + 1, 0);

  for (long i = 1; i <= N; ++i) {
    cin >> A[i];
  }
  for (long j = 1; j <= N - 1; ++j) {
    A[j + 1] += A[j];
  }

  long mindiff = INT_MAX;
  long l = 1;
  long r = 3;
  for (long m = 2; m <= N - 2; m++) {
    while (l <= m - 2 && diff(0, l, m, A) > diff(0, l + 1, m, A)) {
      l++;
    }
    while (r <= N - 2 && diff(m, r, N, A) > diff(m, r + 1, N, A)) {
      r++;
    }

    vector<long> p(4);
    p[0] = A[l] - A[0];
    p[1] = A[m] - A[l];
    p[2] = A[r] - A[m];
    p[3] = A[N] - A[r];
    long diff =
        *max_element(p.begin(), p.end()) - *min_element(p.begin(), p.end());
    mindiff = min(mindiff, diff);
  }
  cout << mindiff << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

long diff(long l, long m, long r, vector<long> &b) {
  long a = b[m] - b[l];
  long c = b[r] - b[m];
  return abs(a - c);
}

int main() {
  long N;
  cin >> N;
  vector<long> A(N + 1, 0);

  for (long i = 1; i <= N; ++i) {
    cin >> A[i];
  }
  for (long j = 1; j <= N - 1; ++j) {
    A[j + 1] += A[j];
  }

  long mindiff = INT_MAX;
  long l = 1;
  long r = 3;
  for (long m = 2; m <= N - 2; m++) {
    while (l <= m - 2 && diff(0, l, m, A) > diff(0, l + 1, m, A)) {
      l++;
    }
    while (r <= N - 2 && diff(m, r, N, A) > diff(m, r + 1, N, A)) {
      r++;
    }

    vector<long> p(4);
    p[0] = A[l] - A[0];
    p[1] = A[m] - A[l];
    p[2] = A[r] - A[m];
    p[3] = A[N] - A[r];
    long diff =
        *max_element(p.begin(), p.end()) - *min_element(p.begin(), p.end());
    mindiff = min(mindiff, diff);
  }
  cout << mindiff << endl;
  return 0;
}
Problem p03310 (C++)

Buggy submission (status: Runtime Error):
/*
g++ -std=c++11 -o2 -Wall ABC102D.cpp -o ./out/ABC102D.out
./out/ABC102D.out
*/

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

typedef long long ll;

int n;
int a[100005];
ll ss[100005];

int main() {
  //  freopen("./input/input.txt", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  ss[0] = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    ss[i + 1] = ss[i] + a[i];
  }
  ll ans = 10000000000000000;
  int lb, rb, mid;
  ll p, q, r, s;
  for (int m = 2; m <= n - 2; ++m) {
    int t1 = (int)(upper_bound(ss + 1, ss + m, ss[m] / 2) - ss);
    int t2 =
        (int)(upper_bound(ss + m + 1, ss + n, (ss[n] - ss[m]) / 2 + ss[m]) -
              ss);
    for (int i = t1 - 1; i <= t1 + 1; ++i)
      for (int j = t2 - 1; j <= t2 + 1; ++j)
        for (int k = m - 1; k <= m + 1; ++k) {
          lb = i;
          rb = j;
          mid = k;
          if (1 <= lb && lb < mid && mid < rb && rb <= n - 1) {
            //        cout << lb << " " << mid << " " << rb << "\n";
            p = ss[lb];
            q = ss[mid] - ss[lb];
            r = ss[rb] - ss[mid];
            s = ss[n] - ss[rb];
            ans = min(ans, max({p, q, r, s}) - min({p, q, r, s}));
          }
        }
    /*
    cout << m << " " << t1 << " " << t2 << " " << m1 << " " << m2 << "\n";
    cout << p << " "<< q << " "<< r << " "<< s << " "<< "\n";
    */
  }
  cout << ans << "\n";
}


Fixed submission:
/*
g++ -std=c++11 -o2 -Wall ABC102D.cpp -o ./out/ABC102D.out
./out/ABC102D.out
*/

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

typedef long long ll;

int n;
int a[200005];
ll ss[200005];

int main() {
  //  freopen("./input/input.txt", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  ss[0] = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    ss[i + 1] = ss[i] + a[i];
  }
  ll ans = 10000000000000000;
  int lb, rb, mid;
  ll p, q, r, s;
  for (int m = 2; m <= n - 2; ++m) {
    int t1 = (int)(upper_bound(ss + 1, ss + m, ss[m] / 2) - ss);
    int t2 =
        (int)(upper_bound(ss + m + 1, ss + n, (ss[n] - ss[m]) / 2 + ss[m]) -
              ss);
    for (int i = t1 - 1; i <= t1 + 1; ++i)
      for (int j = t2 - 1; j <= t2 + 1; ++j)
        for (int k = m - 1; k <= m + 1; ++k) {
          lb = i;
          rb = j;
          mid = k;
          if (1 <= lb && lb < mid && mid < rb && rb <= n - 1) {
            //        cout << lb << " " << mid << " " << rb << "\n";
            p = ss[lb];
            q = ss[mid] - ss[lb];
            r = ss[rb] - ss[mid];
            s = ss[n] - ss[rb];
            ans = min(ans, max({p, q, r, s}) - min({p, q, r, s}));
          }
        }
    /*
    cout << m << " " << t1 << " " << t2 << " " << m1 << " " << m2 << "\n";
    cout << p << " "<< q << " "<< r << " "<< s << " "<< "\n";
    */
  }
  cout << ans << "\n";
}
Problem p03310 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const long long INF = 1e18;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  for (int i = 1; i < N; ++i)
    A[i] += A[i - 1];
  long long ans = INF;
  for (int i = 1; i < N - 1; ++i) {
    int j_ = lower_bound(A.begin(), A.end(), A[i] / 2) - A.begin();
    int k_ = lower_bound(A.begin(), A.end(), (A[i] + A[N - 1]) / 2) - A.begin();
    // cerr << j_ << " " << i << " " << k_ << endl;
    for (int j = max(j_ - 1, 0); j < min(j_ + 2, i); ++j) {
      for (int k = max(i + 1, 0); k < min(k_ + 2, N - 1); ++k) {
        vector<long long> V = {A[j], A[i] - A[j], A[k] - A[i], A[N - 1] - A[k]};
        // cerr << j << " " << i << " " << k << " " << V[0] << " " << V[1] << "
        // " << V[2] << " " << V[3] << endl;
        sort(V.begin(), V.end());
        ans = min(ans, V[3] - V[0]);
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const long long INF = 1e18;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  for (int i = 1; i < N; ++i)
    A[i] += A[i - 1];
  long long ans = INF;
  for (int i = 1; i < N - 1; ++i) {
    int j_ = lower_bound(A.begin(), A.end(), A[i] / 2) - A.begin();
    int k_ = lower_bound(A.begin(), A.end(), (A[i] + A[N - 1]) / 2) - A.begin();
    // cerr << j_ << " " << i << " " << k_ << endl;
    for (int j = max(j_ - 1, 0); j < min(j_ + 2, i); ++j) {
      for (int k = max(i + 1, k_ - 1); k < min(k_ + 2, N - 1); ++k) {
        vector<long long> V = {A[j], A[i] - A[j], A[k] - A[i], A[N - 1] - A[k]};
        // cerr << j << " " << i << " " << k << " " << V[0] << " " << V[1] << "
        // " << V[2] << " " << V[3] << endl;
        sort(V.begin(), V.end());
        ans = min(ans, V[3] - V[0]);
      }
    }
  }
  cout << ans << endl;
}
Problem p03310 (Python)

Buggy submission (status: Runtime Error):
def solve(string):
    n, *a = map(int, string.split())
    l, c, r = 1, 2, 3
    sll, slr, srl, srr = sum(a[r:]), sum(a[c:r]), sum(a[l:c]), sum(a[:l])
    ans = max(sll, slr, srl, srr) - min(sll, slr, srl, srr)
    while c < n - 1:
        d = abs(sll - slr)
        dn = abs(sll + 2 * a[l] - slr)
        while dn < d:
            sll, slr = sll + a[l], slr - a[l]
            l += 1
            d = abs(sll - slr)
            dn = abs(sll + 2 * a[l] - slr)
        d = abs(srl - srr)
        dn = abs(srl + 2 * a[r] - srr)
        while dn < d:
            srl, srr = srl + a[r], srr - a[r]
            r += 1
            d = abs(srl - srr)
            dn = abs(srl + 2 * a[r] - srr)
        ans = min(ans, max(sll, slr, srl, srr) - min(sll, slr, srl, srr))
        slr += a[c]
        srl -= a[c]
        c += 1
    return str(ans)


if __name__ == "__main__":
    print(solve("\n".join([input(), input()])))


Fixed submission:
def solve(string):
    n, *a = map(int, string.split())
    l, c, r = 1, 2, 3
    sll, slr, srl, srr = sum(a[:l]), sum(a[l:c]), sum(a[c:r]), sum(a[r:])
    ans = max(sll, slr, srl, srr) - min(sll, slr, srl, srr)
    while c < n - 1:
        d = abs(sll - slr)
        dn = abs(sll + 2 * a[l] - slr)
        while dn < d:
            sll, slr = sll + a[l], slr - a[l]
            l += 1
            d = abs(sll - slr)
            dn = abs(sll + 2 * a[l] - slr)
        d = abs(srl - srr)
        dn = abs(srl + 2 * a[r] - srr)
        while dn < d:
            srl, srr = srl + a[r], srr - a[r]
            r += 1
            d = abs(srl - srr)
            dn = abs(srl + 2 * a[r] - srr)
        ans = min(ans, max(sll, slr, srl, srr) - min(sll, slr, srl, srr))
        slr += a[c]
        srl -= a[c]
        c += 1
    return str(ans)


if __name__ == "__main__":
    print(solve("\n".join([input(), input()])))
Problem p03311 (C++)

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

int main() {
  ll N;
  cin >> N;
  ll a[100010], b[100010];
  rep(i, 0, N) {
    cin >> a[i];
    b[i] = a[i] - i - 1;
  }
  sort(b, b + N);
  ll ans = 0;
  rep(i, 0, N) ans += abs(b[i] - b[N / 2]);
  cout << ans << endl;
}


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

int main() {
  ll N;
  cin >> N;
  ll a[200010], b[200010];
  rep(i, 0, N) {
    cin >> a[i];
    b[i] = a[i] - i - 1;
  }
  sort(b, b + N);
  ll ans = 0;
  rep(i, 0, N) ans += abs(b[i] - b[N / 2]);
  cout << ans << endl;
}
Problem p03311 (C++)

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

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

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

using ll = long long;
using R = long double;
const R EPS = 1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }
inline R sq(R x) { return sqrt(max(x, 0.0L)); }

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

// Problem Specific Parameter:
ll n;
const int limit = 100010;
ll a[limit];

int main(void) {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) a[i] -= i + 1;
  sort(a, a + n);

  ll ans = 0LL;
  rep(i, n) ans += abs(a[i] - a[n / 2]);
  cout << ans << endl;

  return 0;
}

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

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

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

using ll = long long;
using R = long double;
const R EPS = 1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R &r) { return (r > EPS) - (r < -EPS); }
inline R sq(R x) { return sqrt(max(x, 0.0L)); }

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

// Problem Specific Parameter:
ll n;
const int limit = 200010;
ll a[limit];

int main(void) {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) a[i] -= i + 1;
  sort(a, a + n);

  ll ans = 0LL;
  rep(i, n) ans += abs(a[i] - a[n / 2]);
  cout << ans << endl;

  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define fo(i, w, n) for (int i = (int)w; i < (int)n; i++)
#define qui queue<int>
#define vit vector<int>
#define ll long long
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define prq priority_queue
#define pii pair<int, int>
#define mp make_pair
#define sz size()
#define em empty()
#define ct continue
using namespace std;
ll n, c = 0, a[101001];
int main() {
  cin >> n;
  fo(i, 0, n) {
    cin >> a[i];
    a[i] -= i;
  }
  sort(a, a + n);
  fo(i, 0, n) c += abs(a[i] - a[(n - 1) / 2]);
  cout << c << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define fo(i, w, n) for (int i = (int)w; i < (int)n; i++)
#define qui queue<int>
#define vit vector<int>
#define ll long long
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define prq priority_queue
#define pii pair<int, int>
#define mp make_pair
#define sz size()
#define em empty()
#define ct continue
using namespace std;
ll n, c = 0, a[201001];
int main() {
  cin >> n;
  fo(i, 0, n) {
    cin >> a[i];
    a[i] -= i;
  }
  sort(a, a + n);
  fo(i, 0, n) c += abs(a[i] - a[(n - 1) / 2]);
  cout << c << endl;
  return 0;
}
Problem p03311 (C++)

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

ll A[100100], N, ans, M;

int main() {
  cin >> N;
  for (int i = 1; i <= N; ++i)
    cin >> A[i];
  for (int i = 1; i <= N; ++i)
    A[i] -= i;
  sort(A + 1, A + N + 1);
  M = A[(N + 1) / 2];
  for (int i = 1; i <= N; ++i)
    ans += abs(A[i] - M);
  cout << ans;
}

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

ll A[200100], N, ans, M;

int main() {
  cin >> N;
  for (int i = 1; i <= N; ++i)
    cin >> A[i];
  for (int i = 1; i <= N; ++i)
    A[i] -= i;
  sort(A + 1, A + N + 1);
  M = A[(N + 1) / 2];
  for (int i = 1; i <= N; ++i)
    ans += abs(A[i] - M);
  cout << ans;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <istream>
#include <map>
#include <math.h>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
long long n, a[100010], ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    a[i] = a[i] - i - 1;
  sort(a, a + n);
  if (n % 2 == 1)
    for (int i = 0; i < n; i++)
      ans += abs(a[i] - a[n / 2]);
  else
    for (int i = 0; i < n; i++)
      ans += abs(a[i] - (a[n / 2] + a[n / 2 - 1]) / 2);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <istream>
#include <map>
#include <math.h>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
long long n, a[1000000], ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    a[i] = a[i] - i - 1;
  sort(a, a + n);
  if (n % 2 == 1)
    for (int i = 0; i < n; i++)
      ans += abs(a[i] - a[n / 2]);
  else
    for (int i = 0; i < n; i++)
      ans += abs(a[i] - (a[n / 2] + a[n / 2 - 1]) / 2);
  cout << ans << endl;
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int64_t N, ans;
vector<int64_t> A(20'000);

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

  vector<int64_t> B(N);
  for (int i = 0; i < N; i++) {
    B[i] = A[i] - (i + 1);
  }

  sort(B.begin(), B.end());
  int64_t midd = B[N / 2];

  ans = 0;
  for (int i = 0; i < N; i++) {
    ans += abs(A[i] - (midd + (i + 1)));
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int64_t N, ans;
vector<int64_t> A(200'000);

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

  vector<int64_t> B(N);
  for (int i = 0; i < N; i++) {
    B[i] = A[i] - (i + 1);
  }

  sort(B.begin(), B.end());
  int64_t midd = B[N / 2];

  ans = 0;
  for (int i = 0; i < N; i++) {
    ans += abs(A[i] - (midd + (i + 1)));
  }

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

Buggy submission (status: Runtime Error):
#pragma GCC optimize "03"
#include "bits/stdc++.h"
using namespace std;

#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif

const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e18 + 9;

int n, a[N];

int f(int x) {
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += abs(x + i - a[i]);
  return ans;
}

signed main() {
  IOS;
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int l = -inf, h = inf;
  while (l + 1 < h) {
    int md = (l + h) >> 1;
    if (f(md) < f(md + 1))
      h = md;
    else
      l = md;
  }
  cout << min(f(l), f(h));
  return 0;
}

Fixed submission:
#pragma GCC optimize "03"
#include "bits/stdc++.h"
using namespace std;

#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif

const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e18 + 9;

int n, a[N];

int f(int x) {
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += abs(x + i - a[i]);
  return ans;
}

signed main() {
  IOS;
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int l = -inf, h = inf;
  while (l + 1 < h) {
    int md = (l + h) >> 1;
    if (f(md) < f(md + 1))
      h = md;
    else
      l = md;
  }
  cout << min(f(l), f(h));
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long LL;
int a[100005], n;
LL sum = 0;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i), a[i] -= i;
  sort(a + 1, a + 1 + n);
  int hh = a[(n + 1) / 2];
  for (int i = 1; i <= n; i++)
    sum += abs(a[i] - hh);
  printf("%lld", sum);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long LL;
int a[200005], n;
LL sum = 0;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i), a[i] -= i;
  sort(a + 1, a + 1 + n);
  int hh = a[(n + 1) / 2];
  for (int i = 1; i <= n; i++)
    sum += abs(a[i] - hh);
  printf("%lld", sum);
}
Problem p03311 (C++)

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

int n, eye, med, ans = 0;
int mylist[100001];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> eye;
    mylist[i] = eye - i;
  }
  sort(mylist + 1, mylist + n + 1);
  med = mylist[(n + 1) / 2];
  for (int i = 1; i <= n; i++)
    ans += abs(mylist[i] - med);
  cout << ans;
}

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

long long n, eye, med, ans = 0;
long long mylist[200001];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> eye;
    mylist[i] = eye - i;
  }
  sort(mylist + 1, mylist + n + 1);
  med = mylist[(n + 1) / 2];
  for (int i = 1; i <= n; i++)
    ans += abs(mylist[i] - med);
  cout << ans;
}
Problem p03311 (C++)

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

using namespace std;

using ll = long long;

const int N = 1e5 + 5;

int arr[N];
int n;

ll get_min(int b) {
  ll res = 0;
  for (int i = 0; i < n; ++i) {
    res += abs(arr[i] - b);
  }
  return res;
}

int solve() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &arr[i]);
    arr[i] -= i + 1;
  }
  int l = -1e9, r = 1e9;
  while (r - l > 2) {
    int ml = l + (r - l) / 3;
    int mr = r - (r - l) / 3;
    if (get_min(ml) < get_min(mr)) {
      r = mr;
    } else {
      l = ml;
    }
  }
  cout << min(get_min(l + 1), min(get_min(l), get_min(r))) << endl;

  return 0;
}

int main() {
#ifdef LOCALHOST
  freopen("input", "r", stdin);
#endif
  while (solve())
    ;

  return 0;
}


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

using namespace std;

using ll = long long;

const int N = 2e5 + 5;

int arr[N];
int n;

ll get_min(int b) {
  ll res = 0;
  for (int i = 0; i < n; ++i) {
    res += abs(arr[i] - b);
  }
  return res;
}

int solve() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &arr[i]);
    arr[i] -= i + 1;
  }
  int l = -1e9, r = 1e9;
  while (r - l > 2) {
    int ml = l + (r - l) / 3;
    int mr = r - (r - l) / 3;
    if (get_min(ml) < get_min(mr)) {
      r = mr;
    } else {
      l = ml;
    }
  }
  cout << min(get_min(l + 1), min(get_min(l), get_min(r))) << endl;

  return 0;
}

int main() {
#ifdef LOCALHOST
  freopen("input", "r", stdin);
#endif
  while (solve())
    ;

  return 0;
}
Problem p03311 (C++)

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

using namespace std;
typedef long long LL;

#define MAXN 100000
#define MOD 998244353
#define mp make_pair

int N;
int A[MAXN];

int main(int argc, char **argv) {
#ifdef OJ
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);

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

  sort(A, A + N);
  int idx = N / 2;
  if (N % 2 == 0)
    idx--;

  LL ans = 0;
  for (int i = 0; i < N; i++) {
    ans += abs(A[idx] - A[i]);
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;
typedef long long LL;

#define MAXN 200000
#define MOD 998244353
#define mp make_pair

int N;
int A[MAXN];

int main(int argc, char **argv) {
#ifdef OJ
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);

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

  sort(A, A + N);
  int idx = N / 2;
  if (N % 2 == 0)
    idx--;

  LL ans = 0;
  for (int i = 0; i < N; i++) {
    ans += abs(A[idx] - A[i]);
  }

  cout << ans << endl;

  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using edge = struct {
  int to;
  int cost;
};
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, n, m) for (ll i = (n); i < (m); i++)
#define ALL(a) (a).begin(), (a).end()
#define MAX(vec) *std::max_element(vec.begin(), vec.end())
#define MIN(vec) *std::min_element(vec.begin(), vec.end())
#define MAXI(vec)                                                              \
  std::distance(vec.begin(), *std::max_element(vec.begin(), vec.end()))
#define MINI(vec)                                                              \
  std::distance(vec.begin(), *std::min_element(vec.begin(), vec.end()))

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

template <typename T, typename U> U READ(ll V, T a, U b) {
  REP(i, V) {
    cin >> a;
    b.push_back(a);
  }
  return b;
}

int main() {
  ll N = 0;
  vector<ll> A;
  ll sum = 0;
  ll po = 0;
  cin >> N;
  REP(i, N) {
    cin >> po;
    po -= i + 1;
    A.push_back(po);
    sum += po;
  }
  ll ave = sum / N;
  vector<ll> ans;
  FOR(j, MIN(A), MAX(A) + 1) {
    ans.push_back(0);
    REP(i, N) { ans[j - MIN(A)] += abs(A[i] - j); }
  }
  cout << MIN(ans) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using edge = struct {
  int to;
  int cost;
};
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, n, m) for (ll i = (n); i < (m); i++)
#define ALL(a) (a).begin(), (a).end()
#define MAX(vec) *std::max_element(vec.begin(), vec.end())
#define MIN(vec) *std::min_element(vec.begin(), vec.end())
#define MAXI(vec)                                                              \
  std::distance(vec.begin(), *std::max_element(vec.begin(), vec.end()))
#define MINI(vec)                                                              \
  std::distance(vec.begin(), *std::min_element(vec.begin(), vec.end()))

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

template <typename T, typename U> U READ(ll V, T a, U b) {
  REP(i, V) {
    cin >> a;
    b.push_back(a);
  }
  return b;
}

int main() {
  ll N = 0;
  vector<ll> A;
  ll sum = 0;
  ll po = 0;
  cin >> N;
  REP(i, N) {
    cin >> po;
    po -= i + 1;
    A.push_back(po);
    sum += po;
  }
  ll ave = sum / N;
  ll ans1 = 0, ans2 = 0;
  sort(ALL(A));
  REP(i, N) { ans1 += abs(A[i] - A[A.size() / 2]); }
  REP(i, N) { ans2 += abs(A[i] - A[1 + A.size() / 2]); }

  cout << min(ans1, ans2) << endl;
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
typedef long long ll;
const int N = 100005;
int n;
ll a[N];
using namespace std;

int main() {
#ifdef Kay
  freopen("code.in", "r", stdin);
  freopen("code.out", "w", stdout);
#endif
  cin >> n;
  int i;
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]), a[i] -= i;
  sort(a + 1, a + 1 + n);
  ll ans = 0, val = a[n / 2 + 1];
  for (i = 1; i <= n; i++)
    if (a[i] < val)
      ans += val - a[i];
    else
      ans += a[i] - val;
  printf("%lld", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
typedef long long ll;
const int N = 200005;
int n;
ll a[N];
using namespace std;

int main() {
#ifdef Kay
  freopen("code.in", "r", stdin);
  freopen("code.out", "w", stdout);
#endif
  cin >> n;
  int i;
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]), a[i] -= i;
  sort(a + 1, a + 1 + n);
  ll ans = 0, val = a[n / 2 + 1];
  for (i = 1; i <= n; i++)
    if (a[i] < val)
      ans += val - a[i];
    else
      ans += a[i] - val;
  printf("%lld", ans);
  return 0;
}
Problem p03311 (C++)

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

using namespace std;

int main() {
  map<int, int> mp;
  long long int n, m = 0, tmp = 0, ax = 0, k = 0, t[5000], p[5000], x[5000],
                   res[5000];
  string str, str2[5000];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    x[i] -= i;
  }
  sort(x + 1, x + n + 1);
  tmp = n / 2;
  for (int i = 1; i <= n; i++) {
    m += abs(x[i] - x[tmp - 1]);
    ax += abs(x[i] - x[tmp]);
    k += abs(x[i] - x[tmp + 1]);
  }
  cout << min(m, min(ax, k));
}

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

using namespace std;

int main() {
  map<int, int> mp;
  long long int n, m = 0, tmp = 0, ax = 0, k = 0, t[5000], p[5000], x[500000],
                   res[5000];
  string str, str2[5000];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    x[i] -= i;
  }
  sort(x + 1, x + n + 1);
  tmp = n / 2;
  for (int i = 1; i <= n; i++) {
    m += abs(x[i] - x[tmp - 1]);
    ax += abs(x[i] - x[tmp]);
    k += abs(x[i] - x[tmp + 1]);
  }
  cout << min(m, min(ax, k));
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;
ll n, a[maxn];
ll f(ll x) {
  ll sum = 0;
  for (int i = 1; i <= n; i++)
    sum += abs(a[i] - (x + i));
  return sum;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll l = -1e9, r = 1e9, ans = -1;
  while (l < r - 1) {
    ll mid = (l + r) >> 1;
    ll mmid = (r + mid) >> 1;
    if (f(mid) < f(mmid))
      r = mmid;
    else
      l = mid;
  }
  ans = f(l) > f(r) ? r : l;
  cout << f(ans) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
typedef long long ll;
ll n, a[maxn];
ll f(ll x) {
  ll sum = 0;
  for (int i = 1; i <= n; i++)
    sum += abs(a[i] - (x + i));
  return sum;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll l = -1e9, r = 1e9, ans = -1;
  while (l < r - 1) {
    ll mid = (l + r) >> 1;
    ll mmid = (r + mid) >> 1;
    if (f(mid) < f(mmid))
      r = mmid;
    else
      l = mid;
  }
  ans = f(l) > f(r) ? r : l;
  cout << f(ans) << endl;
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll << (x))
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int P = 1e9 + 7;
inline int add(int a, int b) {
  if ((a += b) >= P)
    a -= P;
  return a;
}
inline int sub(int a, int b) {
  if ((a -= b) < 0)
    a += P;
  return a;
}
inline int mul(int a, int b) { return 1ll * a * b % P; }
inline int kpow(int a, int b) {
  int r = 1;
  for (; b; b >>= 1, a = mul(a, a)) {
    if (b & 1)
      r = mul(r, a);
  }
  return r;
}
//----
const int N = 101010;
int n;
int a[N], b[N];
int main() {
  scanf("%d", &n);
  rep(i, 1, n + 1) scanf("%d", a + i);
  rep(i, 1, n + 1) b[i] = a[i] - i;
  sort(b + 1, b + n + 1);
  ll ans = 1e18, l = 0, r = 0;
  rep(i, 1, n + 1) r += b[i];
  rep(i, 1, n + 1) {
    r -= b[i];
    l += b[i];
    ll t = 1ll * b[i] * i - l + r - 1ll * b[i] * (n - i);
    ans = min(ans, t);
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll << (x))
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int P = 1e9 + 7;
inline int add(int a, int b) {
  if ((a += b) >= P)
    a -= P;
  return a;
}
inline int sub(int a, int b) {
  if ((a -= b) < 0)
    a += P;
  return a;
}
inline int mul(int a, int b) { return 1ll * a * b % P; }
inline int kpow(int a, int b) {
  int r = 1;
  for (; b; b >>= 1, a = mul(a, a)) {
    if (b & 1)
      r = mul(r, a);
  }
  return r;
}
//----
const int N = 201010;
int n;
int a[N], b[N];
int main() {
  scanf("%d", &n);
  rep(i, 1, n + 1) scanf("%d", a + i);
  rep(i, 1, n + 1) b[i] = a[i] - i;
  sort(b + 1, b + n + 1);
  ll ans = 1e18, l = 0, r = 0;
  rep(i, 1, n + 1) r += b[i];
  rep(i, 1, n + 1) {
    r -= b[i];
    l += b[i];
    ll t = 1ll * b[i] * i - l + r - 1ll * b[i] * (n - i);
    ans = min(ans, t);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 100005;

int a[N], n, num;
ll ans = 0;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i), a[i] -= i;
  sort(a + 1, a + n + 1), num = a[(n + 1) >> 1];
  for (int i = 1; i <= n; i++)
    ans += (ll)abs(num - a[i]);
  cout << ans << endl;
  return 0;
}

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

int a[N], n, num;
ll ans = 0;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", a + i), a[i] -= i;
  sort(a + 1, a + n + 1), num = a[(n + 1) >> 1];
  for (int i = 1; i <= n; i++)
    ans += (ll)abs(num - a[i]);
  cout << ans << endl;
  return 0;
}
Problem p03311 (C++)

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

int n;
std::vector<long> a;

long calc(long b) {
  long ret = 0;
  for (int i = 0; i < n; i++) {
    ret += llabs(a[i] - (b + (long)i + 1));
  }

  return ret;
}

long binary() {
  long ok = -1e9, ng = 1e9;

  for (int i = 0; i < 100; i++) {
    long A = (ok + ok + ng) / 3;
    long B = (ok + ng + ng) / 3;

    if (calc(A) < calc(B)) {
      ng = B;
    } else {
      ok = A;
    }
  }

  long mi = 1e18;
  for (int i = -1e5; i < 1e5; i++) {
    mi = std::min(mi, calc(ok + i));
  }

  return mi;
}

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

  cout << binary() << endl;
  return 0;
}


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

int n;
std::vector<long> a;

long calc(long b) {
  long ret = 0;
  for (int i = 0; i < n; i++) {
    ret += llabs(a[i] - (b + (long)i + 1));
  }

  return ret;
}

long binary() {
  long ok = -1e9, ng = 1e9;

  for (int i = 0; i < 100; i++) {
    long A = (ok + ok + ng) / 3;
    long B = (ok + ng + ng) / 3;

    if (calc(A) < calc(B)) {
      ng = B;
    } else {
      ok = A;
    }
  }

  long mi = 1e18;
  for (int i = -1e2; i < 1e2; i++) {
    mi = std::min(mi, calc(ok + i));
  }

  return mi;
}

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

  cout << binary() << endl;
  return 0;
}
Problem p03311 (C++)

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

using namespace std;

#define pq priority_queue
#define MZ 1000000007

long long num[100005];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &num[i]);
    num[i] -= i;
  }
  sort(num + 1, num + 1 + n);
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += abs(num[i] - num[(n + 1) / 2]);
  printf("%lld\n", ans);
  return 0;
}


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

using namespace std;

#define pq priority_queue
#define MZ 1000000007

long long num[200005];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &num[i]);
    num[i] -= i;
  }
  sort(num + 1, num + 1 + n);
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += abs(num[i] - num[(n + 1) / 2]);
  printf("%lld\n", ans);
  return 0;
}