Problem p03223 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 500005
#define int long long
using namespace std;
inline int read() {
  char x = getchar();
  int lin = 0, f = 1;
  while (x < '0' || x > '9') {
    if (x == '-')
      f = -1;
    x = getchar();
  }
  while (x >= '0' && x <= '9') {
    lin = (lin << 1) + (lin << 3) + x - '0';
    x = getchar();
  }
  return lin * f;
}
#define PII pair<int, int>
#define fir first
#define sec second
#define ma(a, b) make_pair(a, b)
#define db double
multiset<int> sett;
deque<int> q;
int n, val[maxn], ans, jie, s[maxn], tot;
void insert_big() {
  int val = *(--sett.end());
  sett.erase(--sett.end());
  if (q.size()) {
    if (abs(q.front() - val) <= abs(q.back() - val))
      ans += abs(q.back() - val), q.push_back(val);
    else
      ans += abs(q.front() - val), q.push_front(val);
  } else
    q.push_front(val);
}
void insert_small() {
  int val = *sett.begin();
  sett.erase(sett.begin());
  if (q.size()) {
    if (abs(q.front() - val) <= abs(q.back() - val))
      ans += abs(q.back() - val), q.push_back(val);
    else
      ans += abs(q.front() - val), q.push_front(val);
  } else
    q.push_front(val);
}
signed main() {
  //	freopen("1.in","r",stdin);
  //	freopen("1.out","w",stdout);
  n = read();
  for (int i = 1; i <= n; i++) {
    val[i] = read();
  }
  if (n & 1) {
    for (int i = 1; i <= n; i++)
      sett.insert(val[i]);
    for (int i = 1; i <= n; i++)
      if (!(i & 1))
        insert_big();
      else
        insert_small();
    jie = ans;
    ans = 0;
    while (q.size())
      q.pop_front();

    for (int i = 1; i <= n; i++)
      sett.insert(val[i]);
    for (int i = 1; i <= n; i++)
      if (i & 1)
        insert_big();
      else
        insert_small();
    jie = max(ans, jie);
    ans = jie;
    cout << ans;
    return 0;
  }
  sort(val + 1, val + 1 + n);
  int v1 = val[n / 2], v2 = val[n / 2 + 1];
  for (int i = 1; i <= n; i++)
    if (i != n / 2 && i != n / 2 + 1)
      s[++tot] = val[i];
  n -= 2;
  for (int i = 1; i <= n; i++)
    val[i] = s[i];

  for (int i = 1; i <= n; i++)
    sett.insert(val[i]);
  for (int i = 1; i <= n; i++)
    if (i & 1)
      insert_big();
    else
      insert_small();
  ans += max(abs(q.front() - v1) + abs(q.back() - v2),
             abs(q.front() - v2) + abs(q.back() - v1));

  jie = ans;
  ans = 0;

  while (q.size())
    q.pop_back();
  for (int i = 1; i <= n; i++)
    sett.insert(val[i]);

  for (int i = 1; i <= n; i++)
    if (!(i & 1))
      insert_big();
    else
      insert_small();
  ans += max(abs(q.front() - v1) + abs(q.back() - v2),
             abs(q.front() - v2) + abs(q.back() - v1));

  ans = max(ans, jie);
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define maxn 500005
#define int long long
using namespace std;
inline int read() {
  char x = getchar();
  int lin = 0, f = 1;
  while (x < '0' || x > '9') {
    if (x == '-')
      f = -1;
    x = getchar();
  }
  while (x >= '0' && x <= '9') {
    lin = (lin << 1) + (lin << 3) + x - '0';
    x = getchar();
  }
  return lin * f;
}
#define PII pair<int, int>
#define fir first
#define sec second
#define ma(a, b) make_pair(a, b)
#define db double
multiset<int> sett;
deque<int> q;
int n, val[maxn], ans, jie, s[maxn], tot;
void insert_big() {
  int val = *(--sett.end());
  sett.erase(--sett.end());
  if (q.size()) {
    if (abs(q.front() - val) <= abs(q.back() - val))
      ans += abs(q.back() - val), q.push_back(val);
    else
      ans += abs(q.front() - val), q.push_front(val);
  } else
    q.push_front(val);
}
void insert_small() {
  int val = *sett.begin();
  sett.erase(sett.begin());
  if (q.size()) {
    if (abs(q.front() - val) <= abs(q.back() - val))
      ans += abs(q.back() - val), q.push_back(val);
    else
      ans += abs(q.front() - val), q.push_front(val);
  } else
    q.push_front(val);
}
signed main() {
  //	freopen("1.in","r",stdin);
  //	freopen("1.out","w",stdout);
  n = read();
  for (int i = 1; i <= n; i++) {
    val[i] = read();
  }
  if (n == 2) {
    cout << abs(val[1] - val[2]);
    return 0;
  }
  if (n & 1) {
    for (int i = 1; i <= n; i++)
      sett.insert(val[i]);
    for (int i = 1; i <= n; i++)
      if (!(i & 1))
        insert_big();
      else
        insert_small();
    jie = ans;
    ans = 0;
    while (q.size())
      q.pop_front();

    for (int i = 1; i <= n; i++)
      sett.insert(val[i]);
    for (int i = 1; i <= n; i++)
      if (i & 1)
        insert_big();
      else
        insert_small();
    jie = max(ans, jie);
    ans = jie;
    cout << ans;
    return 0;
  }
  sort(val + 1, val + 1 + n);
  int v1 = val[n / 2], v2 = val[n / 2 + 1];
  for (int i = 1; i <= n; i++)
    if (i != n / 2 && i != n / 2 + 1)
      s[++tot] = val[i];
  n -= 2;
  for (int i = 1; i <= n; i++)
    val[i] = s[i];

  for (int i = 1; i <= n; i++)
    sett.insert(val[i]);
  for (int i = 1; i <= n; i++)
    if (i & 1)
      insert_big();
    else
      insert_small();
  ans += max(abs(q.front() - v1) + abs(q.back() - v2),
             abs(q.front() - v2) + abs(q.back() - v1));

  jie = ans;
  ans = 0;

  while (q.size())
    q.pop_back();
  for (int i = 1; i <= n; i++)
    sett.insert(val[i]);

  for (int i = 1; i <= n; i++)
    if (!(i & 1))
      insert_big();
    else
      insert_small();
  ans += max(abs(q.front() - v1) + abs(q.back() - v2),
             abs(q.front() - v2) + abs(q.back() - v1));

  ans = max(ans, jie);
  cout << ans;
}