alexjercan/bugnet huggingface dataset:
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.rbegin(), a.rend());

  ll sum = 0;
  for (int i = 1; i < n; i++) {
    sum += (ll)a[i / 2];
  }

  cout << sum << endl;
  return 0;
}


Error: 0

Fixed submission:

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

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

  sort(a.rbegin(), a.rend());
  vector<ll> mx(2 * 100000 + 1, 0LL); // 心地よさごとの残り回数
  ll sum = 0;
  ll idx = a[0];
  sum += idx;
  if (a[1] == idx) {
    mx[a[1]] += 2;
  } else {
    mx[idx] = 0;
    mx[a[1]] += 2;
    idx = a[1];
  }

  for (int i = 2; i < n; i++) {
    ll ai = a[i];
    sum += idx;
    if (ai == idx) {
      mx[ai]++;
    } else {
      mx[idx]--;
      if (mx[idx] <= 0) {
        idx = ai;
      }
      mx[ai] += 2;
    }
  }

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

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  sort(a.begin(), a.end());
  ll res = a[n - 1];
  int idx = n - 2;
  n -= 2;
  while (1) {
    if (n == 0)
      break;
    if (n == 1) {
      res += a[idx];
      break;
    } else {
      n -= 2;
      res += 2 * a[idx];
      idx--;
    }
    if (n == 0)
      break;
  }
  cout << res << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  sort(a.begin(), a.end());
  ll res = a[n - 1];
  int idx = n - 2;
  n -= 2;
  while (1) {
    if (n == 1) {
      res += a[idx];
      break;
    } else {
      n -= 2;
      res += 2 * a[idx];
      idx--;
    }
    if (n == 0)
      break;
  }
  cout << res << "\\n";

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

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<long long> v;
  for (int i = 0; i < n; i++) {
    long long t;
    cin >> t;
    v.push_back(t);
  }
  sort(v.rbegin(), v.rend());
  long long sum = v[0];
  int j = 1;
  int cnt = 0;

  for (int i = 2; i < n; i++) {
    sum += v[j];
    cnt++;
    if (cnt == 2) {
      cnt = 0;
      j += 1;
    }
  }
  cout << sum << endl;
}

Error: -11

Fixed submission:

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

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  long long a[200005];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);

  long long int l = n - 1;
  long long sum = a[n - 1];

  int i = n - 2;
  while (l) {
    sum += 2 * a[i];
    i--;
    l -= 2;
  }
  cout << sum << endl;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long n, a[200010], ans, x;
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  ans += a[n - 1];
  for (long long i = n - 2; i > 0; i--) {
    ans = ans + a[i];
    x++;
    if (x + 2 == n)
      break;
    ans = ans + a[i];
    x++;
    if (x + 2 == n)
      break;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long n, a[100010], ans, x;
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  ans += a[n - 1];
  for (long long i = n - 2; i > 0; i--) {
    ans = ans + a[i];
    x++;
    if (x + 2 == n)
      break;
    ans = ans + a[i];
    x++;
    if (x + 2 == n)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int MAXN = 200010;
const int MAXM = 100010;
const int MAXINT = 2147483647;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n, m, k;
ll ans, tot, cnt;

int a[MAXN];

string s;

inline int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    s = (s << 3) + (s << 1) + ch - 48;
    ch = getchar();
  }
  return s * f;
}

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

int main() {
  int T;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a + 1, a + n + 1, cmp);
  ans = a[1];
  int last = a[2], p = 1;
  for (int i = 3; i <= n; i++) {
    if (i % 2) {
      last = a[++p];
    }
    ans += last;
  }
  cout << ans << endl;
  return 0;
}

/*

*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int MAXN = 100010;
const int MAXM = 100010;
const int MAXINT = 2147483647;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n, m, k;
ll ans, tot, cnt;

int a[MAXN];

string s;

inline int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    s = (s << 3) + (s << 1) + ch - 48;
    ch = getchar();
  }
  return s * f;
}

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

int main() {
  int T;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a + 1, a + n + 1, cmp);
  ans = 0;
  for (int i = 2; i <= n; i++) {
    ans += a[i - 1];
  }
  cout << ans << endl;
  return 0;
}

/*

*/
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  ll a[n], i;
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);

  ll ans = a[n - 1], k = n - 2, m = 0;
  for (i = n - 3; i >= 0; i--) {
    if (m < 2) {
      ans += a[k];
      m++;
    } else {
      ans += a[--k];
      m = 1;
    }
  }

  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  ll a[n], i;
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);

  ll ans = a[n - 1], k = n - 2, m = 0;
  for (i = n - 3; i >= 0; i--) {
    if (m < 2) {
      ans += a[k];
      m++;
    } else {
      ans += a[++k];
      m = 1;
    }
  }

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

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
const int inf = 0x3f3f3f3f;
const int MAXN = 2e5 + 10;
#define LOCAL

int a[MAXN];

int main() {
  ios::sync_with_stdio(false);
  cout.tie(0), cin.tie(0);
  int t;
  cin >> t;
  rep(i, t) { cin >> a[i]; }
  sort(a, a + t, greater<int>());
  std::vector<int> v;
  v.push_back(a[0]);
  for (int i = 1; i < t; i++) {
    v.push_back(a[i]);
    v.push_back(a[i]);
  }
  ll ans = 0;
  rep(i, t - 1) { ans += v[i]; }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
const int inf = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
#define LOCAL

int a[MAXN];

int main() {
  ios::sync_with_stdio(false);
  cout.tie(0), cin.tie(0);
  int t;
  cin >> t;
  rep(i, t) { cin >> a[i]; }
  sort(a, a + t, greater<int>());
  std::vector<int> v;
  v.push_back(a[0]);
  for (int i = 1; i < t; i++) {
    v.push_back(a[i]);
    v.push_back(a[i]);
  }
  ll ans = 0;
  rep(i, t - 1) { ans += v[i]; }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define N 200010
using namespace std;
ll ans;
int n;
int a[N];

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

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1, cmp);
  int hal = n / 2;
  ans += a[1];
  for (int i = 2; i <= hal; i++) {
    ans += (long long)a[i];
    ans += (long long)a[i];
  }
  if (n % 2 == 1)
    ans += (long long)a[hal + 1];
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define N 100010
using namespace std;
ll ans;
int n;
int a[N];

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

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1, cmp);
  int hal = n / 2;
  ans += a[1];
  for (int i = 2; i <= hal; i++) {
    ans += (long long)a[i];
    ans += (long long)a[i];
  }
  if (n % 2 == 1)
    ans += (long long)a[hal + 1];
  cout << ans;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  long long ans = 0;

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

  sort(a.rbegin(), a.rend());

  for (int i = 1; i < n; i++) {
    int j = i / 2;
    ans += a[j];
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

void swap(int *a, int *b) {
  int t = *a;
  *a = *b;
  *b = t;
}

int partition(int array[], int l, int r) {
  int pivot = array[r];
  int i = (l - 1);

  for (int j = l; j <= r - 1; j++) {
    if (array[j] <= pivot) {
      i++;
      swap(&array[i], &array[j]);
    }
  }
  swap(&array[i + 1], &array[r]);
  return (i + 1);
}

void quickSort(int array[], int l, int r) {
  if (l < r) {
    int pivot = partition(array, l, r);
    quickSort(array, l, pivot - 1);
    quickSort(array, pivot + 1, r);
  }
}

int a[200005];

int main() {
  int n;
  cin >> n;
  long long ans = 0;
  int s = 1000000001;

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

  quickSort(a, 0, n - 1);

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

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

const int MAXN = 1000000 + 3;
int a[MAXN];

int main() {
  int N;
  cin >> N;

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

  long long ans = (long long)a[N - 1];
  int k = N - 2;
  for (int i = N - 3; i >= 0; i -= 2) {
    ans += (long long)a[k];
    if (i - 1 >= 0) {
      ans += (long long)a[k];
    }
    k--;
    // cout << ans << endl;
  }
  cout << ans;
  system("pause");
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

const int MAXN = 100000 + 3;
int a[MAXN], c[MAXN];

int main() {
  int N;
  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  memset(c, 0, sizeof(c));
  sort(a, a + N);

  long long ans = (long long)a[N - 1];
  int k = N - 2;
  for (int i = N - 3; i >= 0; i -= 2) {
    ans += (long long)a[k];
    if (i - 1 >= 0) {
      ans += (long long)a[k];
    }
    k--;
    // cout << ans << endl;
  }
  cout << ans;
  system("pause");
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <queue>
using namespace std;
int n, a[200010], max;
long long sum;
priority_queue<int> s1;
priority_queue<pair<int, pair<int, int>>> s2;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    s1.push(a[i]);
  }
  s2.push(make_pair(s1.top(), make_pair(s1.top(), s1.top())));
  s1.pop();
  for (int i = 2; i <= n; ++i) {
    int x = s1.top();
    s1.pop();
    sum += s2.top().first;
    pair<int, pair<int, int>> k;
    k = s2.top();
    s2.pop();
    s2.push(make_pair(x, make_pair(k.second.first, x)));
    s2.push(make_pair(x, make_pair(k.second.second, x)));
  }
  printf("%lld\\n", sum);
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <queue>
using namespace std;
int n, a[100010], max;
long long sum;
priority_queue<int> s1;
priority_queue<pair<int, pair<int, int>>> s2;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    s1.push(a[i]);
  }
  s2.push(make_pair(s1.top(), make_pair(s1.top(), s1.top())));
  s1.pop();
  for (int i = 2; i <= n; ++i) {
    int x = s1.top();
    s1.pop();
    sum += s2.top().first;
    pair<int, pair<int, int>> k;
    k = s2.top();
    s2.pop();
    s2.push(make_pair(x, make_pair(k.second.first, x)));
    s2.push(make_pair(x, make_pair(k.second.second, x)));
  }
  printf("%lld\\n", sum);
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

template <typename T> istream &operator>>(istream &is, vector<T> &vs) {
  for (T &v : vs)
    is >> v;
  return is;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &m) {
  for (const auto [k, v] : m)
    os << "{" << k << "," << v << "} ";
  return os;
}

int main() {
  int N;
  cin >> N;
  vector<LL> A(N);
  cin >> A;
  sort(A.rbegin(), A.rend());

  LL sum = 0;
  map<LL, LL> m;
  m[A[0]] = 1;
  for (int i = 1; i < N; ++i) {
    auto it = m.rbegin();
    auto &[k, v] = *it;
    sum += k;
    --v;
    if (v == 0)
      m.erase(k);
    m[A[i]] += 2;
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

template <typename T> istream &operator>>(istream &is, vector<T> &vs) {
  for (T &v : vs)
    is >> v;
  return is;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &m) {
  for (const auto [k, v] : m)
    os << "{" << k << "," << v << "} ";
  return os;
}

int main() {
  int N;
  cin >> N;
  vector<LL> A(N);
  cin >> A;
  sort(A.rbegin(), A.rend());

  LL sum = 0;
  map<LL, LL> m;
  m[A[0]] = 1;
  for (int i = 1; i < N; ++i) {
    for (auto it = m.rbegin(); it != m.rend(); ++it) {
      auto &[k, v] = *it;
      if (v == 0)
        continue;
      sum += k;
      --v;
      break;
    }
    m[A[i]] += 2;
  }
  cout << sum << endl;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("error.txt", "w", stderr);
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    cout << "\\n";
  }

  cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
  return 0;
}
void solve() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  long long int comfort = 0;

  for (int i = 1; i <= n - 1; i++) {
    int k = floor(float(i) / 2);
    comfort += a[n - k];
  }

  cout << comfort;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("error.txt", "w", stderr);
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    cout << "\\n";
  }

  cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
  return 0;
}
void solve() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1, greater<int>());
  int m = floor(float(n - 2) / 2);
  int comfort = a[1];

  if (n == 1) {
    cout << 0;
    exit(0);
  } else if (n == 2) {
    cout << a[1];
    exit(1);
  } else if (n % 2) {
    for (int i = 2; i <= m + 1 && m + 1 >= 2; i++)
      comfort += 2 * a[i];
    comfort += a[m + 2];
  } else {
    for (int i = 2; i <= m + 1; i++)
      comfort += 2 * a[i];
  }
  cout << comfort;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n, a[200003];
bool cmp(int x, int y) { return x > y; }
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);
  sort(a, a + n, cmp);
  long long ans = a[0];
  int tmp = n - 2;
  for (int i = 1; i < n; i++)
    ans += min(2, tmp) * a[i], tmp = max(tmp - 2, 0);
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, a[100003];
bool cmp(int x, int y) { return x > y; }
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);
  sort(a, a + n, cmp);
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    int x = a[i - 1];
    if (i > 1)
      x = min(x, a[i - 2]);
    ans += x;
  }
  cout << ans;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  long long int c = arr[n - 1];
  int j = n - 2;
  int filled = 2;
  while (1) {
    // cout<<"loop ran"<<endl;
    if (filled >= n)
      break;

    c += arr[j];
    filled++;
    if (filled >= n)
      break;

    c += arr[j];
    filled++;
    j--;
  }
  cout << c << endl;
  // for(int i = 0;i<n;i++)
  //	cout<<arr[i]<<" ";
  // cout<<endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  int c = arr[n - 1];
  int j = n - 2;
  int filled = 2;
  while (1) {
    // cout<<"loop ran"<<endl;
    c += arr[j];
    filled += 1;
    if (filled == n)
      break;
    c += arr[j];
    filled++;
    if (filled == n)
      break;
    j--;
  }
  cout << c << endl;
  // for(int i = 0;i<n;i++)
  //	cout<<arr[i]<<" ";
  // cout<<endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 7;
ll a[maxn], sum, mi = 1e10, n;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  sort(a, a + n);
  reverse(a, a + n);
  for (int i = 0; i < n / 2; i++) {
    sum += a[i];
  }
  for (int i = 1; i <= (n - 1) / 2; i++) {
    sum += a[i];
  }
  printf("%lld\\n", sum);
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 7;
ll a[maxn], sum, mi = 1e10, n;
int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    sum += a[i];
    mi = min(mi, a[i]);
  }
  printf("%lld\\n", sum - mi);
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#pragma GCC optimize("O2")
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#define rep(i, N) for (int i = 0; i < (int)N; i++)
static inline int IN(void) {
  int x = 0, f = 0, c = getchar();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = getchar();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = getchar();
  }
  return f ? -x : x;
}
static inline void Radix32(int *a, const int sz) {
  int x, shift = 0, elem[0400], temp[sz];
  while (shift < 040) {
    int bucket[0400] = {0};
    rep(i, sz) {
      x = (a[i] >> shift) & 0377;
      bucket[x]++;
      temp[i] = a[i];
    }
    elem[0] = 0;
    rep(i, 0377) elem[i + 1] = elem[i] + bucket[i];
    rep(i, sz) {
      x = (temp[i] >> shift) & 0377;
      a[elem[x]] = temp[i];
      elem[x]++;
    }
    shift += 010;
  }
}
int main(void) {
  int N = IN(), A[N];
  long sum = 0;
  rep(i, N) { A[i] = IN(); }
  Radix32(A, N);
  rep(i, N - 1) { sum += A[N - 1 - (i + 1) / 2]; }
  return printf("%ld\\n", sum), 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#pragma GCC optimize("O2")
#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define swap(a, b) ((a != b) && (a += b, b = a - b, a -= b))
static inline int IN(void) {
  int x = 0, f = 0, c = getchar();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = getchar();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = getchar();
  }
  return f ? -x : x;
}
int main() {
  int i = 0, k = 0, N = IN(), A[200000];
  long sum = 0;
  while (i < N) {
    A[i++] = IN();
    rep(j, i) {
      for (k = j; k != 0 && A[k] > A[k - 1]; k--)
        swap(A[k], A[k - 1]);
    }
  }
  rep(j, N - 1) sum += A[(j + 1) / 2];
  return printf("%ld\\n", sum), 0;
}