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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  ll ans = 0;

  sort(a.begin(), a.end());
  ans += a[a.size() - 1];
  ll cnt = 2;
  int pos = a.size() - 2;
  while (cnt < n) {

    rep(i, 2) {
      ans += a[pos];
      cnt++;
      if (cnt == n)
        break;
    }
    pos--;
  }

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  ll ans = 0;

  sort(a.begin(), a.end());
  ans += a[a.size() - 1];
  int cnt = 1;

  bool flag = false;
  for (int i = a.size() - 2; i >= 0; i++) {

    rep(j, 2) {
      ans += a[i];
      cnt++;
      if (cnt == n - 1) {
        flag = true;
        break;
      }
    }

    if (flag)
      break;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define Sort(v) sort((v).begin(), (v).end())
using veci = vector<int>;
using vecs = vector<string>;
using vecd = vector<double>;
using vecl = vector<long long>;
using G = vector<vector<int>>;
using ll = long long;
using P = pair<int, int>;
ll lINF = LLONG_MAX;
ll lmINF = LLONG_MIN;
int INF = INT_MAX;
int mINF = INT_MIN;
int a[1000100];
int main() {
  int n;
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  sort(a, a + n);
  reverse(a, a + n);
  ll ans = 0;
  ans += a[0];
  int count = 2;
  for (int i = 1; count < n; count += 2, i++) {
    ans += a[i];
    if (count + 1 < n)
      ans += a[i];
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define Sort(v) sort((v).begin(), (v).end())
using veci = vector<int>;
using vecs = vector<string>;
using vecd = vector<double>;
using vecl = vector<long long>;
using G = vector<vector<int>>;
using ll = long long;
using P = pair<int, int>;
ll lINF = LLONG_MAX;
ll lmINF = LLONG_MIN;
int INF = INT_MAX;
int mINF = INT_MIN;
int a[100010];
int main() {
  int n;
  cin >> n;
  rep(i, n) { cin >> a[i]; }
  sort(a, a + n);
  reverse(a, a + n);
  ll ans = 0;
  ans += a[0];
  int count = 2;
  for (int i = 2; count < n; count += 2) {
    ans += a[i];
    if (count + 1 < n)
      ans += a[i];
  }
  cout << ans << endl;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
int t, n, a[200005], ans, pnt, tmp;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  t = 1;
  //  cin>>t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    ans = a[n - 1];
    pnt = n - 2;
    tmp = n - 2;
    while (1) {
      if (tmp == 0)
        break;
      tmp--;
      ans += a[pnt];
      if (tmp == 0)
        break;
      ans += a[pnt--];
      tmp--;
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
int t, n, a[100005], ans, pnt, tmp;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  t = 1;
  //  cin>>t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    ans = a[n - 1];
    pnt = n - 2;
    tmp = n - 2;
    while (1) {
      if (tmp == 0)
        break;
      tmp--;
      ans += a[pnt];
      if (tmp == 0)
        break;
      ans += a[pnt--];
      tmp--;
    }
    cout << ans << endl;
  }
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = int64_t;
using Graph = vector<vector<int>>;
const ll M = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ll> a;
  for (int i = 0; i < n; i++) {
    ll f;
    cin >> f;
    a.push_back(f);
  }

  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());

  ll sum = 0;
  int cnt = 1;
  while (cnt < n) {
    sum += a.at(cnt / 2);
    cnt++;
  }
  cout << sum << endl;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at: __n (which is 1) >= this->size() (which is 1)


Fixed submission:

#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int n;
  string s;
  cin >> n >> s;

  int w = 0, r = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == 'W') {
      w++;
    } else {
      r++;
    }
  }

  int wr = 0;
  if (w <= r) {
    for (int i = 0; i < w; i++) {
      if (s.at(n - 1 - i) == 'W')
        wr++;
    }
    cout << w - wr << endl;
  } else {
    for (int i = 0; i < r; i++) {
      if (s.at(i) == 'R')
        wr++;
    }
    cout << r - wr << endl;
  }
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  long n;
  cin >> n;
  vector<long> a(n);
  for (long i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end(), greater<long>());
  vector<unsigned long long> dp(n, 0);
  long cnt = 0;
  for (long i = 0; i < n; i++) {
    // cout << i << endl;
    if (i == 0) {
      dp[i]++;
      cnt++;
      if (cnt >= (n - 1))
        break;
    } else {
      dp[i]++;
      cnt++;
      if (cnt >= (n - 1))
        break;
      dp[i]++;
      cnt++;
      if (cnt >= (n - 1))
        break;
    }
  }
  unsigned long long ans = 0;
  for (unsigned long long i = 0; i < dp.size(); i++) {
    ans += a[i] * dp[i];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long n;
  cin >> n;
  vector<long> a(n);
  for (long i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end(), greater<long>());
  vector<unsigned long long> dp(a[0] + 1, 0);
  long cnt = 0;
  for (long i = 0; i < n; i++) {
    if (i == 0) {
      dp[a[i]]++;
      cnt++;
      if (cnt >= (n - 1))
        break;
    } else {
      dp[a[i]]++;
      cnt++;
      if (cnt >= (n - 1))
        break;
      dp[a[i]]++;
      cnt++;
      if (cnt >= (n - 1))
        break;
    }
  }
  unsigned long long ans = 0;
  for (unsigned long long i = 0; i < dp.size(); i++) {
    ans += i * dp[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
using ll = long long int;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll mod = 1000000007;

vector<ll> dp(400010);

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end(), greater<int>());
  rep(i, n) dp[i] = a[i];
  ll ans = dp[0];
  for (int i = 1; i < (n - 1); i++) {
    int num = i + n / 2 + n % 2 - 1;
    ans += dp[i];
    dp[num] = dp[i];
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
using ll = long long int;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll mod = 1000000007;

vector<ll> dp(200010);

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end(), greater<int>());
  rep(i, n) dp[i] = a[i];
  ll ans = dp[0];
  for (int i = 1; i < (n - 1); i++) {
    int num = i + n / 2 + n % 2 - 1;
    ans += dp[i];
    dp[num] = dp[i];
  }
  cout << ans << endl;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

// author: hitch_hiker42;
#include "bits/stdc++.h"
using namespace std;

// solution:
#define int int64_t
#define span(a) begin(a), end(a)

void hike() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &i : a)
    cin >> i;
  sort(span(a), greater<int>());
  int cost = a.front(), j = 1;
  for (int i = n - 2; i > 0; i -= 2, ++j) {
    cost += a[j] << 1;
  }
  if (n % 2 == 0)
    cout << cost << "\\n";
  else
    cout << cost - a[j - 1] << "\\n";
}

signed main() {
  int t = 1; // cin >> t;
  while (t--)
    hike();
  return 0;
} // farewell, until we meet again..

Error: -11

Fixed submission:

// author: hitch_hiker42;
#include "bits/stdc++.h"
using namespace std;

// solution:
#define int int64_t
#define span(a) begin(a), end(a)

void hike() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &i : a)
    cin >> i;
  sort(span(a), greater<int>());
  int cost = a.front(), j = 1;
  for (int i = n - 2; ~i; i -= 2, ++j) {
    cost += a[j] << 1;
  }
  if (n % 2 == 0)
    cout << cost << "\\n";
  else
    cout << cost - a[j - 1] << "\\n";
}

signed main() {
  int t = 1; // cin >> t;
  while (t--)
    hike();
  return 0;
} // farewell, until we meet again..
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define mod 1000000007
vector<ll> to[1000005];
int main() {
  ll a, b = 0;
  cin >> a;
  vector<ll> c(a * 2 - 1), d(a);
  rep(i, a) cin >> d[i];
  sort(d.begin(), d.end());
  reverse(d.begin(), d.end());
  c[0] = d[0];
  for (ll i = 1; i < a * 2 - 1; i++) {
    c[i] = d[i / 2 + 1];
    i++;
    c[i] = d[i / 2];
  }
  rep(i, a - 1) { b += c[i]; }
  cout << b;
}


Error: WA

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define mod 1000000007
vector<ll> to[1000005];
int main() {
  ll a, b = 0;
  cin >> a;
  vector<ll> c(a * 2 - 1), d(a);
  rep(i, a) cin >> d[i];
  sort(d.begin(), d.end());
  reverse(d.begin(), d.end());
  c[0] = d[0];
  for (ll i = 1; i < a * 2 - 1; i++) {
    c[i] = d[i];
    i++;
    c[i] = d[i / 2];
  }
  rep(i, a - 1) { b += c[i]; }
  cout << b;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

typedef long long ll;
const int maxn = 2e5 + 100;
int a[maxn];
int cmp(int x, int y) { return x > y; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  sort(a, a + n, cmp);
  // for (int i = 0; i < n; ++i)
  //     cout << a[i] << " "; cout << endl;
  int i = 0, now = 1;
  int t = 0;
  ll res = 0;
  while (t < n - 1) {
    res += a[i];
    now = (now + 1) % 2;
    if (now == 0)
      i++;
    t++;
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

typedef long long ll;
const int maxn = 1e5 + 100;
int a[maxn];
int cmp(int x, int y) { return x > y; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  sort(a, a + n, cmp);
  // for (int i = 0; i < n; ++i)
  //     cout << a[i] << " "; cout << endl;
  int i = 0, now = 1;
  int t = 0;
  ll res = 0;
  while (t < n - 1) {
    res += a[i];
    now = (now + 1) % 2;
    if (now == 0)
      i++;
    t++;
  }
  cout << res << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
using i64 = long long;
using f64 = double;

#define FOR(i, s, n) for (size_t i = (s); i < (size_t)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

template <typename T, typename U> inline bool chmax(T &a, U b) {
  return a < b ? (a = b, true) : false;
}

template <typename T, typename U> inline bool chmin(T &a, U b) {
  return a > b ? (a = b, true) : false;
}

int main() {
  int n;
  cin >> n;
  auto vec = vector<int64_t>(n);
  for (auto &x : vec) {
    cin >> x;
  }

  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
  auto max = vec.front();

  int half_down = n / 2;
  int half_up = n - half_down;

  auto sum = 0 - max;
  REP(i, half_up) { sum += vec[i]; }
  REP(i, half_down) { sum += vec[i]; }

  cout << sum << endl;

  return 0;
}


Error: WA

Fixed submission:

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

using namespace std;
using i64 = long long;
using f64 = double;

#define FOR(i, s, n) for (size_t i = (s); i < (size_t)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

template <typename T, typename U> inline bool chmax(T &a, U b) {
  return a < b ? (a = b, true) : false;
}

template <typename T, typename U> inline bool chmin(T &a, U b) {
  return a > b ? (a = b, true) : false;
}

int main() {
  int n;
  auto vec = vector<int64_t>(n);
  for (auto &x : vec) {
    cin >> x;
  }

  auto m = *min(vec.begin(), vec.end());
  auto sum = accumulate(vec.begin(), vec.end(), 0);
  cout << sum - m << endl;

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  string s;
  cin >> n;
  vector<ll> v(n);
  rep(i, n) cin >> v[i];
  priority_queue<ll> pq;
  sort(v.begin(), v.end(), greater<ll>());
  int id = 2;
  ll ans = v[0];
  pq.push(v[1]);
  pq.push(v[1]);
  for (id = 2; id < n; id++) {
    ll score = pq.top();
    pq.pop();
    ans += score;
    pq.push(v[id]);
    pq.push(v[id]);
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  string s;
  vector<ll> v(n);
  cin >> n;
  rep(i, n) cin >> v[i];
  priority_queue<ll> pq;
  sort(v.begin(), v.end(), greater<ll>());
  int id = 2;
  ll ans = v[0];
  pq.push(v[1]);
  pq.push(v[1]);
  for (id = 2; id < n; id++) {
    ll score = pq.top();
    pq.pop();
    ans += score;
    pq.push(v[id]);
    pq.push(v[id]);
  }
  cout << ans << endl;

  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

void swap(long long *a, long long *b) {
  long long temp = *a;
  *a = *b;
  *b = temp;
}

int partition(long long l, long long h, long long arr[]) {
  long long index = l;
  long long temp = arr[h];
  for (long long i = l; i < h; i++) {
    if (arr[i] >= temp) {
      swap(&arr[index], &arr[i]);
      index++;
    }
  }
  swap(&arr[index], &arr[h]);
  return index;
}

void quickSort(long long arr[], long long l, long long h) {

  if (l < h) {
    int index = partition(l, h, arr);
    quickSort(arr, l, index - 1);
    quickSort(arr, index + 1, h);
  }
}

// long long arr[1000000];

int main() {

  long long n;
  cin >> n;
  vector<int> arr(n);

  for (int i = 0; i < n; i++)
    cin >> arr[i];

  sort(arr.begin(), arr.end(), greater<int>());
  // for(int i=0;i<n;i++)printf("%lli ",arr[i]);

  long long comfort = arr[0];

  int temp = n - 2;
  if (temp % 2) {

    for (int i = 1; i <= (temp / 2); i++)
      comfort += 2 * arr[i];
    comfort += arr[(temp / 2) + 1];
  } else {
    for (int i = 1; i <= (temp / 2); i++)
      comfort += 2 * arr[i];
  }

  printf("%lli\\n", comfort);

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

void swap(long long *a, long long *b) {
  long long temp = *a;
  *a = *b;
  *b = temp;
}

int partition(long long l, long long h, long long arr[]) {
  long long index = l;
  long long temp = arr[h];
  for (long long i = l; i < h; i++) {
    if (arr[i] >= temp) {
      swap(&arr[index], &arr[i]);
      index++;
    }
  }
  swap(&arr[index], &arr[h]);
  return index;
}

void quickSort(long long arr[], long long l, long long h) {

  if (l < h) {
    int index = partition(l, h, arr);
    quickSort(arr, l, index - 1);
    quickSort(arr, index + 1, h);
  }
}

long long arr[1000000];

int main() {

  long long n;
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> arr[i];

  quickSort(arr, 0, n - 1);
  // for(int i=0;i<n;i++)printf("%lli ",arr[i]);

  long long comfort = arr[0];

  int temp = n - 2;
  if (temp % 2) {

    for (int i = 1; i <= (temp / 2); i++)
      comfort += 2 * arr[i];
    comfort += arr[(temp / 2) + 1];
  } else {
    for (int i = 1; i <= (temp / 2); i++)
      comfort += 2 * arr[i];
  }

  printf("%lli", comfort);

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

#include <algorithm>
#include <iostream>
using namespace std;
long long a[200010], ans = 0;
int main() {
  int i, n;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  long long num = n - 1;
  ans += a[n - 1];
  num--;
  for (i = n - 2; i >= 0; i--) {
    ans += min(2LL, num) * a[i];
    num -= min(2LL, num);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
long long a[100010], ans = 0;
int main() {
  int i, n;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  long long num = n - 1;
  ans += a[n - 1];
  num--;
  for (i = n - 2; i >= 0; i--) {
    ans += min(2LL, num) * a[i];
    num -= min(2LL, num);
  }
  cout << ans << endl;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, N) for (int i = 0; i < (N); i++)
#define erep(i, N) for (int i = N - 1; i >= 0; i--)
const ll INF = 1000000000;
const ll mod = 1000000007;
#define PI 3.1415926535
using Graph = vector<vector<int>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef pair<int, int> pair;
bool seen[505][505];

// dpTable
int dp[100050];

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A.at(i);
  sort(A.begin(), A.end(), greater<int>());
  ll ans = 0;
  if (N >= 1)
    ans = A.at(0);
  for (int i = 2; i < N; i++) {
    ans += A.at(i / 2);
  }
  cout << ans << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, N) for (int i = 0; i < (N); i++)
#define erep(i, N) for (int i = N - 1; i >= 0; i--)
const ll INF = 1000000000;
const ll mod = 1000000007;
#define PI 3.1415926535
using Graph = vector<vector<int>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef pair<int, int> pair;
bool seen[505][505];

// dpTable
int dp[100050];

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A.at(i);
  sort(A.begin(), A.end());
  int ans = 0;
  rep(i, N) { ans += A.at(N - i / 2); }
  cout << ans << endl;
  return 0;
}