alexjercan/bugnet huggingface dataset:
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <queue>
#define maxn 200005
using namespace std;

int a[maxn], n;

struct Position {
  int lf, rf, comfort;
  inline Position(int l, int r) {
    lf = l, rf = r;
    comfort = min(l, r);
  }
  inline bool operator<(const Position &p) const { return comfort < p.comfort; }
};

priority_queue<Position> q;

inline bool cmp(int x, int y) { return x > y; }

int main(int argc, char **argv) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);
  sort(a, a + n, cmp);
  q.push(Position(a[0], a[1]));
  long long res = a[0];
  for (int i = 2; i < n; i++) {
    Position p = q.top();
    if (q.size() > 1)
      q.pop();
    res += p.comfort;
    q.push(Position(a[i], p.lf));
    q.push(Position(a[i], p.rf));
  }
  printf("%lld\\n", res);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <list>
#define maxn 200005
#define INF 123456789
using namespace std;

int a[maxn], n;
list<int> cycle;
typedef list<int>::iterator lit;

inline bool cmp(int x, int y) { return x > y; }

inline int insertBestPosition(int friendliness) {
  int mx = -INF;
  lit mx_it;
  for (lit it = cycle.begin(); it != cycle.end(); it++) {
    lit tmp = it;
    tmp++;
    int t = min(*it, *tmp);
    if (t > mx)
      mx = t, mx_it = tmp;
  }
  int t = min(*cycle.rbegin(), *cycle.begin());
  if (t > mx) {
    cycle.push_front(friendliness);
    return t;
  }
  cycle.insert(mx_it, friendliness);
  return mx;
}

int main(int argc, char **argv) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);
  sort(a, a + n, cmp);
  cycle.push_back(a[0]);
  cycle.push_back(a[1]);
  long long res = a[0];
  for (int i = 2; i < n; i++)
    res += insertBestPosition(a[i]);
  printf("%lld\\n", res);
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

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

  int64_t N;
  cin >> N;
  // const int64_t L = static_cast<int64_t>(T.length());
  vector<int64_t> A(N);
  for (int64_t i = 0; i < N; ++i) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());

  int64_t result = A[0];
  // cerr << "hoge " << A[0] << endl;
  using mypair = pair<int64_t, Pair>;
  multiset<mypair, std::greater<mypair>> nextscore_pair;
  nextscore_pair.insert(mypair(A[1], Pair(0, 1)));
  nextscore_pair.insert(mypair(A[1], Pair(0, 1)));
  for (int64_t i = 2; i < N; ++i) {
    result += nextscore_pair.begin()->first;
    // cerr << "hoge " << nextscore_pair.begin()->first << endl;
    const int64_t next_1 = nextscore_pair.begin()->second.first;
    const int64_t next_2 = nextscore_pair.begin()->second.second;
    nextscore_pair.erase(nextscore_pair.begin());
    nextscore_pair.insert(mypair(A[i], Pair(i, next_1)));
    nextscore_pair.insert(mypair(A[i], Pair(i, next_2)));
  }
  cout << result << endl;
  return 0;
}


Error: TLE

Fixed submission:

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

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

  int64_t N;
  cin >> N;
  // const int64_t L = static_cast<int64_t>(T.length());
  vector<int64_t> A(N);
  for (int64_t i = 0; i < N; ++i) {
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  int64_t result = 0;
  list<int64_t> ring;
  ring.emplace_back(A[0]);
  for (int64_t i = 1; i < N; ++i) {
    auto prev = ring.begin();
    int64_t max_score = min(*ring.begin(), *ring.rbegin());
    auto ins = ring.begin();
    for (auto itr = ring.begin(); itr != ring.end(); ++itr) {
      const int64_t c_score = min(*itr, *prev);
      if (c_score > max_score) {
        max_score = c_score;
        ins = itr;
      }
      prev = itr;
    }
    ring.insert(ins, A[i]);
    result += max_score;
  }
  cout << result << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#pragma comment(linker, "/STACK: 2000000")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
#define dbg(x) cout << #x << '=' << x << '\\n';
#define ll long long
#define double long double
#define x first
#define y second
#define vi vector<int>
#define L nod << 1
#define R ((nod << 1) | 1)
#define int ll
#define PI 3.14159265358979
const ll mod = 998244353;
int n, a[200010], ans;

int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1, greater<int>());
  for (int i = 2; i <= n; i++)
    ans += a[(i + 1) / 2];
  cout << ans;
}


Error: WA

Fixed submission:

#pragma comment(linker, "/STACK: 2000000")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
#define dbg(x) cout << #x << '=' << x << '\\n';
#define ll long long
#define double long double
#define x first
#define y second
#define vi vector<int>
#define L nod << 1
#define R ((nod << 1) | 1)
#define int ll
#define PI 3.14159265358979
const ll mod = 998244353;
int n, a[100010];

int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1, greater<int>());
  int ans = 0;
  for (int i = 1; i < n; i++)
    ans += a[i];
  cout << ans;
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
#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 all(x) (x).begin(), (x).end()
const ll INF = ll(1e18) + 1;

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

  ll n;
  cin >> n;
  vector<ll> ai(n);
  rep(i, n) cin >> ai[i];

  sort(all(ai), greater<int>());
  ll result = 0;

  vector<ll> check;
  check.push_back(ai[0]);

  rep(i, n - 1) {
    result += check[i];
    check.push_back(ai[i + 1]);
    check.push_back(ai[i + 1]);
  }

  cout << result;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
#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 all(x) (x).begin(), (x).end()
const ll INF = ll(1e18) + 1;

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

  ll n;
  cin >> n;
  vector<ll> ai(n);
  rep(i, n) cin >> ai[i];

  sort(all(ai), greater<int>());
  ll result = 0;

  vector<ll> check;
  check.push_back(ai[0]);

  rep(i, n - 1) {
    result += check[0];
    check[0] = ai[i + 1];
    check.push_back(ai[i + 1]);
    sort(all(check), greater<int>());
  }

  cout << result;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;

int com[N];

bool cmp(int a, int b) { return a > b; }

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> com[i];

  sort(com + 1, com + 1 + n, cmp);
  long long ans = com[1];
  int cnt = n - 2;
  for (int i = 2;; i++) {
    if (cnt > 1) {
      cnt -= 2;
      ans += com[i] * 2;
    } else if (cnt == 1) {
      cnt--;
      ans += com[i];
    } else
      break;
  }

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;

int com[N];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> com[i];

  sort(com + 1, com + 1 + n);
  int ans = com[n];
  int cnt = n - 2;
  for (int i = n - 1;; i++) {
    if (cnt > 1) {
      cnt -= 2;
      ans += com[i] * 2;
    } else if (cnt == 1) {
      cnt--;
      ans += com[i];
    } else
      break;
  }

  cout << ans << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

#define llong long long
#define rep(i, l, n) for (int(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))

using namespace std;

int main() {
  int N;
  cin >> N;
  int A[N];
  rep(i, 0, N) cin >> A[i];
  sort(A, A + N);

  llong score = 0;

  rep(i, 1, N) { score += A[N - 1 - i / 2]; }

  cout << score;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

#define llong long long
#define rep(i, l, n) for (int(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))

using namespace std;

class people;

class heap {
public:
  int f;           // ������min
  int left, right; // �����̐l�ԍ�
  heap(int a, int b) {
    left = a;
    right = b;
  }
  bool operator>(const heap &t) const { return f > t.f; }
  bool operator<(const heap &t) const { return f < t.f; }
};

class people {
public:
  int f;
  heap *left, *right;
};

int main() {
  // ����
  int N;
  cin >> N;
  int A[N];
  rep(i, 0, N) cin >> A[i];
  sort(A, A + N);
  reverse(A, A + N); // �~��
                     // ��@

  people f[N];
  rep(i, 0, N) {
    f[i].f = A[i];
    f[i].left = f[i].right = 0;
  }
  vector<heap> ins;
  heap f0(0, 1);
  f0.f = A[1];
  heap f1(1, 0);
  f1.f = A[1];
  f[0].left = f[1].right = &f1;
  f[1].left = f[0].right = &f0;
  ins.push_back(f0);
  ins.push_back(f1);

  int score = A[0];

  rep(i, 2, N) {
    vector<heap>::iterator min = ins.begin();

    score += min->f;

    heap temp1(min->left, i), temp2(i, min->right);
    temp1.f = _min(f[min->left].f, f[i].f);
    temp2.f = _min(f[i].f, f[min->right].f);

    pop_heap(ins.begin(), ins.end());
    ins.pop_back();
    ins.push_back(temp1);
    push_heap(ins.begin(), ins.end());
    ins.push_back(temp2);
    push_heap(ins.begin(), ins.end());

    f[min->left].right = &temp1;
    f[min->right].left = &temp2;
    f[i].f = _min(f[min->left].f, f[min->right].f);
    f[i].left = &temp1;
    f[i].right = &temp2;
  }

  cout << score;

  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, i, j, a[210000], num;
long long ans;
bool cmp(int x, int y) { return x > y; }
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1, cmp);
  ans = a[1];
  num = n - 2;
  for (i = 2; i <= n; i++) {
    ans += a[i] * min(2, num);
    num -= 2;
    if (num <= 0)
      break;
  }
  printf("%lld", ans);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 100005
#define ll long long

void DBG() { cerr << "]\\n"; }
template <class H, class... T> void DBG(H h, T... t) {
  cerr << h;
  if (sizeof...(t)) {
    cerr << ", ";
  }
  DBG(t...);
}
#define _DEBUG
#ifdef _DEBUG
#define dbg(...)                                                               \\
  cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [",           \\
      DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

int tt, n, k;
int a[maxn];

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

  cin >> n;
  int mn = INF;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mn = min(a[i], mn);
    sum += a[i];
  }
  cout << sum - mn;
  // sort(a,a+n,g/reater<int>());

  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (int i = (int)(start); i < (int)(n); ++i)
static const int INFTY = (1 << 30);
int N;
void func() {
  cin >> N;
  int A[N + 1] = {};
  A[0] = INFTY;
  rep(i, 1, N + 1) cin >> A[i];
  sort(&A[0], &A[N + 1], greater<int>());
  ll ans = A[1];
  rep(i, 2, N / 2 + 1) ans += (A[i] * 2);
  if (N % 2)
    ans += A[N / 2 + 1];
  cout << ans << endl;
}
int main() { func(); }

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i)
static const ll INFTY = (1 << 30);
ll N;
void func() {
  cin >> N;
  vector<ll> A(N);
  rep(i, 0, N) { cin >> A[i]; }
  sort(A.begin(), A.end(), greater<ll>());
  vector<ll> ans(N + 1);
  ans[1] = 0; // 1人目の心地よさは0
  ans[2] = A[0];
  ans[3] = min(A[0], A[1]);
  // ans[4] = max(ans[3], min(A[1], A[2]), min(A[0], A[2]))=max(ans[3],A[2]);
  // ans[5] = max(ans[4], min(A[0], A[3]), min(A[1], A[3]), min(A[2], A[3])) =
  // max(ans[4], min(max(A[0], A[1], A[2]),A[3]);
  for (ll i = 4; i <= N; ++i) {
    ans[i] = max(ans[i - 1], A[i - 2]);
  }
  ll maxans = 0;
  rep(i, 1, N + 1) maxans += ans[i];
  cout << maxans << endl;
}
int main() { func(); }
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
const int max_num = 200001;
vector<int> G;

void swap(long long int v[], int i, int j) {
  long long int temp = v[i];
  v[i] = v[j];
  v[j] = temp;
}

void qsort(long long int v[], int left, int right) {
  int i, last;

  if (left >= right)
    return;
  swap(v, left, (left + right) / 2);
  last = left;
  for (i = left + 1; i <= right; i++)
    if (v[i] > v[left])
      swap(v, ++last, i);
  swap(v, left, last);
  qsort(v, left, last - 1);
  qsort(v, last + 1, right);
}

void insSort(long long int *a, int n, int g) {
  int v, j;
  for (int i = g; i < n; i++) {
    v = a[i];
    j = i - g;
    while (j >= 0 && a[j] < v) {
      a[j + g] = a[j];
      j = j - g;
      a[j + g] = v;
    }
  }
}

void shellSort(long long int *a, int n) {
  for (int h = 1;;) {
    if (h > n)
      break;
    G.push_back(h);
    h = 3 * h + 1;
  }
  for (int i = G.size() - 1; i >= 0; i--)
    insSort(a, n, G[i]);
}

void inputnumbers(int *n, long long int *a) {
  cin >> *n;
  for (int i = 0; i < *n; i++)
    cin >> *(a + i);
}

void printnumbers(int n, long long int *a) {
  for (int i = 0; i < n; i++)
    cout << *(a + i) << ' ';
  cout << endl;
}

long long int solve(int n, long long int a[]) {
  int q = n / 2;
  int r = n % 2;

  // qsort(a,0,n-1);
  shellSort(a, n);

  // printnumbers(n,a);

  long long int sum = a[0];
  for (int i = 1; i < q; i++)
    sum += 2 * a[i];
  if (r == 1)
    sum += a[q];

  return sum;
}

int main() {
  long long int a[max_num];
  int n;

  inputnumbers(&n, a);

  // printnumbers(n,a);
  // qsort(a,0,n-1);
  // printnumbers(n,a);

  cout << solve(n, a) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
const int max_num = 200001;

void swap(long long int v[], int i, int j) {
  long long int temp = v[i];
  v[i] = v[j];
  v[j] = temp;
}

void qsort(long long int v[], int left, int right) {
  int i, last;

  if (left >= right)
    return;
  swap(v, left, (left + right) / 2);
  last = left;
  for (i = left + 1; i <= right; i++)
    if (v[i] > v[left])
      swap(v, ++last, i);
  swap(v, left, last);
  qsort(v, left, last - 1);
  qsort(v, last + 1, right);
}

void inputnumbers(int *n, long long int *a) {
  cin >> *n;
  for (int i = 0; i < *n; i++)
    cin >> *(a + i);
}

void printnumbers(int n, long long int *a) {
  for (int i = 0; i < n; i++)
    cout << *(a + i) << ' ';
  cout << endl;
}

long long int solve(int n, long long int a[]) {
  int q = n / 2;
  int r = n % 2;

  qsort(a, 0, n - 1);

  // printnumbers(n,a);

  long long int sum = a[0];
  for (int i = 1; i < q; i++)
    sum += 2 * a[i];
  if (r == 1)
    sum += a[q];

  return sum;
}

int main() {
  long long int a[max_num];
  int n;

  inputnumbers(&n, a);

  // printnumbers(n,a);
  // qsort(a,0,n-1);
  // printnumbers(n,a);

  cout << solve(n, a) << endl;

  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N];
int num[N], g = 0;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  long long res = a[n - 1], cnt = 0;
  num[g++] = a[n - 2];
  int l = 0;
  for (int i = n - 3; i >= 0; i--) {
    res += num[l];
    cnt++;
    if (cnt == 2)
      l++, cnt = 0;
    num[g++] = a[i];
  }
  cout << res;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  long long res = 0, mn = a[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    res += mn;
    mn = a[i];
  }
  cout << res;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, val) memset(v, val, sizeof(v))
#define lcm(a, b) ((a * b) / (__gcd(a, b)))
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
ll setBit1(ll num, ll idx) { return num | (1ll << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 500000 + 5, mod = 1e9 + 7;
int a[200000 + 9];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  sort(a, a + n);
  ll ans = 0;
  int lim = n - 1;
  for (int i = n - 1; i >= 0 && lim > 0; i--) {

    ans += a[i];
    lim--;
    if (lim > 0 && i != n - 1)
      ans += a[i], lim--;
  }
  cout << ans << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, val) memset(v, val, sizeof(v))
#define lcm(a, b) ((a * b) / (__gcd(a, b)))
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
ll setBit1(ll num, ll idx) { return num | (1ll << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int N = 500000 + 5, mod = 1e9 + 7;
int a[100000 + 9];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  sort(a, a + n);
  ll ans = 0;
  int lim = n - 1;
  for (int i = n - 1; i >= 0 && lim > 0; i--) {

    ans += a[i];
    lim--;
    if (lim > 0 && i != n - 1)
      ans += a[i], lim--;
  }
  cout << ans << "\\n";
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_ }
#define MY_DEBUG(s) s
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#endif

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define EPS (1e-7)
#define IINF ((int)1e9)

MY_NAMESPACE(testbed)
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end(), greater<ll>());

  ll res = 0;
  for (int i = 1; i < n; i++) {
    int idx = i / 2;
    ll this_a = a[idx];
    res += this_a;
  }
  cout << res << "\\n";
  return 0;
}
MY_NAMESPACE_

Error: TLE

Fixed submission:

#ifdef MY_LOCAL
#define MY_NAMESPACE(ns) namespace ns {
#define MY_NAMESPACE_ }
#define MY_DEBUG(s) s
#else
#define MY_NAMESPACE(ns)
#define MY_NAMESPACE_
#define MY_DEBUG(s)
#endif

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define EPS (1e-7)
#define IINF ((int)1e9)

MY_NAMESPACE(testbed)
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  ll res = 0;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end(), greater<ll>());

  vector<ll> loop;
  vector<ll> space;
  for (int i = 0; i < n; i++) {
    ll this_a = a[i];
    if (i == 0) {
      loop.push_back(this_a);
      space.push_back(this_a);
    } else {
      auto max_space = max_element(space.begin(), space.end());
      ll this_conf = *max_space;
      res += this_conf;
      size_t distance = max_space - space.begin();
      loop.insert(loop.begin() + distance + 1, this_a);
      space[distance] = this_a;
      space.insert(space.begin() + distance + 1, this_a);
    }
  }
  cout << res << "\\n";
  return 0;
}
MY_NAMESPACE_
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define speed                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define int long long

using namespace std;

const int N = 1e3 + 7;
const int mod = 1e9 + 7;

int32_t main() {
  speed;
  int n;
  cin >> n;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
    a.push_back(x);
  }
  sort(a.rbegin(), a.rend());
  a.erase(a.begin());
  int sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sum += a[i];
  }
  cout << sum;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int MAXN = 1e5 + 5;
typedef long long LL;

int a[MAXN];

int cmp(int a, int b) { return a > b; }

int main(int argc, char *argv[]) {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);
  sort(a, a + n, cmp);
  LL ans;
  ans = a[0] + (n - 2) * a[1];
  printf("%lld\\n", ans);
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define fori(x) for (int i = 0; i < x; ++i)
#define forj(x) for (int j = 0; j < x; ++j)

typedef long long ll;

const int INF = 2e9 + 5;
const int alength = 100100 * 2;

int main() {
  int n;
  cin >> n;
  ll a[alength] = {};
  fori(n) { cin >> a[i]; }
  sort(a, a + n, greater<ll>());

  unsigned long long ans = a[0];
  for (int i = 1; i * 2 < n - n % 2; ++i) {
    ans += a[i] * 2;
  }
  if (n % 2 != 0) {
    ans += a[n / 2];
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define fori(x) for (int i = 0; i < x; ++i)
#define forj(x) for (int j = 0; j < x; ++j)

typedef long long ll;

const int INF = 2e9 + 5;
const int alength = 100100;

int main() {
  int n;
  cin >> n;
  ll a[alength] = {};
  fori(n) { cin >> a[i]; }
  sort(a, a + n, greater<ll>());

  unsigned long long ans = a[0];
  for (int i = 1; i * 2 < n - n % 2; ++i) {
    ans += a[i] * 2;
  }
  if (n % 2 != 0) {
    ans += a[n / 2];
  }
  cout << ans;
}