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

Buggy submission (status: Runtime Error):
// Kaori Miyazono, did I reach you ?
//"I can't die because... I'm the one who will protect you." -Asuna Yuuki

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= i + 1;
  }
  if (n == 1) {
    cout << 0;
    return 0;
  }
  sort(a.begin(), a.end());
  long long int b = a[(n >> 1) - 1];
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += abs(a[i] - b);
  }
  long long int an = 0;
  b = a[n << 1];
  for (int i = 0; i < n; i++) {
    an += abs(a[i] - b);
  }
  ans = min(ans, an);
  cout << ans << '\n';
  return 0;
}

Fixed submission:
// Kaori Miyazono, did I reach you ?
//"I can't die because... I'm the one who will protect you." -Asuna Yuuki

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= i + 1;
  }
  if (n == 1) {
    cout << 0;
    return 0;
  }
  sort(a.begin(), a.end());
  long long int b = a[(n >> 1) - 1];
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += abs(a[i] - b);
  }
  long long int an = 0;
  b = a[n >> 1];
  for (int i = 0; i < n; i++) {
    an += abs(a[i] - b);
  }
  ans = min(ans, an);
  cout << ans << '\n';
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, a[20001], ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= (i + 1);
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    ans += abs(a[i] - a[n / 2]);
  }
  cout << ans << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[200001];
long long ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= (i + 1);
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    ans += abs(a[i] - a[n / 2]);
  }
  cout << ans << '\n';
}
Problem p03311 (C++)

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

long long a[100009], n;

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

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

long long a[200009], n;

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

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    A[i] -= i + 1;
  }
  sort(A.begin(), A.end());
  long long med = A[N / 2];
  long long res = 0;
  for (int i = 0; i < N; ++i)
    res += abs(A[i] - med);
  cout << res << 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];
    A[i] -= i + 1;
  }
  sort(A.begin(), A.end());
  long long med = A[N / 2];
  long long res = 0;
  for (int i = 0; i < N; ++i)
    res += abs(A[i] - med);
  cout << res << endl;
}
Problem p03311 (C++)

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

using namespace std;

int ab(int x) { return (x < 0) ? -x : x; }

int main() {
  int n;
  scanf("%d", &n);

  int arr[100100];

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

  sort(arr, arr + n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += ab(arr[i] - arr[n / 2]);
  }

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

  return 0;
}


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

using namespace std;

int ab(int x) { return (x < 0) ? -x : x; }

int main() {
  int n;
  scanf("%d", &n);

  int arr[200200];

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

  sort(arr, arr + n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += ab(arr[i] - arr[n / 2]);
  }

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

  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[100006], b[100006];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    b[i] = (a[i] - i - 1);
  }
  sort(b, b + n);
  int mid = n / 2;
  int B = b[mid];
  long long sum = 0;
  for (int i = 0; i < n; ++i)
    sum += abs((a[i] - (i + 1) - B));
  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[1000006], b[1000006];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    b[i] = (a[i] - i - 1);
  }
  sort(b, b + n);
  int mid = n / 2;
  int B = b[mid];
  long long sum = 0;
  for (int i = 0; i < n; ++i)
    sum += abs((a[i] - (i + 1) - B));
  cout << sum << endl;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
#define endl '\n'
#define lcm(a, b) ((a) / __gcd(a, b) * (b))
#define MP make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<int, P> i_P;
typedef pair<LL, LL> L_P;

const int INF = INT_MAX;
const int MIN = INT_MIN;
const LL LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const int SIZE = 100005;

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

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

  sort(a_sort, a_sort + n);
  LL cent = a_sort[n / 2];

  LL res = 0;
  for (int i = 0; i < n; ++i) {
    res += llabs(a[i] - (i + 1 + cent));
  }
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
#define endl '\n'
#define lcm(a, b) ((a) / __gcd(a, b) * (b))
#define MP make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<int, P> i_P;
typedef pair<LL, LL> L_P;

const int INF = INT_MAX;
const int MIN = INT_MIN;
const LL LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const int SIZE = 200005;

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

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

  sort(a_sort, a_sort + n);
  LL cent = a_sort[n / 2];

  LL res = 0;
  for (int i = 0; i < n; ++i) {
    res += llabs(a[i] - (i + 1 + cent));
  }
  cout << res << endl;

  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f(z) for (int i = 1; i <= z; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define mod 1000000007
#define cl(n, m) (n - 1) / m + 1;
#define T()                                                                    \
  int TT;                                                                      \
  cin >> TT;                                                                   \
  while (TT--)
#define P pair<int, int>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int N = 3e4;
int a[N];
signed main() {
  int n, x, ans = 0;
  cin >> n;
  f(n) cin >> x, a[i] = x - i;
  sort(a + 1, a + n + 1);
  int idx = n / 2 + 1;
  f(n) ans += (abs(a[i] - a[idx]));
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f(z) for (int i = 1; i <= z; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define mod 1000000007
#define cl(n, m) (n - 1) / m + 1;
#define T()                                                                    \
  int TT;                                                                      \
  cin >> TT;                                                                   \
  while (TT--)
#define P pair<int, int>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int N = 3e5;
int a[N];
signed main() {
  int n, x, ans = 0;
  cin >> n;
  f(n) cin >> x, a[i] = x - i;
  sort(a + 1, a + n + 1);
  int idx = n / 2 + 1;
  f(n) ans += (abs(a[i] - a[idx]));
  cout << ans;
}
Problem p03311 (C++)

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

using namespace std;

int n, a[120000];
long long ans;

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);
  for (int i = 1; i <= n / 2; ++i)
    ans += a[n - i + 1] - a[i];
  printf("%lld\n", ans);

  return 0;
}


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

using namespace std;

int n, a[300000];
long long ans;

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);
  for (int i = 1; i <= n / 2; ++i)
    ans += a[n - i + 1] - a[i];
  printf("%lld\n", ans);

  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxn 100020
#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define inf 0x3f3f3f3f
typedef long long ll;
typedef pair<int, int> pr;

int a[maxn], n;
ll ans;

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", &a[i]), a[i] -= i;
  sort(a + 1, a + n + 1);
  int b = a[(n + 1) / 2];
  rep(i, 1, n) ans += abs(b - a[i]);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn 200020
#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define inf 0x3f3f3f3f
typedef long long ll;
typedef pair<int, int> pr;

int a[maxn], n;
ll ans;

int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", &a[i]), a[i] -= i;
  sort(a + 1, a + n + 1);
  int b = a[(n + 1) / 2];
  rep(i, 1, n) ans += abs(b - a[i]);
  cout << ans << endl;
}
Problem p03311 (C++)

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

using namespace std;

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

  auto middleVal = Anorm.at((Anorm.size() + 1) / 2);
  auto ans = 0;
  for (int k = 0; k < N; ++k) {
    ans += abs(Anorm[k] - middleVal);
  }
  cout << ans << endl;
}

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

using namespace std;

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

  auto middleVal = Anorm.at((Anorm.size()) / 2);
  long ans = 0;
  for (int k = 0; k < N; ++k) {
    ans += abs(Anorm[k] - middleVal);
  }
  cout << ans << endl;
}
Problem p03311 (C++)

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

int a[100005];

int main() {
  int n;
  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 res = 0;
  for (int i = 1; i <= n; i++)
    res += abs(a[i] - a[(n + 1) / 2]);
  printf("%lld", res);
  return 0;
}


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

int a[200005];

int main() {
  int n;
  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 res = 0;
  for (int i = 1; i <= n; i++)
    res += abs(a[i] - a[(n + 1) / 2]);
  printf("%lld", res);
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;

#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define FOR(i, s, e) for (int i = int(s); i < int(e); i++)
#define REP(i, x) FOR(i, 0, x)
#define CLR(a) memset((a), 0, sizeof(a))

const ll MOD = 1e+9 + 7;
const double PI = acos(-1.0);

int main() {
  int n, input;
  ll tot = 0;
  cin >> n;
  vi a;
  REP(i, n) {
    cin >> input;
    a.push_back(input - i - 1);
  }
  sort(a.begin(), a.end());
  REP(i, n) { tot += a[i] - a[0]; }
  int pt = 0, tg = a[0];
  ll ret = tot;
  ll ret_tmp = tot;
  while (tg <= a[n - 1]) {
    tg += 1;
    while (tg > a[pt])
      pt++;
    ret_tmp = ret_tmp + pt - (n - pt);
    ret = min(ret, ret_tmp);
  }
  cout << ret << "\n";
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;

#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define FOR(i, s, e) for (int i = int(s); i < int(e); i++)
#define REP(i, x) FOR(i, 0, x)
#define CLR(a) memset((a), 0, sizeof(a))

const ll MOD = 1e+9 + 7;
const double PI = acos(-1.0);

int main() {
  int n, input;
  ll tot = 0;
  cin >> n;
  vi a;
  REP(i, n) {
    cin >> input;
    a.push_back(input - i - 1);
  }
  sort(a.begin(), a.end());
  REP(i, n) { tot += a[i] - a[0]; }
  int pt = 0, tg = a[0];
  ll ret = tot;
  ll ret_tmp = tot;
  while (tg <= a[n - 1]) {
    tg += 1;
    while (pt < n && tg > a[pt])
      pt++;
    ret_tmp = ret_tmp + pt - (n - pt);
    ret = min(ret, ret_tmp);
  }
  cout << ret << "\n";
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
#define fr(i, a, b) for (int i = a; i <= b; i++)
typedef long long ll;
int a[100010], n;
int main() {
  scanf("%d", &n);
  fr(i, 1, n) scanf("%d", a + i), a[i] -= i;
  sort(a + 1, a + 1 + n);
  int mid = 1 + n >> 1;
  ll ans = 0;
  fr(i, 1, n) ans += 1LL * abs(a[i] - a[mid]);
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
#define fr(i, a, b) for (int i = a; i <= b; i++)
typedef long long ll;
int a[200010], n;
int main() {
  scanf("%d", &n);
  fr(i, 1, n) scanf("%d", a + i), a[i] -= i;
  sort(a + 1, a + 1 + n);
  int mid = 1 + n >> 1;
  ll ans = 0;
  fr(i, 1, n) ans += 1LL * abs(a[i] - a[mid]);
  printf("%lld\n", ans);
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007
#define F first
#define S second

typedef long long ll;
using namespace std;

int n, a[100010], mean;
ll sum;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  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)
    sum += abs(a[n / 2] - a[i]);
  cout << sum;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
#define F first
#define S second

typedef long long ll;
using namespace std;

int n, a[200010];
ll sum;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  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)
    sum += abs(a[n / 2] - a[i]);
  cout << sum;
  return 0;
}
Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = (1 << 28);
int n;
ll a[100000];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll b[n];
  for (int i = 0; i < n; i++) {
    b[i] = a[i] - i - 1;
  }
  sort(b, b + n);
  ll axis;
  if (n % 2 == 0) {
    axis = (b[n / 2] + b[n / 2 - 1]) / 2;
  } else {
    axis = b[n / 2];
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += abs(b[i] - axis);
  }
  cout << ans << "\n";
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = (1 << 28);
int n;
ll a[200000];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll b[n];
  for (int i = 0; i < n; i++) {
    b[i] = a[i] - i - 1;
  }
  sort(b, b + n);
  ll axis;
  if (n % 2 == 0) {
    axis = (b[n / 2] + b[n / 2 - 1]) / 2;
  } else {
    axis = b[n / 2];
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += abs(b[i] - axis);
  }
  cout << ans << "\n";
}
Problem p03311 (C++)

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

int n;
ll sum, l, ans, a[100100];

int main() { // freopen("_in.txt","r",stdin);
  scanf("%d", &n);
  ans = 1e17;
  for (int i = 1; i <= n; i++)
    scanf("%lld", a + i), a[i] -= i, sum += a[i];
  sort(a + 1, a + n + 1);
  ans = min(ans, sum - n * a[1]);
  for (int i = 1; i <= n; i++) {
    l += a[i];
    ans = min(ans, i * a[i] - l + (sum - l) - (n - i) * a[i]);
  }
  printf("%lld\n", ans);
}

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

int n;
ll sum, l, ans, a[200100];

int main() { // freopen("_in.txt","r",stdin);
  scanf("%d", &n);
  ans = 1e17;
  for (int i = 1; i <= n; i++)
    scanf("%lld", a + i), a[i] -= i, sum += a[i];
  sort(a + 1, a + n + 1);
  ans = min(ans, sum - n * a[1]);
  for (int i = 1; i <= n; i++) {
    l += a[i];
    ans = min(ans, i * a[i] - l + (sum - l) - (n - i) * a[i]);
  }
  printf("%lld\n", ans);
}
Problem p03311 (C++)

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

int n, a[100000];

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

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

int n, a[200000];

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

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

#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int N;
int A[110000];

signed main() {
  cin >> N;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 1; i < N; i++) {
    A[i] += A[i - 1];
  }
  int ans = LLONG_MAX;
  for (int i = 2; i <= N - 2; i++) {
    int a = lower_bound(A, A + i, A[i - 1] / 2) - A;
    int x = A[a];
    if (a != 0 &&
        abs(A[a] - (A[i - 1] - A[a])) > abs(A[a - 1] - (A[i - 1] - A[a - 1]))) {
      x = A[a - 1];
    }
    int b = lower_bound(A + i, A + N, (A[N - 1] - A[i - 1]) / 2 + A[i - 1]) - A;
    int y = A[b];
    if (b != i && abs((A[b] - A[i - 1]) - (A[N - 1] - A[b])) >
                      abs((A[b - 1] - A[i - 1]) - (A[N - 1] - A[b - 1]))) {
      y = A[b - 1];
    }
    int up = max(max(x, A[i - 1] - x), max(y - A[i - 1], A[N - 1] - y));
    int down = min(min(x, A[i - 1] - x), min(y - A[i - 1], A[N - 1] - y));
    ans = min(ans, up - down);
  }
  cout << ans << endl;
}

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

#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int N;
int A[210000];

signed main() {
  cin >> N;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 1; i < N; i++) {
    A[i] += A[i - 1];
  }
  int ans = LLONG_MAX;
  for (int i = 2; i <= N - 2; i++) {
    int a = lower_bound(A, A + i, A[i - 1] / 2) - A;
    int x = A[a];
    if (a != 0 &&
        abs(A[a] - (A[i - 1] - A[a])) > abs(A[a - 1] - (A[i - 1] - A[a - 1]))) {
      x = A[a - 1];
    }
    int b = lower_bound(A + i, A + N, (A[N - 1] - A[i - 1]) / 2 + A[i - 1]) - A;
    int y = A[b];
    if (b != i && abs((A[b] - A[i - 1]) - (A[N - 1] - A[b])) >
                      abs((A[b - 1] - A[i - 1]) - (A[N - 1] - A[b - 1]))) {
      y = A[b - 1];
    }
    int up = max(max(x, A[i - 1] - x), max(y - A[i - 1], A[N - 1] - y));
    int down = min(min(x, A[i - 1] - x), min(y - A[i - 1], A[N - 1] - y));
    ans = min(ans, up - down);
  }
  cout << ans << endl;
}
Problem p03312 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
const long double EPS = 1e-14;
const int maxn = 100500;
const int mod = 1e9 + 7;

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &x : a)
    cin >> x;
  for (int i = 1; i < n; ++i)
    a[i] += a[i - 1];
  int ans = 3e18;
  for (int mid = 1; mid < n - 1; ++mid) {
    int smL = a[mid];
    int smR = a.back() - a[mid];
    int idxL = 0, idxR = n - 2;
    int lo = 0, hi = mid - 1;
    while (lo <= hi) {
      int md = (lo + hi) >> 1;
      if (a[md] <= smL / 2)
        lo = md + 1;
      else
        hi = md - 1;
    }
    if (lo)
      idxL = lo - 1;
    lo = mid + 1, hi = n - 2;
    while (lo <= hi) {
      int md = (lo + hi) >> 1;
      if (a[md] - a[mid] <= smR / 2)
        lo = md + 1;
      else
        hi = md - 1;
    }
    if (lo)
      idxR = lo - 1;
    for (int midL = max(0LL, idxL - 5); midL < min(mid, idxL + 5); ++midL)
      for (int midR = max(mid + 1, idxR - 5); midR < min(n - 1, idxR + 5);
           ++midR) {
        vector<int> sms;
        sms.push_back(a[midL]);
        sms.push_back(a[mid] - a[midL]);
        sms.push_back(a[midR] - a[mid]);
        sms.push_back(a.back() - a[midR]);
        sort(all(sms));
        ans = min(ans, sms.back() - sms[0]);
      }
  }
  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
const long double EPS = 1e-14;
const int maxn = 100500;
const int mod = 1e9 + 7;

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &x : a)
    cin >> x;
  for (int i = 1; i < n; ++i)
    a[i] += a[i - 1];
  int ans = 3e18;
  for (int mid = 1; mid < n - 1; ++mid) {
    int smL = a[mid];
    int smR = a.back() - a[mid];
    int idxL = 0, idxR = n - 2;
    int lo = 0, hi = mid - 1;
    while (lo <= hi) {
      int md = (lo + hi) >> 1;
      if (a[md] <= smL / 2)
        lo = md + 1;
      else
        hi = md - 1;
    }
    if (lo)
      idxL = lo - 1;
    lo = mid + 1, hi = n - 2;
    while (lo <= hi) {
      int md = (lo + hi) >> 1;
      if (a[md] - a[mid] <= smR / 2)
        lo = md + 1;
      else
        hi = md - 1;
    }
    if (lo)
      idxR = lo - 1;
    for (int midL = max(0LL, idxL - 2); midL < min(mid, idxL + 2); ++midL)
      for (int midR = max(mid + 1, idxR - 2); midR < min(n - 1, idxR + 2);
           ++midR) {
        vector<int> sms;
        sms.push_back(a[midL]);
        sms.push_back(a[mid] - a[midL]);
        sms.push_back(a[midR] - a[mid]);
        sms.push_back(a.back() - a[midR]);
        sort(all(sms));
        ans = min(ans, sms.back() - sms[0]);
      }
  }
  cout << ans << '\n';
  return 0;
}
Problem p03312 (C++)

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

using namespace std;

typedef long long ll;
const ll mk = 100055;
const ll logn = 29;
const ll mod = 1e9 + 7;

#define endl "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define fi first
#define se second
#define pii pair<ll, ll>
#define pq priority_queue
#define UwU uwu
#define UwU Mikuwu
#define Miku best_waifu
#define Miku_dancing god_waving
#define Miku_singing god_shaking
#define a_Miku cho_doi_bot_ngu

ll n;
ll a[mk];
long long ans = 1e9;
long long pref[mk];

long long f(int pos, int i, int j) {
  if (!pos || pos >= i || j <= i || j == n)
    return 1e9;
  long long x1 = pref[pos], x2 = pref[i] - pref[pos], x3 = pref[j] - pref[i],
            x4 = pref[n] - pref[j];
  return max(max(x1, x2), max(x3, x4)) - min(min(x1, x2), min(x3, x4));
}

signed main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    pref[i] = pref[i - 1] + a[i];
  }
  for (int i = 2; i <= n - 2; ++i) {
    int l = upper_bound(pref, pref + n + 1, pref[i] / 2) - pref;
    int r = upper_bound(pref, pref + n + 1, pref[i] + (pref[n] - pref[i]) / 2) -
            pref;
    for (int l1 = l - 1; l1 <= l; ++l1) {
      for (int l2 = r - 1; l2 <= r; ++l2)
        ans = min(ans, f(l1, i, l2));
    }
  }
  cout << ans;
}


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

using namespace std;

typedef long long ll;
const ll mk = 200055;
const ll logn = 29;
const ll mod = 1e9 + 7;

#define endl "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define fi first
#define se second
#define pii pair<ll, ll>
#define pq priority_queue
#define UwU uwu
#define UwU Mikuwu
#define Miku best_waifu
#define Miku_dancing god_waving
#define Miku_singing god_shaking
#define a_Miku cho_doi_bot_ngu

ll n;
ll a[mk];
long long ans = 1e9;
long long pref[mk];

long long f(int pos, int i, int j) {
  if (!pos || pos >= i || j <= i || j == n)
    return 1e9;
  long long x1 = pref[pos], x2 = pref[i] - pref[pos], x3 = pref[j] - pref[i],
            x4 = pref[n] - pref[j];
  return max(max(x1, x2), max(x3, x4)) - min(min(x1, x2), min(x3, x4));
}

signed main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    pref[i] = pref[i - 1] + a[i];
  }
  for (int i = 2; i <= n - 2; ++i) {
    int l = upper_bound(pref, pref + n + 1, pref[i] / 2) - pref;
    int r = upper_bound(pref, pref + n + 1, pref[i] + (pref[n] - pref[i]) / 2) -
            pref;
    for (int l1 = l - 1; l1 <= l; ++l1) {
      for (int l2 = r - 1; l2 <= r; ++l2)
        ans = min(ans, f(l1, i, l2));
    }
  }
  cout << ans;
}
Problem p03312 (C++)

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

using namespace std;

#define int long long
const int N = 1e5 + 5;
int n, a[5], f[N], ans = 1e9 + 7;

int cnp1(int l, int r, int i) {
  int res = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (f[mid] <= f[i] - f[mid])
      res = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return res;
}

int cnp2(int l, int r, int id) {
  int res = id;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (f[mid] - f[id] <= f[n] - f[mid])
      res = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return res;
}

int solve(int l, int mid, int r) {
  int res = 0;
  a[1] = f[l];
  a[2] = f[mid] - f[l];
  a[3] = f[r] - f[mid];
  a[4] = f[n] - f[r];
  for (int i = 1; i <= 4; ++i)
    for (int j = i + 1; j <= 4; ++j)
      res = max(res, abs(a[i] - a[j]));
  return res;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> f[i], f[i] += f[i - 1];
  for (int i = 2; i < n - 1; ++i) {
    int mid_r, mid_l;
    int l = cnp1(1, i, i);
    if (f[i] - 2 * f[l] < 2 * f[l + 1] - f[i])
      mid_l = l;
    else
      mid_l = l + 1;
    int r = cnp2(i + 1, n, i);
    if (f[n] + f[i] - 2 * f[r] < 2 * f[r + 1] - f[i] - f[n])
      mid_r = r;
    else
      mid_r = r + 1;
    ans = min(ans, solve(mid_l, i, mid_r));
  }
  cout << ans;
}


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

using namespace std;

#define int long long

const int N = 2e5 + 5;
int n, a[5], f[N], ans = 1e18 + 7;

int cnp1(int l, int r, int i) {
  int res = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (f[mid] <= f[i] - f[mid])
      res = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return res;
}

int cnp2(int l, int r, int id) {
  int res = id;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (f[mid] - f[id] <= f[n] - f[mid])
      res = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return res;
}

int solve(int l, int mid, int r) {
  int res = 0;
  a[1] = f[l];
  a[2] = f[mid] - f[l];
  a[3] = f[r] - f[mid];
  a[4] = f[n] - f[r];
  for (int i = 1; i <= 4; ++i)
    for (int j = i + 1; j <= 4; ++j)
      res = max(res, abs(a[i] - a[j]));
  return res;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> f[i], f[i] += f[i - 1];
  for (int i = 2; i < n - 1; ++i) {
    int mid_r, mid_l;
    int l = cnp1(1, i, i);
    if (f[i] - 2 * f[l] < 2 * f[l + 1] - f[i])
      mid_l = l;
    else
      mid_l = l + 1;
    int r = cnp2(i + 1, n, i);
    if (f[n] + f[i] - 2 * f[r] < 2 * f[r + 1] - f[i] - f[n])
      mid_r = r;
    else
      mid_r = r + 1;
    ans = min(ans, solve(mid_l, i, mid_r));
  }
  cout << ans;
}
Problem p03312 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
using namespace std;

typedef long long ll;

const int N = 1e5 + 5;
const ll INF = 1e18;
int a[N];
int main() {
  int n;
  scanf("%d", &n);
  ll sum = 0;
  rep(i, n) {
    scanf("%d", &a[i]);
    sum += a[i];
  }

  // initial first cut at 0
  //         second cut at 1
  //         third cut at 2
  int fi = 0, se = 1, thi = 2;
  ll w = a[0], x = a[1], y = a[2], z = sum - w - x - y;
  ll ans = INF;
  for (; se + 2 < n; se++) {
    // printf("%lld %lld %lld %lld\n", w, x, y, z);
    while (fi + 1 < se && abs(w + a[fi + 1] - x + a[fi + 1]) < abs(w - x)) {
      fi++;
      w += a[fi];
      x -= a[fi];
    }
    while (thi + 1 < n && abs(y + a[thi + 1] - z + a[thi + 1]) < abs(y - z)) {
      thi++;
      y += a[thi];
      z -= a[thi];
    }
    ll mx = max({w, x, y, z});
    ll mi = min({w, x, y, z});
    ans = min(ans, mx - mi);
    x += a[se + 1];
    y -= a[se + 1];
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
using namespace std;

typedef long long ll;

const int N = 2e5 + 5;
const ll INF = 1e18;
int a[N];
int main() {
  int n;
  scanf("%d", &n);
  ll sum = 0;
  rep(i, n) {
    scanf("%d", &a[i]);
    sum += a[i];
  }

  // initial first cut at 0
  //         second cut at 1
  //         third cut at 2
  int fi = 0, se = 1, thi = 2;
  ll w = a[0], x = a[1], y = a[2], z = sum - w - x - y;
  ll ans = INF;
  for (; se + 2 < n; se++) {
    // printf("%lld %lld %lld %lld\n", w, x, y, z);
    while (fi + 1 < se && abs(w + a[fi + 1] - x + a[fi + 1]) < abs(w - x)) {
      fi++;
      w += a[fi];
      x -= a[fi];
    }
    while (thi + 1 < n && abs(y + a[thi + 1] - z + a[thi + 1]) < abs(y - z)) {
      thi++;
      y += a[thi];
      z -= a[thi];
    }
    ll mx = max({w, x, y, z});
    ll mi = min({w, x, y, z});
    ans = min(ans, mx - mi);
    x += a[se + 1];
    y -= a[se + 1];
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03312 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
int n, p[100005], q[100005];
long long a[100005], f[100005], ans = 1000000000000000;
long long solve(int l, int r) { return f[r] - f[l - 1]; }
long long make(int k1, int k2, int k3) {
  if (!((1 <= k1) && (k1 < k2) && (k2 < k3) && (k3 < n)))
    return 1000000000000000;
  long long xx = std::max(
      std::max(std::max(solve(1, k1), solve(k1 + 1, k2)), solve(k2 + 1, k3)),
      solve(k3 + 1, n));
  long long yy = std::min(
      std::min(std::min(solve(1, k1), solve(k1 + 1, k2)), solve(k2 + 1, k3)),
      solve(k3 + 1, n));
  return xx - yy;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++)
    f[i] = f[i - 1] + a[i];
  int k = 1;
  p[1] = 1;
  for (int i = 2; i <= n; i++) {
    while (solve(1, k) < solve(k + 1, i))
      ++k;
    p[i] = k;
  }
  k = n;
  q[n] = n;
  for (int i = n - 1; i >= 1; i--) {
    while (solve(k, n) < solve(i, k - 1))
      --k;
    q[i] = k;
  }
  for (int i = 1; i <= n; i++) {
    ans = std::min(ans, make(p[i], i, q[i + 1] - 1));
    ans = std::min(ans, make(p[i] - 1, i, q[i + 1] - 1));
    ans = std::min(ans, make(p[i], i, q[i + 1]));
    ans = std::min(ans, make(p[i] - 1, i, q[i + 1]));
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
int n, p[200005], q[200005];
long long a[200005], f[200005], ans = 1000000000000000;
long long solve(int l, int r) { return f[r] - f[l - 1]; }
long long make(int k1, int k2, int k3) {
  if (!((1 <= k1) && (k1 < k2) && (k2 < k3) && (k3 < n)))
    return 1000000000000000;
  long long xx = std::max(
      std::max(std::max(solve(1, k1), solve(k1 + 1, k2)), solve(k2 + 1, k3)),
      solve(k3 + 1, n));
  long long yy = std::min(
      std::min(std::min(solve(1, k1), solve(k1 + 1, k2)), solve(k2 + 1, k3)),
      solve(k3 + 1, n));
  return xx - yy;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++)
    f[i] = f[i - 1] + a[i];
  int k = 1;
  p[1] = 1;
  for (int i = 2; i <= n; i++) {
    while (solve(1, k) < solve(k + 1, i))
      ++k;
    p[i] = k;
  }
  k = n;
  q[n] = n;
  for (int i = n - 1; i >= 1; i--) {
    while (solve(k, n) < solve(i, k - 1))
      --k;
    q[i] = k;
  }
  for (int i = 1; i <= n; i++) {
    ans = std::min(ans, make(p[i], i, q[i + 1] - 1));
    ans = std::min(ans, make(p[i] - 1, i, q[i + 1] - 1));
    ans = std::min(ans, make(p[i], i, q[i + 1]));
    ans = std::min(ans, make(p[i] - 1, i, q[i + 1]));
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03312 (C++)

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

ll N;
vector<ll> A(200001, 0);
pair<ll, ll> L[100001];
pair<ll, ll> R[100001];

void MakePair(ll tail, ll t_sum, ll h_sum, pair<ll, ll> *P) {
  for (ll head = 1; head < N - 2; ++head) {
    h_sum += A[head];
    while (tail < head - 1 && abs(h_sum - t_sum) > abs((h_sum - A[tail + 1]) -
                                                       (t_sum + A[tail + 1]))) {
      ++tail;
      t_sum += A[tail], h_sum -= A[tail];
    }
    P[head] = make_pair(t_sum, h_sum);
  }
  return;
}

int main(void) {

  cin >> N;
  for (ll i = 0; i < N; ++i)
    cin >> A[i];

  MakePair(0, A[0], 0, L);
  reverse(A.begin(), A.begin() + N);
  MakePair(0, A[0], 0, R);

  ll j = N - 3, ans = LLONG_MAX;
  for (ll i = 1; i < N - 2; ++i) {
    ll mx = max(max(L[i].first, L[i].second), max(R[j].first, R[j].second));
    ll mn = min(min(L[i].first, L[i].second), min(R[j].first, R[j].second));
    --j;
    ans = min(ans, mx - mn);
  }
  cout << ans << endl;

  return 0;
}


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

ll N;
vector<ll> A(200001, 0);
pair<ll, ll> L[200001];
pair<ll, ll> R[200001];

void MakePair(ll tail, ll t_sum, ll h_sum, pair<ll, ll> *P) {
  for (ll head = 1; head < N - 2; ++head) {
    h_sum += A[head];
    while (tail < head - 1 && abs(h_sum - t_sum) > abs((h_sum - A[tail + 1]) -
                                                       (t_sum + A[tail + 1]))) {
      ++tail;
      t_sum += A[tail], h_sum -= A[tail];
    }
    P[head] = make_pair(t_sum, h_sum);
  }
  return;
}

int main(void) {

  cin >> N;
  for (ll i = 0; i < N; ++i)
    cin >> A[i];

  MakePair(0, A[0], 0, L);
  reverse(A.begin(), A.begin() + N);
  MakePair(0, A[0], 0, R);

  ll j = N - 3, ans = LLONG_MAX;
  for (ll i = 1; i < N - 2; ++i) {
    ll mx = max(max(L[i].first, L[i].second), max(R[j].first, R[j].second));
    ll mn = min(min(L[i].first, L[i].second), min(R[j].first, R[j].second));
    --j;
    ans = min(ans, mx - mn);
  }
  cout << ans << endl;

  return 0;
}
Problem p03312 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
#define ll long long
using namespace std;
int n;
long long a[100005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] += a[i - 1];
  }
  int p1 = 1, p2 = 2, p3 = 3;
  long long ans = 9999999999999999LL;
  for (; p2 < n - 1; p2++) {
    long long mn = 9999999999999999LL, mx = 0;
    for (int i = p1; i < p2; i++) {
      if (abs((ll)((ll)a[p2] - a[i]) - a[i]) < mn) {
        mn = abs((ll)((ll)a[p2] - a[i]) - a[i]);
        p1 = i;
      } else
        break;
    }
    mn = 9999999999999999LL;
    p3 = max(p3, p2 + 1);
    for (int i = p3; i < n; i++) {
      if (abs((ll)((ll)a[n] - a[i]) - ((ll)a[i] - a[p2])) < mn) {
        mn = abs((ll)((ll)a[n] - a[i]) - ((ll)a[i] - a[p2]));
        p3 = i;
      } else
        break;
    }
    mn = min(a[p1],
             min((ll)a[p2] - a[p1], min((ll)a[p3] - a[p2], (ll)a[n] - a[p3])));
    mx = max(a[p1],
             max((ll)a[p2] - a[p1], max((ll)a[p3] - a[p2], (ll)a[n] - a[p3])));
    ans = min(ans, (ll)mx - mn);
  }
  printf("%lld", ans);
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
#define ll long long
using namespace std;
int n;
long long a[200005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] += a[i - 1];
  }
  int p1 = 1, p2 = 2, p3 = 3;
  long long ans = 9999999999999999LL;
  for (; p2 < n - 1; p2++) {
    long long mn = 9999999999999999LL, mx = 0;
    for (int i = p1; i < p2; i++) {
      if (abs((ll)((ll)a[p2] - a[i]) - a[i]) < mn) {
        mn = abs((ll)((ll)a[p2] - a[i]) - a[i]);
        p1 = i;
      } else
        break;
    }
    mn = 9999999999999999LL;
    p3 = max(p3, p2 + 1);
    for (int i = p3; i < n; i++) {
      if (abs((ll)((ll)a[n] - a[i]) - ((ll)a[i] - a[p2])) < mn) {
        mn = abs((ll)((ll)a[n] - a[i]) - ((ll)a[i] - a[p2]));
        p3 = i;
      } else
        break;
    }
    mn = min(a[p1],
             min((ll)a[p2] - a[p1], min((ll)a[p3] - a[p2], (ll)a[n] - a[p3])));
    mx = max(a[p1],
             max((ll)a[p2] - a[p1], max((ll)a[p3] - a[p2], (ll)a[n] - a[p3])));
    ans = min(ans, (ll)mx - mn);
  }
  printf("%lld", ans);
}
Problem p03312 (C++)

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

#define endl '\n'
#define fi first
#define se second
#define MOD(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, n) for (int i = 0; i < n; i++)
#define forr(i, a, b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb(x) push_back(x)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

const int MX = 200005;
int n, a[MX];
ll s[MX], res = 1e18;

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

  cin >> n;

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

  forn(i, n - 1) {
    int p1 = lower_bound(s, s + n, s[i] / 2) - s;
    int p2 = lower_bound(s, s + n, s[i] + (s[n - 1] - s[i]) / 2) - s;

    for (int x = p1 - 10; x < p1 + 10; x++)
      for (int y = p2 - 10; y < p2 + 10; y++)
        if (0 <= x && x < i && i < y && y < n - 1) {
          vector<ll> v = {s[x], s[i] - s[x], s[y] - s[i], s[n - 1] - s[y]};
          sort(all(v));
          res = min(res, v[3] - v[0]);
        }
  }

  cout << res << endl;

  return 0;
}


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

#define endl '\n'
#define fi first
#define se second
#define MOD(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, n) for (int i = 0; i < n; i++)
#define forr(i, a, b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb(x) push_back(x)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

const int MX = 200005;
int n, a[MX];
ll s[MX], res = 1e18;

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

  cin >> n;

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

  forn(i, n - 1) {
    int p1 = lower_bound(s, s + n, s[i] / 2) - s;
    int p2 = lower_bound(s, s + n, s[i] + (s[n - 1] - s[i]) / 2) - s;

    for (int x = p1 - 4; x < p1 + 4; x++)
      for (int y = p2 - 4; y < p2 + 4; y++)
        if (0 <= x && x < i && i < y && y < n - 1) {
          vector<ll> v = {s[x], s[i] - s[x], s[y] - s[i], s[n - 1] - s[y]};
          sort(all(v));
          res = min(res, v[3] - v[0]);
        }
  }

  cout << res << endl;

  return 0;
}
Problem p03312 (C++)

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

using ll = long long;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<ll> pref(N + 1);
  for (int i = 0; i < N; i++)
    pref[i + 1] = pref[i] + A[i];
  ll ans = pref[N];
  auto getHalf = [&](const multiset<ll> ms, ll v) {
    auto it = ms.lower_bound((v + 1) / 2);
    ll res = 0;
    if (it != ms.end())
      res = max(res, v - *it);
    if (it != ms.begin())
      res = max(res, *(--it));
    return res;
  };
  multiset<ll> prefs;
  multiset<ll> suffs;
  prefs.insert(pref[1]);
  for (int i = 2; i <= N - 1; i++)
    suffs.insert(pref[N] - pref[i]);
  for (int i = 2; i <= N - 2; i++) {
    suffs.erase(suffs.find(pref[N] - pref[i]));
    ll p = pref[i];
    ll s = pref[N] - pref[i];
    ll ph = getHalf(prefs, p);
    ll sh = getHalf(suffs, s);
    ans = min(ans, max(s - sh, p - ph) - min(ph, sh));
    prefs.insert(pref[i]);
  }
  cout << ans << '\n';
}

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

using ll = long long;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  vector<ll> pref(N + 1);
  for (int i = 0; i < N; i++)
    pref[i + 1] = pref[i] + A[i];
  ll ans = pref[N];
  auto getHalf = [&](const multiset<ll> &ms, ll v) {
    auto it = ms.lower_bound((v + 1) / 2);
    ll res = 0;
    if (it != ms.end())
      res = max(res, v - *it);
    if (it != ms.begin())
      res = max(res, *(--it));
    return res;
  };
  multiset<ll> prefs;
  multiset<ll> suffs;
  prefs.insert(pref[1]);
  for (int i = 2; i <= N - 1; i++)
    suffs.insert(pref[N] - pref[i]);
  for (int i = 2; i <= N - 2; i++) {
    suffs.erase(suffs.find(pref[N] - pref[i]));
    ll p = pref[i];
    ll s = pref[N] - pref[i];
    ll ph = getHalf(prefs, p);
    ll sh = getHalf(suffs, s);
    ans = min(ans, max(s - sh, p - ph) - min(ph, sh));
    prefs.insert(pref[i]);
  }
  cout << ans << '\n';
}
Problem p03312 (C++)

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

int main() {
  static long long N, A, B[100010] = {}, ANS = 1000000000000;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A;
    B[i + 1] = B[i] + A;
  }
  for (int i = 2; i <= N - 2; i++) {
    long long P, Q, R, S;
    long long X = B[i], Y = B[N] - B[i];
    int Left = 0, Right = i;
    while (Right - Left > 1) {
      int Mid = (Left + Right) / 2;
      if (B[Mid] * 2 <= X)
        Left = Mid;
      else
        Right = Mid;
    }
    P = max(B[Left], X - B[Right]), Q = min(B[Right], X - B[Left]);
    Left = i, Right = N;
    while (Right - Left > 1) {
      int Mid = (Left + Right) / 2;
      if ((B[Mid] - B[i]) * 2 <= Y)
        Left = Mid;
      else
        Right = Mid;
    }
    R = max(B[Left] - X, B[N] - B[Right]),
    S = min(B[Right] - X, B[N] - B[Left]);
    ANS = min(ANS, max(Q, S) - min(P, R));
  }
  cout << ANS;
}

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

int main() {
  static long long N, A, B[200010] = {}, ANS = 1000000000000;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A;
    B[i + 1] = B[i] + A;
  }
  for (int i = 2; i <= N - 2; i++) {
    long long P, Q, R, S;
    long long X = B[i], Y = B[N] - B[i];
    int Left = 0, Right = i;
    while (Right - Left > 1) {
      int Mid = (Left + Right) / 2;
      if (B[Mid] * 2 <= X)
        Left = Mid;
      else
        Right = Mid;
    }
    P = max(B[Left], X - B[Right]), Q = min(B[Right], X - B[Left]);
    Left = i, Right = N;
    while (Right - Left > 1) {
      int Mid = (Left + Right) / 2;
      if ((B[Mid] - B[i]) * 2 <= Y)
        Left = Mid;
      else
        Right = Mid;
    }
    R = max(B[Left] - X, B[N] - B[Right]),
    S = min(B[Right] - X, B[N] - B[Left]);
    ANS = min(ANS, max(Q, S) - min(P, R));
  }
  cout << ANS;
}