alexjercan/bugnet huggingface dataset:
Problem p02615 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define exf(x)                                                                 \\
  for (auto &&tmp : x) {                                                       \\
    cout << tmp << endl;                                                       \\
  } // 拡張for
#define PI 3.14159265358979323846264338327950288
const int MOD{int(1e9 + 7)}; // = 1000000007
const int inf{2100000000};   // = 2100000000 (MAX:2147483647)
typedef long long ll;
using namespace std;

// -------------------------------------------------- //
// ------------------ ここから本編 ------------------ //
// -------------------------------------------------- //

int main(void) {

  int N;
  cin >> N;
  vector<int> a(N);
  rep(i, N) cin >> a[i];

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

  ll ans = 0;
  tmp.push_back(a[0]);

  for (int i = 1; i < N; i++) {
    int x = a[i];
    ans += tmp[i - 1];
    tmp.push_back(x);
    tmp.push_back(x);
  }

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


Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define exf(x)                                                                 \\
  for (auto &&tmp : x) {                                                       \\
    cout << tmp << endl;                                                       \\
  } // 拡張for
#define PI 3.14159265358979323846264338327950288
const int MOD{int(1e9 + 7)}; // = 1000000007
const int inf{2100000000};   // = 2100000000 (MAX:2147483647)
typedef long long ll;
using namespace std;

// -------------------------------------------------- //
// ------------------ ここから本編 ------------------ //
// -------------------------------------------------- //

int main(void) {

  int N;
  cin >> N;
  vector<int> a(N);
  rep(i, N) cin >> a[i];

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

  ll ans = 0;
  tmp.push_back(a[0]);
  a.erase(a.begin());

  while (!a.empty()) {
    int x = a[0];
    ans += tmp[0];
    tmp.erase(tmp.begin());
    tmp.push_back(x);
    tmp.push_back(x);
    a.erase(a.begin());
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const double PI = acos(-1);

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  cin >> a;
  sort(a.rbegin(), a.rend());
  ll sum = 0, index = 0;
  rep(i, n) {
    if (i == 0) {
      continue;
    } else {
      sum += a[i / 2];
    }
  }
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const double PI = acos(-1);

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  cin >> a;
  sort(a.rbegin(), a.rend());
  ll sum = 0, index = 0;
  rep(i, n) {
    if (i == 0) {
      continue;
    } else if (i == 1) {
      sum += a[index];
      index++;
    } else {
      ll c = i - 2;
      sum += a[index];
      index += c / 2;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using VVI = vector<vector<int>>;
using VI = vector<int>;
const static string el = "\\n";
int h, w, k;
int result = 0;
int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    long long tmp;
    cin >> tmp;
    a[i] = tmp;
  }

  sort(a.begin(), a.end(), greater<long long>());
  queue<vector<long long>> que;
  long long result = 0;
  if (n == 2) {
    cout << a[0] << endl;
    return 0;
  }
  if (n == 3) {
    cout << a[0] + a[1] << endl;
    return 0;
  }
  que.push(vector<long long>{a[0], a[1]});
  que.push(vector<long long>{a[0], a[2]});
  que.push(vector<long long>{a[1], a[2]});

  result += a[0];
  result += a[1];

  for (int i = 3; i < n; i++) {
    vector<long long> tmp = que.front();
    // cout << tmp[0] << " " << tmp[1] << " " << a[i] << endl;
    que.pop();
    que.push(vector<long long>{tmp[0], a[i]});
    que.push(vector<long long>{tmp[1], a[i]});
    result += min(tmp[0], tmp[1]);
  }

  cout << result << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using VVI = vector<vector<int>>;
using VI = vector<int>;
const static string el = "\\n";
int h, w, k;
int result = 0;
int main() {
  int n;
  vector<long long> a(n);
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long tmp;
    cin >> tmp;
    a[i] = tmp;
  }

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

  long long result = 0;
  n--;
  for (int i = 0; i < n; i++) {
    result += a[i];
  }
  cout << result << endl;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  long long int a[200005];
  long long int n;
  cin >> n;
  for (int i = 1; i <= n; i++)

    cin >> a[i];
  long long int s = 0;
  sort(a + 1, a + n + 1);
  if (n % 2 == 0) {
    int d = n / 2;
    for (int i = n - 1; i > d; i--)
      s += a[i];
    s *= 2;
    s += a[n];

  } else {
    int d = (n + 1) / 2;
    for (int i = n - 1; i > d; i--)
      s += a[i];
    s *= 2;
    s += a[n];
    s += a[d];
  }

  cout << s << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int a[2005];
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)

    cin >> a[i];
  int s = 0;
  sort(a + 1, a + n + 1);
  if (n % 2 == 0) {
    int d = n / 2;
    for (int i = n - 1; i > d; i--)
      s += a[i];
    s *= 2;
    s += a[n];

  } else {
    int d = (n + 1) / 2;
    for (int i = n - 1; i > d; i--)
      s += a[i];
    s *= 2;
    s += a[n];
    s += a[d];
  }

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

#include <bits/stdc++.h>

using namespace std;

const long long maxn = 1e5 + 7;

long long n, ans;

vector<long long> a;

void solve() {
  cin >> n;
  a.resize(n);
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  ans = a[0];
  for (long long i = 2, j = 1; i < n; i += 2) {
    ans += a[j];
    if (i != n - 1) {
      ans += a[j];
    }
    j++;
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const long long maxn = 1e5 + 7;

long long n, a[maxn], ans;

void solve() {
  cin >> n;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  reverse(a, a + n);
  ans = a[0];
  for (long long i = 2, j = 1; i < n; i += 2) {
    ans += a[j];
    if (i != n - 1) {
      ans += a[j];
    }
    j++;
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  solve();
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  ll arr[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  sort(arr + 1, arr + n + 1);
  ll ans = arr[n];
  int k = n - 1;
  for (int i = n - 2; i > 0; i -= 2) {
    if (i == 1) {
      ans += arr[k];
      break;
    }
    ans = ans + 2LL * arr[k--];
  }
  cout << ans << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
;
int main() {
  int n;
  cin >> n;
  ll arr[n];
  vector<ll> freq(200001);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    freq[arr[i]]++;
  }
  sort(arr, arr + n);
  ll sum = (freq[arr[n - 1]] - 1) * arr[n - 1];
  int index = -1;
  for (int i = n - 1; i >= 0; i--) {
    if (arr[i] != arr[n - 1]) {
      index = i;
      break;
    }
  }
  int k = n - 1;
  for (int i = index; i >= 0; i--) {
    sum += arr[k];
    freq[arr[k]]--;
    if (freq[arr[k]] == 0)
      k--;
  }
  cout << sum << "\\n";
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int player[200005]; // 心地よさ
  int n;
  long long ans = 0;
  cin >> n;
  rep(i, n) { cin >> player[i]; }
  sort(player, player + n);

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

  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

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

int main() {
  vector<int> pos;
  vector<int> v;      // 次の要素との間に挿入した時の心地よさ
  int player[200005]; // 心地よさ
  int n;
  long long ans = 0;
  cin >> n;
  rep(i, n) { cin >> player[i]; }
  sort(player, player + n);
  for (int i = 0; i < n; i++) {
    int now = player[n - 1 - i];
    if (i == 0) {
      pos.emplace_back(now);
      v.emplace_back(now);
      continue;
    }
    auto it = max_element(v.begin(), v.end());
    int index = it - v.begin(); // indexの位置
    ans += v[index];
    pos[index] = now;
    v[index] = now;
    v.insert(v.begin() + index, now);
  }
  cout << ans;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: 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) {
    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;
}

Error: 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) {
    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, verdict: 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 = 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;
}

Error: 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 = 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;
}
Problem p02615 (C++) — buggy submission, verdict: 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 = 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;
}


Error: 0

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 = 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;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define repr(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;
const int dx[] = {0, 1, -1, 0};
const int dy[] = {1, 0, 0, -1};
const ll mod = 1e9 + 7;
const ll INF = 1e9;
const ll LINF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  //////////////////////////////////////
  ll n, sum = 0;
  cin >> n;
  vector<ll> a(n);
  vector<pair<ll, ll>> cnt;
  rep(i, n) { cin >> a[i]; }
  sort(ALL(a), greater<>());
  rep(i, n - 1) {
    int f = (i + 1) / 2;
    sum += a[f];
  }
  cout << sum << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define repr(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<ll, ll>;
const int dx[] = {0, 1, -1, 0};
const int dy[] = {1, 0, 0, -1};
const ll mod = 1e9 + 7;
const ll INF = 1e9;
const ll LINF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  //////////////////////////////////////
  ll n, sum = 0;
  cin >> n;
  vector<ll> a(n);
  vector<pair<ll, ll>> cnt;
  rep(i, n) { cin >> a[i]; }
  sort(ALL(a), greater<>());
  // sum += a[0];
  int x = 1;
  repi(i, 1, n) {
    if (a[i] == a[i - 1])
      x++;
    else {
      cnt.push_back(make_pair(a[i - 1], x));
      x = 1;
    }
  }
  cnt.push_back(make_pair(a[n - 1], x));
  ll count = n - 1, now = 0, flag = 0;
  while (count > 0) {
    ll numsize = cnt[flag].first;
    ll can = cnt[flag].second;
    ll k = max(0LL, now - can);
    if (flag == 0 && can == 1) {
      flag++;
      now += 1;
      count -= 1;
      sum += a[0];
      continue;
    }
    if (count - (can * 2 - k) >= 0) {
      sum += (numsize * (can * 2 - k));
      now += (can * 2 - k);
      count -= (can * 2 - k);
      flag++;
    } else {
      sum += (numsize * count);
      count = 0;
      break;
    }
  }
  // if (flag == 0) sum -= a[0];
  cout << sum << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#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;
}

Error: 0

Fixed submission:

#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;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

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

int n, A[200005];

signed main() {

  cin >> n;

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

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

  int ans = A[0];

  int cnt = n - 2;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      if (cnt == 0)
        continue;
      cnt--;
      ans += A[i];
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

int n, A[100005];

signed main() {

  cin >> n;

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

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

  int ans = 0;

  for (int i = 0; i < n - 1; i++)
    ans += A[i];

  cout << ans << endl;

  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;

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.at(i);
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());

  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.at(i);
        t--;
      }
    }
  }
  cout << ans << endl;
  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.at(i);
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());

  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.at(i);
        t--;
      }
    }
  }
  cout << ans << endl;
}