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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
#include <vector>

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

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  ll ans = a[0];
  ll cnt = 1;
  ll i = 1;
  while (true) {
    if (cnt < n - 2) {
      ans += 2 * a[i];
      cnt += 2;
      i++;
    } else if (cnt == n - 2) {
      ans += a[i];
      break;
    } else
      break;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <stdio.h>
#include <vector>

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

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  ll ans = a[0];
  ll cnt = 1;
  ll i = 1;
  while (true) {
    ans += a[i];
    cnt++;
    if (cnt == n - 1)
      break;
    ans += a[i];
    cnt++;
    if (cnt == n - 1)
      break;
    i++;
  }
  cout << ans << endl;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

constexpr uint32_t mxN = 2e5;
uint64_t A[mxN];

void solve() {
  uint32_t N;
  cin >> N;
  for (uint32_t i = 0; i < N; i++) {
    cin >> A[i];
  }
  std::sort(A, A + N, std::greater<uint64_t>());
  // cout << "A " << endl;
  //  for(uint32_t i = 0 ; i < N ; i++){
  //      cout << A[i] << " ";
  //  }
  // cout << endl;

  uint64_t sum = A[0];
  std::queue<uint32_t> q;
  q.push(1);
  for (uint64_t i = 2; i < N; i++) {
    sum += A[q.front()];
    // cout << "Got " << A[q.front()] << endl;
    q.push(i);
    if (i % 2) {
      q.pop();
    }
  }
  cout << sum << endl;
}

int main() {
  // making data IO Fast
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  /****************************/

#ifdef DEBUG
  int TEST_CASES = 2;
  while (TEST_CASES--) {
#endif // DEBBUG

    solve();

#ifdef DEBUG
  }
#endif // DEBUG

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

constexpr uint32_t mxN = 2e5;
uint64_t A[mxN];

void solve() {
  uint32_t N;
  cin >> N;
  for (uint32_t i = 0; i < N; i++) {
    cin >> A[i];
  }
  std::sort(A, A + N, std::greater<uint64_t>());
  // cout << "A " << endl;
  //  for(uint32_t i = 0 ; i < N ; i++){
  //      cout << A[i] << " ";
  //  }
  // cout << endl;

  uint64_t sum = A[0];
  std::queue<uint32_t> q;
  q.push(1);
  for (uint64_t i = 2; i < N; i++) {
    sum += A[q.front()];
    // cout << "Got " << A[q.front()] << endl;
    q.push(A[i]);
    if (i % 2) {
      q.pop();
    }
  }
  cout << sum << endl;
}

int main() {
  // making data IO Fast
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  /****************************/

#ifdef DEBUG
  int TEST_CASES = 2;
  while (TEST_CASES--) {
#endif // DEBBUG

    solve();

#ifdef DEBUG
  }
#endif // DEBUG

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

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

int main() {
  int64_t N, K;
  int64_t ans = 0;
  cin >> N;
  vector<int64_t> A(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());
  if (N % 2 == 0) {
    K = (N - 2) / 2;
    ans = A.at(N - 1);
    for (int64_t j = 0; j < K; j++) {
      ans = ans + 2 * A.at(N - (j + 2));
    }
    cout << ans << endl;
  } else {
    K = (N - 3) / 2;
    ans = A.at(N - 1) + A.at(N - K - 2);
    for (int64_t l = 0; l < K; l++) {
      ans = ans + 2 * A.at(N - (l + 2));
    }
    cout << ans << endl;
  }
}


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 <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int64_t N, K;
  int64_t ans = 0;
  cin >> N;
  vector<int64_t> A(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());
  if (N % 2 == 0) {
    K = (N - 2) / 2;
    ans = A.at(N);
    for (int64_t j = 0; j < K; j++) {
      ans = ans + 2 * A.at(N - (j + 1));
    }
    cout << ans << endl;
  } else {
    K = (N - 3) / 2;
    ans = A.at(N) + A.at(N - K - 1);
    for (int64_t l = 0; l < K; l++) {
      ans = ans + 2 * A.at(N - (l + 1));
    }
    cout << ans << endl;
  }
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  int N;
  cin >> N;
  VL A(N);
  rep(i, N) cin >> A[i];
  sort(all(A), greater<ll>());
  ll ans = 0;
  if (N % 2) {
    ans += A[0];
    rep(i, N / 2 - 1) ans += A[i + 1] * 2;
    ans += A[N / 2];
  } else {
    ans += A[0];
    rep(i, N / 2 - 1) ans += A[i + 1] * 2;
  }
  cout << ans << endl;
}


Error: -6
b639587f-1ed2-4edd-9763-343f4e704528.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02615/C++/s311285132.cpp:42: int main(): Assertion `N%2' failed.


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  int N;
  cin >> N;
  VL A(N);
  rep(i, N) cin >> A[i];
  sort(all(A), greater<ll>());
  ll ans = 0;
  assert(N % 2);
  if (N % 2) {
    ans += A[0];
    rep(i, N / 2 - 1) ans += A[i + 1] * 2;
    ans += A[N / 2];
  } else {
    ans += A[0];
    rep(i, N / 2 - 1) ans += A[i + 1] + 2;
  }
  cout << ans << endl;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

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

Error: WA

Fixed submission:

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

/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\\|/__\\|/__\\|/__\\|/__\\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 200002;

int n;

int f[N_MAX];

multiset<int> s;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> f[i];
  sort(f + 1, f + n + 1);
  reverse(f + 1, f + n + 1);
  ll ans = 0;
  s.insert(-f[1]);
  for (int i = 2; i <= n; i++) {
    int t = -(*s.begin());
    s.erase(s.begin());
    ans += t;
    s.insert(-f[i]);
    s.insert(-f[i]);
  }
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\\|/__\\|/__\\|/__\\|/__\\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 100002;

int n;

int f[N_MAX];

multiset<int> s;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> f[i];
  sort(f + 1, f + n + 1);
  reverse(f + 1, f + n + 1);
  ll ans = 0;
  s.insert(-f[1]);
  for (int i = 2; i <= n; i++) {
    int t = -(*s.begin());
    s.erase(s.begin());
    ans += t;
    s.insert(-f[i]);
    s.insert(-f[i]);
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //  long long int n,k,ans=1;
  long long int n;
  cin >> n;
  long long int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n, greater<int>());
  if (n < 3) {
    cout << a[0];
  } else {
    long long int ans = 0, i = 1, j = n - 2;
    while (1) {
      ans += a[i];
      j--;
      if (j == 0)
        break;
      ans += a[i];
      j--;
      if (j == 0)
        break;
      i++;
    }
    ans += a[0];
    cout << ans;
  }
}

Error: WA

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //  long long int n,k,ans=1;
  long long int n;
  cin >> n;
  long long int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n, greater<int>());
  long long int ans = 0, i = 1, j = n - 2;
  while (1) {
    ans += a[i];
    j--;
    if (j == 0)
      break;
    ans += a[i];
    j--;
    if (j == 0)
      break;
    i++;
  }
  ans += a[0];
  cout << ans;
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define along long(x) x.begin(), x.end()
#define MOD 998244353
const unsigned long long M = 1000000007;
const long double PI = 3.141592653589793236L;

typedef long long ll;
typedef long double ld;
#define MAX 50
using namespace std;
void solve() {
  ll n;
  cin >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  reverse(arr, arr + n);

  ll count = arr[0];
  ll size = n - 2;
  ll i = 1;
  while (size > 0) {
    count += arr[i];
    size--;
    if (size > 0) {
      count += arr[i];
      size--;
    } else {
      break;
    }
    i++;
  }
  cout << count << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define along long(x) x.begin(), x.end()
#define MOD 998244353
const unsigned long long M = 1000000007;
const long double PI = 3.141592653589793236L;

typedef long long ll;
typedef long double ld;
#define MAX 50
using namespace std;
void solve() {
  ll n;
  cin >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  reverse(arr, arr + n);

  vector<ll> vec;
  for (ll i = 0; i < n; i++) {
    vec.push_back(arr[i]);
  }
  ll count = arr[0];
  for (ll i = 0; i < n - 1; i++) {
    arr[i] = arr[i + 1];
  }
  vec.erase(vec.begin());
  vec.erase(vec.begin());
  ll i = 0;
  while (vec.size() > 0) {
    count += arr[i];
    vec.erase(vec.begin());
    if (vec.size() > 0) {
      count += arr[i];
      vec.erase(vec.begin());
    }
    i++;
  }
  cout << count << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef vector<vpll> vvpll;
typedef vector<vpii> vvpii;

#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(c) (c).begin(), (c).end()
#define ff first
#define ss second
#define mp(x, y) make_pair((x), (y))
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

void solve() {

  int n;
  cin >> n;
  vi a(n);
  for (int &u : a)
    cin >> u;
  sort(all(a));
  ll sum = 0;
  sum += a[n - 1];
  int pos = n - 2;
  for (int i = n - 3; i >= 0; i--) {
    sum += a[pos];
    i--;
    if (i >= 0)
      sum += a[pos];
    pos--;
  }

  cout << sum << '\\n';
}

int main() {
  boost;
  int t = 1;
  // 	cin >> t;
  // 	precompute();
  for (int i = 1; i <= t; i++) {
    // cout << "Case #" << i << ": ";
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
typedef vector<vpll> vvpll;
typedef vector<vpii> vvpii;

#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(c) (c).begin(), (c).end()
#define ff first
#define ss second
#define mp(x, y) make_pair((x), (y))
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

void solve() {

  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  ll sum = 0;
  for (int i = n - 1; i >= 1; i++)
    sum += a[i];
  cout << sum << '\\n';
}

int main() {
  boost;
  int t = 1;
  // 	cin >> t;
  // 	precompute();
  for (int i = 1; i <= t; i++) {
    // cout << "Case #" << i << ": ";
    solve();
  }
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.rbegin(), a.rend());
  ll ans = 0;
  int t = n - 1;
  rep(i, n) {
    int lim = 2;
    if (i == 0)
      lim = 1;
    rep(j, lim) {
      if (t > 0) {
        ans += a[i];
        t--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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());
  long long ans = 0;
  int t = n - 1;
  for (int i = 0; i < n; ++i) {
    int lim = 2;
    if (i == 0)
      lim = 1;
    for (int j = 0; j < lim; ++lim) {
      if (t > 0) {
        ans += a[i];
        t--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>

int main() {
  int n;
  long comfort = 0;
  long array[300000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%ld", &array[i]);
  }
  std::sort(array, array + n);
  std::reverse(array, array + n);

  // comfort += array[0];
  for (int i = 1; i < n; i++) {
    comfort += array[i / 2];
    // printf("%d ", array[i / 2]);
  }
  /*for (int i = 0; i < n; i++)
  {
      printf("%d ", array[i]);
  }*/
  printf("%ld\\n", comfort);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>

int main() {
  int n;
  int comfort = 0;
  int array[30000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &array[i]);
  }
  std::sort(array, array + n);
  std::reverse(array, array + n);

  comfort += array[0];
  for (int i = 1; i < n - 1; i++) {
    comfort += array[(i + 1) / 2];
  }
  /*for (int i = 0; i < n; i++)
  {
      printf("%d ", array[i]);
  }*/
  printf("%d\\n", comfort);
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

int main() {
  using namespace std;
  unsigned long N;
  cin >> N;
  vector<unsigned long> A, B(N);
  for (auto &&i : B)
    cin >> i;
  copy(begin(B), end(B), back_inserter(A));
  copy(begin(B), end(B), back_inserter(A));
  nth_element(begin(A), end(A) - N, end(A));
  cout << accumulate(end(A) - N, end(A), -*max_element(begin(A), end(A)))
       << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

int main() {
  using namespace std;
  unsigned long N;
  cin >> N;
  vector<unsigned long> A(N);
  for (auto &&i : A)
    cin >> i;
  nth_element(begin(A), end(A) - 1, end(A));
  copy_n(begin(A), N - 1, back_inserter(A));
  nth_element(begin(A), end(A) - N + 1, end(A));
  cout << accumulate(end(A) - N + 1, end(A), 0UL) << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;

int main(void) {
  int N;
  cin >> N;
  vector<int> a(N * 2);
  int i = 0;

  while (N > i) {
    long long tmp = 0;
    cin >> tmp;
    a.push_back(tmp);
    a.push_back(tmp);
    i++;
  }
  sort(a.begin(), a.end(), greater<int>());
  int count = 0;
  long long sum = 0;
  a[0] = 0;
  while (N > count) {
    sum += a[count];
    count++;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;

int main(void) {
  int N;
  cin >> N;
  vector<int> a(N);
  map<int, int> m;
  int i = 0;

  while (N > i) {
    cin >> a[i];
    if (m.count(a[i])) {
      m[a[i]] += 2;
    } else {
      m[a[i]] = 2;
    }
    i++;
  }
  sort(a.begin(), a.end(), greater<int>());
  int count = 0;
  long long sum = 0;
  m[a[0]] -= 1;
  while (N > count + 1) {
    int j = 0;
    while (N > j) {
      if (m[a[j]] > 0) {
        m[a[j]] -= 1;
        sum += a[j];
        break;
      }
      j++;
    }
    count++;
  }
  cout << sum << endl;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

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

  sort(all(a));
  reverse(all(a));

  ll ans = a[0];

  ll cnt = 0;
  ll id = 1;

  if (n == 2) {
    cout << ans << endl;
    return 0;
  }

  while (1) {
    rep(i, 2) {
      ans += a[id];
      cnt++;
      if (cnt == n - 2) {
        cout << ans << endl;
        return 0;
      }
    }
    id++;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

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

  sort(all(a));
  reverse(all(a));

  ll ans = a[0];

  ll cnt = 0;
  ll id = 1;

  while (1) {
    rep(i, 2) {
      ans += a[id];
      cnt++;
      if (cnt == n - 2) {
        cout << ans << endl;
        return 0;
      }
    }
    id++;
  }
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 200010;
LL a[N];
bool cmp(LL a, LL b) { return a > b; }
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", a + i);
  }
  sort(a + 1, a + n + 1, cmp);
  LL ans = a[1];
  for (int i = 3; i <= n; i++) {
    ans += a[(i + 1) / 2];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 100010;
int a[N];
bool cmp(int a, int b) { return a > b; }
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  sort(a + 1, a + n + 1, cmp);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i == 2) {
      ans += a[1];
    } else if (i > 2) {
      ans += min(a[i - 2], a[i - 1]);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

int main() {
  using namespace std;
  long long n, a[200000] = {}, s, w;
  bool flag = 0;
  cin >> n;
  for (long i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  s = a[n - 1];
  w = n - 2;
  for (long j = n - 2; j > 0; --j) {
    s += a[w];
    if (flag)
      w--;
    flag = !flag;
  }
  cout << s;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

int main() {
  using namespace std;
  long long n, a[2000] = {}, s, w;
  bool flag = 0;
  cin >> n;
  for (long i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  s = a[n - 1];
  w = n - 2;
  for (long j = n - 2; j > 0; --j) {
    s += a[w];
    if (flag)
      w--;
    flag = !flag;
  }
  cout << s;
  return 0;
}