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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k, ans = 0;
  cin >> h >> w >> k;
  vector<vector<char>> c(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  for (int tmp1 = 0; tmp1 < (1 << h); tmp1++) {
    for (int tmp2 = 0; tmp2 < (1 << w); tmp2++) {
      int kuro = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (((tmp1 >> i) & 1) == 0 && ((tmp2 >> j) & 1) == 0 &&
              c[i][j] == '#') {
            kuro++;
          }
        }
      }
      if (kuro == k) {
        ans++;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k, ans = 0;
  cin >> h >> w >> k;
  vector<vector<char>> c(h, vector<char>(w));
  vector<vector<int>> d(h, vector<int>(w, 0));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
      if (c[i][j] == '#') {
        d[i][j] = 1;
      }
    }
  }
  for (int tmp1 = 0; tmp1 < (1 << h); tmp1++) {
    bitset<6> s(tmp1);
    for (int i = 0; i < h; i++) {
      if (s.test(i)) {
        for (int j = 0; j < w; j++) {
          d[i][j] = 2;
        }
      }
    }
    for (int tmp2 = 0; tmp2 < (1 << w); tmp2++) {
      bitset<6> t(tmp2);

      for (int i = 0; i < w; i++) {
        if (t.test(i)) {
          for (int j = 0; j < w; j++) {
            d[j][i] = 3;
          }
        }
      }
      int kuro = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (d[i][j] == 1) {
            kuro++;
          }
        }
      }
      if (kuro == k) {
        ans++;
      }
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (d[i][j] == 3) {
            d[i][j] = 1;
          }
        }
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (d[i][j] == 2) {
          d[i][j] = 1;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

/**
 *    author: gxfireball
 **/
#include <bits/stdc++.h>
#define ll long long
#define endl '\\n'
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;

void solve() {
  int n;
  cin >> n;
  vector<int> arr;
  for (int i = 0; i < n; ++i) {
    int temp;
    cin >> temp;
    arr.push_back(temp);
  }
  if (n == 1) {
    cout << 0;
    return;
  }
  sort(arr.begin(), arr.end(), greater<int>());

  ll sum = 0, j = 0;
  for (int i = 0; i < n - 1; ++i) {
    sum += arr[j];
    if (i % 2 == 0)
      j++;
  }
  cout << sum;
}

int main() {
  fastio;
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

/**
 *    author: gxfireball
 **/
#include <bits/stdc++.h>
#define ll long long
#define endl '\\n'
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;

void solve() {
  ll n;
  cin >> n;
  ll arr[100005];
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  if (n == 1) {
    cout << 0 << endl;
    return;
  }
  sort(arr, arr + n, greater<ll>());

  ll sum = 0, j = 0;
  for (int i = 0; i < n - 1; ++i) {
    sum += arr[j];
    if (!i & 1)
      j++;
  }
  cout << sum << endl;
}

int main() {
  fastio;
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int N = 200000 + 10;

int a[N];

int main() {
  long long res;
  int n, i;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  res = a[n - 1];
  if (n % 2 == 0)
    for (i = n - 2; i >= n / 2; i--)
      res += a[i] * 2;
  else {
    for (i = n - 2; i >= n / 2 + 1; i--)
      res += a[i] * 2;
    res += a[n / 2];
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 100000 + 10;

int a[N];

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using Graph = vector<vector<int>>;
const int INF = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  sort(all(v));
  reverse(all(v));
  ll ans = 0;
  int x = n - 1;
  rep(i, n) {
    int cnt = 2;
    if (i == 0)
      cnt = 1;
    rep(j, cnt) {
      if (x > 0) {
        ans += v[i];
        x--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using Graph = vector<vector<int>>;
const int INF = 1000000007;
int main() {
  int n;
  cin >> n;
  priority_queue<int> pq;
  rep(i, n) {
    int tmp;
    cin >> tmp;
    pq.push(tmp);
  }
  ll ans = 0;
  int mae = -1;
  int max = pq.top();
  rep(i, n) {
    int now = pq.top();
    pq.pop();
    if (mae == -1) {
      mae = now;
      continue;
    } else {
      ans += mae;
      mae = now;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const int maxn = 200010;
const int INF = INT_MAX / 2;

int n;
int A[maxn];

multiset<int> ms;

int32_t main() {
  // FAST
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  sort(A, A + n, greater<int>());
  int ans = 0;

  ans += A[0];
  ms.insert(A[1]);
  ms.insert(A[1]);
  // cout << "HELLO\\n";

  for (int i = 2; i < n; i++) {
    ans += *ms.rbegin();
    ms.erase(ms.find(*ms.rbegin()));
    ms.insert(A[i]);
    ms.insert(A[i]);
    // cout << A[i] << " " << ans << "\\n";
  }

  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pi;
#define f first
#define s second
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const int maxn = 100010;
const int INF = INT_MAX / 2;

int n;
int A[maxn];

multiset<int> ms;

int32_t main() {
  // FAST
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  sort(A, A + n, greater<int>());
  int ans = 0;

  ans += A[0];
  ms.insert(A[1]);
  ms.insert(A[1]);

  // cout << "HELLO\\n";

  for (int i = 2; i < n; i++) {
    ans += *ms.rbegin();
    ms.erase(ms.find(*ms.rbegin()));
    ms.insert(A[i]);
    ms.insert(A[i]);
    // cout << A[i] << " " << ans << "\\n";
  }

  cout << ans;
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define nl '\\n'

using namespace std;

typedef long long ll;

void solve() {
  int N;
  cin >> N;
  int A[N];
  for (int &i : A)
    cin >> i;

  sort(A, A + N, greater<int>());
  vector<int> comf;
  ll comfort = A[0];
  for (int i = 1; i < N; i++)
    comf.push_back(A[i]), comf.push_back(A[i]);
  for (int i = 0; i < N - 2; i++)
    comfort += comf[i];

  cout << comfort;
}

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

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  cout << nl;
  // }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define nl '\\n'

using namespace std;

typedef long long ll;

void solve() {
  int N;
  cin >> N;
  int A[N];
  for (int &i : A)
    cin >> i;

  sort(A, A + N, greater<int>());
  vector<int> cycle = {A[0], A[1]};
  ll comfort = A[0];
  function<pair<int, int>()> search = [&]() {
    int comf = 0, pos = 0;
    for (int i = 0; i < (int)cycle.size(); i++) {
      int prev = cycle[i];
      int next = i + 1 < (int)cycle.size() ? cycle[i + 1] : cycle[0];
      if (comf < min(prev, next)) {
        comf = min(prev, next);
        pos = i + 1;
      }
    }
    return pair<int, int>(pos, comf);
  };

  for (int i = 2; i < N; i++) {
    pair<int, int> pos = search();
    cycle.insert(cycle.begin() + pos.first, A[i]);
    comfort += pos.second;
  }

  cout << comfort;
}

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

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  cout << nl;
  // }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pll pair<long long int, long long int>
#define great (long long int)(1e9 + 7)
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fst first
#define scd second

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

  sort(a, a + n);
  ll ans = 0;
  for (ll k = 1; k <= n - 1; k++) {
    ans += a[n - 1 - (k / 2)];
  }
  cout << ans << endl;
  return;
}
int main() {
  // fastio;
  ll t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pll pair<long long int, long long int>
#define great (long long int)(1e9 + 7)
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fst first
#define scd second

void solve() {
  ll n;
  cin >> n;
  set<ll> st;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    st.insert(a[i]);
  }
  if (st.size() == 1) {
    cout << n - 1 << endl;
    return;
  }
  sort(a, a + n);
  reverse(a, a + n);

  if (n == 3) {
    cout << a[0] + a[1] << endl;
    return;
  }
  ll ans = a[0] + 2 * a[1];
  ll cnt = n - 4;
  ll i = 2;
  while (cnt--) {
    ans += a[i];
    i--;
  }
  cout << ans << endl;
  return;
}
int main() {
  // fastio;
  ll t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

/*模範解答*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using st = string;
using P = pair<int, int>;
#define mrep(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) mrep(i, 0, n)
#define chmax(x, y)                                                            \\
  { x = max(x, y); }
#define chmin(x, y)                                                            \\
  { x = min(x, y); }

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  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.at(i);
        t--;
      }
    }
  }
  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 st = string;
using P = pair<int, int>;
#define mrep(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define chmax(x, y)                                                            \\
  { x = max(x, y); }
#define chmin(x, y)                                                            \\
  { x = min(x, y); }

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;
  mrep(k, 1, n) { ans += a.at(n - (k / 2)); }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
#define ll long long
#define pb push_back
#define mp make_pair

using namespace std;

int main() {

  ll n;
  cin >> n;
  ll ar[n];
  for (int i = 0; i < n; i++)
    cin >> ar[i];
  sort(ar, ar + n, greater<ll>());

  vector<int> vec;

  ll comf = ar[0];

  for (int i = 1; i < n; i++) {
    vec.pb(ar[i]);
    vec.pb(ar[i]);
    if (vec.size() >= n - 1)
      break;
  }

  for (int i = 0; i < n - 2; i++) {
    comf += vec[i];
  }

  cout << comf;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define ll long long
#define pb push_back
#define mp make_pair

using namespace std;

int main() {

  ll n;
  cin >> n;
  ll ar[n];
  for (int i = 0; i < n; i++)
    cin >> ar[i];
  sort(ar, ar + n, greater<ll>());

  vector<pair<int, int>> vec;

  ll comf = 0;
  vec.pb(mp(ar[0], ar[0]));

  for (int i = 1; i < n; i++) {
    ll mx = 0;
    ll ind = -1;
    for (int j = 0; j < vec.size(); j++) {
      if (mx < min(vec[j].first, vec[j].second)) {
        mx = min(vec[j].first, vec[j].second);
        ind = j;
      }
    }
    comf += mx;
    vec.pb(mp(vec[ind].first, ar[i]));
    vec.pb(mp(vec[ind].second, ar[i]));
    vec.erase(vec.begin() + ind);
  }

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

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

int arr[200000] = {0};

int main() {
  int n;
  long long result = 0;

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

  sort(arr, arr + n);

  int maxim = n - 1;
  result += arr[maxim];

  for (int i = 2; i < n; i++) {
    if (i % 2 == 0) {
      maxim--;
    }

    result += arr[maxim];
  }

  cout << result << endl;
}


Error: 0

Fixed submission:

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

int n, arr[100000];
long long sum = 0;

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

  sort(arr, arr + n);

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

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

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

  sort(ALL(a), greater<ll>());

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

Error: 0

Fixed submission:

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

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

  sort(ALL(a), greater<int>());

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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

vector<ll> a(200001);

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(a.begin(), a.begin() + n, greater<ll>());
  ll ans = a[0];
  if (n == 2) {
    cout << ans << endl;
    return 0;
  }
  int coun = 1;
  for (int i = 1;; i++) {
    ans += a[i];
    coun++;
    if (coun == n - 1)
      break;
    ans += a[i];
    coun++;
    if (coun == n - 1)
      break;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

vector<ll> a(200001);

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.begin() + n, greater<ll>());
  ll ans = a[0];
  int coun = 1;
  for (int i = 1;; i++) {
    ans += a[i];
    coun++;
    if (coun == n - 1)
      break;
    ans += a[i];
    coun++;
    if (coun == n - 1)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

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

const int mod = 1000000007, MAX = 200005, INF = 1 << 30;
#define all(x) (x).begin(), (x).end()

int main() {

  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(all(A));
  reverse(all(A)); // 昇順にして

  ll ans = 0;

  for (int i = 1; i < N; i++)
    ans += A
        [i /
         2]; // 一番大きい数を1回
             // それ以降大きい順に2回足す
             // 理屈は分かったけどすごい実装だ
             // 出来上がった円形じゃなくて着いた順にカウントされるのが肝なんだな
             // 割り込み利益が大きいところにどんどん入れていくから最小値だけ除いた数よりずいぶん大きくなるんだな

  cout << ans << endl;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

int main() {
  ll n;
  vector<ll> a(n);

  ll ans = 1000000000;
  ll min = 1000000000;

  cin >> n;

  rep(i, n) {
    cin >> a[i];

    ans += a[i];

    if (min > a[i]) {
      ans += min;
      min = a[i];
      ans -= a[i];
    }
  }

  ans -= 2000000000;

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

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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, 0, n) cin >> a[i];

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

  ll ans = 0;

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

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, 0, n) cin >> a[i];

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

  ll ans = 0;

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

  cout << ans << endl;

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

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

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

  sort(a.begin(), a.end(), greater<int>());

  ll ans = 0, mxi = 0;
  for (int i = 1; i < n; i++) {
    ans += a[mxi];
    if (i % 2 == 1)
      mxi++;
  }

  cout << ans << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <string.h>
#include <vector>
using namespace std;
int a[100001];

int main() {
  deque<int> d;
  int n;
  int sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n, greater<int>());
  for (int i = 0; i < n; i++) {
    d.push_back(a[i]);
  }

  for (int i = 0; i < d.size(); i++) {
    if (d.front() > d.back()) {
      sum += d.back();
      d.pop_front();
      d.pop_back();
    } else {
      sum += d.front();
      d.pop_front();
      d.pop_back();
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch,
 * system("pause") or input loop */
int n;
int a[200005];
long long ans = 0;
int main(int argc, char **argv) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  ans += a[n - 1];
  for (int i = 1; i <= (n - 2) / 2; i++) {
    ans += a[n - i - 1] * 2;
  }
  if ((n - 2) % 2 != 0) {
    ans += a[n - (n - 2) / 2 - 2];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch,
 * system("pause") or input loop */
int a[100005];
int n;
int main(int argc, char **argv) {
  long long ans = 0;
  int m = 1e9;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += a[i];
    m = min(m, a[i]);
  }
  ans -= m;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
int a[N];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  queue<pair<int, int>> q;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n, greater<int>());
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (q.empty())
      q.push(make_pair(a[i], a[i]));
    else {
      int x1 = q.front().first;
      int x2 = q.front().second;
      ans += min(x1, x2);
      q.pop();
      q.push(make_pair(x1, a[i]));
      q.push(make_pair(a[i], x2));
    }
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const int N = 100002, M = 10010, mod = 998244353;
int n, m, T, k, h, w, cnt, x, y, z, indx, t, ans, a[N], r[N], c[N], b[N];
string s;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1, greater<int>());
  if (n == 1) {
    printf("0");
    return 0;
  }
  ans += a[1];
  int m = n - 2;
  for (int i = 2; i <= n; i++) {
    if (m > 1)
      m -= 2, ans += a[i] * 2;
    else if (m)
      m--, ans += a[i];
  }
  printf("%d", ans);
}