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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stdexcept>
#include <tuple>
#include <unordered_map>
#include <vector>

#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
#define mod LL(1e9 + 7)
#define mp make_pair
#define pb push_back
#define sqr(xx) ((xx) * (xx))

const int sz = 2e6;

ll n, a[int(1e5) + 20], ans;

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= i;
  }
  ll l = -1e10, r = 1e10;
  while (r - l > 3) {
    ll m1 = l + (r - l) / 3;
    ll m2 = r - (r - l) / 3;
    if (f(m1) > f(m2))
      l = m1;
    else
      r = m2;
  }

  ll ans = 1e18;
  for (int i = l - 10; i <= l + 10; i++)
    ans = min(ans, f(i));
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stdexcept>
#include <tuple>
#include <unordered_map>
#include <vector>

#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
#define mod LL(1e9 + 7)
#define mp make_pair
#define pb push_back
#define sqr(xx) ((xx) * (xx))

const int sz = 2e6;

ll n, a[int(2e5) + 20], ans;

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= i;
  }
  ll l = -1e10, r = 1e10;
  while (r - l > 3) {
    ll m1 = l + (r - l) / 3;
    ll m2 = r - (r - l) / 3;
    if (f(m1) > f(m2))
      l = m1;
    else
      r = m2;
  }

  ll ans = 1e18;
  for (int i = l - 10; i <= l + 10; i++)
    ans = min(ans, f(i));
  cout << ans;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long lint;
const int N = 1e5 + 5;
int n, val[N];

namespace utils {
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
template <class T> inline void apn(T &x, const T y) { x = x < y ? x : y; }
template <class T> inline void apx(T &x, const T y) { x = x > y ? x : y; }
inline int nxi() {
  int x = 0;
  char c;
  while (((c = getchar()) > '9' || c < '0') && c != '-')
    ;
  const bool f = (c == '-') && (c = getchar());
  while (x = x * 10 - 48 + c, (c = getchar()) >= '0' && c <= '9')
    ;
  return f ? -x : x;
}
} // namespace utils
using namespace utils;

int main() {
  n = nxi();
  for (int i = 1; i <= n; ++i) {
    val[i] = nxi() + (n - i);
  }
  std::nth_element(val + 1, val + ((n + 1) >> 1), val + n + 1);
  lint ans = 0;
  for (int mid = (n + 1) >> 1, i = 1; i <= n; ++i) {
    ans += i <= mid ? val[mid] - val[i] : val[i] - val[mid];
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long lint;
const int N = 2e5 + 5;
int n, val[N];

namespace utils {
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
template <class T> inline void apn(T &x, const T y) { x = x < y ? x : y; }
template <class T> inline void apx(T &x, const T y) { x = x > y ? x : y; }
inline int nxi() {
  int x = 0;
  char c;
  while (((c = getchar()) > '9' || c < '0') && c != '-')
    ;
  const bool f = (c == '-') && (c = getchar());
  while (x = x * 10 - 48 + c, (c = getchar()) >= '0' && c <= '9')
    ;
  return f ? -x : x;
}
} // namespace utils
using namespace utils;

int main() {
  n = nxi();
  for (int i = 1; i <= n; ++i) {
    val[i] = nxi() + (n - i);
  }
  std::nth_element(val + 1, val + ((n + 1) >> 1), val + n + 1);
  lint ans = 0;
  for (int mid = (n + 1) >> 1, i = 1; i <= n; ++i) {
    ans += i <= mid ? val[mid] - val[i] : val[i] - val[mid];
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
/**********************************************************************/
/********                                                      ********/
/********                     Writer:孙~~                      ********/
/********                       Lang:C++                       ********/
/********                     Status:Unknow                    ********/
/********                                                      ********/
/**********************************************************************/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int SIZE = 100005;

long long a[SIZE];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
    a[i] -= i + 1;
  }
  sort(a, a + n);
  if (n == 1) {
    puts("0");
  } else if (n % 2 == 0) {
    long long tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp += abs(a[i] - a[n / 2]);
    }
    long long tmp2 = 0;
    for (int i = 0; i < n; i++) {
      tmp2 += abs(a[i] - a[n / 2 - 1]);
    }
    printf("%lld\n", min(tmp, tmp2));
  } else {
    long long tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp += abs(a[i] - a[n / 2]);
    }
    printf("%lld\n", tmp);
  }
  return 0;
}


Fixed submission:
/**********************************************************************/
/********                                                      ********/
/********                     Writer:孙~~                      ********/
/********                       Lang:C++                       ********/
/********                     Status:Unknow                    ********/
/********                                                      ********/
/**********************************************************************/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int SIZE = 200005;

long long a[SIZE];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
    a[i] -= i + 1;
  }
  sort(a, a + n);
  if (n == 1) {
    puts("0");
  } else if (n % 2 == 0) {
    long long tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp += abs(a[i] - a[n / 2]);
    }
    long long tmp2 = 0;
    for (int i = 0; i < n; i++) {
      tmp2 += abs(a[i] - a[n / 2 - 1]);
    }
    printf("%lld\n", min(tmp, tmp2));
  } else {
    long long tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp += abs(a[i] - a[n / 2]);
    }
    printf("%lld\n", tmp);
  }
  return 0;
}
Problem p03311 (C++)

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

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

  scanf("%d", &n);

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

  sort(a, a + n);

  ll ans = 0;

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

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

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <complex>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

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

  scanf("%d", &n);

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

  sort(a, a + n);

  ll ans = 0;

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

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

  return 0;
}
Problem p03311 (C++)

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

using namespace std;
#define taskname ""
#define pb push_back
typedef long double ld;
typedef long long ll;
const int maxn = 1e5 + 5;
int a[maxn], n;
ll b[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen(taskname".INP", "r",stdin);
  // freopen(taskname".OUT", "w",stdout);
  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);
  for (int i = 1; i <= n; ++i)
    b[i] = b[i - 1] + a[i];
  ll res = LLONG_MAX;
  for (int i = 1; i <= n; ++i) {
    ll cal = -b[i] + 1ll * i * a[i] + (b[n] - b[i]) - 1ll * a[i] * (n - i);
    res = min(res, cal);
  }
  cout << res;
}


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

using namespace std;
#define taskname ""
#define pb push_back
typedef long double ld;
typedef long long ll;
const int maxn = 2e5 + 5;
int a[maxn], n;
ll b[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen(taskname".INP", "r",stdin);
  // freopen(taskname".OUT", "w",stdout);
  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);
  for (int i = 1; i <= n; ++i)
    b[i] = b[i - 1] + a[i];
  ll res = LLONG_MAX;
  for (int i = 1; i <= n; ++i) {
    ll cal = -b[i] + 1ll * i * a[i] + (b[n] - b[i]) - 1ll * a[i] * (n - i);
    res = min(res, cal);
  }
  cout << res;
}
Problem p03311 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

#define x first
#define y second
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define bit __builtin_popcount
#define all(x) x.begin(), x.end()

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const ll INF = 1e18 + 123;
const ld EPS = 1e-9;
const int inf = 1e9 + 123;
const int MOD = 1e9 + 7;
const int N = 2e5 + 123;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

int a[N], n;

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll l = -1e12, r = 1e12;
  while (l < r) {
    ll mid = (l + r) / 2;
    if (calc(mid) > calc(mid + 1)) {
      l = mid + 1;
    } else
      r = mid;
  }
  cout << calc(r);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <ext/pb_ds/assoc_container.hpp>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

#define x first
#define y second
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define bit __builtin_popcount
#define all(x) x.begin(), x.end()

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const ll INF = 1e18 + 123;
const ld EPS = 1e-9;
const int inf = 1e9 + 123;
const int MOD = 1e9 + 7;
const int N = 2e5 + 123;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

int a[N], n;

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll l = -1e12, r = 1e12;
  while (l < r) {
    ll mid = (l + r) / 2;
    if (calc(mid) >= calc(mid + 1)) {
      l = mid + 1;
    } else
      r = mid;
  }
  cout << calc(r);
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <vector>
#define LL long long
#define ft first
#define sd second
#define mp(x, y) make_pair(x, y)
#define db double
#define int LL
using namespace std;
const int N = 1e5 + 233;
// const int mod = ;
const int INF = numeric_limits<int>::max();

#define rep(i, x, y) for (int i = x; i <= y; ++i)
void read(int &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch))
    (ch == '-' ? f = -1 : 0), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}

void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int n, a[N];
int check(int p) {
  int tmp = 0;
  rep(i, 1, n) tmp += abs(a[i] - p);
  return tmp;
}

signed main() {
  read(n);
  rep(i, 1, n) read(a[i]), a[i] -= i;
  sort(a + 1, a + 1 + n);
  int mid = 1 + n >> 1;
  int l = max(1ll, mid - 10);
  int r = min(n, mid + 10);
  int ans = INF;
  rep(i, l, r) ans = min(ans, check(a[i]));
  printf("%lld", ans);
  return 0;
}

Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <vector>
#define LL long long
#define ft first
#define sd second
#define mp(x, y) make_pair(x, y)
#define db double
#define int LL
using namespace std;
const int N = 3e5 + 233;
// const int mod = ;
const int INF = numeric_limits<int>::max();

#define rep(i, x, y) for (int i = x; i <= y; ++i)
void read(int &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  while (!isdigit(ch))
    (ch == '-' ? f = -1 : 0), ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}

void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
int n, a[N];
int check(int p) {
  int tmp = 0;
  rep(i, 1, n) tmp += abs(a[i] - p);
  return tmp;
}

signed main() {
  read(n);
  rep(i, 1, n) read(a[i]), a[i] -= i;
  sort(a + 1, a + 1 + n);
  int mid = 1 + n >> 1;
  int l = max(1ll, mid - 10);
  int r = min(n, mid + 10);
  int ans = INF;
  rep(i, l, r) ans = min(ans, check(a[i]));
  printf("%lld", ans);
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define enter putchar('\n')
#define space putchar(' ')
#define fi first
#define se second
#define mp make_pair
#define MAXN 100005
// #define ivorysi
#define pii pair<int, int>
using namespace std;
typedef long long int64;
template <class T> void read(T &res) {
  res = 0;
  char c = getchar();
  T f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}

template <class T> void out(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10)
    out(x / 10);
  putchar('0' + x % 10);
}
int64 A[MAXN];
int N;

void Solve() {
  read(N);
  for (int i = 1; i <= N; ++i) {
    read(A[i]);
    A[i] -= i;
  }
  sort(A + 1, A + N + 1);
  int64 t = A[N / 2 + 1], ans = 0;
  for (int i = 1; i <= N; ++i) {
    ans += abs(A[i] - t);
  }
  out(ans);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define enter putchar('\n')
#define space putchar(' ')
#define fi first
#define se second
#define mp make_pair
#define MAXN 200005
// #define ivorysi
#define pii pair<int, int>
using namespace std;
typedef long long int64;
template <class T> void read(T &res) {
  res = 0;
  char c = getchar();
  T f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}

template <class T> void out(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10)
    out(x / 10);
  putchar('0' + x % 10);
}
int64 A[MAXN];
int N;

void Solve() {
  read(N);
  for (int i = 1; i <= N; ++i) {
    read(A[i]);
    A[i] -= i;
  }
  sort(A + 1, A + N + 1);
  int64 t = A[N / 2 + 1], ans = 0;
  for (int i = 1; i <= N; ++i) {
    ans += abs(A[i] - t);
  }
  out(ans);
  enter;
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Solve();
}
Problem p03311 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define R register

typedef long long ll;

const int MN = 2e5 + 5;

inline int read() {
  R int x;
  R bool f;
  R char c;
  for (f = 0; (c = getchar()) < '0' || c > '9'; f = c == '-')
    ;
  for (x = c ^ '0'; (c = getchar()) >= '0' && c <= '9';
       x = (x << 1) + (x << 3) + (c ^ '0'))
    ;
  return f ? -x : x;
}

int n, a[MN];
ll ans;

inline ll getsol(int x) {
  R ll res = 0;
  for (R int i = 1; i <= n; ++i)
    res += abs(x - a[i]);
  ans = std::min(ans, res);
  return res;
}

int main() {
  n = read();
  for (R int i = 1; i <= n; ++i)
    ans += abs(a[i] = read() - i);
  R int l = -1e9, r = 1e9;
  for (; r - l > 5;) {
    R int md1 = (l + r) / 3, md2 = r - (r - l) / 3;
    if (getsol(md1) < getsol(md2))
      r = md2;
    else
      l = md1;
  }
  for (; l <= r; ++l)
    getsol(l);

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

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

typedef long long ll;

const int MN = 2e5 + 5;

inline int read() {
  R int x;
  R bool f;
  R char c;
  for (f = 0; (c = getchar()) < '0' || c > '9'; f = c == '-')
    ;
  for (x = c ^ '0'; (c = getchar()) >= '0' && c <= '9';
       x = (x << 1) + (x << 3) + (c ^ '0'))
    ;
  return f ? -x : x;
}

int n, a[MN];
ll ans;

inline ll getsol(int x) {
  R ll res = 0;
  for (R int i = 1; i <= n; ++i)
    res += abs(x - a[i]);
  ans = std::min(ans, res);
  return res;
}

int main() {
  n = read();
  for (R int i = 1; i <= n; ++i)
    ans += abs(a[i] = read() - i);
  R int l = -1e9, r = 1e9;
  for (; r - l > 5;) {
    R int md1 = l + (r - l) / 3, md2 = r - (r - l) / 3;
    if (getsol(md1) < getsol(md2))
      r = md2;
    else
      l = md1;
  }
  for (; l <= r; ++l)
    getsol(l);

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

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

using namespace std;

const int MAXN = 100100;

int arr[MAXN];

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

  int n, i;
  long long sol = 0;

  cin >> n;

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

  sort(arr, arr + n);

  for (i = 0; i < n; ++i) {
    sol += abs(arr[i] - arr[n / 2]);
  }

  cout << sol << '\n';
}


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

using namespace std;

const int MAXN = 200100;

int arr[MAXN];

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

  int n, i;
  long long sol = 0;

  cin >> n;

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

  sort(arr, arr + n);

  for (i = 0; i < n; ++i) {
    sol += abs(arr[i] - arr[n / 2]);
  }

  cout << sol << '\n';
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  ll ave = 0;
  rep1(i, n) {
    cin >> a[i];
    a[i] -= i + 1;
  }
  sort(a.begin(), a.end());
  ll mid;
  if (n % 2 == 0) {
    mid = (a[n / 2] + a[n / 2 - 1]) / 2;
    mid += (a[n / 2] - mid < mid - a[n / 2 - 1]);
  } else
    mid = a[n / 2];
  vector<ll> ans(5);
  rep1(i, 5) {
    mid++;
    rep1(j, n) { ans[i] += abs(a[j] - a[mid]); }
  }
  // cout << min(ans[1], ans[1]) << endl;
  cout << *min_element(ans.begin(), ans.end()) << endl;
  // printf("%.12f", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  ll ave = 0;
  rep1(i, n) {
    cin >> a[i];
    a[i] -= i + 1;
  }
  sort(a.begin(), a.end());
  ll mid;
  if (n % 2 == 0) {
    mid = (a[n / 2] + a[n / 2 - 1]) / 2;
    mid += (a[n / 2] - mid < mid - a[n / 2 - 1]);
  } else
    mid = a[n / 2];
  ll ans = 0;
  rep1(j, n) ans += abs(a[j] - mid);
  cout << ans << endl;
  // printf("%.12f", ans);
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, a[100005];
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);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += abs(a[i] - a[(n + 1) / 2]);
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[200005];
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);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += abs(a[i] - a[(n + 1) / 2]);
  }
  cout << ans;
}
Problem p03311 (C++)

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

using namespace std;
typedef long long ll;
ll a[maxn], n, ans;

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


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

using namespace std;
typedef long long ll;
ll a[maxn], n, ans;

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
typedef long long ll;
ll a[100000 + 5];
ll b[100000 + 5];
ll mabs(ll x) {
  if (x < 0)
    return x * -1;
  return x;
}
int main() {
  int n;
  scanf("%d", &n);
  ll _min = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    a[i] -= (i + 1);
    _min += mabs(a[i]);
  }
  sort(a, a + n);
  b[0] = a[0];
  for (int i = 1; i < n; i++) {
    b[i] = b[i - 1] + a[i];
  }
  _min = min(_min, b[n - 1] - b[0] - (n - 1) * a[0]);
  for (int i = 1; i < n; i++) {
    _min = min(_min, mabs(b[i - 1] - i * a[i]) + b[n - 1] - b[i - 1] -
                         (n - i) * a[i]);
  }
  printf("%lld\n", _min);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
typedef long long ll;
ll a[200000 + 5];
ll b[200000 + 5];
ll mabs(ll x) {
  if (x < 0)
    return x * -1;
  return x;
}
int main() {
  int n;
  scanf("%d", &n);
  ll _min = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    a[i] -= (i + 1);
    _min += mabs(a[i]);
  }
  sort(a, a + n);
  b[0] = a[0];
  for (int i = 1; i < n; i++) {
    b[i] = b[i - 1] + a[i];
  }
  _min = min(_min, b[n - 1] - b[0] - (n - 1) * a[0]);
  for (int i = 1; i < n; i++) {
    _min = min(_min, mabs(b[i - 1] - i * a[i]) + b[n - 1] - b[i - 1] -
                         (n - i) * a[i]);
  }
  printf("%lld\n", _min);
  return 0;
}
Problem p03311 (C++)

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

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; ++i)
    cin >> A[i];
  for (ll i = 0; i < N; ++i)
    A[i] = A[i] - (i + 1);
  sort(A.begin(), A.end());
  vector<ll> candidates;
  ll s_min = 2e18;
  for (ll b = A[N / 2] - 10000000; b < A[N / 2] + 10000000; ++b) {
    ll s = 0;
    for (ll i = 0; i < N; ++i) {
      s += abs(b - A[i]);
    }
    if (s < s_min) {
      s_min = s;
    }
  }
  cout << s_min << endl;
  return 0;
}


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

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll i = 0; i < N; ++i)
    cin >> A[i];
  for (ll i = 0; i < N; ++i)
    A[i] = A[i] - (i + 1);
  sort(A.begin(), A.end());
  vector<ll> candidates;
  ll s_min = 2e18;
  for (ll b = A[N / 2] - 1000; b < A[N / 2] + 1000; ++b) {
    ll s = 0;
    for (ll i = 0; i < N; ++i) {
      s += abs(b - A[i]);
    }
    if (s < s_min) {
      s_min = s;
    }
  }
  cout << s_min << endl;
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

#define ll long long
#define Max 20005

using namespace std;

inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline ll read() {
  ll x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}

inline void write(ll x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

inline void writeln(ll x) {
  write(x);
  puts("");
}

ll n, ans, sum, a[Max];

int main() {
  n = read();
  for (ll i = 1; i <= n; i++)
    a[i] = read() - i;
  sort(a + 1, a + n + 1);
  for (ll i = 1; i <= n; i++) {
    ans += abs(a[i] - a[n / 2]);
    sum += abs(a[i] - a[n / 2 + 1]);
  }
  writeln(min(ans, sum));
  return 0;
}


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

#define ll long long
#define Max 200005

using namespace std;

inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline ll read() {
  ll x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}

inline void write(ll x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

inline void writeln(ll x) {
  write(x);
  puts("");
}

ll n, ans, sum, a[Max];

int main() {
  n = read();
  for (ll i = 1; i <= n; i++)
    a[i] = read() - i;
  sort(a + 1, a + n + 1);
  for (ll i = 1; i <= n; i++) {
    ans += abs(a[i] - a[n / 2]);
    sum += abs(a[i] - a[n / 2 + 1]);
  }
  writeln(min(ans, sum));
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mod 1000000007
#define ten5 100005
#define ten52 200005
#define ten53 300005
#define ten6 1000005
#define PI 3.1415926
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mkpr(x1, x2) make_pair(x1, x2)
typedef long long int ll;

// stack<char> stk;
// set<ll> sll;
// set<pair<ll,ll>> spll;
// map<string,ll> mp;
// map<ll,ll> mp;
// deque<ll> deq;
vector<ll> vll;
ll num[ten5], num2[ten5];
// priority_queue<pair<ll,ll>> pq;          //decreasing

int main(void) {
  ll m, n, i, j, k = 0, k1 = 0;
  string s;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> m;
    num[i - 1] = m - i;
  }
  sort(num, num + n);
  m = n / 2;
  if (n & 1 == 1) {
    for (i = 0; i < n; i++)
      k += abs(num[i] - num[m]);
  } else {
    for (i = 0; i < n; i++) {
      k += abs(num[i] - num[m]);
      k1 += abs(num[i] - num[m - 1]);
    }
    k = min(k, k1);
  }
  cout << k;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mod 1000000007
#define ten5 100005
#define ten52 200005
#define ten53 300005
#define ten6 1000005
#define PI 3.1415926
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mkpr(x1, x2) make_pair(x1, x2)
typedef long long int ll;

// stack<char> stk;
// set<ll> sll;
// set<pair<ll,ll>> spll;
// map<string,ll> mp;
// map<ll,ll> mp;
// deque<ll> deq;
vector<ll> vll;
ll num[ten52], num2[ten52];
// priority_queue<pair<ll,ll>> pq;          //decreasing

int main(void) {
  ll m, n, i, j, k = 0, k1 = 0;
  string s;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> m;
    num[i - 1] = m - i;
  }
  sort(num, num + n);
  m = n / 2;
  if (n & 1 == 1) {
    for (i = 0; i < n; i++)
      k += abs(num[i] - num[m]);
  } else {
    for (i = 0; i < n; i++) {
      k += abs(num[i] - num[m]);
      k1 += abs(num[i] - num[m - 1]);
    }
    k = min(k, k1);
  }
  cout << k;
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define fr(i, k, N) for (ll i = k; i < N; i++)
#define rep(i, N) for (ll i = 0; i < N; i++)
#define alll(v) v.begin(), v.end()
using namespace std;
template <class X> void pr(X test) { cout << test << endl; }
template <class X> void prr(X test) {
  for (auto it : test) {
    cout << it;
  }
  cout << endl;
}

int main() {
  ll k;
  cin >> k;
  vector<ll> v(k);
  ll sum = 0;
  rep(i, k) {
    cin >> v[i];
    v[i] -= (1 + i);
    //        sum+=v[i];
  }
  //    sum/=k;
  ll case1 = 0, case2 = 0, case3 = 0;
  //    sum--;
  rep(j, k) { case1 += abs(0 - v[j]); }
  rep(i, k) {
    ll tmp = 0;
    rep(j, k) { tmp += abs(v[i] - v[j]); }
    case1 = min(tmp, case1);
  }
  pr(case1);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define fr(i, k, N) for (ll i = k; i < N; i++)
#define rep(i, N) for (ll i = 0; i < N; i++)
#define alll(v) v.begin(), v.end()
using namespace std;
template <class X> void pr(X test) { cout << test << endl; }
template <class X> void prr(X test) {
  for (auto it : test) {
    cout << it;
  }
  cout << endl;
}

int main() {
  ll k;
  cin >> k;
  vector<ll> v(k);
  ll sum = 0;
  rep(i, k) {
    cin >> v[i];
    v[i] -= (1 + i);
    //        sum+=v[i];
  }
  //    sum/=k;
  ll case1 = 0, case2 = 0, case3 = 0;
  //    sum--;
  sort(alll(v));
  rep(j, k) { case1 += abs(v[0] - v[j]); }
  //    ll l =0, r = v.size()-1;
  ll tmp = case1;
  rep(i, k - 1) {
    ll j = i + 1;
    //        ll tmp =case1;
    auto diff = v[j] - v[i];
    tmp -= diff * (v.size() - j);
    tmp += diff * j;
    case1 = min(tmp, case1);
  }
  pr(case1);
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <utility>
#include <vector>
using namespace std;
const int maxn = 1e5 + 233;
long long ans = 0;
int a[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    a[i] -= i;
  sort(a + 1, a + 1 + n);
  int t = n >> 1;
  if (n % 2 == 1)
    t++;
  int b = a[t];
  for (int i = 1; i <= n; i++)
    ans += abs(a[i] - b);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <utility>
#include <vector>
using namespace std;
const int maxn = 2e5 + 233;
long long ans = 0;
int a[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    a[i] -= i;
  sort(a + 1, a + 1 + n);
  int t = n >> 1;
  if (n % 2 == 1)
    t++;
  int b = a[t];
  for (int i = 1; i <= n; i++)
    ans += abs(a[i] - b);
  cout << ans << endl;
  return 0;
}
Problem p03311 (C++)

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

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

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 10;
ll b[N];

int main() {
  int n;
  scanf("%d", &n);
  ll ans = 0;
  for (int i = 1, x; i <= n; ++i) {
    scanf("%d", &x);
    b[i] = x - i;
  }
  sort(b + 1, b + 1 + n);
  ll base;
  base = b[n / 2 + 1];
  for (int i = 1; i <= n; ++i) {
    ans += abs(b[i] - base);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03311 (C++)

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    a[i] = temp - (i + 1);
  }
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }
  ll small = sum / n;
  ll ans = 1e18;
  while (1) {
    ll tans = 0;
    ll pn = 0;
    ll mn = 0;
    ll zn = 0;
    for (ll j = 0; j < n; j++) {
      tans += abs(a[j] - small);
      if (a[j] - small < 0) {
        mn++;
      } else if (a[j] - small > 0) {
        pn++;
      } else {
        zn++;
      }
    }
    if (ans == tans)
      break;
    ans = min(ans, tans);
    if (mn > zn + pn) {
      small--;
    } else if (pn > zn + mn) {
      small++;
    } else
      break;
  }
  cout << ans << endl;
}

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    a[i] = temp - (i + 1);
  }
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += a[i];
  }
  sort(a.begin(), a.end());
  ll small = a[a.size() / 2];
  ll ans = 1e18;
  while (1) {
    ll tans = 0;
    ll pn = 0;
    ll mn = 0;
    ll zn = 0;
    for (ll j = 0; j < n; j++) {
      tans += abs(a[j] - small);
      if (a[j] - small < 0) {
        mn++;
      } else if (a[j] - small > 0) {
        pn++;
      } else {
        zn++;
      }
    }
    if (ans == tans)
      break;
    ans = min(ans, tans);
    if (mn > zn + pn) {
      small--;
    } else if (pn > zn + mn) {
      small++;
    } else
      break;
  }
  cout << ans << endl;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int n, a[N], f, zero, z;
vector<int> v1, v2;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i] -= i;
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans += abs(a[i]);
  for (int i = 1; i <= n; i++)
    if (a[i] < 0)
      v1.push_back(-a[i]);
    else if (a[i] > 0)
      v2.push_back(a[i]);
    else
      zero++;
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  if (v1.size() > v2.size() + zero) {
    int tep = v2.size() + zero, thi = v1.size(), res = 0;
    for (int i = 0; i < v1.size(); i++) {
      if (thi <= tep)
        break;
      ans -= (ll)(thi - tep) * (v1[i] - res);
      thi--;
      tep++;
      res = v1[i];
    }
  } else if (v2.size() > v1.size() + zero) {
    int tep = v1.size() + zero, thi = v2.size(), res = 0;
    for (int i = 0; i < v2.size(); i++) {
      if (thi <= tep)
        break;
      ans -= (ll)(v2[i] - res) * (thi - tep);
      thi--;
      tep++;
      res = v2[i];
    }
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int n, a[N], f, zero, z;
vector<int> v1, v2;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i] -= i;
  ll ans = 0;
  for (int i = 1; i <= n; i++)
    ans += abs(a[i]);
  for (int i = 1; i <= n; i++)
    if (a[i] < 0)
      v1.push_back(-a[i]);
    else if (a[i] > 0)
      v2.push_back(a[i]);
    else
      zero++;
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  if (v1.size() > v2.size() + zero) {
    int tep = v2.size() + zero, thi = v1.size(), res = 0;
    for (int i = 0; i < v1.size(); i++) {
      if (thi <= tep)
        break;
      ans -= (ll)(thi - tep) * (v1[i] - res);
      thi--;
      tep++;
      res = v1[i];
    }
  } else if (v2.size() > v1.size() + zero) {
    int tep = v1.size() + zero, thi = v2.size(), res = 0;
    for (int i = 0; i < v2.size(); i++) {
      if (thi <= tep)
        break;
      ans -= (ll)(v2[i] - res) * (thi - tep);
      thi--;
      tep++;
      res = v2[i];
    }
  }
  printf("%lld\n", ans);
}
Problem p03311 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    A.at(i) -= (i + 1);
  }
  sort(A.begin(), A.end());

  int64_t b;
  if (N / 2 == 0)
    b = (A.at(N / 2 - 1) + A.at(N / 2)) / 2;
  else
    b = A.at(N / 2);

  int64_t sum = 0;
  for (int i = 0; i < N; i++) {
    if (A.at(i) < b)
      sum += b - A.at(i);
    else
      sum += A.at(i) - b;
  }
  cout << sum << endl;

  return 0;
}


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

int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    A.at(i) -= (i + 1);
  }
  sort(A.begin(), A.end());

  int64_t b;
  if (N % 2 == 0)
    b = (A.at(N / 2 - 1) + A.at(N / 2)) / 2;
  else
    b = A.at(N / 2);

  int64_t sum = 0;
  for (int i = 0; i < N; i++) {
    if (A.at(i) < b)
      sum += b - A.at(i);
    else
      sum += A.at(i) - b;
  }
  cout << sum << endl;

  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
// typedef pair<int, pair<int, int>> P;
// typedef pair<ll, pair<int, int>> P;
typedef pair<ll, pair<ll, ll>> T;
ll INFL = 1000000000000000010; // 10^18 = 2^60
int INF = 2147483600;          // 10^9
int INFM = 100000000;
ll MOD = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

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

  sort(A.begin(), A.end());
  int mid = N / 2;
  ll res1 = 0;
  for (int i = 0; i < N; i++) {
    res1 += abs(A.at(i) - A.at(mid));
  }
  mid = N / 2 - 1;
  ll res2 = 0;
  for (int i = 0; i < N; i++) {
    res2 += abs(A.at(i) - A.at(mid));
  }
  cout << min(res1, res2) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
// typedef pair<int, pair<int, int>> P;
// typedef pair<ll, pair<int, int>> P;
typedef pair<ll, pair<ll, ll>> T;
ll INFL = 1000000000000000010; // 10^18 = 2^60
int INF = 2147483600;          // 10^9
int INFM = 100000000;
ll MOD = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

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

  sort(A.begin(), A.end());
  int mid = N / 2;
  ll res1 = 0;
  for (int i = 0; i < N; i++) {
    res1 += abs(A.at(i) - A.at(mid));
  }
  mid = N / 2 - 1;
  if (mid < 0) {
    mid = 0;
  }
  ll res2 = 0;
  for (int i = 0; i < N; i++) {
    res2 += abs(A.at(i) - A.at(mid));
  }
  cout << min(res1, res2) << endl;
}
Problem p03311 (C++)

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

using ll = long long;
const int MAXN = 100005;
const ll INF = (ll)1e9;
int n;
ll a[MAXN];

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

int main() {
#ifdef LOCAL_PROJECT
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif

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

  ll l = -INF, r = INF;

  while (r - l > 1) {
    ll mid = (r + l) >> 1LL;
    if (f(mid) < f(mid + 1)) {
      r = mid;
    } else {
      l = mid;
    }
    // cout << f(l) << " " << f(r) << "\n";
  }
  cout << min(f(l), f(r)) << endl;
  return 0;
}


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

using ll = long long;
const int MAXN = 1000005;
const ll INF = (ll)1e9;
int n;
ll a[MAXN];

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

int main() {
#ifdef LOCAL_PROJECT
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif

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

  ll l = -INF, r = INF;

  while (r - l > 1) {
    ll mid = (r + l) >> 1LL;
    if (f(mid) < f(mid + 1)) {
      r = mid;
    } else {
      l = mid;
    }
    // cout << f(l) << " " << f(r) << "\n";
  }
  cout << min(f(l), f(r)) << endl;
  return 0;
}
Problem p03311 (C++)

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

using namespace std;
// 131073
typedef pair<int, int> pii;
typedef long long ll;
multiset<int> s;
multiset<int>::iterator it;

const int maxn = 100000 + 10;
ll a[maxn];
ll sum[maxn];

int main() {
  ios_base::sync_with_stdio(0);
  //    freopen("input.txt", "r", stdin);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i] -= i;
  }
  sort(a, a + n);
  memset(sum, 0, sizeof(sum));
  ll x = a[(n - 1) / 2], y = a[n / 2];
  ll r1 = 0, r2 = 0;
  for (int j = 0; j < n; ++j) {
    //        cerr << a[j] << " ";
    r1 += abs(x - a[j]);
    r2 += abs(y - a[j]);
  }
  cout << min(r1, r2) << '\n';
  return 0;
}

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

using namespace std;
// 131073
typedef pair<int, int> pii;
typedef long long ll;
multiset<int> s;
multiset<int>::iterator it;

const int maxn = 200000 + 10;
ll a[maxn];
ll sum[maxn];

int main() {
  ios_base::sync_with_stdio(0);
  //    freopen("input.txt", "r", stdin);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i] -= i;
  }
  sort(a, a + n);
  memset(sum, 0, sizeof(sum));
  ll x = a[(n - 1) / 2], y = a[n / 2];
  ll r1 = 0, r2 = 0;
  for (int j = 0; j < n; ++j) {
    //        cerr << a[j] << " ";
    r1 += abs(x - a[j]);
    r2 += abs(y - a[j]);
  }
  cout << min(r1, r2) << '\n';
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define MAX 300000
typedef long long ll;

int main() {
  ll N, A[110000], data, sum = 0;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> data;
    data -= i;
    A[i - 1] = data;
  }
  sort(A, A + N);

  ll center;
  center = A[N / 2];
  for (int i = 0; i < N; i++) {
    sum += abs(center - A[i]);
  }
  cout << sum << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define MAX 300000
typedef long long ll;

int main() {
  ll N, A[210000], data, sum = 0;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> data;
    data -= i;
    A[i - 1] = data;
  }
  sort(A, A + N);

  ll center;
  center = A[N / 2];
  for (int i = 0; i < N; i++) {
    sum += abs(center - A[i]);
  }
  cout << sum << endl;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define sz(v) (ll((v).size()))
#define forn(i, n) for (ll i = 0; i < (n); i++)
#define forr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define forv(i, v) forn(i, sz(v))
#define fors(i, s) for (auto i = (s).begin(); i != (s).end(); ++i)
#define all(v) (v).begin(), (v).end()
#define clr(x) memset(x, 0, sizeof(x))

const ll N = 1e5 + 5;
ll a[N];

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

  ll n;
  cin >> n;
  forn(i, n) {
    cin >> a[i];
    a[i] -= (i + 1);
  }
  sort(a, a + n);
  ll x = (n / 2);
  ll s = 0;
  forn(i, n) s += abs(a[i] - a[x]);
  cout << s << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define sz(v) (ll((v).size()))
#define forn(i, n) for (ll i = 0; i < (n); i++)
#define forr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define forv(i, v) forn(i, sz(v))
#define fors(i, s) for (auto i = (s).begin(); i != (s).end(); ++i)
#define all(v) (v).begin(), (v).end()
#define clr(x) memset(x, 0, sizeof(x))

const ll N = 2e5 + 5;
ll a[N];

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

  ll n;
  cin >> n;
  forn(i, n) {
    cin >> a[i];
    a[i] -= (i + 1);
  }
  sort(a, a + n);
  ll x = (n / 2);
  ll s = 0;
  forn(i, n) s += abs(a[i] - a[x]);
  cout << s << endl;
}