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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define N 2010
#define INF 1e18
using namespace std;
template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  int fh = 1;
  while (!isdigit(c)) {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  x *= fh;
}
ll n, l, a[N], b[N], sa[N], sb[N], ansn;
ll getans(int x) {
  ll sum = 0;
  int y = n - x;
  if (x <= y) {
    sum = (sa[x] + sb[x + x] - sb[x]) * 2 - a[x];
  } else {
    sum = (sa[x] - sa[x - y - 1] + sb[n] - sb[x]) * 2 - a[x];
  }
  return sum;
}
int main() {
  read(l);
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = 1; i <= n; i++)
    b[i] = l - a[i];
  for (int i = 1; i <= n; i++)
    sa[i] = sa[i - 1] + a[i], sb[i] = sb[i - 1] + b[i];
  for (int i = 1; i <= n; i++) {
    ansn = max(ansn, getans(i));
  }
  reverse(a + 1, a + n + 1);
  reverse(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    swap(a[i], b[i]);
  for (int i = 1; i <= n; i++)
    sa[i] = sa[i - 1] + a[i], sb[i] = sb[i - 1] + b[i];
  for (int i = 1; i <= n; i++) {
    ansn = max(ansn, getans(i));
  }
  cout << ansn << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define N 200010
#define INF 1e18
using namespace std;
template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  int fh = 1;
  while (!isdigit(c)) {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  x *= fh;
}
ll n, l, a[N], b[N], sa[N], sb[N], ansn;
ll getans(int x) {
  ll sum = 0;
  int y = n - x;
  if (x <= y) {
    sum = (sa[x] + sb[x + x] - sb[x]) * 2 - a[x];
  } else {
    sum = (sa[x] - sa[x - y - 1] + sb[n] - sb[x]) * 2 - a[x];
  }
  return sum;
}
int main() {
  read(l);
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = 1; i <= n; i++)
    b[i] = l - a[i];
  for (int i = 1; i <= n; i++)
    sa[i] = sa[i - 1] + a[i], sb[i] = sb[i - 1] + b[i];
  for (int i = 1; i <= n; i++) {
    ansn = max(ansn, getans(i));
  }
  reverse(a + 1, a + n + 1);
  reverse(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++)
    swap(a[i], b[i]);
  for (int i = 1; i <= n; i++)
    sa[i] = sa[i - 1] + a[i], sb[i] = sb[i - 1] + b[i];
  for (int i = 1; i <= n; i++) {
    ansn = max(ansn, getans(i));
  }
  cout << ansn << endl;
  return 0;
}
Problem p03187 (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;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// cout << fixed << setprecision(20);

int L, N;
int X[110000];
int l[110000], r[110000];
int lsum[110000], rsum[110000];

signed main() {
  cin >> L >> N;
  for (int i = 0; i < N; i++)
    cin >> X[i];
  for (int i = 0; i < N; i++)
    l[i + 1] = X[i], r[N - i] = L - X[i];
  for (int i = 1; i <= N; i++)
    lsum[i] = l[i] + lsum[i - 1], rsum[i] = r[i] + rsum[i - 1];
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    int ll = i, rr = N - i;
    if (ll < rr) {
      ans = max(ans, l[i] + 2 * (lsum[ll - 1] + rsum[rr] - rsum[rr - ll]));
    } else {
      ans = max(ans, l[i] + 2 * (lsum[ll - 1] - lsum[ll - 1 - rr] + rsum[rr]));
    }
    rr = i, ll = N - i;
    if (rr < ll) {
      ans = max(ans, r[i] + 2 * (rsum[rr - 1] + lsum[ll] - lsum[ll - rr]));
    } else {
      ans = max(ans, r[i] + 2 * (rsum[rr - 1] - rsum[rr - 1 - ll] + lsum[ll]));
    }
  }
  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;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// cout << fixed << setprecision(20);

int L, N;
int X[220000];
int l[220000], r[220000];
int lsum[220000], rsum[220000];

signed main() {
  cin >> L >> N;
  for (int i = 0; i < N; i++)
    cin >> X[i];
  for (int i = 0; i < N; i++)
    l[i + 1] = X[i], r[N - i] = L - X[i];
  for (int i = 1; i <= N; i++)
    lsum[i] = l[i] + lsum[i - 1], rsum[i] = r[i] + rsum[i - 1];
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    int ll = i, rr = N - i;
    if (ll < rr) {
      ans = max(ans, l[i] + 2 * (lsum[ll - 1] + rsum[rr] - rsum[rr - ll]));
    } else {
      ans = max(ans, l[i] + 2 * (lsum[ll - 1] - lsum[ll - 1 - rr] + rsum[rr]));
    }
    rr = i, ll = N - i;
    if (rr < ll) {
      ans = max(ans, r[i] + 2 * (rsum[rr - 1] + lsum[ll] - lsum[ll - rr]));
    } else {
      ans = max(ans, r[i] + 2 * (rsum[rr - 1] - rsum[rr - 1 - ll] + lsum[ll]));
    }
  }
  cout << ans << endl;
}
Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 100005;
#define ll long long
int l[N], r[N];
ll sl[N], sr[N];
inline int read() {
  int x = 0;
  char e = getchar();
  for (; e < '0' || e > '9'; e = getchar())
    ;
  for (; e >= '0' && e <= '9'; e = getchar())
    x = (x << 1) + (x << 3) + (e ^ 48);
  return x;
}
inline ll Max(ll a, ll b) { return a > b ? a : b; }
int main() {
  int len = read(), n = read();
  for (int i = 1; i <= n; i++)
    l[i] = read();
  for (int i = 1; i <= n; i++)
    r[i] = len - l[n + 1 - i];
  for (int i = 1; i <= n; i++) {
    sl[i] = sl[i - 1] + l[i];
    sr[i] = sr[i - 1] + r[i];
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ll cur;
    int j = i + ((n - i) >> 1), k = (n - i + 1) >> 1;
    cur = (sl[j] - sl[i - 1] + sr[k]) << 1ll;
    if ((n - i) & 1)
      cur -= r[k];
    else
      cur -= l[j];
    ans = Max(ans, cur);
    cur = (sr[j] - sr[i - 1] + sl[k]) << 1ll;
    if ((n - i) & 1)
      cur -= l[k];
    else
      cur -= r[j];
    ans = Max(ans, cur);
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 200005;
#define ll long long
int l[N], r[N];
ll sl[N], sr[N];
inline int read() {
  int x = 0;
  char e = getchar();
  for (; e < '0' || e > '9'; e = getchar())
    ;
  for (; e >= '0' && e <= '9'; e = getchar())
    x = (x << 1) + (x << 3) + (e ^ 48);
  return x;
}
inline ll Max(ll a, ll b) { return a > b ? a : b; }
int main() {
  int len = read(), n = read();
  for (int i = 1; i <= n; i++)
    l[i] = read();
  for (int i = 1; i <= n; i++)
    r[i] = len - l[n + 1 - i];
  for (int i = 1; i <= n; i++) {
    sl[i] = sl[i - 1] + l[i];
    sr[i] = sr[i - 1] + r[i];
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ll cur;
    int j = i + ((n - i) >> 1), k = (n - i + 1) >> 1;
    cur = (sl[j] - sl[i - 1] + sr[k]) << 1ll;
    if ((n - i) & 1)
      cur -= r[k];
    else
      cur -= l[j];
    ans = Max(ans, cur);
    cur = (sr[j] - sr[i - 1] + sl[k]) << 1ll;
    if ((n - i) & 1)
      cur -= l[k];
    else
      cur -= r[j];
    ans = Max(ans, cur);
  }
  printf("%lld", ans);
  return 0;
}
Problem p03187 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll n, m;

int main() {
  ll l;
  cin >> l >> n;
  ll x[n + 1], cumsum[n + 1], icumsum[n + 1];
  for (ll i = 1; i <= n; i++) {
    cin >> x[i];
  }
  for (ll i = 0; i <= n; i++) {
    cumsum[i] = 0;
    icumsum[i] = 0;
  }

  for (ll i = 1; i <= n; i++) {
    for (ll j = i; j <= n; j++) {
      cumsum[j] += x[i];
      icumsum[j] += l - x[n - i + 1];
    }
  }
  ll max = 0;
  // for(ll i = 0; i <= n; i++){
  //	cout << cumsum[i] << "," << icumsum[i] << endl;
  // }
  for (ll i = 1; i <= n; i++) {
    ll buf = 0;
    if ((n - i) % 2 == 0) {
      ll end = (n + i) / 2;
      buf += 2 * (cumsum[end - 1] - cumsum[i - 1] + icumsum[n - end]) + x[end];
    } else {
      ll end = (n + i) / 2 + 1;
      buf += 2 * (cumsum[end - 1] - cumsum[i - 1] + icumsum[n - end]) +
             (l - x[end]);
    }
    if (buf > max) {
      max = buf;
    }
  }
  for (ll i = 1; i <= n; i++) {
    ll buf = 0;
    if (i % 2 == 0) {
      ll end = i / 2;
      buf += 2 * (cumsum[end - 1] + icumsum[n - end] - icumsum[n - i]) + x[end];
    } else {
      ll end = i / 2 + 1;
      buf += 2 * (cumsum[end - 1] + icumsum[n - end] - icumsum[n - i]) +
             (l - x[end]);
    }
    if (buf > max) {
      max = buf;
    }
  }
  cout << max;

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll n, m;

int main() {
  ll l;
  cin >> l >> n;
  ll x[n + 1], cumsum[n + 1], icumsum[n + 1];
  for (ll i = 1; i <= n; i++) {
    cin >> x[i];
  }
  for (ll i = 0; i <= n; i++) {
    cumsum[i] = 0;
    icumsum[i] = 0;
  }

  for (ll i = 1; i <= n; i++) {
    cumsum[i] = cumsum[i - 1] + x[i];
    icumsum[i] = icumsum[i - 1] + l - x[n - i + 1];
  }
  ll max = 0;
  // for(ll i = 0; i <= n; i++){
  //	cout << cumsum[i] << "," << icumsum[i] << endl;
  // }
  for (ll i = 1; i <= n; i++) {
    ll buf = 0;
    if ((n - i) % 2 == 0) {
      ll end = (n + i) / 2;
      buf += 2 * (cumsum[end - 1] - cumsum[i - 1] + icumsum[n - end]) + x[end];
    } else {
      ll end = (n + i) / 2 + 1;
      buf += 2 * (cumsum[end - 1] - cumsum[i - 1] + icumsum[n - end]) +
             (l - x[end]);
    }
    if (buf > max) {
      max = buf;
    }
  }
  for (ll i = 1; i <= n; i++) {
    ll buf = 0;
    if (i % 2 == 0) {
      ll end = i / 2;
      buf += 2 * (cumsum[end - 1] + icumsum[n - end] - icumsum[n - i]) + x[end];
    } else {
      ll end = i / 2 + 1;
      buf += 2 * (cumsum[end - 1] + icumsum[n - end] - icumsum[n - i]) +
             (l - x[end]);
    }
    if (buf > max) {
      max = buf;
    }
  }
  cout << max;

  return 0;
}
Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
int n, m;
long long pre[100001][2], las[100011][2], x[100001], l, ans;
long long getmax(long long aa, long long bb) {
  if (aa > bb)
    return (aa);
  else
    return (bb);
}
int main() {
  scanf("%lld%d", &l, &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &x[i]);
  pre[0][0] = pre[0][1] = 0;
  for (int i = 1; i <= n; i++)
    pre[i][0] = x[i];
  las[n + 1][0] = las[n + 1][1] = 0;
  for (int i = n; i >= 1; i--)
    las[i][0] = l - x[i];
  for (int i = 1; i <= n; i++)
    pre[i][1] = pre[i - 1][1] + pre[i][0];
  for (int i = n; i >= 1; i--)
    las[i][1] = las[i + 1][1] + las[i][0];
  ans = las[n][0];
  if (pre[n][0] > ans)
    ans = pre[n][0];
  for (int i = 1; i < n; i++) {
    if ((n - i) > i)
      ans =
          getmax(ans, pre[i][1] * 2 + (las[i + 1][1] - las[2 * i + 2][1]) * 2 -
                          las[i + 1][0]);
    else
      ans = getmax(ans, (pre[i][1] - pre[2 * i - n][1]) * 2 +
                            las[i + 1][1] * 2 - las[i + 1][0]);
    if ((n - i) < i)
      ans = getmax(ans, (pre[i][1] - pre[2 * i - n - 1][1]) * 2 - pre[i][0] +
                            las[i + 1][1] * 2);
    else
      ans = getmax(ans, pre[i][1] * 2 - pre[i][0] +
                            (las[i + 1][1] - las[2 * i + 1][1]) * 2);
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <cstdio>
int n, m;
long long pre[200011][2], las[200011][2], x[200011], l, ans;
long long getmax(long long aa, long long bb) {
  if (aa > bb)
    return (aa);
  else
    return (bb);
}
int main() {
  scanf("%lld%d", &l, &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &x[i]);
  pre[0][0] = pre[0][1] = 0;
  for (int i = 1; i <= n; i++)
    pre[i][0] = x[i];
  las[n + 1][0] = las[n + 1][1] = 0;
  for (int i = n; i >= 1; i--)
    las[i][0] = l - x[i];
  for (int i = 1; i <= n; i++)
    pre[i][1] = pre[i - 1][1] + pre[i][0];
  for (int i = n; i >= 1; i--)
    las[i][1] = las[i + 1][1] + las[i][0];
  ans = las[n][0];
  if (pre[n][0] > ans)
    ans = pre[n][0];
  for (int i = 1; i < n; i++) {
    if ((n - i) > i)
      ans =
          getmax(ans, pre[i][1] * 2 + (las[i + 1][1] - las[2 * i + 2][1]) * 2 -
                          las[i + 1][0]);
    else
      ans = getmax(ans, (pre[i][1] - pre[2 * i - n][1]) * 2 +
                            las[i + 1][1] * 2 - las[i + 1][0]);
    if ((n - i) < i)
      ans = getmax(ans, (pre[i][1] - pre[2 * i - n - 1][1]) * 2 - pre[i][0] +
                            las[i + 1][1] * 2);
    else
      ans = getmax(ans, pre[i][1] * 2 - pre[i][0] +
                            (las[i + 1][1] - las[2 * i + 1][1]) * 2);
  }
  printf("%lld\n", ans);
}
Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long
#define M 100020
using namespace std;
LL read() {
  LL nm = 0, fh = 1;
  char cw = getchar();
  for (; !isdigit(cw); cw = getchar())
    if (cw == '-')
      fh = -fh;
  for (; isdigit(cw); cw = getchar())
    nm = nm * 10 + (cw - '0');
  return nm * fh;
}
LL n, m, D, p[M], F[M], ans, pre[M], suf[M];
void solve() {
  for (int i = 1; i <= n; i++)
    pre[i] = pre[i - 1] + p[i];
  for (int i = n; i; i--)
    suf[i] = suf[i + 1] + D - p[i];
  for (int i = 1; i <= n; i++) {
    LL lt, rt, now = 0;
    lt = ((n + i) >> 1), rt = lt + 1;
    now = ((pre[lt] - pre[i - 1] + suf[rt]) << 1);
    if ((n - i - 1) % 2 == 0)
      now -= D - p[rt];
    else
      now -= p[lt];
    ans = max(ans, now);
  }
}
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  D = read(), n = read();
  for (int i = 1; i <= n; i++)
    p[i] = read();
  ans = max(D - p[1], p[n]), solve();
  for (int i = 1; i <= n; i++)
    p[i] = D - p[i];
  reverse(p + 1, p + n + 1);
  solve(), printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long
#define M 1000020
using namespace std;
LL read() {
  LL nm = 0, fh = 1;
  char cw = getchar();
  for (; !isdigit(cw); cw = getchar())
    if (cw == '-')
      fh = -fh;
  for (; isdigit(cw); cw = getchar())
    nm = nm * 10 + (cw - '0');
  return nm * fh;
}
LL n, m, D, p[M], F[M], ans, pre[M], suf[M];
void solve() {
  for (int i = 1; i <= n; i++)
    pre[i] = pre[i - 1] + p[i];
  for (int i = n; i; i--)
    suf[i] = suf[i + 1] + D - p[i];
  for (int i = 1; i <= n; i++) {
    LL lt, rt, now = 0;
    lt = ((n + i) >> 1), rt = lt + 1;
    now = ((pre[lt] - pre[i - 1] + suf[rt]) << 1);
    if ((n - i - 1) % 2 == 0)
      now -= D - p[rt];
    else
      now -= p[lt];
    ans = max(ans, now);
  }
}
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  D = read(), n = read();
  for (int i = 1; i <= n; i++)
    p[i] = read();
  ans = max(D - p[1], p[n]), solve();
  for (int i = 1; i <= n; i++)
    p[i] = D - p[i];
  reverse(p + 1, p + n + 1);
  solve(), printf("%lld\n", ans);

  return 0;
}
Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define int long long
using namespace std;
const int N = 1e5 + 10, inf = 1e9;
int x[N], tmp[N], d1[N], d2[N];
int L, n, ans;
int D1(int l, int r) { return r < l ? 0 : (d1[r] - d1[l - 1]); }
int D2(int l, int r) { return r < l ? 0 : (d2[r] - d2[l - 1]); }
void Calc() {
  for (int i = 1; i <= n; i++)
    d1[i] = d1[i - 1] + x[i];
  for (int i = 1; i <= n; i++)
    d2[i] = d2[i - 1] + L - x[i];
  for (int i = 1, ps; i <= n / 2; i++) {
    ps = n - i * 2;
    ans = max(ans, D1(ps, ps + i - 1) * 2 + x[ps + i] + D2(ps + i + 1, n) * 2);
    ans = max(ans, D1(ps + 1, ps + i) * 2 + D2(ps + i + 2, n) * 2 + L -
                       x[ps + i + 1]);
  }
}
signed main() {
  cin >> L >> n;
  for (int i = 1; i <= n; i++)
    cin >> x[i];
  ans = max(ans, max(x[n], L - x[1]));
  Calc();
  for (int i = 1; i <= n; i++)
    tmp[i] = L - x[n - i + 1];
  for (int i = 1; i <= n; i++)
    x[i] = tmp[i];
  Calc();
  cout << ans << endl;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int x[N], tmp[N], d1[N], d2[N];
int L, n, ans;
int D1(int l, int r) { return r < l ? 0 : (d1[r] - d1[l - 1]); }
int D2(int l, int r) { return r < l ? 0 : (d2[r] - d2[l - 1]); }
void Calc() {
  for (int i = 1; i <= n; i++)
    d1[i] = d1[i - 1] + x[i];
  for (int i = 1; i <= n; i++)
    d2[i] = d2[i - 1] + L - x[i];
  for (int i = 1, ps; i <= n / 2; i++) {
    ps = n - i * 2;
    ans = max(ans, D1(ps, ps + i - 1) * 2 + x[ps + i] + D2(ps + i + 1, n) * 2);
    ans = max(ans, D1(ps + 1, ps + i) * 2 + D2(ps + i + 2, n) * 2 + L -
                       x[ps + i + 1]);
  }
}
signed main() {
  cin >> L >> n;
  for (int i = 1; i <= n; i++)
    cin >> x[i];
  ans = max(ans, max(x[n], L - x[1]));
  Calc();
  for (int i = 1; i <= n; i++)
    tmp[i] = L - x[n - i + 1];
  for (int i = 1; i <= n; i++)
    x[i] = tmp[i];
  Calc();
  cout << ans << endl;
}
Problem p03187 (C++)

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

typedef long long LL;
const int MAXN = 200011;
LL ld[MAXN], rd[MAXN];
LL a[MAXN];
int n, m;
LL l, ans;

int main() {
  scanf("%lld%d", &l, &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &a[i]);
  }
  ld[n] = 0;
  ld[n - 1] = l - (a[n] - a[n - 1]);
  int pre = n;
  for (int i = n - 2; i; --i) {
    if ((n - i + 1) & 1) {
      ld[i] = ld[i + 1] - (a[pre - 1] - a[i + 1]) + (l - (a[pre] - a[i]));
    } else {
      ld[i] = ld[i + 1] - (a[pre - 1] - a[i + 1]) + (l - (a[pre - 1] - a[i]));
      --pre;
    }
  }
  rd[1] = 0;
  rd[2] = l - (a[2] - a[1]);
  pre = 1;
  for (int i = 3; i <= n; ++i) {
    if (i & 1) {
      rd[i] = rd[i - 1] - (a[i - 1] - a[pre + 1]) + (l - (a[i] - a[pre]));
    } else {
      rd[i] = rd[i - 1] - (a[i - 1] - a[pre + 1]) + (l - (a[i] - a[pre + 1]));
      ++pre;
    }
  }
  ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, a[i] + ld[i]);
    ans = max(ans, l - a[i] + rd[i]);
  }
  printf("%lld\n", ans);
  return 0;
}

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

typedef long long LL;
const int MAXN = 200011;
LL ld[MAXN], rd[MAXN];
LL a[MAXN];
int n, m;
LL l, ans;

int main() {
  scanf("%lld%d", &l, &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &a[i]);
  }
  ld[n] = 0;
  ld[n - 1] = l - (a[n] - a[n - 1]);
  int pre = n;
  for (int i = n - 2; i > 0; --i) {
    if ((n - i + 1) & 1) {
      ld[i] = ld[i + 1] - (a[pre - 1] - a[i + 1]) + (l - (a[pre] - a[i]));
    } else {
      ld[i] = ld[i + 1] - (a[pre - 1] - a[i + 1]) + (l - (a[pre - 1] - a[i]));
      --pre;
    }
  }
  rd[1] = 0;
  rd[2] = l - (a[2] - a[1]);
  pre = 1;
  for (int i = 3; i <= n; ++i) {
    if (i & 1) {
      rd[i] = rd[i - 1] - (a[i - 1] - a[pre + 1]) + (l - (a[i] - a[pre]));
    } else {
      rd[i] = rd[i - 1] - (a[i - 1] - a[pre + 1]) + (l - (a[i] - a[pre + 1]));
      ++pre;
    }
  }
  ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, a[i] + ld[i]);
    ans = max(ans, l - a[i] + rd[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#define RI register int
#define CI const int &
using namespace std;
const int N = 100005;
int l, n, a[N];
long long pfx[N], sfx[N], ans;
int main() {
  RI i;
  for (scanf("%d%d", &l, &n), i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (i = 1; i <= n; ++i)
    pfx[i] = pfx[i - 1] + a[i];
  for (i = n; i; --i)
    sfx[i] = sfx[i + 1] + l - a[i];
  for (i = 1; i < n; ++i)
    ans = max(ans, 2LL * a[i] + 2LL * (pfx[i + n >> 1] - pfx[i]) +
                       2LL * sfx[(i + n >> 1) + 1] -
                       ((n - i) & 1 ? l - a[(i + n >> 1) + 1] : a[i + n >> 1]));
  for (i = n; i > 1; --i)
    ans = max(ans, 2LL * (l - a[i]) + 2LL * (sfx[i + 2 >> 1] - sfx[i]) +
                       2LL * pfx[(i + 2 >> 1) - 1] -
                       ((i - 1) & 1 ? a[(i + 2 >> 1) - 1] : l - a[i + 2 >> 1]));
  return printf("%lld", max(ans, (long long)max(a[n], l - a[1]))), 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define RI register int
#define CI const int &
using namespace std;
const int N = 200005;
int l, n, a[N];
long long pfx[N], sfx[N], ans;
int main() {
  RI i;
  for (scanf("%d%d", &l, &n), i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (i = 1; i <= n; ++i)
    pfx[i] = pfx[i - 1] + a[i];
  for (i = n; i; --i)
    sfx[i] = sfx[i + 1] + l - a[i];
  for (i = 1; i < n; ++i)
    ans = max(ans, 2LL * a[i] + 2LL * (pfx[i + n >> 1] - pfx[i]) +
                       2LL * sfx[(i + n >> 1) + 1] -
                       ((n - i) & 1 ? l - a[(i + n >> 1) + 1] : a[i + n >> 1]));
  for (i = n; i > 1; --i)
    ans = max(ans, 2LL * (l - a[i]) + 2LL * (sfx[i + 2 >> 1] - sfx[i]) +
                       2LL * pfx[(i + 2 >> 1) - 1] -
                       ((i - 1) & 1 ? a[(i + 2 >> 1) - 1] : l - a[i + 2 >> 1]));
  return printf("%lld", max(ans, (long long)max(a[n], l - a[1]))), 0;
}
Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int nmax = 2e3 + 4;
long long l, n;
long long x[nmax];
long long ac[nmax];

long long solve(bool right, int s) {
  long long ans;
  if (right) {
    if (!((n - s) & 1)) {
      ans = 2 * (-ac[s - 1] + ac[(s + n) / 2 - 1] + ac[(s + n) / 2] - ac[n]) +
            x[(s + n) / 2] + (n - s) * l;
    } else {
      ans = 2 * (-ac[s - 1] + ac[(s + n) / 2] + ac[(s + n) / 2 + 1] - ac[n]) -
            x[(s + n) / 2 + 1] + (n - s) * l;
    }
  } else {
    if (!(s & 1)) {
      ans = 2 * (ac[s / 2 - 1] + ac[s / 2] - ac[s]) + x[s / 2] + s * l;
    } else {
      ans = 2 * (ac[s / 2] + ac[s / 2 + 1] - ac[s]) - x[(s + 1) / 2] + s * l;
    }
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> l >> n;
  ac[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    ac[i] = ac[i - 1] + x[i];
  }

  long long maxv = 0;
  for (int start = 1; start <= n; start++) {
    for (int right = 0; right < 2; right++) {
      maxv = max(maxv, solve((bool)right, start));
    }
  }
  cout << maxv << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int nmax = 2e5 + 4;
long long l, n;
long long x[nmax];
long long ac[nmax];

long long solve(bool right, int s) {
  long long ans;
  if (right) {
    if (!((n - s) & 1)) {
      ans = 2 * (-ac[s - 1] + ac[(s + n) / 2 - 1] + ac[(s + n) / 2] - ac[n]) +
            x[(s + n) / 2] + (n - s) * l;
    } else {
      ans = 2 * (-ac[s - 1] + ac[(s + n) / 2] + ac[(s + n) / 2 + 1] - ac[n]) -
            x[(s + n) / 2 + 1] + (n - s) * l;
    }
  } else {
    if (!(s & 1)) {
      ans = 2 * (ac[s / 2 - 1] + ac[s / 2] - ac[s]) + x[s / 2] + s * l;
    } else {
      ans = 2 * (ac[s / 2] + ac[s / 2 + 1] - ac[s]) - x[(s + 1) / 2] + s * l;
    }
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> l >> n;
  ac[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    ac[i] = ac[i - 1] + x[i];
  }

  long long maxv = 0;
  for (int start = 1; start <= n; start++) {
    for (int right = 0; right < 2; right++) {
      maxv = max(maxv, solve((bool)right, start));
    }
  }
  cout << maxv << '\n';
  return 0;
}
Problem p03187 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define MAXN 200005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll L, n, a[MAXN], ans;
ll dp[MAXN];
ll go(ll x, ll y, bool dir) {
  ll ret;
  if (dir)
    ret = y - x;
  else
    ret = x - y;
  if (ret < 0)
    ret += L;
  return ret;
}
ll findans(ll step) {
  if (step == n)
    return go(0, a[n], true);
  else if (step == n - 1)
    return go(0, a[n - 1], true) + go(a[n - 1], a[n], false);
  if (dp[step] != -1)
    return dp[step];
  ll st = step, ed = (n - step) / 2;
  return findans(step + 2) + 2 * go(0, a[st], true) +
         2 * go(a[n - ed + 1], 0, true) +
         ((n - step) & 1 ? 1 : -1) * (a[n - ed + 1] - a[n - ed]) -
         2 * go(a[st + 1], a[n - ed], true);
}
int main() {
  scanf("%lld%lld", &L, &n);
  for (ll i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  memset(dp, -1, sizeof(dp));
  ll ans = 0;
  for (ll i = 1; i <= n; i++)
    ans = max(ans, findans(i));
  reverse(a + 1, a + n + 1);
  for (ll i = 1; i <= n; i++)
    a[i] = L - a[i];
  memset(dp, -1, sizeof(dp));
  for (ll i = 1; i <= n; i++)
    ans = max(ans, findans(i));
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define MAXN 200005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
ll L, n, a[MAXN], ans;
ll dp[MAXN];
ll go(ll x, ll y, bool dir) {
  ll ret;
  if (dir)
    ret = y - x;
  else
    ret = x - y;
  if (ret < 0)
    ret += L;
  return ret;
}
ll findans(ll step) {
  if (step == n)
    return go(0, a[n], true);
  else if (step == n - 1)
    return go(0, a[n - 1], true) + go(a[n - 1], a[n], false);
  if (dp[step] != -1)
    return dp[step];
  ll st = step, ed = (n - step) / 2;
  return dp[step] = findans(step + 2) + 2 * go(0, a[st], true) +
                    2 * go(a[n - ed + 1], 0, true) +
                    ((n - step) & 1 ? 1 : -1) * (a[n - ed + 1] - a[n - ed]) -
                    2 * go(a[st + 1], a[n - ed], true);
}
int main() {
  scanf("%lld%lld", &L, &n);
  for (ll i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  memset(dp, -1, sizeof(dp));
  ll ans = 0;
  for (ll i = 1; i <= n; i++)
    ans = max(ans, findans(i));
  reverse(a + 1, a + n + 1);
  for (ll i = 1; i <= n; i++)
    a[i] = L - a[i];
  memset(dp, -1, sizeof(dp));
  for (ll i = 1; i <= n; i++)
    ans = max(ans, findans(i));
  printf("%lld\n", ans);
  return 0;
}
Problem p03188 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define title "title"
#define ll long long
#define ull unsigned ll
#define fix(x) fixed << setprecision(x)
#define pii pair<int, int>
#define vint vector<int>
#define pb push_back
using namespace std;
void Freopen() {
  freopen(title ".in", "r", stdin);
  freopen(title ".out", "w", stdout);
}
int read() {
  int g = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    g = g * 10 + ch - '0';
    ch = getchar();
  }
  return g * f;
}
const int N = 505;
const int maxn = 500;
int k;
namespace solve1 {
void Main() {
  cout << k << '\n';
  for (int i = 1; i <= k; i++, cout << '\n')
    for (int j = 1; j <= k; j++, cout << ' ')
      cout << i;
}
} // namespace solve1
namespace solve2 {
int a[N][N];
void Main() {
  cout << maxn << '\n';
  for (int i = 1; i <= maxn; i++)
    for (int j = 1; j <= maxn; j++)
      a[i][j] = (i + j - 2) % maxn + 1;
  for (int i = maxn + 1; i <= k; i++)
    for (int j = 1; j <= maxn; j += 2) {
      int x = i - maxn - 1 + j, y = (i - x) % maxn + 1;
      a[x][y] = i;
    }
  for (int i = 1; i <= maxn; i++, cout << '\n')
    for (int j = 1; j <= maxn; j++, cout << ' ')
      cout << a[i][j];
}
} // namespace solve2
signed main() {
  k = read();
  if (k <= 500)
    solve1::Main();
  else
    solve2::Main();
  return signed();
}

Fixed submission:
#include <bits/stdc++.h>
#define title "title"
#define ll long long
#define ull unsigned ll
#define fix(x) fixed << setprecision(x)
#define pii pair<int, int>
#define vint vector<int>
#define pb push_back
using namespace std;
void Freopen() {
  freopen(title ".in", "r", stdin);
  freopen(title ".out", "w", stdout);
}
int read() {
  int g = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    g = g * 10 + ch - '0';
    ch = getchar();
  }
  return g * f;
}
const int N = 505;
const int maxn = 500;
int k;
namespace solve1 {
void Main() {
  cout << k << '\n';
  for (int i = 1; i <= k; i++, cout << '\n')
    for (int j = 1; j <= k; j++, cout << ' ')
      cout << i;
}
} // namespace solve1
namespace solve2 {
int a[N][N];
void Main() {
  cout << maxn << '\n';
  for (int i = 1; i <= maxn; i++)
    for (int j = 1; j <= maxn; j++)
      a[i][j] = (i + j - 2) % maxn + 1;
  for (int i = maxn + 1; i <= k; i++)
    for (int j = 1; j <= maxn; j += 2) {
      int x = (i - maxn - 2 + j) % maxn + 1, y = (i - x) % maxn + 1;
      a[x][y] = i;
    }
  for (int i = 1; i <= maxn; i++, cout << '\n')
    for (int j = 1; j <= maxn; j++, cout << ' ')
      cout << a[i][j];
}
} // namespace solve2
signed main() {
  k = read();
  if (k <= 500)
    solve1::Main();
  else
    solve2::Main();
  return signed();
}
Problem p03188 (C++)

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

using namespace std;

using int64 = long long;

int main() {

  int X[500][500];

  int C;
  cin >> C;

  int K = 2 * (C + 3) / 4;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      if (i % 2 == 0)
        X[i][j] = (i + j) % K;
      else
        X[i][j] = K + (i + j) % K;
    }
  }

  cout << K << endl;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      ++X[i][j];
      if (X[i][j] > C)
        X[i][j] -= K;
      cout << X[i][j] << " ";
    }
    cout << endl;
  }
}


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

using namespace std;

using int64 = long long;

int main() {

  int X[500][500];

  int C;
  cin >> C;
  if (C == 1) {
    cout << 1 << endl;
    cout << 1 << endl;
    return 0;
  }
  int K = 2 * ((C + 3) / 4);
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      if (i % 2 == 0)
        X[i][j] = (i + j) % K;
      else
        X[i][j] = K + (i + j) % K;
    }
  }

  cout << K << endl;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      ++X[i][j];
      if (X[i][j] > C)
        X[i][j] -= K;
      cout << X[i][j] << " ";
    }
    cout << endl;
  }
}
Problem p03188 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <cstdio>

using namespace std;

int K;

int main() {
  scanf("%d", &K);
  if (K <= 500) {
    printf("%d\n", K);
    for (int i = 1; i <= K; ++i) {
      for (int j = 1; j <= K; ++j)
        printf("%d ", i);
      printf("\n");
    }
  } else
    while (true)
      ;
  return 0;
}

Fixed submission:

#include <cstdio>

using namespace std;

int K;

int main() {
  scanf("%d", &K);
  if (K <= 500) {
    printf("%d\n", K);
    for (int i = 1; i <= K; ++i) {
      for (int j = 1; j <= K; ++j)
        printf("%d ", i);
      printf("\n");
    }
  } else {
    int N = ((K / 4) + (K % 4 != 0)) * 2;
    printf("%d\n", N);
    for (int i = 1; i <= N; ++i) {
      for (int j = 1, v; j <= N; ++j) {
        v = (i + j) % N + (i & 1) * N + 1;
        if (v > K)
          v -= N;
        printf("%d ", v);
      }
      printf("\n");
    }
  }
  return 0;
}
Problem p03188 (C++)

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

int main() {
  int K;
  scanf("%d", &K);
  if (K % 2 == 0) {
    printf("%d\n", K / 2);
    for (int i = 1; i <= K / 2; ++i)
      for (int j = 1; j <= K / 2; ++j)
        printf("%d%c", (i % 2 == 1 ? 0 : K / 2) + j, " \n"[j == K / 2]);
  } else
    return 1;
}


Fixed submission:
#include <cstdio>

int main() {
  int K;
  scanf("%d", &K);
  if (K == 1)
    return !puts("1\n1");
  int n = (K + 3) / 4 * 2;
  printf("%d\n", n);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      printf("%d%c", (i % 2 && n + (i + j) % n < K ? n : 0) + (i + j) % n + 1,
             " \n"[j + 1 == n]);
}
Problem p03189 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define MP make_pair

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MOD = 1e9 + 7;
const int INV2 = (MOD + 1) / 2;

int dp[2005][2005];
int n, p[2005];
PII pinv[2005];
int m, u[2005], v[2005];
int cdp[2005];

int main() {
  scanf("%d%d", &n, &m);
  rep1(i, n) scanf("%d", &p[i]);
  rep(i, m) scanf("%d%d", &u[i], &v[i]);

  rep1(i, n) dp[i][i] = 1;
  rep(i, m) rep1(j, n) {
    int cur = 1LL * (dp[j][u[i]] + dp[j][v[i]]) * INV2 % MOD;
    dp[j][u[i]] = dp[j][v[i]] = cur;
  }

  rep1(i, n) pinv[i] = MP(p[i], i);
  sort(pinv + 1, pinv + n + 1);
  int ans = 0;
  rep1(i, n) {
    int tot = 0;
    for (int j = n; j >= 1; j--) {
      ans = (ans +
             1LL * (tot + 1LL * cdp[j] * INV2 % MOD) * dp[pinv[i].second][j]) %
            MOD;
      tot = (tot + cdp[j]) % MOD;
    }
    if (i == n || pinv[i].first != pinv[i + 1].first)
      for (int i0 = i; i0 >= 0 && pinv[i0].first == pinv[i].first; i0--)
        rep1(j, n) cdp[j] = (cdp[j] + dp[pinv[i0].second][j]) % MOD;
  }

  rep(i, m) ans = ans * 2 % MOD;
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define MP make_pair

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MOD = 1e9 + 7;
const int INV2 = (MOD + 1) / 2;

int dp[3005][3005];
int n, p[3005];
PII pinv[3005];
int m, u[3005], v[3005];
int cdp[3005];

int main() {
  scanf("%d%d", &n, &m);
  rep1(i, n) scanf("%d", &p[i]);
  rep(i, m) scanf("%d%d", &u[i], &v[i]);

  rep1(i, n) dp[i][i] = 1;
  rep(i, m) rep1(j, n) {
    int cur = 1LL * (dp[j][u[i]] + dp[j][v[i]]) * INV2 % MOD;
    dp[j][u[i]] = dp[j][v[i]] = cur;
  }

  rep1(i, n) pinv[i] = MP(p[i], i);
  sort(pinv + 1, pinv + n + 1);
  int ans = 0;
  rep1(i, n) {
    int tot = 0;
    for (int j = n; j >= 1; j--) {
      ans = (ans +
             1LL * (tot + 1LL * cdp[j] * INV2 % MOD) * dp[pinv[i].second][j]) %
            MOD;
      tot = (tot + cdp[j]) % MOD;
    }
    if (i == n || pinv[i].first != pinv[i + 1].first)
      for (int i0 = i; i0 >= 0 && pinv[i0].first == pinv[i].first; i0--)
        rep1(j, n) cdp[j] = (cdp[j] + dp[pinv[i0].second][j]) % MOD;
  }

  rep(i, m) ans = ans * 2 % MOD;
  printf("%d\n", ans);
  return 0;
}
Problem p03189 (C++)

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

#define N 1010

using namespace std;

typedef long long ll;

const int mod = 1000000007;

const int inv2 = 500000004;

char *p1, *p2, buf[100000];

#define nc()                                                                   \
  (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)      \
       ? EOF                                                                   \
       : *p1++)

int rd() {
  int x = 0, f = 1;
  char c = nc();
  while (c < 48) {
    if (c == '-')
      f = -1;
    c = nc();
  }
  while (c > 47) {
    x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
  }
  return x * f;
}

int a[N], f[N][N];

int main() {
  int n = rd(), q = rd(), mdl = 1;
  for (int i = 1; i <= n; i++) {
    a[i] = rd();
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (a[i] < a[j]) {
        f[i][j] = 1;
      }
    }
  }

  for (int i = 1; i <= q; i++) {
    mdl = (ll)mdl * 2 % mod;
    int x = rd(), y = rd();
    f[x][y] = f[y][x] = ((ll)f[x][y] + f[y][x]) * inv2 % mod;

    for (int j = 1; j <= n; j++) {
      if (j != x && j != y) {
        f[x][j] = f[y][j] = ((ll)f[x][j] + f[y][j]) * inv2 % mod;
        f[j][x] = f[j][y] = ((ll)f[j][x] + f[j][y]) * inv2 % mod;
      }
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      (ans += f[i][j]) %= mod;
    }
  }

  ans = (ll)ans * mdl % mod, (ans += mod) %= mod;
  cout << ans << endl;
  return 0;
}

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

#define N 3010

using namespace std;

typedef long long ll;

const int mod = 1000000007;

const int inv2 = 500000004;

char *p1, *p2, buf[100000];

#define nc()                                                                   \
  (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)      \
       ? EOF                                                                   \
       : *p1++)

int rd() {
  int x = 0, f = 1;
  char c = nc();
  while (c < 48) {
    if (c == '-')
      f = -1;
    c = nc();
  }
  while (c > 47) {
    x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
  }
  return x * f;
}

int a[N], f[N][N];

int main() {
  int n = rd(), q = rd(), mdl = 1;
  for (int i = 1; i <= n; i++) {
    a[i] = rd();
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (a[i] < a[j]) {
        f[i][j] = 1;
      }
    }
  }

  for (int i = 1; i <= q; i++) {
    mdl = (ll)mdl * 2 % mod;
    int x = rd(), y = rd();
    f[x][y] = f[y][x] = ((ll)f[x][y] + f[y][x]) * inv2 % mod;

    for (int j = 1; j <= n; j++) {
      if (j != x && j != y) {
        f[x][j] = f[y][j] = ((ll)f[x][j] + f[y][j]) * inv2 % mod;
        f[j][x] = f[j][y] = ((ll)f[j][x] + f[j][y]) * inv2 % mod;
      }
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      (ans += f[i][j]) %= mod;
    }
  }

  ans = (ll)ans * mdl % mod, (ans += mod) %= mod;
  cout << ans << endl;
  return 0;
}
Problem p03189 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using i64 = long long;

const int N = 3000 + 7;
const i64 MOD = 1000000007;
const i64 Inv2 = MOD - MOD / 2;

int n, q;
int a[N];
i64 f[N][N], g[N][N], ans, coe = 1;

int main() {
  //	freopen("code.in", "r", stdin);
  //	freopen("code.out", "w", stdout);

  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      f[i][j] = g[i][j] = a[i] > a[j];
  for (int x, y; q; --q) {
    scanf("%d%d", &x, &y);
    for (int i = 1; i <= n; ++i) {
      if (i != x)
        g[i][x] = (f[i][x] + f[i][y]) * Inv2 % MOD,
        g[x][i] = (f[x][i] + f[y][i]) * Inv2 % MOD;
      if (i != y)
        g[i][y] = (f[i][x] + f[i][y]) * Inv2 % MOD,
        g[y][i] = (f[x][i] + f[y][i]) * Inv2 % MOD;
    }
    g[x][y] = g[y][x] = (f[x][y] + f[y][x]) * Inv2 % MOD;
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j)
        f[i][j] = g[i][j];
    coe = coe * 2 % MOD;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      (ans += f[i][j]) %= MOD;
  printf("%lld ", ans * coe % MOD);
  return 0;
}


Fixed submission:
#include <iostream>

using i64 = long long;

const int N = 3000 + 7;
const i64 MOD = 1000000007;
const i64 Inv2 = MOD - MOD / 2;

int n, q;
int a[N];
i64 f[N][N], g[N][N], ans, coe = 1;

int main() {
  //	freopen("code.in", "r", stdin);
  //	freopen("code.out", "w", stdout);

  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      f[i][j] = g[i][j] = a[i] > a[j];
  for (int x, y; q; --q) {
    scanf("%d%d", &x, &y);
    for (int i = 1; i <= n; ++i) {
      if (i != x)
        g[i][x] = (f[i][x] + f[i][y]) * Inv2 % MOD,
        g[x][i] = (f[x][i] + f[y][i]) * Inv2 % MOD;
      if (i != y)
        g[i][y] = (f[i][x] + f[i][y]) * Inv2 % MOD,
        g[y][i] = (f[x][i] + f[y][i]) * Inv2 % MOD;
    }
    g[x][y] = g[y][x] = (f[x][y] + f[y][x]) * Inv2 % MOD;
    for (int i = 1; i <= n; ++i)
      f[i][x] = g[i][x], f[x][i] = g[x][i], f[i][y] = g[i][y],
      f[y][i] = g[y][i];
    f[x][y] = f[y][x] = g[x][y];
    coe = coe * 2 % MOD;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      (ans += f[i][j]) %= MOD;
  printf("%lld ", ans * coe % MOD);
  return 0;
}
Problem p03189 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ld long double
// This code is written by Itst
using namespace std;

inline int read() {
  int a = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c) && c != EOF) {
    if (c == '-')
      f = 1;
    c = getchar();
  }
  if (c == EOF)
    exit(0);
  while (isdigit(c)) {
    a = a * 10 + c - 48;
    c = getchar();
  }
  return f ? -a : a;
}

const int MOD = 1e9 + 7, inv2 = 5e8 + 4;
long long dp[2007][2007];
int num[2007], N, M;

int main() {
  N = read();
  M = read();
  for (int i = 1; i <= N; ++i)
    num[i] = read();
  for (int i = 1; i <= N; ++i)
    for (int j = i - 1; j; --j) {
      dp[i][j] = num[i] > num[j];
      dp[j][i] = num[j] > num[i];
    }
  for (int i = 1; i <= M; ++i) {
    int a = read(), b = read();
    for (int j = 1; j <= N; ++j)
      if (j != a && j != b) {
        dp[j][a] = dp[j][b] = (dp[j][a] + dp[j][b]) * inv2 % MOD;
        dp[a][j] = dp[b][j] = (dp[a][j] + dp[b][j]) * inv2 % MOD;
      }
    dp[a][b] = dp[b][a] = (dp[a][b] + dp[b][a]) * inv2 % MOD;
  }
  long long sum = 0;
  for (int i = 1; i <= N; ++i)
    for (int j = i - 1; j; --j)
      sum = (sum + dp[j][i]) % MOD;
  for (int i = 1; i <= M; ++i)
    sum = sum * 2 % MOD;
  cout << sum;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ld long double
// This code is written by Itst
using namespace std;

inline int read() {
  int a = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c) && c != EOF) {
    if (c == '-')
      f = 1;
    c = getchar();
  }
  if (c == EOF)
    exit(0);
  while (isdigit(c)) {
    a = a * 10 + c - 48;
    c = getchar();
  }
  return f ? -a : a;
}

const int MOD = 1e9 + 7, inv2 = 5e8 + 4;
long long dp[3007][3007];
int num[3007], N, M;

int main() {
  N = read();
  M = read();
  for (int i = 1; i <= N; ++i)
    num[i] = read();
  for (int i = 1; i <= N; ++i)
    for (int j = i - 1; j; --j) {
      dp[i][j] = num[i] > num[j];
      dp[j][i] = num[j] > num[i];
    }
  for (int i = 1; i <= M; ++i) {
    int a = read(), b = read();
    for (int j = 1; j <= N; ++j)
      if (j != a && j != b) {
        dp[j][a] = dp[j][b] = (dp[j][a] + dp[j][b]) * inv2 % MOD;
        dp[a][j] = dp[b][j] = (dp[a][j] + dp[b][j]) * inv2 % MOD;
      }
    dp[a][b] = dp[b][a] = (dp[a][b] + dp[b][a]) * inv2 % MOD;
  }
  long long sum = 0;
  for (int i = 1; i <= N; ++i)
    for (int j = i - 1; j; --j)
      sum = (sum + dp[j][i]) % MOD;
  for (int i = 1; i <= M; ++i)
    sum = sum * 2 % MOD;
  cout << sum;
  return 0;
}
Problem p03189 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = 0;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return f ? x : -x;
}

typedef long long LL;
const int maxn = 3010, mod = 1e9 + 7;
int a[maxn], f[maxn][maxn];

int Pow(int x, int p) {
  int r = 1;
  while (p) {
    if (p & 1)
      r = (LL)x * r % mod;
    x = (LL)x * x % mod;
    p >>= 1;
  }
  return r;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif

  int n = read(), Q = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (a[i] < a[j])
        f[i][j] = 1;

  int inv2 = 500000004, tmp = Pow(2, Q);
  while (Q--) {
    int x = read(), y = read();
    f[x][y] = f[y][x] = (LL)(f[x][y] + f[y][x]) * inv2 % mod;
    for (int i = 1; i <= n; ++i)
      if (i != x && i != y)
        f[i][x] = f[i][y] = (LL)inv2 * (f[i][x] + f[i][y]) % mod,
        f[x][i] = f[y][i] = (LL)inv2 * (f[x][i] + f[y][i]) % mod;
  }

  int ans = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j < i; ++j)
      (ans += f[i][j]) %= mod;

  cout << (LL)ans * tmp % mod << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = 0;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return f ? x : -x;
}

typedef long long LL;
const int maxn = 3010, mod = 1e9 + 7;
int a[maxn], f[maxn][maxn];

int Pow(int x, int p) {
  int r = 1;
  while (p) {
    if (p & 1)
      r = (LL)x * r % mod;
    x = (LL)x * x % mod;
    p >>= 1;
  }
  return r;
}

int main() {

  int n = read(), Q = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (a[i] < a[j])
        f[i][j] = 1;

  int inv2 = 500000004, tmp = Pow(2, Q);
  while (Q--) {
    int x = read(), y = read();
    f[x][y] = f[y][x] = (LL)(f[x][y] + f[y][x]) * inv2 % mod;
    for (int i = 1; i <= n; ++i)
      if (i != x && i != y)
        f[i][x] = f[i][y] = (LL)inv2 * (f[i][x] + f[i][y]) % mod,
        f[x][i] = f[y][i] = (LL)inv2 * (f[x][i] + f[y][i]) % mod;
  }

  int ans = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j < i; ++j)
      (ans += f[i][j]) %= mod;

  cout << (LL)ans * tmp % mod << endl;

  return 0;
}
Problem p03190 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
#define N 5005
int a[N], b[N], n, s1, s2;
char s[N], t[N];
long long solve(int t) {
  long long as = 0;
  int fg = 0;
  if (t < 0) {
    t = -t;
    fg = 1;
    for (int i = 1; i <= n; i++)
      a[i] ^= b[i] ^= a[i] ^= b[i];
    s1 ^= s2 ^= s1 ^= s2;
  }
  int l1 = 1, r1 = 1 - t;
  while (1) {
    int tp1 = a[l1], tp2 = r1 < 0 ? 0 : b[r1];
    if (l1 > s1)
      tp1 = n;
    if (r1 > s2)
      tp2 = n;
    as += tp1 < tp2 ? tp2 - tp1 : tp1 - tp2;
    if (tp1 == tp2 && tp1 == n)
      break;
    l1++, r1++;
  }
  if (fg) {
    for (int i = 1; i <= n; i++)
      a[i] ^= b[i] ^= a[i] ^= b[i];
    s1 ^= s2 ^= s1 ^= s2;
  }
  return as;
}
int main() {
  scanf("%d%s%s", &n, s + 1, t + 1);
  for (int i = 1; i <= n; i++)
    if (s[i] != s[i + 1])
      a[++s1] = i;
  for (int i = 1; i <= n; i++)
    if (t[i] != t[i + 1])
      b[++s2] = i;
  solve(-1);
  long long mn = 1e18;
  for (int i = s[1] == t[1] ? -5000 : -5001; i <= 5000; i += 2) {
    long long tp = solve(i);
    if (tp < mn)
      mn = tp;
  }
  printf("%lld\n", mn);
}

Fixed submission:
#include <cstdio>
using namespace std;
#define N 15005
int a[N], b[N], n, s1, s2;
char s[N], t[N];
long long solve(int t) {
  long long as = 0;
  int fg = 0;
  if (t < 0) {
    t = -t;
    fg = 1;
    for (int i = 1; i <= n; i++)
      a[i] ^= b[i] ^= a[i] ^= b[i];
    s1 ^= s2 ^= s1 ^= s2;
  }
  int l1 = 1, r1 = 1 - t;
  while (1) {
    int tp1 = a[l1], tp2 = r1 < 0 ? 0 : b[r1];
    if (l1 > s1)
      tp1 = n;
    if (r1 > s2)
      tp2 = n;
    as += tp1 < tp2 ? tp2 - tp1 : tp1 - tp2;
    if (tp1 == tp2 && tp1 == n)
      break;
    l1++, r1++;
  }
  if (fg) {
    for (int i = 1; i <= n; i++)
      a[i] ^= b[i] ^= a[i] ^= b[i];
    s1 ^= s2 ^= s1 ^= s2;
  }
  return as;
}
int main() {
  scanf("%d%s%s", &n, s + 1, t + 1);
  for (int i = 1; i <= n; i++)
    if (s[i] != s[i + 1])
      a[++s1] = i;
  for (int i = 1; i <= n; i++)
    if (t[i] != t[i + 1])
      b[++s2] = i;
  solve(-1);
  long long mn = 1e18;
  for (int i = s[1] == t[1] ? -5000 : -5001; i <= 5000; i += 2) {
    long long tp = solve(i);
    if (tp < mn)
      mn = tp;
  }
  printf("%lld\n", mn);
}