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

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

using namespace std;

void func() {
  uint32_t N, maxAi(0), minAi(UINT32_MAX);
  cin >> N;
  for (size_t i = 0; i < N; ++i) {
    uint32_t Ai;
    cin >> Ai;
    maxAi = max(Ai, maxAi);
    minAi = min(Ai, minAi);
  }
  cout << maxAi - minAi << endl;
}

int main() {

  while (1)
    func();
  return 0;
}


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

using namespace std;

void func() {
  uint32_t N, maxAi(0), minAi(UINT32_MAX);
  cin >> N;
  for (size_t i = 0; i < N; ++i) {
    uint32_t Ai;
    cin >> Ai;
    maxAi = max(Ai, maxAi);
    minAi = min(Ai, minAi);
  }
  cout << maxAi - minAi << endl;
}

int main() {

  //	while(1)
  func();
  return 0;
}
Problem p03308 (C++)

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

int main() {
  int N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());

  cout << abs(A[N - 1] - A[0]) << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());

  cout << abs(A[N - 1] - A[0]) << endl;
}
Problem p03308 (C++)

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

int main() {
  int N;
  vector<long int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  sort(vec.begin(), vec.end());
  long int a = vec.at(0);
  reverse(vec.begin(), vec.end());
  long int b = vec.at(0);
  cout << b - a << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<long int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  sort(vec.begin(), vec.end());
  long int a = vec.at(0);
  reverse(vec.begin(), vec.end());
  long int b = vec.at(0);
  cout << b - a << endl;
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
#define int long long
int a[115614];
signed main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= i + 1;
  }
  sort(a, a + n);
  int ans1 = 0, ans2 = 0;
  for (int i = 0; i < n; i++)
    ans1 += abs(a[n / 2] - a[i]);
  for (int i = 0; i < n; i++)
    ans2 += abs(a[n / 2 - 1] - a[i]);
  cout << min(ans2, ans1) << endl;
  cin >> n;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
#define int long long
int a[215614];
signed main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= i + 1;
  }
  sort(a, a + n);
  int ans1 = 0, ans2 = 0;
  for (int i = 0; i < n; i++)
    ans1 += abs(a[n / 2] - a[i]);
  for (int i = 0; i < n; i++)
    ans2 += abs(a[n / 2 - 1] - a[i]);
  cout << min(ans2, ans1) << endl;
  cin >> n;
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MAX_N 100000
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;

  int A[MAX_N];
  int B[MAX_N];
  rep(i, N) {
    cin >> A[i];
    B[i] = A[i] - (i + 1);
  }
  sort(B, B + N);

  int b = B[N / 2];
  int64_t Ans = 0;
  rep(i, N) { Ans += abs(B[i] - b); }
  cout << Ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MAX_N 200000
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;

  int A[MAX_N];
  int B[MAX_N];
  rep(i, N) {
    cin >> A[i];
    B[i] = A[i] - (i + 1);
  }
  sort(B, B + N);

  int b = B[N / 2];
  int64_t Ans = 0;
  rep(i, N) { Ans += abs(B[i] - b); }
  cout << Ans << endl;
  return 0;
}
Problem p03309 (C++)

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

long long s = 0;
int n;
int a[200000];
int cmp(const void *a, const void *b) {
  int x = *(int *)a;
  int y = *(int *)b;
  if (x > y) {
    return 1;
  } else {
    return -1;
  }
}
int main() {
  int i;
  long long b;
  cin >> n;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[i] = a[i] - i;
  }
  qsort(a + 1, n, sizeof(a[1]), cmp);
  if (n % 2 == 0) {
    b = a[n / 2];
  } else {
    b = a[(n + 1) / 2];
  }
  for (i = 1; i <= n; i++) {
    a[i] -= b;
    if (a[i] > 0) {
      s += a[i];
    } else {
      s -= a[i];
    }
  }
  cout << s << endl;
  return 0;
}


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

long long s = 0;
int n;
int a[200005];
int cmp(const void *a, const void *b) {
  int x = *(int *)a;
  int y = *(int *)b;
  if (x > y) {
    return 1;
  } else {
    return -1;
  }
}
int main() {
  int i;
  long long b;
  cin >> n;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[i] = a[i] - i;
  }
  qsort(a + 1, n, sizeof(a[1]), cmp);
  if (n % 2 == 0) {
    b = a[n / 2];
  } else {
    b = a[(n + 1) / 2];
  }
  for (i = 1; i <= n; i++) {
    a[i] -= b;
    if (a[i] > 0) {
      s += a[i];
    } else {
      s -= a[i];
    }
  }
  cout << s << endl;
  return 0;
}
Problem p03309 (C++)

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

int main() {
  int n;
  long long a[20000], sum{};

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

  std::sort(a, a + n);

  for (int i = 0; i < n; ++i) {
    sum += std::abs(a[i] - a[n / 2]);
  }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>

int main() {
  int n;
  long long a[200000], sum{};

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

  std::sort(a, a + n);

  for (int i = 0; i < n; ++i) {
    sum += std::abs(a[i] - a[n / 2]);
  }

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

  return 0;
}
Problem p03309 (C++)

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

  return 0;
}

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

  return 0;
}
Problem p03309 (C++)

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

using namespace std;

int n;
int a[20010];
long b[20010];

long calc(int b) {
  long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += abs(a[i] - (b + i + 1));
  }
  return ans;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i] - (i + 1);
  }

  sort(b, b + n, greater<>());

  int median = b[n / 2];
  long answer[3] = {calc(median + 1), calc(median), calc(median - 1)};

  cout << min(min(answer[0], answer[1]), answer[2]) << endl;

  return 0;
}

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

using namespace std;

int n;
int a[200010];
long b[200010];

long calc(int b) {
  long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += abs(a[i] - (b + i + 1));
  }
  return ans;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i] - (i + 1);
  }

  sort(b, b + n, greater<>());

  int median = b[n / 2];
  long answer[3] = {calc(median + 1), calc(median), calc(median - 1)};

  cout << min(min(answer[0], answer[1]), answer[2]) << endl;

  return 0;
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(0, i, N) {
    int a;
    cin >> a;
    A.at(i) = a - i + 1;
  }
  sort(all(A));
  ll ans = 0;
  ll ans1 = 0, ans2 = 0;
  if (N % 2 == 0) {
    rep(0, i, N) { ans += abs(A.at(i) - A.at(N / 2)); }
  } else {
    rep(0, i, N) {
      ans1 += abs(A.at(i) - A.at(N / 2));
      ans2 += abs(A.at(i) - A.at(N / 2 + 1));
    }
    ans = min(ans1, ans2);
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(0, i, N) {
    int a;
    cin >> a;
    A.at(i) = a - i + 1;
  }
  sort(all(A));
  ll ans = 0;
  ll ans1 = 0, ans2 = 0;
  if (N % 2 == 0 || N == 1) {
    rep(0, i, N) { ans += abs(A.at(i) - A.at(N / 2)); }
  } else {
    rep(0, i, N) {
      ans1 += abs(A.at(i) - A.at(N / 2));
      ans2 += abs(A.at(i) - A.at(N / 2 + 1));
    }
    ans = min(ans1, ans2);
  }

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

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORR(i, a, b) for (int i = (a); i >= (b); --i)
#define REPR(i, n) FORR(i, n, 0)

using namespace std;

int main() {
  ll N, b, c;
  vector<ll> A;
  scanf("%lld", &N);
  FOR(i, 1, N + 1) {
    scanf(" %lld", &c);
    A.push_back(c - i);
  }
  sort(A.begin(), A.end());
  b = A.at((N + 1) / 2);
  ll ans = 0;
  FOR(i, 0, N) { ans += abs(A.at(i) - b); }
  printf("%lld\n", ans);

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORR(i, a, b) for (int i = (a); i >= (b); --i)
#define REPR(i, n) FORR(i, n, 0)

using namespace std;

int main() {
  ll N, b, c;
  vector<ll> A;
  scanf("%lld", &N);
  FOR(i, 1, N + 1) {
    scanf(" %lld", &c);
    A.push_back(c - i);
  }
  sort(A.begin(), A.end());
  b = A.at((N + 1) / 2 - 1);
  ll ans = 0;
  FOR(i, 0, N) { ans += abs(A.at(i) - b); }
  printf("%lld\n", ans);

  return 0;
}
Problem p03309 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long

ll sad(ll b, vector<ll> *B) {
  ll res = 0;
  for (int i = 0; i < B->size(); i++)
    res += abs((*B)[i] - b);

  return res;
}

int main() {
  long long int N;
  scanf("%lld", &N);

  ll sum = 0;
  vector<ll> B(N);

  scanf("%lld", &B[0]);
  B[0] -= 1;
  ll maximam = B[0];
  ll minimam = B[0];

  for (ll i = 1; i < N; i++) {
    ll A;
    scanf("%lld", &A);
    B[i] = A - (i + 1);
    sum += B[i];
    maximam = max(maximam, B[i]);
    minimam = min(minimam, B[i]);
  }

  ll minsad = sad(minimam, &B);
  for (ll i = minimam + 1; i <= maximam; i++) {
    minsad = min(minsad, sad(i, &B));
  }

  printf("%lld", minsad);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long

ll sad(ll b, vector<ll> *B) {
  ll res = 0;
  for (int i = 0; i < B->size(); i++)
    res += abs((*B)[i] - b);

  return res;
}

int main() {
  long long int N;
  scanf("%lld", &N);

  ll sum = 0;
  vector<ll> B(N);

  scanf("%lld", &B[0]);
  B[0] -= 1;
  ll maximam = B[0];
  ll minimam = B[0];

  for (ll i = 1; i < N; i++) {
    ll A;
    scanf("%lld", &A);
    B[i] = A - (i + 1);
    sum += B[i];
    maximam = max(maximam, B[i]);
    minimam = min(minimam, B[i]);
  }

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

  ll sad1 = sad(B[N / 2 - 1], &B);
  ll sad2 = sad(B[N / 2], &B);
  ll minsad = min(sad1, sad2);

  printf("%lld", minsad);
}
Problem p03309 (C++)

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

typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i] -= (1 + i);
  }
  sort(a.begin(), a.end());
  ll ans = 1000000000000;
  for (int i = a[0]; i <= a[n - 1]; i++) {
    ll x = 0;
    rep(j, n) { x += abs(a[j] - i); }
    ans = min(ans, x);
  }
  cout << ans << endl;
  return 0;
}


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

typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    a[i] -= (1 + i);
  }
  sort(a.begin(), a.end());
  ll ans;

  rep(j, n) { ans += abs(a[j] - a[n / 2]); }

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

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

using namespace std;

long long inf = 1000000007;

long long solve(vector<long long> num, long long mid) {
  long long ret = 0;

  for (int i = 0; i < (int)num.size(); i++) {
    ret += abs(num.at(i) - mid);
  }

  return ret;
}

int main() {
  int n;
  cin >> n;
  vector<long long> num(n);

  for (int i = 0; i < n; i++) {
    cin >> num.at(i);
    num.at(i) -= i + 1;
  }

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

  cout << min(solve(num, num.at(n / 2)), solve(num, num.at(n / 2 - 1))) << endl;

  return 0;
}


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

using namespace std;

long long inf = 1000000007;

long long solve(vector<long long> num, long long mid) {
  long long ret = 0;

  for (int i = 0; i < (int)num.size(); i++) {
    ret += abs(num.at(i) - mid);
  }

  return ret;
}

int main() {
  int n;
  cin >> n;
  vector<long long> num(n);

  for (int i = 0; i < n; i++) {
    cin >> num.at(i);
    num.at(i) -= i + 1;
  }

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

  cout << min(solve(num, num.at(n / 2)), solve(num, num.at(max(n / 2 - 1, 0))))
       << endl;

  return 0;
}
Problem p03309 (C++)

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

int main(void) {
  int N = 0;
  long long int A[100] = {0};
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] -= i;
  }
  sort(A, A + N);
  long int mid = A[N / 2];
  long int min = 0;
  for (int d = mid - 100; d <= mid + 100; d++) {
    long int v = 0;
    for (int i = 0; i < N; i++) {
      v += abs(A[i] - d);
    }
    if (d == mid - 100)
      min = v;
    else if (v < min)
      min = v;
  }
  cout << min << endl;
  return 0;
}


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

int main(void) {
  int N = 0;
  long long int A[200000] = {0};
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] -= i;
  }
  sort(A, A + N);
  long int mid = A[N / 2];
  long int min = 0;
  for (int d = mid - 100; d <= mid + 100; d++) {
    long int v = 0;
    for (int i = 0; i < N; i++) {
      v += abs(A[i] - d);
    }
    if (d == mid - 100)
      min = v;
    else if (v < min)
      min = v;
  }
  cout << min << endl;
  return 0;
}
Problem p03309 (C++)

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

int main() {
  int N;
  long long a[20001];

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

  sort(a, a + N);

  long long mid = a[N / 2];
  long long ans = 0;

  for (int i = 0; i < N; i++) {
    ans += abs(mid - a[i]);
  }

  cout << ans << endl;

  return 0;
}

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

int main() {
  int N;
  long long a[200010];

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

  sort(a, a + N);

  long long mid = a[N / 2];
  long long ans = 0;

  for (int i = 0; i < N; i++) {
    ans += abs(mid - a[i]);
  }

  cout << ans << endl;

  return 0;
}
Problem p03309 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define int long long
using namespace std;
typedef vector<int> vint;
typedef pair<int, int> pint;

signed main() {
  int N;
  cin >> N;
  vint a(N);
  REP(i, N) {
    cin >> a[i];
    a[i] -= (i + 1);
  }
  sort(ALL(a));

  int ans = 1e18;
  REP(i, N) {
    int sum = 0;
    REP(j, N) sum += abs(a[j] - a[i]);
    ans = min(ans, sum);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define int long long
using namespace std;
typedef vector<int> vint;
typedef pair<int, int> pint;

signed main() {
  int N;
  cin >> N;
  vint a(N);
  REP(i, N) {
    cin >> a[i];
    a[i] -= (i + 1);
  }
  sort(ALL(a));

  int ans = 0;
  REP(i, N) { ans += abs(a[i] - a[N / 2]); }
  cout << ans << endl;
}
Problem p03309 (C++)

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

vector<int> v;

long eval(long b) {
  long sum = 0;
  for (int i = 0; i < v.size(); i++)
    sum += abs(v[i] - b);
  return sum;
}

int main() {
  long n, m, t = 0;
  cin >> n;
  v.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    v[i] -= i + 1;
  }
  sort(v.begin(), v.end());
  t = v[n / 2 + (n % 2)];

  while (true) {
    long a = eval(t), am = eval(t - 1), ap = eval(t + 1);
    if (am >= a && a <= ap) {
      cout << a << endl;
      return 0;
    } else if (a >= am) {
      t--;
    } else if (a >= ap) {
      t++;
    }
  }
}


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

vector<int> v;

long eval(long b) {
  long sum = 0;
  for (int i = 0; i < v.size(); i++)
    sum += abs(v[i] - b);
  return sum;
}

int main() {
  long n, m, t = 0;
  cin >> n;
  v.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    v[i] -= i + 1;
  }
  sort(v.begin(), v.end());
  t = v[n / 2];

  while (true) {
    long a = eval(t), am = eval(t - 1), ap = eval(t + 1);
    if (am >= a && a <= ap) {
      cout << a << endl;
      return 0;
    } else if (a >= am) {
      t--;
    } else if (a >= ap) {
      t++;
    }
  }
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
int main() {
  ll n, a, b[114514];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    b[i] = a - i - 1;
  }
  sort(b, b + n);
  ll ans = 0;
  if (n == 1)
    ans = 0;
  else if (n % 2) {
    ll x = n / 2;
    ll num1 = 0, num2 = 0;
    for (int i = 0; i < n; i++)
      num1 += abs(b[i] - b[x]);
    x++;
    for (int i = 0; i < n; i++)
      num2 += abs(b[i] - b[x]);
    ans = min(num1, num2);
  } else {
    ll x = n / 2;
    for (int i = 0; i < n; i++)
      ans += abs(b[i] - b[x]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
int main() {
  ll n, a, b[214514];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    b[i] = a - i - 1;
  }
  sort(b, b + n);
  ll ans = 0;
  if (n == 1)
    ans = 0;
  else if (n % 2) {
    ll x = n / 2;
    ll num1 = 0, num2 = 0;
    for (int i = 0; i < n; i++)
      num1 += abs(b[i] - b[x]);
    x++;
    for (int i = 0; i < n; i++)
      num2 += abs(b[i] - b[x]);
    ans = min(num1, num2);
  } else {
    ll x = n / 2;
    for (int i = 0; i < n; i++)
      ans += abs(b[i] - b[x]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03309 (C++)

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

using namespace std;

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

  sort(a, a + n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += abs(a[i] - a[n / 2]);
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

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

  sort(a, a + n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += abs(a[i] - a[n / 2]);
  }
  cout << ans << endl;

  return 0;
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
unsigned long long sum = 0;
int n, a[100001], k;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= i;
  }
  sort(a + 1, a + n + 1);
  if (n % 2)
    k = a[n / 2 + 1];
  else
    k = ((a[n / 2] + a[n / 2 + 1]) / 2 + 0.5);
  for (int i = 1; i <= n; i++)
    sum += abs(a[i] - k);
  cout << sum;
  return 0;
} // https://abc102.contest.atcoder.jp/submissions/2783177

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
unsigned long long sum = 0;
int n, a[300001], k;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= i;
  }
  sort(a + 1, a + n + 1);
  if (n % 2)
    k = a[n / 2 + 1];
  else
    k = ((a[n / 2] + a[n / 2 + 1]) / 2 + 0.5);
  for (int i = 1; i <= n; i++)
    sum += abs(a[i] - k);
  cout << sum;
  return 0;
} // https://abc102.contest.atcoder.jp/submissions/2783177
Problem p03309 (C++)

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

int main() {
  int N;
  cin >> N;
  int A[N + 1];
  A[0] = -N;
  for (int i = 1; i < N + 1; i++) {
    cin >> A[i];
    A[i] = A[i] - i;
  }
  sort(A, A + N + 1);
  long long ans;
  long long sum;
  ans = 100000000000000;
  for (int j = 1; j < N + 1; j++) {
    sum = 0;
    for (int i = 1; i < N + 1; i++) {
      sum = sum + abs(A[i] - A[j]);
    }
    if (ans >= sum) {
      ans = sum;
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  int A[N + 1];
  A[0] = -N;
  for (int i = 1; i < N + 1; i++) {
    cin >> A[i];
    A[i] = A[i] - i;
  }
  sort(A, A + N + 1);
  long long ans;
  long long sum;
  sum = 0;
  for (int i = 1; i < N + 1; i++) {
    ans = ans + abs(A[i] - A[N / 2 + 1]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03309 (C++)

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

int main() {
  long int N, x, y, z, ans;
  cin >> N;
  ans = 0;
  vector<long int> a(N);
  for (long int i = 0; i < N; i++) {
    cin >> y;
    a.at(i) = y - i - 1;
  }
  sort(a.begin(), a.end());
  z = (N / 2) - 1;
  x = a.at(z);
  for (long int k = 0; k < N; k++) {
    ans += abs(a.at(k) - x);
  }
  cout << ans << endl;
}


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

int main() {
  long int N, x, y, z, ans;
  cin >> N;
  ans = 0;
  vector<long int> a(N);
  for (long int i = 0; i < N; i++) {
    cin >> y;
    a.at(i) = y - i - 1;
  }
  sort(a.begin(), a.end());
  z = (N / 2);
  x = a.at(z);
  for (long int k = 0; k < N; k++) {
    ans += abs(a.at(k) - x);
  }
  cout << ans << endl;
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> b(n);
  rep(i, n) { b[i] = a[i] - i; }
  sort(b.begin(), b.end());

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

  ll ans = 0;
  rep(i, n) { ans += abs(b[i] - mid); }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> b(n);
  rep(i, n) { b[i] = a[i] - i; }
  sort(b.begin(), b.end());

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

  ll ans = 0;
  rep(i, n) { ans += abs(b[i] - mid); }
  cout << ans << endl;
  return 0;
}
Problem p03309 (C++)

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

int main() {
  int N;
  cin >> N;
  int temp, B[100000];
  for (int i = 0; i < N; i++) {
    cin >> temp;
    B[i] = temp - (i + 1);
  }

  // 中央値を求める
  sort(B, B + N); // 昇順ソート
  int b;
  b = B[N / 2];
  // 和を求める
  ll ans = 0;
  for (int i = 0; i < N; i++)
    ans += abs(B[i] - b);

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

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

int main() {
  int N;
  cin >> N;
  int temp, B[200000];
  for (int i = 0; i < N; i++) {
    cin >> temp;
    B[i] = temp - (i + 1);
  }

  // 中央値を求める
  sort(B, B + N); // 昇順ソート
  int b;
  b = B[N / 2];
  // 和を求める
  ll ans = 0;
  for (int i = 0; i < N; i++)
    ans += abs(B[i] - b);

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

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long

int A[100010];
int N;

int sad(int b) {
  int ret = 0;
  rep(i, N) ret += abs(A[i] - (b + i + 1));
  return ret;
}

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  rep(i, N) cin >> A[i];

  int lower = -1000000000;
  int upper = 1000000000;
  int mid;
  while (true) {
    mid = (lower + upper) / 2;
    // cout << sad(mid - 1) << " " << sad(mid) << endl;
    int diff = sad(mid) - sad(mid - 1);
    if (diff < 0)
      lower = mid + 1;
    else
      upper = mid;
    if (lower == upper || diff == 0) {
      cout << min({sad(mid), sad(mid - 1), sad(mid + 1)}) << endl;
      return 0;
    }
  }

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long

int A[200010];
int N;

int sad(int b) {
  int ret = 0;
  rep(i, N) ret += abs(A[i] - (b + i + 1));
  return ret;
}

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  rep(i, N) cin >> A[i];

  int lower = -1000000000;
  int upper = 1000000000;
  int mid;
  while (true) {
    mid = (lower + upper) / 2;
    // cout << sad(mid - 1) << " " << sad(mid) << endl;
    int diff = sad(mid) - sad(mid - 1);
    if (diff < 0)
      lower = mid + 1;
    else
      upper = mid;
    if (lower == upper || diff == 0) {
      cout << min({sad(mid), sad(mid - 1), sad(mid + 1)}) << endl;
      return 0;
    }
  }

  return 0;
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int a[114514], b[114514];
int n, cnta = 0;
signed main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) b[i] = a[i] - (i + 1);
  sort(b, b + n);
  rep(i, n) {
    if (n % 2 == 1)
      cnta += abs(b[i] - b[(n - 1) / 2]);
    else
      cnta += abs(b[i] - (b[n / 2] + b[n / 2 - 1]) / 2);
  }
  cout << cnta << endl;
  getchar();
  getchar();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int a[214514], b[214514];
int n, cnta = 0;
signed main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n) b[i] = a[i] - (i + 1);
  sort(b, b + n);
  rep(i, n) {
    if (n % 2 == 1)
      cnta += abs(b[i] - b[(n - 1) / 2]);
    else
      cnta += abs(b[i] - (b[n / 2] + b[n / 2 - 1]) / 2);
  }
  cout << cnta << endl;
  getchar();
  getchar();
  return 0;
}
Problem p03309 (C++)

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

int abs(int a, int b) {
  if (a >= b) {
    return a - b;
  } else
    return b - a;
}

int main() {
  int N;
  cin >> N;
  vector<int> MY(N);
  for (int i = 0; i < N; i++) {
    cin >> MY.at(i);
  }
  for (int i = 0; i < N; i++) {
    MY.at(i) -= i + 1;
  }
  sort(MY.begin(), MY.end());
  int x = MY.at(N / 2 - 1);
  int count = 0;
  for (int i = 0; i < N; i++) {
    count += abs(MY.at(i), x);
  }
  cout << count << endl;
}


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

int abs(int a, int b) {
  if (a >= b) {
    return a - b;
  } else
    return b - a;
}

int main() {
  int N;
  cin >> N;
  vector<int> MY(N);
  for (int i = 0; i < N; i++) {
    cin >> MY.at(i);
  }
  for (int i = 0; i < N; i++) {
    MY.at(i) -= i + 1;
  }
  sort(MY.begin(), MY.end());
  int x = MY.at(N / 2);
  int64_t count = 0;
  for (int i = 0; i < N; i++) {
    count += abs(MY.at(i), x);
  }
  cout << count << endl;
}
Problem p03309 (Python)

Buggy submission (status: Runtime Error):
import math

N = int(input())
A = list(map(int, input().split()))
# print(N, A)

_A = [A[i] - (i + 1) for i in range(len(A))]
_A.sort()
median = _A[math.ceil(len(A) / 2)]

sadness = 0
for i in range(len(A)):
    sadness += abs(A[i] - (i + 1 + median))

print(sadness)


Fixed submission:
import math

N = int(input())
A = list(map(int, input().split()))
# print(N, A)

_A = [A[i] - (i + 1) for i in range(len(A))]
_A.sort()
median = _A[math.floor(len(A) / 2)]

sadness = 0
for i in range(len(A)):
    sadness += abs(A[i] - (i + 1 + median))

print(sadness)
Problem p03309 (Python)

Buggy submission (status: Time Limit Exceeded):
# https://atcoder.jp/contests/abc102/tasks/arc100_a
# C - Linear Approximation

N = int(input().split()[0])
a_list = list(map(int, input().split()))
min_b = float("inf")
max_a = max(a_list)

work_list = [a - i - 1 for i, a in enumerate(a_list)]
work_list = list(set(work_list))

for b in work_list:  # 10 ** 5
    total_b = 0
    for i, a in enumerate(a_list):  # 10 ** 5
        total_b += abs(a - (b + i + 1))
    min_b = min(min_b, total_b)

print(min_b)


Fixed submission:
# https://atcoder.jp/contests/abc102/tasks/arc100_a
# C - Linear Approximation

N = int(input().split()[0])
a_list = list(map(int, input().split()))
min_b = float("inf")
max_a = max(a_list)

work_list = [a - (i + 1) for i, a in enumerate(a_list)]
w = sorted(work_list)[int(N / 2)]
ans = sum([abs(x - w) for x in work_list])
print(ans)
Problem p03309 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
long long keisan(vector<int> A, int B, int N) {
  long long count = 0;
  rep(i, N) { count += abs(A[i] - B); }
  return count;
}
int main() {
  long long count = 0;
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) {
    int x;
    cin >> x;
    A[i] = x - i;
  }
  count = keisan(A, A[1], N);
  rep(i, N) { count = min(keisan(A, A[i], N), count); }
  cout << count;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
long long keisan(vector<int> A, int B, int N) {
  long long count = 0;
  rep(i, N) { count += abs(A[i] - B); }
  return count;
}
int main() {
  long long count = 0;
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) {
    int x;
    cin >> x;
    A[i] = x - i;
  }
  sort(A.begin(), A.end());
  if (N % 2) {
    cout << keisan(A, A[N / 2], N);
    return 0;
  } else {
    cout << keisan(A, (A[N / 2] + A[N / 2 - 1]) / 2, N);
  }
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
int main() {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= (i + 1);
  }
  sort(a.begin(), a.end());
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (a[i] < 0)
      ans -= a[i];
    else
      ans += a[i];
  }
  ll mcnt = 0;
  for (ll i = 0; i < n; i++) {
    if (a[i] > 0) {
      break;
    }
    mcnt++;
  }
  ll pcnt = n - mcnt;
  ll da = 0;
  if (n % 2 == 1) {
    ll mid = n / 2;
    if (mid > mcnt) {
      while (a[mid] != 0) {
        a[mcnt] -= da;
        ll b = a[mcnt];
        ans -= b * (pcnt - mcnt);
        da += b;
        pcnt--;
        mcnt++;
      }
    } else {
      while (a[mid] != 0) {
        a[n - pcnt - 1] -= da;
        ll b = a[n - pcnt - 1];
        a[n - pcnt - 1] = 0;
        ans -= b * (pcnt - mcnt);
        da += b;
        pcnt++;
        mcnt--;
      }
    }
  } else {
    ll mid = n / 2;
    if (mid > mcnt) {
      while (mcnt != pcnt) {
        a[mcnt] -= da;
        ll b = a[mcnt];
        ans -= b * (pcnt - mcnt);
        da += b;
        pcnt--;
        mcnt++;
      }
    } else {
      while (mcnt != pcnt) {
        a[n - pcnt - 1] -= da;
        ll b = a[n - pcnt - 1];
        ans -= b * (pcnt - mcnt);
        da += b;
        pcnt++;
        mcnt--;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
int main() {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= (i + 1);
  }
  sort(a.begin(), a.end());
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (a[i] < 0)
      ans -= a[i];
    else
      ans += a[i];
  }
  ll mcnt = 0;
  for (ll i = 0; i < n; i++) {
    if (a[i] > 0) {
      break;
    }
    mcnt++;
  }
  ll pcnt = n - mcnt;
  ll da = 0;
  if (n % 2 == 1) {
    ll mid = n / 2;
    if (mid > mcnt) {
      while (a[mid] != 0) {
        a[mcnt] -= da;
        ll b = a[mcnt];
        a[mcnt] = 0;
        ans -= b * (pcnt - mcnt);
        da += b;
        pcnt--;
        mcnt++;
      }
    } else {
      while (a[mid] != 0) {
        a[n - pcnt - 1] -= da;
        ll b = a[n - pcnt - 1];
        a[n - pcnt - 1] = 0;
        ans -= b * (pcnt - mcnt);
        da += b;
        pcnt++;
        mcnt--;
      }
    }
  } else {
    ll mid = n / 2;
    if (mid > mcnt) {
      while (mcnt != pcnt) {
        a[mcnt] -= da;
        ll b = a[mcnt];
        ans -= b * (pcnt - mcnt);
        da += b;
        pcnt--;
        mcnt++;
      }
    } else {
      while (mcnt != pcnt) {
        a[n - pcnt - 1] -= da;
        ll b = a[n - pcnt - 1];
        ans -= b * (pcnt - mcnt);
        da += b;
        pcnt++;
        mcnt--;
      }
    }
  }
  cout << ans << endl;
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
/* Dsingh_24 */

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;

#define N 100005
ll a[N], b[N], n;
ll check(ll x) {
  ll i, j, sum = 0;
  for (i = 1; i <= n; i++) {
    sum += abs(a[i] - x);
  }
  // cout<<sum;
  return sum;
}
int main() {
  ios ll i, j;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i], a[i] -= i;
  sort(a + 1, a + n + 1);
  cout << min(check(a[n / 2]), check(a[n / 2 + 1]));
  return 0;
}

Fixed submission:
/* Dsingh_24 */

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;

#define N 200005
ll a[N], b[N], n;
ll check(ll x) {
  ll i, j, sum = 0;
  for (i = 1; i <= n; i++) {
    sum += abs(a[i] - x);
  }
  // cout<<sum;
  return sum;
}
int main() {
  ios ll i, j;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i], a[i] -= i;
  sort(a + 1, a + n + 1);
  cout << min(check(a[n / 2]), check(a[n / 2 + 1]));
  return 0;
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int ABS(int a) { return a > 0 ? a : -a; }
int N, m[111111];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  int x = 1;
  for (int i = 0; i < N; i++) {
    cin >> m[i];
    m[i] -= x;
    x++;
  }
  nth_element(m, m + N / 2, m + N);
  long long ans = 0;
  for (int i = 0; i < N; i++)
    ans += ABS(m[i] - m[N / 2]);
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int ABS(int a) { return a > 0 ? a : -a; }
int N, m[222222];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  int x = 1;
  for (int i = 0; i < N; i++) {
    cin >> m[i];
    m[i] -= x;
    x++;
  }
  nth_element(m, m + N / 2, m + N);
  long long ans = 0;
  for (int i = 0; i < N; i++)
    ans += ABS(m[i] - m[N / 2]);
  cout << ans << '\n';
  return 0;
}
Problem p03309 (C++)

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

long Cal(vector<long> &A, long b) {
  long S = 0;
  for (int i = 1; i < A.size(); i++) {
    S += abs(A.at(i) - (b + i));
  }
  return S;
}

int main() {
  int N;
  cin >> N;
  vector<long> A(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> A.at(i);
  }
  vector<long> B;
  B = A;
  sort(B.begin(), B.end());
  long b1 = B.at(N / 2);
  while (true) {
    long b0 = b1 - 1;
    long b2 = b1 + 1;
    long S0 = Cal(A, b0);
    long S1 = Cal(A, b1);
    long S2 = Cal(A, b2);
    if (S1 == min(S0, min(S1, S2)))
      break;
    if (S2 < S1)
      b1 = b2;
    else
      b1 = b0;
  }
  cout << Cal(A, b1) << endl;
  return 0;
};

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

long Cal(vector<long> &A, long b) {
  long S = 0;
  for (int i = 1; i < A.size(); i++) {
    S += abs(A.at(i) - (b + i));
  }
  return S;
}

int main() {
  int N;
  cin >> N;
  vector<long> A(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> A.at(i);
  }
  vector<long> B(N);
  for (int i = 1; i <= N; i++)
    B.at(i - 1) = A.at(i) - i;
  sort(B.begin(), B.end());
  long b1 = B.at(N / 2);
  while (true) {
    long b0 = b1 - 1;
    long b2 = b1 + 1;
    long S0 = Cal(A, b0);
    long S1 = Cal(A, b1);
    long S2 = Cal(A, b2);
    if (S1 == min(S0, min(S1, S2)))
      break;
    if (S2 < S1)
      b1 = b2;
    else
      b1 = b0;
  }
  cout << Cal(A, b1) << endl;
  return 0;
};
Problem p03309 (C++)

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

long long a[100010];
int n;
long long getans(int x) {
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += llabs(a[i] - x);
  }
  return ans;
}

int main() {
  cin >> n;

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

  sort(a, a + n);

  if (n % 2 == 0) {
    cout << min(getans(a[n / 2]), getans(a[n / 2 - 1])) << endl;
  } else {
    cout << getans(a[n / 2]) << endl;
  }

  return 0;
}

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

long long a[200010];
int n;
long long getans(int x) {
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += llabs(a[i] - x);
  }
  return ans;
}

int main() {
  cin >> n;

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

  sort(a, a + n);

  if (n % 2 == 0) {
    cout << min(getans(a[n / 2]), getans(a[n / 2 - 1])) << endl;
  } else {
    cout << getans(a[n / 2]) << endl;
  }

  return 0;
}
Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

void LinearApporoximation() {
  int N;
  cin >> N;
  const int MAX_N = 100010;
  ll t[MAX_N];
  for (int i = 0; i < N; i++) {
    cin >> t[i];
    t[i] -= (i + 1);
  }

  sort(t, t + N);
  ll v;
  if (N % 2 == 0) {
    v = (t[N / 2] + t[N / 2 - 1]) / 2;
  } else {
    v = t[N / 2];
  }
  ll res = 0;
  for (int i = 0; i < N; i++) {
    res += abs(t[i] - v);
  }
  cout << res << endl;
}

int main() {
  LinearApporoximation();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;

void LinearApporoximation() {
  int N;
  cin >> N;
  const int MAX_N = 200010;
  ll t[MAX_N];
  for (int i = 0; i < N; i++) {
    cin >> t[i];
    t[i] -= (i + 1);
  }

  sort(t, t + N);
  ll v;
  if (N % 2 == 0) {
    v = (t[N / 2] + t[N / 2 - 1]) / 2;
  } else {
    v = t[N / 2];
  }
  ll res = 0;
  for (int i = 0; i < N; i++) {
    res += abs(t[i] - v);
  }
  cout << res << endl;
}

int main() {
  LinearApporoximation();
  return 0;
}
Problem p03309 (C++)

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

using namespace std;

int n;
int a[100001];

void debug_print_ints(int *as, int n) {
  printf("%d\n", n);
  for (int i = 0; i < n; i++) {
    printf("%d, ", as[i]);
  }
  printf("\n");
}

long long calc(long long b) {
  long long res = 0;
  for (int i = 0; i < n; i++) {
    res += abs(a[i] - b);
  }
  return res;
}

int main() {

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

  sort(a, a + n);
  // debug_print_ints(a, n);

  long long l = a[0];
  long long r = a[n - 1] + 1;
  while (1) {
    // printf("%d %d\n", l, r);
    if (r - l <= 2) {
      break;
    }
    long long b_midleft = (r + l * 2) / 3;
    long long b_midright = (r * 2 + l) / 3;
    long long sad_midleft = calc(b_midleft);
    long long sad_midright = calc(b_midright);
    // printf("%d %d %d %d\n", b_midleft, b_midright, sad_midleft,
    // sad_midright);
    if (sad_midleft <= sad_midright) {
      r = b_midright;
    } else {
      l = b_midleft;
    }
  }
  long long x[3];
  x[0] = calc(l);
  x[1] = calc(l + 1);
  x[2] = calc(l + 2);
  // debug_print_ints(x, 3);
  printf("%lld\n", *min_element(x, x + 3));

  return 0;
}


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

using namespace std;

int n;
int a[200001];

void debug_print_ints(int *as, int n) {
  printf("%d\n", n);
  for (int i = 0; i < n; i++) {
    printf("%d, ", as[i]);
  }
  printf("\n");
}

long long calc(long long b) {
  long long res = 0;
  for (int i = 0; i < n; i++) {
    res += abs(a[i] - b);
  }
  return res;
}

int main() {

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

  sort(a, a + n);
  // debug_print_ints(a, n);

  long long l = a[0];
  long long r = a[n - 1] + 1;
  while (1) {
    // printf("%d %d\n", l, r);
    if (r - l <= 2) {
      break;
    }
    long long b_midleft = (r + l * 2) / 3;
    long long b_midright = (r * 2 + l) / 3;
    long long sad_midleft = calc(b_midleft);
    long long sad_midright = calc(b_midright);
    // printf("%d %d %d %d\n", b_midleft, b_midright, sad_midleft,
    // sad_midright);
    if (sad_midleft <= sad_midright) {
      r = b_midright;
    } else {
      l = b_midleft;
    }
  }
  long long x[3];
  x[0] = calc(l);
  x[1] = calc(l + 1);
  x[2] = calc(l + 2);
  // debug_print_ints(x, 3);
  printf("%lld\n", *min_element(x, x + 3));

  return 0;
}