prithivMLmods/Coder-Stat huggingface dataset:
Problem p02614 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

int main() {
  int H, W, K;
  scanf("%d%d%d", &H, &W, &K);
  string m[H];
  for (int i = 0; i < H; i++) {
    cin >> m[i];
  }
  int count = 0;
  for (int b = 0; b < 1 << (H + W); b++) {
    int black = 0;
    for (int i = 0; i < H * W; i++) {
      if (b >> (i / H) & 1 && b >> (H + i % H) & 1) {
        if (m[i / H][i % H] == '#') {
          if (++black > K) {
            break;
          }
        }
      }
    }
    if (black == K) {
      count++;
    }
  }
  printf("%d\n", count);
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>

using namespace std;

int main() {
  int H, W, K;
  scanf("%d%d%d", &H, &W, &K);
  string m[H];
  for (int i = 0; i < H; i++) {
    cin >> m[i];
  }
  int count = 0;
  for (int b = 0; b < 1 << (H + W); b++) {
    int black = 0;
    for (int i = 0; i < H * W; i++) {
      if (b >> (i / W) & 1 && b >> (H + i % W) & 1) {
        if (m[i / W][i % W] == '#') {
          if (++black > K) {
            break;
          }
        }
      }
    }
    if (black == K) {
      count++;
    }
  }
  printf("%d\n", count);
  return 0;
}
Problem p02614 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<int, int>;

int main(void) {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> c(w);

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

  int ans = 0;
  for (int bith = 0; bith < (1 << h); bith++) {
    for (int bitw = 0; bitw < (1 << w); bitw++) {

      int cntb = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (c[i][j] == '#') {
            if (((bith >> i) & 1) == 0 && ((bitw >> j) & 1) == 0) {
              cntb++;
            }
          }
        }
      }
      if (cntb == k)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main(void) {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> c(h);

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

  int ans = 0;
  for (int bith = 0; bith < (1 << h); bith++) {
    for (int bitw = 0; bitw < (1 << w); bitw++) {

      int cntb = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (c[i][j] == '#') {
            if (((bith >> i) & 1) == 0 && ((bitw >> j) & 1) == 0) {
              cntb++;
            }
          }
        }
      }
      if (cntb == k)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02614 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;

  vector<string> vec(w);
  for (int i = 0; i < w; i++) {
    cin >> vec.at(i);
    // cout << vec.at(i) << endl;
  }

  int ans = 0;
  for (int mask_h = 0; mask_h < (1 << h) - 1; mask_h++) {
    for (int mask_w = 0; mask_w < (1 << w) - 1; mask_w++) {
      // マスクするパターン(2^h * 2^w 通り)

      // 全マスのmask_hとmask_w以外の黒色をカウントする
      int cnt = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          // マスク対象の行・列は除外する
          if (mask_h & (1 << i))
            continue;
          if (mask_w & (1 << j))
            continue;
          // 黒色のマスをカウントする
          if (vec.at(i).at(j) == '#')
            cnt++;
        }
      }
      // cout << mask_h << ":" << mask_w << "...マスク後の黒マスは" << cnt <<
      // endl;
      if (cnt == k)
        ans++;
    }
  }
  cout << ans << endl;
}

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

int main() {
  int h, w, k;
  cin >> h >> w >> k;

  vector<string> vec(h);
  for (int i = 0; i < h; i++) {
    cin >> vec.at(i);
    // cout << vec.at(i) << endl;
  }

  int ans = 0;
  for (int mask_h = 0; mask_h < (1 << h) - 1; mask_h++) {
    for (int mask_w = 0; mask_w < (1 << w) - 1; mask_w++) {
      // マスクするパターン(2^h * 2^w 通り)

      // 全マスのmask_hとmask_w以外の黒色をカウントする
      int cnt = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          // マスク対象の行・列は除外する
          if (mask_h & (1 << i))
            continue;
          if (mask_w & (1 << j))
            continue;
          // 黒色のマスをカウントする
          if (vec.at(i).at(j) == '#')
            cnt++;
        }
      }
      // cout << mask_h << ":" << mask_w << "...マスク後の黒マスは" << cnt <<
      // endl;
      if (cnt == k)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02614 (Python)

Buggy submission (status: Runtime Error):
import itertools
from typing import List


def main():
    h, w, k = map(int, input().split())
    c = []
    for _ in range(h):
        c.append(input().split())

    print(hv(c, h, w, k))


def hv(c: List[List[str]], h: int, w: int, k: int) -> int:
    ret = 0
    for comb_h in itertools.product((False, True), repeat=h):
        for comb_w in itertools.product((False, True), repeat=w):
            cnt = 0
            for i in range(h):
                for j in range(w):
                    if comb_h[i] and comb_w[j] and c[i][j] == "#":
                        cnt += 1
            if cnt == k:
                ret += 1
    return ret


if __name__ == "__main__":
    main()


Fixed submission:
import itertools
from typing import List


def main():
    h, w, k = map(int, input().split())
    c = []
    for _ in range(h):
        c.append(list(input()))

    print(hv(c, h, w, k))


def hv(c: List[List[str]], h: int, w: int, k: int) -> int:
    ret = 0
    for comb_h in itertools.product((False, True), repeat=h):
        for comb_w in itertools.product((False, True), repeat=w):
            cnt = 0
            for i in range(h):
                for j in range(w):
                    if comb_h[i] and comb_w[j] and c[i][j] == "#":
                        cnt += 1
            if cnt == k:
                ret += 1
    return ret


if __name__ == "__main__":
    main()
Problem p02614 (Python)

Buggy submission (status: Runtime Error):
import itertools

import numpy as np


def _run(H, W, K, C):
    input = np.zeros([H, W], dtype=np.int8)
    for h in range(H):
        for w in range(W):
            if C[h][w] == "#":
                input[h, w] = 1

    count = 0
    for r in itertools.product(*[[0, 1] for _ in range(H + W)]):
        copy_input = input.copy()
        for h in range(H):
            if r[h] == 1:
                copy_input[h] = 0
        for w in range(W):
            if r[H + w] == 1:
                copy_input[:, w] = 0

        if np.sum(copy_input) == K:
            count += 1
    print(count)


def _main():
    H, W, K = map(int, input().split())
    C = []
    for _ in range(H):
        C.append(list(map(str, input().split())))
    _run(H, W, K, C)


if __name__ == "__main__":
    _main()


Fixed submission:
import itertools

import numpy as np


def _run(H, W, K, C):
    input = np.zeros([H, W], dtype=np.int8)
    for h in range(H):
        for w in range(W):
            if C[h][w] == "#":
                input[h, w] = 1

    count = 0
    for r in itertools.product(*[[0, 1] for _ in range(H + W)]):
        copy_input = input.copy()
        for h in range(H):
            if r[h] == 1:
                copy_input[h] = 0
        for w in range(W):
            if r[H + w] == 1:
                copy_input[:, w] = 0

        if np.sum(copy_input) == K:
            count += 1
    print(count)


def _main():
    H, W, K = map(int, input().split())
    C = [str(input()) for _ in range(H)]
    _run(H, W, K, C)


if __name__ == "__main__":
    _main()
Problem p02614 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, need;
  long long ans = 0;
  cin >> h >> w >> need;
  vector<string> s(w);
  for (int i = 0; i < w; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < (1 << h); i++) {
    for (int j = 0; j < (1 << w); j++) {
      int bl = 0;
      for (int k = 0; k < h; k++) {
        for (int l = 0; l < w; l++) {
          if (!(i & (1 << k)) && !(j & (1 << l)) && s[k][l] == '#')
            bl++;
        }
      }
      if (bl == need)
        ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, need;
  long long ans = 0;
  cin >> h >> w >> need;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < (1 << h); i++) {
    for (int j = 0; j < (1 << w); j++) {
      int bl = 0;
      for (int k = 0; k < h; k++) {
        for (int l = 0; l < w; l++) {
          if (!(i & (1 << k)) && !(j & (1 << l)) && s[k][l] == '#')
            bl++;
        }
      }
      if (bl == need)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02614 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> c(h);
  for (int i = 0; i < h; i++) {
    cin >> c[i];
  }
  int count = 0;
  for (int ih = 0; ih < (1 << h); ih++) {
    for (int jw = 0; jw < (1 << w); jw++) {

      int cnt = 0;
      for (int i = 0; i < h; h++) {
        for (int j = 0; j < w; j++) {
          if (ih >> i & 1)
            continue;
          if (jw >> j & 1)
            continue;
          if (c[i][j] == '#')
            cnt++;
        }
      }
      if (cnt == k)
        count++;
    }
  }

  cout << count << endl;
  return 0;
}


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

int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> c(h);
  for (int i = 0; i < h; i++) {
    cin >> c[i];
  }
  int count = 0;
  for (int ih = 0; ih < (1 << h); ih++) {
    for (int jw = 0; jw < (1 << w); jw++) {

      int cnt = 0;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          if (ih >> i & 1)
            continue;
          if (jw >> j & 1)
            continue;
          if (c[i][j] == '#')
            cnt++;
        }
      }
      if (cnt == k)
        count++;
    }
  }

  cout << count << endl;
  return 0;
}
Problem p02614 (C++)

Buggy submission (status: Runtime Error):
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

int main() {
  long long int H, W, K, res = 0;
  vector<vector<bool>> m, tmp0, tmp1;

  cin >> H >> W >> K;

  m.resize(H);
  rep(i, H) {
    string s;
    cin >> s;
    rep(j, W) { m[i].push_back((s[j] == '#')); }
  }
  rep(i, H) { m.resize(W); }

  for (int biti = 0; biti < (1 << H); ++biti) {
    tmp0 = m;
    for (int i = 0; i < H; ++i) {
      if (biti & (1 << i)) {
        rep(k, W) { tmp0[i][k] = false; }
      }
    }
    tmp1 = tmp0;
    for (int bitj = 0; bitj < (1 << W); ++bitj) {
      for (int j = 0; j < W; ++j) {
        if (bitj & (1 << j)) {
          rep(l, H) { tmp0[l][j] = false; }
        }
      }
      long long int count = 0;
      rep(p, H) {
        rep(q, W) {
          if (tmp0[p][q]) {
            count++;
          }
        }
      }
      if (count == K) {
        res++;
      }
      tmp0 = tmp1;
    }
  }

  cout << res;

  return 0;
}

Fixed submission:
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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

int main() {
  long long int H, W, K, res = 0;
  vector<vector<bool>> m, tmp0, tmp1;

  cin >> H >> W >> K;

  m.resize(H);
  rep(i, H) {
    string s;
    cin >> s;
    rep(j, W) { m[i].push_back((s[j] == '#')); }
  }

  for (int biti = 0; biti < (1 << H); ++biti) {
    tmp0 = m;
    for (int i = 0; i < H; ++i) {
      if (biti & (1 << i)) {
        rep(k, W) { tmp0[i][k] = false; }
      }
    }
    tmp1 = tmp0;
    for (int bitj = 0; bitj < (1 << W); ++bitj) {
      for (int j = 0; j < W; ++j) {
        if (bitj & (1 << j)) {
          rep(l, H) { tmp0[l][j] = false; }
        }
      }
      long long int count = 0;
      rep(p, H) {
        rep(q, W) {
          if (tmp0[p][q]) {
            count++;
          }
        }
      }
      if (count == K) {
        res++;
      }
      tmp0 = tmp1;
    }
  }

  cout << res;

  return 0;
}
Problem p02614 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long ans = 0, ha[10] = {0}, wi[10] = {0}, k, h, w;
string c[10];
bool judge(void) {
  int i, j, cntk = 0;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (c[i][j] == '#' && ha[i] == 0 && wi[j] == 0)
        cntk++;
    }
  }
  if (cntk == k)
    return true;
  else
    return false;
}
int dfs(int cnt) {
  if (cnt == (h + w)) {
    if (judge())
      ans++;
    return 0;
  }
  if (cnt < h) {
    cnt++;
    ha[cnt - 1] = 0;
    dfs(cnt);
    ha[cnt - 1] = 1;
    dfs(cnt);
  } else {
    cnt++;
    wi[cnt - h - 1] = 0;
    dfs(cnt);
    wi[cnt - h - 1] = 1;
    dfs(cnt);
  }
}
int main() {
  long long i;
  cin >> h >> w >> k;
  for (i = 0; i < h; i++) {
    cin >> c[i];
  }
  dfs(0);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long ans = 0, ha[10] = {0}, wi[10] = {0}, k, h, w;
string c[10];
bool judge(void) {
  int i, j, cntk = 0;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (c[i][j] == '#' && ha[i] == 0 && wi[j] == 0)
        cntk++;
    }
  }
  if (cntk == k)
    return true;
  else
    return false;
}
int dfs(int cnt) {
  if (cnt == (h + w)) {
    if (judge())
      ans++;
    return 0;
  }
  if (cnt < h) {
    cnt++;
    ha[cnt - 1] = 0;
    dfs(cnt);
    ha[cnt - 1] = 1;
    dfs(cnt);
  } else {
    cnt++;
    wi[cnt - h - 1] = 0;
    dfs(cnt);
    wi[cnt - h - 1] = 1;
    dfs(cnt);
  }
  return 0;
}
int main() {
  long long i;
  cin >> h >> w >> k;
  for (i = 0; i < h; i++) {
    cin >> c[i];
  }
  dfs(0);
  cout << ans << endl;
  return 0;
}
Problem p02614 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, n, m, ans = 0, k, t, r, c;
  char ch;
  cin >> n >> m >> k;
  int a[n][m];
  memset(a, 0, sizeof(a));
  for (i = 0; i < n; i++) {
    for (i = 0; i < m; i++) {
      cin >> ch;
      if (ch == '#')
        a[i][j] = 1;
    }
  }
  for (r = 0; r < (1 << n); r++) {
    for (c = 0; c < (1 << m); c++) {
      t = 0;
      for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
          if ((1 << i) & r)
            continue;
          if ((1 << j) & c)
            continue;
          t += a[i][j];
        }
      }
      if (t == k)
        ans++;
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, n, m, ans = 0, k, t, r, c;
  char ch;
  cin >> n >> m >> k;
  int a[n][m];
  memset(a, 0, sizeof(a));
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      cin >> ch;
      if (ch == '#')
        a[i][j] = 1;
    }
  }
  for (r = 0; r < (1 << n); r++) {
    for (c = 0; c < (1 << m); c++) {
      t = 0;
      for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
          if ((1 << i) & r)
            continue;
          if ((1 << j) & c)
            continue;
          t += a[i][j];
        }
      }
      if (t == k)
        ans++;
    }
  }
  cout << ans;
  return 0;
}
Problem p02614 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> vec(W);
  for (int i = 0; i < H; i++) {
    cin >> vec[i];
  }
  int ans = 0;
  for (int is = 0; is < (1 << H); is++) {
    for (int js = 0; js < (1 << W); js++) {
      int cnt = 0;
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
          if (is >> i & 1) {
            continue;
          }
          if (js >> j & 1) {
            continue;
          }
          if (vec[i][j] == '#') {
            cnt++;
          }
        }
      }
      if (cnt == K) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}


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

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> vec(H);
  for (int i = 0; i < H; i++) {
    cin >> vec[i];
  }
  int ans = 0;
  for (int is = 0; is < (1 << H); is++) {
    for (int js = 0; js < (1 << W); js++) {
      int cnt = 0;
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
          if (is >> i & 1) {
            continue;
          }
          if (js >> j & 1) {
            continue;
          }
          if (vec[i][j] == '#') {
            cnt++;
          }
        }
      }
      if (cnt == K) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02615 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

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

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

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

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


Fixed submission:
/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

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

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

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

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

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

Buggy submission (status: 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;

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

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;

  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++;
  }
}
Problem p02615 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

ll a[212345];
multiset<pair<ll, ll>, greater<pair<ll, ll>>> st;

int main() {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%lld", a + i);
  sort(a, a + n, greater<ll>());
  st.emplace(a[0], a[0]);
  ll ans = 0;
  FOR(i, 1, n) {
    ll b = a[i];
    auto it = st.begin();
    auto [c, d] = *it;
    st.erase(it);
    ans += c;
    st.emplace(min(b, c), max(b, c));
    st.emplace(min(b, d), max(b, d));
    for (auto [e, f] : st)
      DEBUG("(%lld, %lld) ", e, f);
    DEBUG("\n");
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define LAR(a, b) ((a) = max((a), (b)))
#define SML(a, b) ((a) = min((a), (b)))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template <typename T> using pque = priority_queue<T, vector<T>, greater<T>>;
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

ll a[212345];
multiset<pair<ll, ll>, greater<pair<ll, ll>>> st;

int main() {
  int n;
  scanf("%d", &n);
  REP(i, n) scanf("%lld", a + i);
  sort(a, a + n, greater<ll>());
  st.emplace(a[0], a[0]);
  ll ans = 0;
  FOR(i, 1, n) {
    ll b = a[i];
    auto it = st.begin();
    auto [c, d] = *it;
    st.erase(it);
    ans += c;
    st.emplace(min(b, c), max(b, c));
    st.emplace(min(b, d), max(b, d));
    // for(auto [e, f] : st) DEBUG("(%lld, %lld) ", e, f); DEBUG("\n");
  }
  printf("%lld\n", ans);
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02615 (C++)

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

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

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


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

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

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

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int main() {
  ll c;
  int a[100000];
  int x;
  scanf("%d", &x);
  for (int i = 1; i <= x; i++) {
    scanf("%lld", &a[i]);
  }
  sort(a + 1, a + 1 + x);
  c = a[x];
  int d = 0;
  for (int i = 1; i <= x - 2; i++) {
    if (i & 1)
      d++;
    c += a[x - d];
  }
  printf("%lld\n", c);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int main() {
  ll c;
  int a[200005];
  int x;
  scanf("%d", &x);
  for (int i = 1; i <= x; i++) {
    scanf("%lld", &a[i]);
  }
  sort(a + 1, a + 1 + x);
  c = a[x];
  int d = 0;
  for (int i = 1; i <= x - 2; i++) {
    if (i & 1)
      d++;
    c += a[x - d];
  }
  printf("%lld\n", c);
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pi;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
#define rep3(i, i0, n) for (int i = i0; i < (n); i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
  if (a.first != b.first) {
    return a.first < b.first;

  } else {
    return a.second < b.second;
  }
}

bool In_map(ll y, ll x, ll h, ll w) {
  if (y < 0 || x < 0 || y >= h || x >= w) {
    return 0;
  } else {
    return 1;
  }
}
const vector<ll> dx{1, 0, -1, 0};
const vector<ll> dy{0, 1, 0, -1};

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 i = 1;
  ll cnt = 1;
  while (1) {
    ans += A[i];
    cnt++;
    if (cnt == N - 1)
      break;
    ans += A[i];
    cnt++;
    if (cnt == N - 1)
      break;
    i++;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pi;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
#define rep3(i, i0, n) for (int i = i0; i < (n); i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
  if (a.first != b.first) {
    return a.first < b.first;

  } else {
    return a.second < b.second;
  }
}

bool In_map(ll y, ll x, ll h, ll w) {
  if (y < 0 || x < 0 || y >= h || x >= w) {
    return 0;
  } else {
    return 1;
  }
}
const vector<ll> dx{1, 0, -1, 0};
const vector<ll> dy{0, 1, 0, -1};

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 i = 1;
  ll cnt = 1;
  while (1) {
    if (cnt == N - 1)
      break;
    ans += A[i];
    cnt++;
    if (cnt == N - 1)
      break;
    ans += A[i];
    cnt++;
    if (cnt == N - 1)
      break;
    i++;
  }
  cout << ans << endl;

  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
using namespace std;
using ll = long long;
const int MAXN = 100001;

int a[MAXN];

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

  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  reverse(a, a + n);

  ll ans = a[0];
  map<int, int> spaces{{a[1], 2}};

  for (int i = 2; i < n; ++i) {
    int best_space = spaces.rbegin()->first;
    ans += best_space;
    spaces[best_space]--;
    if (spaces[best_space] == 0) {
      spaces.erase(best_space);
    }
    spaces[a[i]] += 2;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
using namespace std;
using ll = long long;
const int MAXN = 200001;

int a[MAXN];

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

  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  sort(a, a + n);
  reverse(a, a + n);

  ll ans = a[0];
  map<int, int> spaces{{a[1], 2}};

  for (int i = 2; i < n; ++i) {
    int best_space = spaces.rbegin()->first;
    ans += best_space;
    spaces[best_space]--;
    if (spaces[best_space] == 0) {
      spaces.erase(best_space);
    }
    spaces[a[i]] += 2;
  }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define me(a, b) memset(a, b, sizeof(a))
#define INIT() std::ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int MAX_N = 100000 + 5;
const int INF = 0x7fffffff;
const int inf = 100000;
const double EPS = 1e-6;
const ull base = 123;
const ll mod = 1e9 + 7;
const double pi = 4 * atan(1.0);
ll a[MAX_N];
bool cmp(ll a, ll b) { return a > b; }
int main(void) {
  int n;
  ll sum = 0;
  cin >> n;
  int i;
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  sort(a + 1, a + 1 + n, cmp);
  sum += a[1];
  int pos = 2;
  for (i = 2; i < n; i++) {
    if (i % 2 == 0)
      sum += a[pos];
    else
      sum += a[pos++];
  }
  cout << sum << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define fi first
#define se second
#define pb push_back
#define me(a, b) memset(a, b, sizeof(a))
#define INIT() std::ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int MAX_N = 200000 + 5;
const int INF = 0x7fffffff;
const int inf = 100000;
const double EPS = 1e-6;
const ull base = 123;
const ll mod = 1e9 + 7;
const double pi = 4 * atan(1.0);
ll a[MAX_N];
bool cmp(ll a, ll b) { return a > b; }
int main(void) {
  int n;
  ll sum = 0;
  cin >> n;
  int i;
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  sort(a + 1, a + 1 + n, cmp);
  sum += a[1];
  int pos = 2;
  for (i = 2; i < n; i++) {
    if (i % 2 == 0)
      sum += a[pos];
    else
      sum += a[pos++];
  }
  cout << sum << endl;
}
Problem p02615 (C++)

Buggy submission (status: 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(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;
}


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(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 (status: Runtime Error):
/**
 ____ ____ ____ ____ ____
||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;
}


Fixed submission:
/**
 ____ ____ ____ ____ ____
||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;
}
Problem p02615 (C++)

Buggy submission (status: 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) {
    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;
}


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) {
    auto it = m.rbegin();
    auto &[k, v] = *it;
    sum += k;
    --v;
    if (v == 0)
      m.erase(k);
    m[A[i]] += 2;
  }
  cout << sum << endl;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <queue>

using namespace std;

typedef long long ll;
const int N = 100005;

int n;
ll ans;
int w[N];
priority_queue<int> q;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", w + i);
  sort(w, w + n);
  reverse(w, w + n);
  q.push(w[0]);
  for (int i = 1; i < n; i++) {
    ans += q.top(), q.pop();
    q.push(w[i]), q.push(w[i]);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <queue>

using namespace std;

typedef long long ll;
const int N = 200005;

int n;
ll ans;
int w[N];
priority_queue<int> q;

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", w + i);
  sort(w, w + n);
  reverse(w, w + n);
  q.push(w[0]);
  for (int i = 1; i < n; i++) {
    ans += q.top(), q.pop();
    q.push(w[i]), q.push(w[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: 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 == 1) {
      res += a[idx];
      break;
    } else {
      n -= 2;
      res += 2 * a[idx];
      idx--;
    }
    if (n == 0)
      break;
  }
  cout << res << "\n";

  return 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 == 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;
}
Problem p02615 (C++)

Buggy submission (status: 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 - (j / 2);
    j--;
    ans += a[j];
  }

  cout << ans << endl;

  return 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 - (i / 2);
    j--;
    ans += a[j];
  }

  cout << ans << endl;

  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;

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

typedef long long ll;

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

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

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

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

using namespace std;

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

typedef long long ll;

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

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int main() {
  long int N, a[100000], count = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  sort(a, a + N, greater<int>());
  for (int j = 0; j < N; j++) {
    count += a[j / 2];
  }
  count -= a[0];
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int main() {
  long int N, a[200000], count = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  sort(a, a + N, greater<int>());
  for (int j = 0; j < N; j++) {
    count += a[j / 2];
  }
  count -= a[0];
  cout << count << endl;
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/abc173/tasks/abc173_d
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  // 降順にソートして、大きい要素から並べる
  std::sort(A.begin(), A.end(), std::greater_equal<ll>());
  ll ans = A[0];
  if (N == 2) {
    cout << ans << endl;
    return 0;
  }

  for (int i = 2; i < N; ++i) {
    ans += A[i / 2];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// https://atcoder.jp/contests/abc173/tasks/abc173_d
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  // 降順にソートして、大きい要素から並べる
  std::sort(A.begin(), A.end(), std::greater<ll>());
  ll ans = A[0];
  if (N == 2) {
    cout << ans << endl;
    return 0;
  }

  for (int i = 2; i < N; ++i) {
    ans += A[i / 2];
  }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
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);
  long long ans = arr[N - 1];
  int j = arr[N - 2];
  for (int i = N - 3; i >= 0;) {
    ans += arr[j], --i;
    if (i >= 0)
      ans += arr[j], --i;
    j--;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
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);
  long long ans = arr[N - 1];
  int j = N - 2;
  for (int i = N - 3; i >= 0;) {
    ans += arr[j], --i;
    if (i >= 0)
      ans += arr[j], --i;
    j--;
  }
  cout << ans << endl;
}
Problem p02615 (Python)

Buggy submission (status: Time Limit Exceeded):
# -*- coding: utf-8 -*-

N = int(input())
A = list(map(int, input().split()))

a_sorted = sorted(A, key=lambda x: -x)

circle = [a_sorted[0]]
ans = 0
for a in a_sorted:
    tmp = 0
    tmp_idx = -1
    for i in range(len(circle) - 1):
        cand = min(circle[i], circle[i + 1])
        if cand > tmp:
            tmp = cand
            tmp_idx = i
    ans += tmp
    circle.insert(tmp_idx + 1, a)

print(ans)


Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())
A = list(map(int, input().split()))

a_sorted = sorted(A, key=lambda x: -x)

circle = [a_sorted[0]]
ans = 0
for idx, a in enumerate(a_sorted[1:]):
    ans += circle[idx]
    circle.extend([a, a])

print(ans)
Problem p02615 (Python)

Buggy submission (status: Runtime Error):
from collections import deque

n = int(input())
a = sorted(map(int, input().split()), reverse=True)

ans, cnt = 0, deque([a[0]])
for i in a[1:]:
    ans += cnt.popleft()
    cnt.append(a[i])
    cnt.append(a[i])
print(ans)


Fixed submission:
from collections import deque

n = int(input())
a = sorted(map(int, input().split()), reverse=True)

ans, cnt = 0, deque([a[0]])
for i in a[1:]:
    ans += cnt.popleft()
    cnt.append(i)
    cnt.append(i)
print(ans)
Problem p02615 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
a = a[::-1]

cur = [a[0], a[1]]
now = a[0]
for i in a[2:]:
    loc = 0
    maxer = min(cur[0], cur[-1])
    for j in range(1, len(cur)):
        temp = min(cur[j - 1], cur[j])
        if temp > maxer:
            loc = j
            maxer = temp
    now += maxer
    cur = cur[:loc] + [i] + cur[loc:]

print(now)


Fixed submission:
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
a = a[::-1]

if n % 3 == 0:
    if n % 2 == 0:
        print(a[0] + sum(a[1 : n // 2]) * 2)
    else:
        print(a[0] + sum(a[1 : n // 2]) * 2 + a[n // 2])
elif n % 3 == 2:
    if n % 2 == 0:
        print(a[0] + sum(a[1 : n // 2]) * 2)
    else:
        print(a[0] + sum(a[1 : n // 2]) * 2 + a[n // 2])
else:
    if n % 2 == 0:
        print(a[0] + sum(a[1 : n // 2]) * 2)
    else:
        print(a[0] + sum(a[1 : n // 2]) * 2 + a[n // 2])